KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewCloneableTopComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.multiview;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.io.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.AbstractAction JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.ActionMap JavaDoc;
32 import javax.swing.InputMap JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import org.netbeans.core.api.multiview.MultiViewHandler;
39 import org.netbeans.core.api.multiview.MultiViewPerspective;
40 import org.netbeans.core.multiview.MultiViewModel.ActionRequestObserverFactory;
41 import org.netbeans.core.multiview.MultiViewModel.ElementSelectionListener;
42 import org.netbeans.core.spi.multiview.CloseOperationHandler;
43 import org.netbeans.core.spi.multiview.CloseOperationState;
44 import org.netbeans.core.spi.multiview.MultiViewDescription;
45 import org.netbeans.core.spi.multiview.MultiViewElement;
46 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
47 import org.netbeans.core.spi.multiview.MultiViewFactory;
48 import org.openide.awt.UndoRedo;
49 import org.openide.text.CloneableEditorSupport;
50 import org.openide.util.HelpCtx;
51 import org.openide.util.Lookup;
52 import org.openide.util.lookup.Lookups;
53 import org.openide.util.lookup.ProxyLookup;
54 import org.openide.windows.CloneableTopComponent;
55 import org.openide.windows.TopComponent;
56
57
58 /** Special subclass of TopComponent which shows and handles set of
59  * MultiViewElements, shows them in switchable toggle buttons style, along
60  * with toolbsrs af actions asociated with individual view elements.
61  *
62  *
63  * @author Dafe Simonek, Milos Kleint
64  */

65
66
67 public final class MultiViewCloneableTopComponent extends CloneableTopComponent
68                                                   implements ActionRequestObserverFactory, CloneableEditorSupport.Pane {
69
70     MultiViewPeer peer;
71                                            
72     public MultiViewCloneableTopComponent() {
73         super();
74         peer = new MultiViewPeer(this, this);
75         // initializes the multiview component.
76
peer.initComponents();
77         setFocusCycleRoot(false);
78         setName("");
79         // assocuate lookup needs to come after the init.. initComponents() initializes actionMap
80
associateLookup(peer.getLookup());
81         
82 // setFocusable(false);
83
}
84     
85     
86     public void setMultiViewDescriptions(MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) {
87         peer.setMultiViewDescriptions(descriptions, defaultDesc);
88     }
89     
90     public void setCloseOperationHandler(CloseOperationHandler handler) {
91         peer.setCloseOperationHandler(handler);
92     }
93     
94     private void setDeserializedMultiViewDescriptions(MultiViewDescription[] descriptions,
95                                                       MultiViewDescription defaultDesc, Map JavaDoc existingElements) {
96         peer.setDeserializedMultiViewDescriptions(descriptions, defaultDesc, existingElements);
97     }
98     
99     MultiViewModel getModel() {
100         return peer.getModel();
101     }
102     
103     
104     
105     protected void componentClosed() {
106         super.componentClosed();
107         peer.peerComponentClosed();
108     }
109     
110     protected void componentShowing() {
111         super.componentShowing();
112         peer.peerComponentShowing();
113     }
114     
115     protected void componentHidden() {
116         super.componentHidden();
117         peer.peerComponentHidden();
118     }
119     
120     protected void componentDeactivated() {
121         super.componentDeactivated();
122         peer.peerComponentDeactivated();
123     }
124     
125     protected void componentActivated() {
126         super.componentActivated();
127         peer.peerComponentActivated();
128     }
129     
130     protected void componentOpened() {
131         super.componentOpened();
132         peer.peerComponentOpened();
133     }
134
135     /**
136      * delegate to the apppropriate active element's component
137      */

138     public boolean requestFocusInWindow() {
139         return peer.requestFocusInWindow();
140     }
141
142     /**
143      * delegate to the apppropriate active element's component
144      */

145     public void requestFocus() {
146         peer.requestFocus();
147     }
148     
149     /**
150      * merge action for the topcomponent and the enclosed MultiViewElement..
151      *
152      */

153     public Action JavaDoc[] getActions() {
154         //TEMP don't delegate to element's actions..
155
Action JavaDoc[] superActions = super.getActions();
156         Action JavaDoc[] acts = peer.peerGetActions(superActions);
157         return acts;
158 // return acts;
159
}
160     
161     public MultiViewHandlerDelegate getMultiViewHandlerDelegate() {
162         // TODO have one handler only or create a new one each time?
163
return peer.getMultiViewHandlerDelegate();
164     }
165     
166     /**
167      * Delegates the value to the element descriptions.
168      */

169     public int getPersistenceType() {
170         return peer.getPersistenceType();
171     }
172     
173     protected String JavaDoc preferredID() {
174         return peer.preferredID();
175     }
176     
177     
178     
179     /** Serialize this top component.
180     * Subclasses wishing to store state must call the super method, then write to the stream.
181     * @param out the stream to serialize to
182     */

183     public void writeExternal (ObjectOutput out) throws IOException {
184         super.writeExternal(out);
185         peer.peerWriteExternal(out);
186     }
187
188     /** Deserialize this top component.
189     * Subclasses wishing to store state must call the super method, then read from the stream.
190     * @param in the stream to deserialize from
191     */

192     public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
193         super.readExternal(in);
194         peer.peerReadExternal(in);
195     }
196     
197     
198     Action JavaDoc[] getDefaultTCActions() {
199         return super.getActions();
200     }
201     
202     public MultiViewElementCallback createElementCallback(MultiViewDescription desc) {
203         return SpiAccessor.DEFAULT.createCallback(new ActReqObserver(desc));
204     }
205     
206     public CloneableTopComponent getComponent() {
207         return this;
208     }
209     
210     public javax.swing.JEditorPane JavaDoc getEditorPane() {
211         if (peer == null || peer.model == null) {
212             return null;
213         }
214         MultiViewElement paneEl = findPaneElement();
215         if (paneEl != null) {
216             CloneableEditorSupport.Pane pane = (CloneableEditorSupport.Pane)paneEl.getVisualRepresentation();
217             return pane.getEditorPane();
218         }
219         // hopeless case, don't try to create new elements. it's users responsibility to
220
// switch to the editor element before getEditorPane()
221
return null;
222     }
223     
224     private MultiViewElement findPaneElement() {
225         MultiViewElement el = peer.model.getActiveElement(false);
226         if (el != null && el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) {
227             return el;
228         }
229         // now try a best guess.. iterate the already created elements and check if any of
230
// them is a Pane
231
Collection JavaDoc col = peer.model.getCreatedElements();
232         Iterator JavaDoc it = col.iterator();
233         while (it.hasNext()) {
234             el = (MultiViewElement)it.next();
235             if (el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) {
236                 // fingers crossed and hope for the best... could result in bad results once
237
// we have multiple editors in the multiview component.
238
return el;
239             }
240         }
241         // hopeless case, don't try to create new elements. it's users responsibility to
242
// switch to the editor element before getEditorPane()
243
return null;
244     }
245     
246     public HelpCtx getHelpCtx() {
247         return peer.getHelpCtx();
248     }
249    
250     public String JavaDoc toString() {
251         return "MVCTC[name=" + getDisplayName() + ", peer=" + peer + "]"; // NOI18N
252
}
253  
254     /**
255      * Get the undo/redo support for this component.
256      * The default implementation returns a dummy support that cannot
257      * undo anything.
258      *
259      * @return undoable edit for this component
260      */

261     public UndoRedo getUndoRedo() {
262         UndoRedo retValue;
263         retValue = peer.peerGetUndoRedo();
264         if (retValue == null) {
265             retValue = super.getUndoRedo();
266         }
267         return retValue;
268     }
269     
270     /**
271      * This method is called when this <code>TopComponent</code> is about to close.
272      * Delegates to CloseOperationHandler.
273      */

274     public boolean canClose() {
275         return peer.canClose();
276     }
277     
278     // from CloneableEditor.Pane
279
public void updateName() {
280         // ensure to trigger update name from AWT -> #44012 - will ultimately trigger winsys.
281
if (peer != null) {
282             if (SwingUtilities.isEventDispatchThread() ) {
283                 peer.updateName();
284             } else {
285                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
286                     public void run() {
287                         peer.updateName();
288                     }
289                 });
290             }
291         }
292     }
293     
294     /**
295      * callback for the Pane implementation to adjust itself to the openAt() request.
296      */

297     public void ensureVisible() {
298         MultiViewElement paneEl = findPaneElement();
299         if (paneEl != null) {
300             open();
301             MultiViewElementCallback call = peer.getModel().getCallbackForElement(paneEl);
302             call.requestVisible();
303         }
304     }
305     
306     
307     /**
308      * implementation of the MultiViewElement.ActionRequestObserver, manages activatation of the elements
309      * and the TC itself based on requests from the elements.
310      */

311     class ActReqObserver implements Serializable, MultiViewElementCallbackDelegate {
312         
313         private static final long serialVersionUID =-3126744916624172415L;
314         private MultiViewDescription description;
315         
316         ActReqObserver(MultiViewDescription desc) {
317             description = desc;
318         }
319         
320         public void requestActive() {
321             boolean activated = peer.isActivated();
322             if (!activated) {
323                 MultiViewCloneableTopComponent.this.requestActive();
324             }
325             if (peer.model.getActiveDescription() != description) {
326                 if (activated) {
327                     peer.model.getActiveElement().componentDeactivated();
328                 }
329                 peer.tabs.changeActiveManually(description);
330                 if (activated) {
331                     peer.model.getActiveElement().componentActivated();
332                 }
333             }
334
335         }
336         
337         public void requestVisible() {
338             peer.tabs.changeVisibleManually(description);
339         }
340         
341         public Action JavaDoc[] createDefaultActions() {
342             return MultiViewCloneableTopComponent.this.getDefaultTCActions();
343         }
344         
345         public void updateTitle(String JavaDoc title) {
346             MultiViewCloneableTopComponent.this.setDisplayName(title);
347         }
348         
349         /** replace as null - should not be stored and read..*/
350         public Object JavaDoc writeReplace() throws ObjectStreamException {
351             return null;
352         }
353
354         /** Resolve as null -should not be stored and read..*/
355         public Object JavaDoc readResolve() throws ObjectStreamException {
356             return null;
357         }
358         
359         public boolean isSelectedElement() {
360             return (description.equals(peer.model.getActiveDescription()));
361         }
362         
363         public TopComponent getTopComponent() {
364             return MultiViewCloneableTopComponent.this;
365         }
366         
367     }
368     
369 }
370
Popular Tags