001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.multiview;
043:
044: import java.io.*;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.util.Map;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.swing.Action;
051: import javax.swing.SwingUtilities;
052: import org.netbeans.core.multiview.MultiViewModel.ActionRequestObserverFactory;
053: import org.netbeans.core.spi.multiview.CloseOperationHandler;
054: import org.netbeans.core.spi.multiview.MultiViewDescription;
055: import org.netbeans.core.spi.multiview.MultiViewElement;
056: import org.netbeans.core.spi.multiview.MultiViewElementCallback;
057: import org.netbeans.core.spi.multiview.SourceViewMarker;
058: import org.openide.awt.UndoRedo;
059: import org.openide.text.CloneableEditorSupport;
060: import org.openide.util.HelpCtx;
061: import org.openide.windows.CloneableTopComponent;
062: import org.openide.windows.TopComponent;
063:
064: /** Special subclass of TopComponent which shows and handles set of
065: * MultiViewElements, shows them in switchable toggle buttons style, along
066: * with toolbsrs af actions asociated with individual view elements.
067: *
068: *
069: * @author Dafe Simonek, Milos Kleint
070: */
071:
072: public final class MultiViewCloneableTopComponent extends
073: CloneableTopComponent implements ActionRequestObserverFactory,
074: CloneableEditorSupport.Pane {
075:
076: MultiViewPeer peer;
077:
078: public MultiViewCloneableTopComponent() {
079: super ();
080: peer = new MultiViewPeer(this , this );
081: // initializes the multiview component.
082: peer.initComponents();
083: setFocusCycleRoot(false);
084: setName("");
085: // assocuate lookup needs to come after the init.. initComponents() initializes actionMap
086: associateLookup(peer.getLookup());
087:
088: // setFocusable(false);
089: }
090:
091: public void setMultiViewDescriptions(
092: MultiViewDescription[] descriptions,
093: MultiViewDescription defaultDesc) {
094: peer.setMultiViewDescriptions(descriptions, defaultDesc);
095: }
096:
097: public void setCloseOperationHandler(CloseOperationHandler handler) {
098: peer.setCloseOperationHandler(handler);
099: }
100:
101: private void setDeserializedMultiViewDescriptions(
102: MultiViewDescription[] descriptions,
103: MultiViewDescription defaultDesc, Map existingElements) {
104: peer.setDeserializedMultiViewDescriptions(descriptions,
105: defaultDesc, existingElements);
106: }
107:
108: MultiViewModel getModel() {
109: return peer.getModel();
110: }
111:
112: protected void componentClosed() {
113: super .componentClosed();
114: peer.peerComponentClosed();
115: }
116:
117: protected void componentShowing() {
118: super .componentShowing();
119: peer.peerComponentShowing();
120: }
121:
122: protected void componentHidden() {
123: super .componentHidden();
124: peer.peerComponentHidden();
125: }
126:
127: protected void componentDeactivated() {
128: super .componentDeactivated();
129: peer.peerComponentDeactivated();
130: }
131:
132: protected void componentActivated() {
133: super .componentActivated();
134: peer.peerComponentActivated();
135: }
136:
137: protected void componentOpened() {
138: super .componentOpened();
139: peer.peerComponentOpened();
140: }
141:
142: /**
143: * delegate to the apppropriate active element's component
144: */
145: public boolean requestFocusInWindow() {
146: return peer.requestFocusInWindow();
147: }
148:
149: /**
150: * delegate to the apppropriate active element's component
151: */
152: public void requestFocus() {
153: peer.requestFocus();
154: }
155:
156: /**
157: * merge action for the topcomponent and the enclosed MultiViewElement..
158: *
159: */
160: public Action[] getActions() {
161: //TEMP don't delegate to element's actions..
162: Action[] super Actions = super .getActions();
163: Action[] acts = peer.peerGetActions(super Actions);
164: return acts;
165: // return acts;
166: }
167:
168: public MultiViewHandlerDelegate getMultiViewHandlerDelegate() {
169: // TODO have one handler only or create a new one each time?
170: return peer.getMultiViewHandlerDelegate();
171: }
172:
173: /**
174: * Delegates the value to the element descriptions.
175: */
176: public int getPersistenceType() {
177: return peer.getPersistenceType();
178: }
179:
180: protected String preferredID() {
181: return peer.preferredID();
182: }
183:
184: /** Serialize this top component.
185: * Subclasses wishing to store state must call the super method, then write to the stream.
186: * @param out the stream to serialize to
187: */
188: public void writeExternal(ObjectOutput out) throws IOException {
189: super .writeExternal(out);
190: peer.peerWriteExternal(out);
191: }
192:
193: /** Deserialize this top component.
194: * Subclasses wishing to store state must call the super method, then read from the stream.
195: * @param in the stream to deserialize from
196: */
197: public void readExternal(ObjectInput in) throws IOException,
198: ClassNotFoundException {
199: super .readExternal(in);
200: peer.peerReadExternal(in);
201: }
202:
203: Action[] getDefaultTCActions() {
204: return super .getActions();
205: }
206:
207: public MultiViewElementCallback createElementCallback(
208: MultiViewDescription desc) {
209: return SpiAccessor.DEFAULT.createCallback(new ActReqObserver(
210: desc));
211: }
212:
213: public CloneableTopComponent getComponent() {
214: return this ;
215: }
216:
217: public javax.swing.JEditorPane getEditorPane() {
218: if (peer == null || peer.model == null) {
219: return null;
220: }
221: MultiViewElement paneEl = findPaneElement();
222: if (paneEl != null) {
223: CloneableEditorSupport.Pane pane = (CloneableEditorSupport.Pane) paneEl
224: .getVisualRepresentation();
225: return pane.getEditorPane();
226: }
227: // hopeless case, don't try to create new elements. it's users responsibility to
228: // switch to the editor element before getEditorPane()
229: return null;
230: }
231:
232: private MultiViewElement findPaneElement() {
233: MultiViewElement el = peer.model.getActiveElement(false);
234: if (el != null
235: && el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) {
236: return el;
237: }
238: // now try a best guess.. iterate the already created elements and check if any of
239: // them is a Pane
240: Collection col = peer.model.getCreatedElements();
241: Iterator it = col.iterator();
242: while (it.hasNext()) {
243: el = (MultiViewElement) it.next();
244: if (el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) {
245: // fingers crossed and hope for the best... could result in bad results once
246: // we have multiple editors in the multiview component.
247: return el;
248: }
249: }
250:
251: MultiViewDescription[] descs = peer.model.getDescriptions();
252: for (MultiViewDescription desc : descs) {
253: if (desc instanceof SourceViewMarker) {
254: peer.tabs.changeVisibleManually(desc);
255: el = peer.model.getActiveElement();
256: if (el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) {
257: return el;
258: } else {
259: Logger
260: .getLogger(getClass().getName())
261: .info(
262: "MultiViewDescription "
263: + desc.getDisplayName()
264: + "("
265: + desc.getClass()
266: + ") claimed to contain sources, but it's MutliViewElement.getVisualRepresentation() didn't return a valid CloeanbleEditorSupport.Pane instance.");
267: }
268: }
269: }
270: // hopeless case, don't try to create new elements. it's users responsibility to
271: // switch to the editor element before getEditorPane()
272: return null;
273: }
274:
275: public HelpCtx getHelpCtx() {
276: return peer.getHelpCtx();
277: }
278:
279: public String toString() {
280: return "MVCTC[name=" + getDisplayName() + ", peer=" + peer
281: + "]"; // NOI18N
282: }
283:
284: /**
285: * Get the undo/redo support for this component.
286: * The default implementation returns a dummy support that cannot
287: * undo anything.
288: *
289: * @return undoable edit for this component
290: */
291: public UndoRedo getUndoRedo() {
292: UndoRedo retValue;
293: retValue = peer.peerGetUndoRedo();
294: if (retValue == null) {
295: retValue = super .getUndoRedo();
296: }
297: return retValue;
298: }
299:
300: /**
301: * This method is called when this <code>TopComponent</code> is about to close.
302: * Delegates to CloseOperationHandler.
303: */
304: public boolean canClose() {
305: return peer.canClose();
306: }
307:
308: // from CloneableEditor.Pane
309: public void updateName() {
310: // ensure to trigger update name from AWT -> #44012 - will ultimately trigger winsys.
311: if (peer != null) {
312: if (SwingUtilities.isEventDispatchThread()) {
313: peer.updateName();
314: } else {
315: SwingUtilities.invokeLater(new Runnable() {
316: public void run() {
317: peer.updateName();
318: }
319: });
320: }
321: }
322: }
323:
324: /**
325: * callback for the Pane implementation to adjust itself to the openAt() request.
326: */
327: public void ensureVisible() {
328: MultiViewElement paneEl = findPaneElement();
329: if (paneEl != null) {
330: open();
331: MultiViewElementCallback call = peer.getModel()
332: .getCallbackForElement(paneEl);
333: call.requestVisible();
334: }
335: }
336:
337: /**
338: * implementation of the MultiViewElement.ActionRequestObserver, manages activatation of the elements
339: * and the TC itself based on requests from the elements.
340: */
341: class ActReqObserver implements Serializable,
342: MultiViewElementCallbackDelegate {
343:
344: private static final long serialVersionUID = -3126744916624172415L;
345: private MultiViewDescription description;
346:
347: ActReqObserver(MultiViewDescription desc) {
348: description = desc;
349: }
350:
351: public void requestActive() {
352: boolean activated = peer.isActivated();
353: if (!activated) {
354: MultiViewCloneableTopComponent.this .requestActive();
355: }
356: if (peer.model.getActiveDescription() != description) {
357: if (activated) {
358: peer.model.getActiveElement()
359: .componentDeactivated();
360: }
361: peer.tabs.changeActiveManually(description);
362: if (activated) {
363: peer.model.getActiveElement().componentActivated();
364: }
365: }
366:
367: }
368:
369: public void requestVisible() {
370: peer.tabs.changeVisibleManually(description);
371: }
372:
373: public Action[] createDefaultActions() {
374: return MultiViewCloneableTopComponent.this
375: .getDefaultTCActions();
376: }
377:
378: public void updateTitle(String title) {
379: MultiViewCloneableTopComponent.this .setDisplayName(title);
380: }
381:
382: /** replace as null - should not be stored and read..*/
383: public Object writeReplace() throws ObjectStreamException {
384: return null;
385: }
386:
387: /** Resolve as null -should not be stored and read..*/
388: public Object readResolve() throws ObjectStreamException {
389: return null;
390: }
391:
392: public boolean isSelectedElement() {
393: return (description.equals(peer.model
394: .getActiveDescription()));
395: }
396:
397: public TopComponent getTopComponent() {
398: return MultiViewCloneableTopComponent.this;
399: }
400:
401: }
402:
403: }
|