KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.core.api.multiview.MultiViewHandler;
38 import org.netbeans.core.api.multiview.MultiViewPerspective;
39 import org.netbeans.core.multiview.MultiViewModel.ActionRequestObserverFactory;
40 import org.netbeans.core.multiview.MultiViewModel.ElementSelectionListener;
41 import org.netbeans.core.spi.multiview.CloseOperationHandler;
42 import org.netbeans.core.spi.multiview.CloseOperationState;
43 import org.netbeans.core.spi.multiview.MultiViewDescription;
44 import org.netbeans.core.spi.multiview.MultiViewElement;
45 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
46 import org.netbeans.core.spi.multiview.MultiViewFactory;
47 import org.openide.awt.UndoRedo;
48 import org.openide.text.CloneableEditorSupport;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.Lookup;
51 import org.openide.util.lookup.Lookups;
52 import org.openide.util.lookup.ProxyLookup;
53 import org.openide.windows.CloneableTopComponent;
54 import org.openide.windows.TopComponent;
55
56
57 /** Special subclass of TopComponent which shows and handles set of
58  * MultiViewElements, shows them in switchable toggle buttons style, along
59  * with toolbsrs af actions asociated with individual view elements.
60  *
61  *
62  * @author Dafe Simonek, Milos Kleint
63  */

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

138     public Action JavaDoc[] getActions() {
139         //TEMP don't delegate to element's actions..
140
Action JavaDoc[] superActions = super.getActions();
141         Action JavaDoc[] acts = peer.peerGetActions(superActions);
142         return acts;
143     }
144     
145     public MultiViewHandlerDelegate getMultiViewHandlerDelegate() {
146         // TODO have one handler only or create a new one each time?
147
return peer.getMultiViewHandlerDelegate();
148     }
149     
150     /**
151      * Delegates the value to the element descriptions.
152      */

153     public int getPersistenceType() {
154         return peer.getPersistenceType();
155     }
156     
157     protected String JavaDoc preferredID() {
158         return peer.preferredID();
159     }
160     
161     
162     
163     /** Serialize this top component.
164     * Subclasses wishing to store state must call the super method, then write to the stream.
165     * @param out the stream to serialize to
166     */

167     public void writeExternal (ObjectOutput out) throws IOException {
168         super.writeExternal(out);
169         peer.peerWriteExternal(out);
170     }
171
172     /** Deserialize this top component.
173     * Subclasses wishing to store state must call the super method, then read from the stream.
174     * @param in the stream to deserialize from
175     */

176     public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
177         super.readExternal(in);
178         peer.peerReadExternal(in);
179     }
180     
181     
182     Action JavaDoc[] getDefaultTCActions() {
183         return super.getActions();
184     }
185     
186     public MultiViewElementCallback createElementCallback(MultiViewDescription desc) {
187         return SpiAccessor.DEFAULT.createCallback(new ActReqObserver(desc));
188     }
189     
190     
191     public HelpCtx getHelpCtx() {
192         return peer.getHelpCtx();
193     }
194
195     public String JavaDoc toString() {
196         return "MultiViewTopComponent[name=" + getDisplayName() + ", peer=" + peer + "]"; // NOI18N
197
}
198
199     /**
200      * Get the undo/redo support for this component.
201      * The default implementation returns a dummy support that cannot
202      * undo anything.
203      *
204      * @return undoable edit for this component
205      */

206     public UndoRedo getUndoRedo() {
207         UndoRedo retValue;
208         retValue = peer.peerGetUndoRedo();
209         if (retValue == null) {
210             retValue = super.getUndoRedo();
211         }
212         return retValue;
213     }
214
215     /**
216      * This method is called when this <code>TopComponent</code> is about to close.
217      * Delegates to CloseOperationHandler.
218      */

219     public boolean canClose() {
220         return peer.canClose();
221     }
222
223     /**
224      * delegate to the apppropriate active element's component
225      */

226     public boolean requestFocusInWindow() {
227         return peer.requestFocusInWindow();
228     }
229
230     /**
231      * delegate to the apppropriate active element's component
232      */

233     public void requestFocus() {
234         peer.requestFocus();
235     }
236     
237 // public Lookup getLookup() {
238
// return peer.getLookup(super.getLookup());
239
// }
240

241     /**
242      * implementation of the MultiViewElement.ActionRequestObserver, manages activatation of the elements
243      * and the TC itself based on requests from the elements.
244      */

245     class ActReqObserver implements Serializable, MultiViewElementCallbackDelegate {
246         
247         private static final long serialVersionUID =-3126744916624172415L;
248         private MultiViewDescription description;
249         
250         ActReqObserver(MultiViewDescription desc) {
251             description = desc;
252         }
253         
254         public void requestActive() {
255             boolean activated = peer.isActivated();
256             if (!activated) {
257                 MultiViewTopComponent.this.requestActive();
258             }
259             if (peer.model.getActiveDescription() != description) {
260                 if (activated) {
261                     peer.model.getActiveElement().componentDeactivated();
262                 }
263                 peer.tabs.changeActiveManually(description);
264                 if (activated) {
265                     peer.model.getActiveElement().componentActivated();
266                 }
267             }
268         }
269         
270         public void requestVisible() {
271             peer.tabs.changeVisibleManually(description);
272         }
273         
274         public Action JavaDoc[] createDefaultActions() {
275             return MultiViewTopComponent.this.getDefaultTCActions();
276         }
277         
278         public void updateTitle(String JavaDoc title) {
279             MultiViewTopComponent.this.setDisplayName(title);
280         }
281         
282         /** replace as null - should not be stored and read..*/
283         public Object JavaDoc writeReplace() throws ObjectStreamException {
284             return null;
285         }
286
287         /** Resolve as null -should not be stored and read..*/
288         public Object JavaDoc readResolve() throws ObjectStreamException {
289             return null;
290         }
291         
292         public boolean isSelectedElement() {
293             return (description.equals(peer.model.getActiveDescription()));
294         }
295         
296         public TopComponent getTopComponent() {
297             return MultiViewTopComponent.this;
298         }
299         
300     }
301     
302 }
Popular Tags