KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > ModeImpl


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.windows;
21
22
23 import org.openide.windows.Mode;
24 import org.openide.windows.TopComponent;
25 import org.openide.windows.Workspace;
26
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import java.awt.Image JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35
36
37 /** This class is an implementation of Mode interface.
38  * It designates 'place' on screen, at wich TopComponent can occure.
39  *
40  * @author Peter Zavadsky
41  */

42 public final class ModeImpl implements Mode {
43
44     /** Name constant as a base for nonamed modes. */
45     private static final String JavaDoc MODE_ANONYMOUS_NAME = "anonymousMode"; // NOI18N
46

47     /** asociated property change support for firing property changes */
48     private final PropertyChangeSupport JavaDoc changeSupport = new PropertyChangeSupport JavaDoc(this);
49
50     /** Debugging flag. */
51     private static final boolean DEBUG = Debug.isLoggable(ModeImpl.class);
52     
53     
54     /** Construct new mode with given properties */
55     private ModeImpl(String JavaDoc name, int state, int kind, boolean permanent) {
56         getCentral().createModeModel(this, name, state, kind, permanent);
57     }
58     
59     
60     /** Factory method which creates <code>ModeImpl</code> instances. */
61     public static ModeImpl createModeImpl(String JavaDoc name, int state, int kind, boolean permanent) {
62         return new ModeImpl(name, state, kind, permanent);
63     }
64     
65     
66     ///////////////////////////////////////////////////////////////////
67
// Start of org.openide.windows.Mode interface implementation.
68
///////////////////////////////////////////////////////////////////
69
/** Gets the programmatic name of this mode.
70      * This name should be unique, as it is used to find modes etc.
71      * Implements <code>Mode</code> interface method.
72      * @return programmatic name of this mode */

73     public String JavaDoc getName () {
74         WindowManagerImpl.assertEventDispatchThreadWeak();
75         
76         return getCentral().getModeName(this);
77     }
78     
79     /** Gets display name of this mode.
80      ** Implements <code>Mode</code> interface method.
81      * @return Human presentable name of this mode implementation
82      * @deprecated It is not used anymore. This impl delegated to {@link #getName} method. */

83     public String JavaDoc getDisplayName () {
84         WindowManagerImpl.assertEventDispatchThreadWeak();
85         
86         return getName();
87     }
88
89     /** Gets icon for this mode.
90      * Implements <code>Mode</code> interface method.
91      * @return null
92      * @deprecated It is not used anymore. */

93     public Image JavaDoc getIcon () {
94         WindowManagerImpl.assertEventDispatchThreadWeak();
95         
96         return null;
97     }
98
99     /** Indicates whether specified <code>TopComponent</code> can be docked
100      * into this <code>Mode</code>.
101      * Implements <code>Mode</code> interface method.
102      * @return <code>true</code> */

103     public boolean canDock(TopComponent tc) {
104         WindowManagerImpl.assertEventDispatchThreadWeak();
105         
106         return true;
107     }
108     
109     /** Attaches a component to a mode for this workspace.
110      * If the component is in different mode on this desktop, it is
111      * removed from the original and moved to this one.
112      * Implements <code>Mode</code> interface method.
113      *
114      * @param tc top component to dock into this mode
115      * @return true if top component was succesfully docked to this
116      * mode, false otherwise */

117     public boolean dockInto(TopComponent tc) {
118         WindowManagerImpl.assertEventDispatchThreadWeak();
119         
120         return dockIntoImpl(tc, true);
121     }
122     
123     /** Sets bounds of this mode.
124      * Implements <code>Mode</code> interface method.
125      * @param rect bounds for the mode */

126     public void setBounds (Rectangle JavaDoc bounds) {
127         WindowManagerImpl.assertEventDispatchThreadWeak();
128         
129         getCentral().setModeBounds(this, bounds);
130     }
131
132     /** Getter for current bounds of the mode.
133      * Implements <code>Mode</code> interface method.
134      * @return the bounds of the mode
135      */

136     public Rectangle JavaDoc getBounds () {
137         WindowManagerImpl.assertEventDispatchThreadWeak();
138         
139         return getCentral().getModeBounds(this);
140     }
141     
142
143     /** Getter for asociated workspace.
144      * Implements <code>Mode</code> interface method.
145      * @return The workspace instance to which is this mode asociated.
146      * @deprecated XXX Don't use anymore.
147      */

148     public Workspace getWorkspace () {
149         WindowManagerImpl.assertEventDispatchThreadWeak();
150         
151         // Here is the only fake workspace.
152
return WindowManagerImpl.getInstance();
153     }
154     
155     /** Gets array of <code>TopComponent</code>S in this mode.
156      * Implements <code>Mode</code> interface method.
157      * @return array of top components which are currently
158      * docked in this mode. May return empty array if no top component
159      * is docked in this mode.
160      */

161     public TopComponent[] getTopComponents() {
162         WindowManagerImpl.assertEventDispatchThreadWeak();
163         
164         return getCentral().getModeTopComponents(this).toArray(new TopComponent[0]);
165     }
166
167     /** Adds listener to the property changes.
168      * Implements <code>Mode</code> interface support. */

169     public void addPropertyChangeListener (PropertyChangeListener JavaDoc pchl) {
170         changeSupport.addPropertyChangeListener(pchl);
171     }
172
173     /** Removes listener to the property changes.
174      * Implements <code>Mode</code> interface method. */

175     public void removePropertyChangeListener (PropertyChangeListener JavaDoc pchl) {
176         changeSupport.removePropertyChangeListener(pchl);
177     }
178     ///////////////////////////////////////////////////////////////////
179
// End of org.openide.windows.Mode interface implementation.
180
///////////////////////////////////////////////////////////////////
181

182     
183     /** Actually performs the docking operation.
184      * @param tc top component to dock into this mode
185      * @param orderWeight weight for ordering. Smaller weight number means
186      * smaller position index, which means closer to the top or start in
187      * visual representations
188      * @param select <code>true</code> if the docked <code>TopComponent</code>
189      * will be selected afterwards
190      * @return true if top component was succesfully docked to this */

191     private boolean dockIntoImpl(final TopComponent tc, final boolean select) {
192         if(DEBUG) {
193             Debug.log(ModeImpl.class, "Docking tc=" + tc.getName() + " into mode=" + this); // NOI18N
194
Debug.dumpStack(ModeImpl.class);
195         }
196         
197         // PENDING
198
// Preferably all in one step.
199
ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc);
200         if(mode != null && mode != this) {
201             // XXX if only closin (mode.close(tc)) there could happen,
202
// there is the same TopComponent as closed in two modes. Revise.
203
mode.removeTopComponent(tc);
204         }
205         
206         addClosedTopComponent(tc);
207         return true;
208     }
209     
210     /** Closes given top component. */
211     public void close(TopComponent tc) {
212         if(!getOpenedTopComponents().contains(tc)) {
213             return;
214         }
215         
216         if(WindowManagerImpl.getInstance().isTopComponentPersistentWhenClosed(tc)) {
217             addClosedTopComponent(tc);
218         } else {
219             removeTopComponent(tc);
220         }
221     }
222
223     /** Gets list of opened TopComponentS. */
224     public List JavaDoc<TopComponent> getOpenedTopComponents() {
225         return getCentral().getModeOpenedTopComponents(this);
226     }
227     
228     /** Sets selected TopComponent. */
229     public void setSelectedTopComponent(TopComponent tc) {
230         if(!getOpenedTopComponents().contains(tc)) {
231             return;
232         }
233         
234         TopComponent old = getSelectedTopComponent();
235         if(tc == old) {
236             return;
237         }
238         
239         getCentral().setModeSelectedTopComponent(this, tc);
240     }
241     
242     /** Gets selected TopComponent. */
243     public TopComponent getSelectedTopComponent() {
244         WindowManagerImpl.assertEventDispatchThread();
245         
246         return getCentral().getModeSelectedTopComponent(this);
247     }
248     
249     /**
250      * Remember which top component was previously the selected one.
251      * Used when switching to/from maximized mode.
252      */

253     public void setPreviousSelectedTopComponent(TopComponent tc) {
254         TopComponent old = getPreviousSelectedTopComponent();
255         if(tc == old) {
256             return;
257         }
258         
259         getCentral().setModePreviousSelectedTopComponent(this, tc);
260     }
261     
262     /**
263      * @return The top component that was the selected one before switching to/from
264      * the maximized mode.
265      */

266     public TopComponent getPreviousSelectedTopComponent() {
267         WindowManagerImpl.assertEventDispatchThread();
268         
269         return getCentral().getModePreviousSelectedTopComponent(this);
270     }
271     
272     public void addOpenedTopComponent(TopComponent tc) {
273         getCentral().addModeOpenedTopComponent(this, tc);
274     }
275     
276     public void addOpenedTopComponent(TopComponent tc, int index) {
277         getCentral().insertModeOpenedTopComponent( this, tc, index );
278     }
279     
280     public void addClosedTopComponent(TopComponent tc) {
281         getCentral().addModeClosedTopComponent(this, tc);
282     }
283     
284     public void addUnloadedTopComponent(String JavaDoc tcID) {
285         getCentral().addModeUnloadedTopComponent(this, tcID);
286     }
287     
288     public void setUnloadedSelectedTopComponent(String JavaDoc tcID) {
289         getCentral().setUnloadedSelectedTopComponent(this, tcID);
290     }
291     
292     public void setUnloadedPreviousSelectedTopComponent(String JavaDoc tcID) {
293         getCentral().setUnloadedPreviousSelectedTopComponent(this, tcID);
294     }
295     
296     // XXX
297
public List JavaDoc<String JavaDoc> getOpenedTopComponentsIDs() {
298         return getCentral().getModeOpenedTopComponentsIDs(this);
299     }
300     // XXX
301
public List JavaDoc getClosedTopComponentsIDs() {
302         return getCentral().getModeClosedTopComponentsIDs(this);
303     }
304     // XXX
305
public List JavaDoc getTopComponentsIDs() {
306         return getCentral().getModeTopComponentsIDs(this);
307     }
308     
309     /** Sets and updates the state of associated frame, if frame exists.
310      * Otherwise remembers state for futher use
311      */

312     public void setFrameState(int state) {
313         getCentral().setModeFrameState(this, state);
314     }
315     
316     /** @return state of the frame
317      * If frame exists, its real state is returned.
318      * Last remembered frame state is returned if frame currently
319      * doesn't exist. FrameType.NORMAL is returned as default if state cannot be
320      * obtained by mentioned procedures.
321      */

322     public int getFrameState () {
323         return getCentral().getModeFrameState(this);
324     }
325     
326     /** Indicates whether this mode is permanent, it means it is kept in model
327      * even in case it becomes empty. */

328     public boolean isPermanent () {
329         return getCentral().isModePermanent(this);
330     }
331     
332     /** Indicates whether this mode has no TopComponents. */
333     public boolean isEmpty() {
334         return getCentral().isModeEmpty(this);
335     }
336
337     public boolean containsTopComponent(TopComponent tc) {
338         return getCentral().containsModeTopComponent(this, tc);
339     }
340     
341     /** Gets state of mode. Either split or separate. */
342     public int getState() {
343         return getCentral().getModeState(this);
344     }
345     
346     /** Gets kind, either editor or view. */
347     public int getKind() {
348         return getCentral().getModeKind(this);
349     }
350     /** Gets side, either null for view and editor kinds, a side constant for sliding kind.. */
351     public String JavaDoc getSide() {
352         return getCentral().getModeSide(this);
353     }
354     
355     // Contstraints and split weights are saved in split structure at wm model level.
356
/** Sets constraints for mode. */
357     public void setConstraints(SplitConstraint[] constraints) {
358         WindowManagerImpl.getInstance().setModeConstraints(this, constraints);
359     }
360
361     /** @return Current constraints of this mode, null by default */
362     public SplitConstraint[] getConstraints() {
363         return WindowManagerImpl.getInstance().getModeConstraints(this);
364     }
365     
366     /** Removes TopComponent from this mode. */
367     public void removeTopComponent(TopComponent tc) {
368         getCentral().removeModeTopComponent(this, tc);
369     }
370     
371     public void removeTopComponents(Set JavaDoc topComponentSet) {
372         for(Iterator JavaDoc it = topComponentSet.iterator(); it.hasNext(); ) {
373             TopComponent tc = (TopComponent)it.next();
374             removeTopComponent(tc);
375         }
376     }
377     
378     // XXX Only use for yet unloaded components, for PersistenceHandler only.
379
public void removeClosedTopComponentID(String JavaDoc tcID) {
380         getCentral().removeModeClosedTopComponentID(this, tcID);
381     }
382     
383     // XXX It is used for user actions only, to prohibit mixing
384
// of view and editor components.
385
/** Indicates whether this mode can contain specified TopComponent. */
386     public boolean canContain(TopComponent tc) {
387         if(Constants.SWITCH_MODE_ADD_NO_RESTRICT
388         || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(tc)) {
389             return true;
390         }
391         
392         ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc);
393         if(mode == null) {
394             return true;
395         }
396         // allow mixing of view and sliding modes
397
int myKind = getKind();
398         int otherKind = mode.getKind();
399         
400         return (myKind == otherKind) ||
401                (myKind != Constants.MODE_KIND_EDITOR && otherKind != Constants.MODE_KIND_EDITOR);
402     }
403     
404     void doFirePropertyChange(final String JavaDoc propName,
405     final Object JavaDoc oldValue, final Object JavaDoc newValue) {
406         // PENDING When #37529 finished, then uncomment the next row and move the
407
// checks of AWT thread away.
408
// WindowManagerImpl.assertEventDispatchThread();
409
if(SwingUtilities.isEventDispatchThread()) {
410             changeSupport.firePropertyChange(propName, oldValue, newValue);
411         } else {
412             SwingUtilities.invokeLater(new Runnable JavaDoc() {
413                 public void run() {
414                     changeSupport.firePropertyChange(propName, oldValue, newValue);
415                 }
416             });
417         }
418     }
419     
420     /** @return string description of this mode */
421     public String JavaDoc toString () {
422         // #42995 - don't scream when toString called from non-AWT thread
423
return super.toString () + "[" + getCentral().getModeName(this) + "]"; // NOI18N
424
}
425     
426     /** Accessor to central unit. Helper method. */
427     private static Central getCentral() {
428         return WindowManagerImpl.getInstance().getCentral();
429     }
430     
431     
432     ////////////////////
433
// Utility methods>>
434
/*private*/ static String JavaDoc getUnusedModeName() {
435         String JavaDoc base = MODE_ANONYMOUS_NAME;
436         
437         // don't allow base to be too long, because will act as file name too
438
// PENDING Maximal length is 20.
439
if (base.length() > 20) {
440             base = base.substring(0, 20);
441         }
442         
443         // add numbers to the name
444
String JavaDoc result;
445         int modeNumber = 1;
446         WindowManagerImpl wm = WindowManagerImpl.getInstance();
447         while(wm.findMode(result = base + "_" + modeNumber) != null) { // NOI18N
448
modeNumber++;
449         }
450         return result;
451     }
452     // Utility methods<<
453
////////////////////
454

455     
456 }
457
458
Popular Tags