KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > DummyWindowManager


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.openide.windows;
21
22 import java.awt.Frame JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.event.WindowAdapter JavaDoc;
26 import java.awt.event.WindowEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.TreeMap JavaDoc;
38 import java.util.WeakHashMap JavaDoc;
39 import javax.swing.Action JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import org.openide.nodes.Node;
43 import org.openide.util.Utilities;
44 import org.openide.util.actions.SystemAction;
45
46 /**
47  * Trivial window manager that just keeps track of "workspaces" and "modes"
48  * according to contract but does not really use them, and just opens all
49  * top components in their own frames.
50  * Useful in case core-windows.jar is not installed, e.g. in standalone usage.
51  * @author Jesse Glick
52  * @see "#29933"
53  *
54  */

55 @SuppressWarnings JavaDoc("deprecation")
56 final class DummyWindowManager extends WindowManager {
57
58     private static final boolean VISIBLE = Boolean.parseBoolean(System.getProperty("org.openide.windows.DummyWindowManager.VISIBLE", "true"));
59     private static final long serialVersionUID = 1L;
60     private static Action JavaDoc[] DEFAULT_ACTIONS_CLONEABLE;
61     private static Action JavaDoc[] DEFAULT_ACTIONS_NOT_CLONEABLE;
62     private final Map JavaDoc<String JavaDoc,Workspace> workspaces;
63     private transient Frame JavaDoc mw;
64     private transient PropertyChangeSupport JavaDoc pcs;
65     private transient R r;
66
67     public DummyWindowManager() {
68         workspaces = new TreeMap JavaDoc<String JavaDoc,Workspace>();
69         createWorkspace("default", null).createMode(/*CloneableEditorSupport.EDITOR_MODE*/"editor", "editor", null); // NOI18N
70
}
71
72     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
73         if (pcs == null) {
74             pcs = new PropertyChangeSupport JavaDoc(this);
75         }
76
77         pcs.addPropertyChangeListener(l);
78     }
79
80     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
81         if (pcs != null) {
82             pcs.removePropertyChangeListener(l);
83         }
84     }
85
86     protected TopComponent.Registry componentRegistry() {
87         TopComponent.Registry reg = super.componentRegistry();
88
89         if (reg != null) {
90             return reg;
91         } else {
92             return registry();
93         }
94     }
95
96     synchronized R registry() {
97         if (r == null) {
98             r = new R();
99         }
100
101         return r;
102     }
103
104     protected WindowManager.Component createTopComponentManager(TopComponent c) {
105         return null; // Not used anymore.
106
}
107
108     public synchronized Workspace createWorkspace(String JavaDoc name, String JavaDoc displayName) {
109         Workspace w = new W(name);
110         workspaces.put(name, w);
111
112         if (pcs != null) {
113             pcs.firePropertyChange(PROP_WORKSPACES, null, null);
114             pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
115         }
116
117         return w;
118     }
119
120     synchronized void delete(Workspace w) {
121         workspaces.remove(w.getName());
122
123         if (workspaces.isEmpty()) {
124             createWorkspace("default", null); // NOI18N
125
}
126
127         if (pcs != null) {
128             pcs.firePropertyChange(PROP_WORKSPACES, null, null);
129             pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
130         }
131     }
132
133     public synchronized Workspace findWorkspace(String JavaDoc name) {
134         return workspaces.get(name);
135     }
136
137     public synchronized Workspace getCurrentWorkspace() {
138         return workspaces.values().iterator().next();
139     }
140
141     public synchronized Workspace[] getWorkspaces() {
142         return workspaces.values().toArray(new Workspace[0]);
143     }
144
145     public synchronized void setWorkspaces(Workspace[] ws) {
146         if (ws.length == 0) {
147             throw new IllegalArgumentException JavaDoc();
148         }
149
150         workspaces.clear();
151
152         for (int i = 0; i < ws.length; i++) {
153             workspaces.put(ws[i].getName(), ws[i]);
154         }
155
156         if (pcs != null) {
157             pcs.firePropertyChange(PROP_WORKSPACES, null, null);
158             pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
159         }
160     }
161
162     public synchronized Frame JavaDoc getMainWindow() {
163         if (mw == null) {
164             mw = new JFrame JavaDoc("dummy"); // NOI18N
165
}
166
167         return mw;
168     }
169
170     public void updateUI() {
171     }
172
173     // Modes
174
public Set JavaDoc<Mode> getModes() {
175         Set JavaDoc<Mode> s = new HashSet JavaDoc<Mode>();
176
177         for (Iterator JavaDoc<Workspace> it = new HashSet JavaDoc<Workspace>(workspaces.values()).iterator(); it.hasNext();) {
178             Workspace w = it.next();
179             s.addAll(w.getModes());
180         }
181
182         return s;
183     }
184
185     public Mode findMode(TopComponent tc) {
186         for (Iterator JavaDoc<Mode> it = getModes().iterator(); it.hasNext();) {
187             Mode m = it.next();
188
189             if (Arrays.asList(m.getTopComponents()).contains(tc)) {
190                 return m;
191             }
192         }
193
194         return null;
195     }
196
197     public Mode findMode(String JavaDoc name) {
198         if (name == null) {
199             return null;
200         }
201
202         for (Iterator JavaDoc<Mode> it = getModes().iterator(); it.hasNext();) {
203             Mode m = it.next();
204
205             if (name.equals(m.getName())) {
206                 return m;
207             }
208         }
209
210         return null;
211     }
212
213     // PENDING Groups not supported.
214
public TopComponentGroup findTopComponentGroup(String JavaDoc name) {
215         return null;
216     }
217
218     //Not supported. Need to access PersistenceManager.
219
public TopComponent findTopComponent(String JavaDoc tcID) {
220         return null;
221     }
222
223     protected String JavaDoc topComponentID(TopComponent tc, String JavaDoc preferredID) {
224         return preferredID;
225     }
226
227     protected Action JavaDoc[] topComponentDefaultActions(TopComponent tc) {
228         // XXX It could be better to provide non-SystemAction instances.
229
synchronized (DummyWindowManager.class) {
230             //Bugfix #33557: Do not provide CloneViewAction when
231
//TopComponent does not implement TopComponent.Cloneable
232
if (tc instanceof TopComponent.Cloneable) {
233                 if (DEFAULT_ACTIONS_CLONEABLE == null) {
234                     DEFAULT_ACTIONS_CLONEABLE = loadActions(
235                             new String JavaDoc[] { "Save", // NOI18N
236
"CloneView", // NOI18N
237
null, "CloseView" // NOI18N
238
}
239                         );
240                 }
241
242                 return DEFAULT_ACTIONS_CLONEABLE;
243             } else {
244                 if (DEFAULT_ACTIONS_NOT_CLONEABLE == null) {
245                     DEFAULT_ACTIONS_NOT_CLONEABLE = loadActions(
246                             new String JavaDoc[] { "Save", // NOI18N
247
null, "CloseView" // NOI18N
248
}
249                         );
250                 }
251
252                 return DEFAULT_ACTIONS_NOT_CLONEABLE;
253             }
254         }
255     }
256
257     private static Action JavaDoc[] loadActions(String JavaDoc[] names) {
258         ArrayList JavaDoc<Action JavaDoc> arr = new ArrayList JavaDoc<Action JavaDoc>();
259         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
260
261         for (int i = 0; i < names.length; i++) {
262             if (names[i] == null) {
263                 arr.add(null);
264
265                 continue;
266             }
267
268             try {
269                 Class JavaDoc<? extends SystemAction> sa = Class.forName("org.openide.actions." + names[i] + "Action", true, loader).asSubclass(SystemAction.class);
270                 arr.add(SystemAction.get(sa)); // NOI18N
271
} catch (ClassNotFoundException JavaDoc e) {
272                 // ignore it, missing org-openide-actions.jar
273
}
274         }
275
276         return arr.toArray(new Action JavaDoc[0]);
277     }
278
279     protected boolean topComponentIsOpened(TopComponent tc) {
280         return tc.isShowing();
281     }
282
283     protected void topComponentActivatedNodesChanged(TopComponent tc, Node[] nodes) {
284         registry().setActivatedNodes(tc, nodes);
285     }
286
287     protected void topComponentIconChanged(TopComponent tc, Image JavaDoc icon) {
288         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
289
290         if (f != null) {
291             f.setIconImage(icon);
292         }
293     }
294
295     protected void topComponentToolTipChanged(TopComponent tc, String JavaDoc tooltip) {
296         // No op.
297
}
298
299     protected void topComponentDisplayNameChanged(TopComponent tc, String JavaDoc displayName) {
300         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
301
302         if (f != null) {
303             f.setTitle(displayName);
304         }
305     }
306     
307     protected void topComponentHtmlDisplayNameChanged(TopComponent tc, String JavaDoc htmlDisplayName) {
308         // no operarion, html looks ugly in frame titles
309
}
310
311     protected void topComponentOpen(TopComponent tc) {
312         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
313
314         if (f == null) {
315             f = new JFrame JavaDoc(tc.getName());
316
317             Image JavaDoc icon = tc.getIcon();
318
319             if (icon != null) {
320                 f.setIconImage(icon);
321             }
322
323             f.getContentPane().add(tc);
324             f.pack();
325
326             final java.lang.ref.WeakReference JavaDoc<TopComponent> ref = new java.lang.ref.WeakReference JavaDoc<TopComponent>(tc);
327             f.addWindowListener(
328                 new WindowAdapter JavaDoc() {
329                     public void windowClosing(WindowEvent JavaDoc ev) {
330                         TopComponent tc = ref.get();
331
332                         if (tc == null) {
333                             return;
334                         }
335
336                         tc.close();
337                     }
338
339                     public void windowActivated(WindowEvent JavaDoc e) {
340                         TopComponent tc = ref.get();
341
342                         if (tc == null) {
343                             return;
344                         }
345
346                         tc.requestActive();
347                     }
348                 }
349             );
350         }
351
352         if (!tc.isShowing()) {
353             componentOpenNotify(tc);
354             componentShowing(tc);
355             if (VISIBLE) {
356                 f.setVisible(true);
357             }
358             registry().open(tc);
359         }
360     }
361
362     protected void topComponentClose(TopComponent tc) {
363         componentHidden(tc);
364         componentCloseNotify(tc);
365
366         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
367
368         if (f != null) {
369             if (VISIBLE) {
370                 f.setVisible(false);
371             }
372             tc.getParent().remove(tc);
373         }
374
375         registry().close(tc);
376
377         java.util.Iterator JavaDoc it = workspaces.values().iterator();
378
379         while (it.hasNext()) {
380             W w = (W) it.next();
381             w.close(tc);
382         }
383     }
384
385     protected void topComponentRequestVisible(TopComponent tc) {
386         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
387
388         if (f != null) {
389             if (VISIBLE) {
390                 f.setVisible(true);
391             }
392         }
393     }
394
395     protected void topComponentRequestActive(TopComponent tc) {
396         JFrame JavaDoc f = (JFrame JavaDoc) SwingUtilities.getAncestorOfClass(JFrame JavaDoc.class, tc);
397
398         if (f != null) {
399             f.toFront();
400         }
401
402         registry().setActive(tc);
403         activateComponent(tc);
404     }
405
406     protected void topComponentRequestAttention(TopComponent tc) {
407         //TODO what to do here?
408
}
409
410     protected void topComponentCancelRequestAttention(TopComponent tc) {
411         //TODO what to do here?
412
}
413
414     private final class W implements Workspace {
415         private static final long serialVersionUID = 1L;
416         private final String JavaDoc name;
417         private final Map JavaDoc<String JavaDoc,Mode> modes = new HashMap JavaDoc<String JavaDoc,Mode>();
418         private final Map JavaDoc<TopComponent,Mode> modesByComponent = new WeakHashMap JavaDoc<TopComponent,Mode>();
419         private transient PropertyChangeSupport JavaDoc pcs;
420
421         public W(String JavaDoc name) {
422             this.name = name;
423         }
424
425         public void activate() {
426         }
427
428         public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc list) {
429             if (pcs == null) {
430                 pcs = new PropertyChangeSupport JavaDoc(this);
431             }
432
433             pcs.addPropertyChangeListener(list);
434         }
435
436         public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc list) {
437             if (pcs != null) {
438                 pcs.removePropertyChangeListener(list);
439             }
440         }
441
442         public void remove() {
443             DummyWindowManager.this.delete(this);
444         }
445
446         public synchronized Mode createMode(String JavaDoc name, String JavaDoc displayName, URL JavaDoc icon) {
447             Mode m = new M(name);
448             modes.put(name, m);
449
450             if (pcs != null) {
451                 pcs.firePropertyChange(PROP_MODES, null, null);
452             }
453
454             return m;
455         }
456
457         public synchronized Set JavaDoc<Mode> getModes() {
458             return new HashSet JavaDoc<Mode>(modes.values());
459         }
460
461         public synchronized Mode findMode(String JavaDoc name) {
462             return modes.get(name);
463         }
464
465         public synchronized Mode findMode(TopComponent c) {
466             return modesByComponent.get(c);
467         }
468
469         synchronized void dock(Mode m, TopComponent c) {
470             modesByComponent.put(c, m);
471         }
472
473         public Rectangle JavaDoc getBounds() {
474             return Utilities.getUsableScreenBounds();
475         }
476
477         public String JavaDoc getName() {
478             return name;
479         }
480
481         public String JavaDoc getDisplayName() {
482             return getName();
483         }
484
485         public void close(TopComponent tc) {
486             java.util.Iterator JavaDoc it = modes.values().iterator();
487
488             while (it.hasNext()) {
489                 M m = (M) it.next();
490                 m.close(tc);
491             }
492         }
493
494         private final class M implements Mode {
495             private static final long serialVersionUID = 1L;
496             private final String JavaDoc name;
497             private final Set JavaDoc<TopComponent> components = new HashSet JavaDoc<TopComponent>();
498
499             public M(String JavaDoc name) {
500                 this.name = name;
501             }
502
503             public void close(TopComponent tc) {
504                 components.remove(tc);
505             }
506
507             /* Not needed:
508             private transient PropertyChangeSupport pcs;
509             public synchronized void addPropertyChangeListener(PropertyChangeListener list) {
510                 if (pcs == null) {
511                     pcs = new PropertyChangeSupport(this);
512                 }
513                 pcs.addPropertyChangeListener(list);
514             }
515             public synchronized void removePropertyChangeListener(PropertyChangeListener list) {
516                 if (pcs != null) {
517                     pcs.removePropertyChangeListener(list);
518                 }
519             }
520              */

521             public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
522             }
523
524             public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
525             }
526
527             public boolean canDock(TopComponent tc) {
528                 return true;
529             }
530
531             public synchronized boolean dockInto(TopComponent c) {
532                 if (components.add(c)) {
533                     Mode old = findMode(c);
534
535                     if ((old != null) && (old != this) && old instanceof M) {
536                         synchronized (old) {
537                             ((M) old).components.remove(c);
538                         }
539                     }
540
541                     dock(this, c);
542                 }
543
544                 return true;
545             }
546
547             public String JavaDoc getName() {
548                 return name;
549             }
550
551             public String JavaDoc getDisplayName() {
552                 return getName();
553             }
554
555             public Image JavaDoc getIcon() {
556                 return null;
557             }
558
559             public synchronized TopComponent[] getTopComponents() {
560                 return components.toArray(new TopComponent[0]);
561             }
562
563             public Workspace getWorkspace() {
564                 return W.this;
565             }
566
567             public synchronized Rectangle JavaDoc getBounds() {
568                 return W.this.getBounds();
569             }
570
571             public void setBounds(Rectangle JavaDoc s) {
572             }
573
574             public TopComponent getSelectedTopComponent() {
575                 TopComponent[] tcs = components.toArray(new TopComponent[0]);
576
577                 return (tcs.length > 0) ? tcs[0] : null;
578             }
579         }
580     }
581
582     private static final class R implements TopComponent.Registry {
583         private TopComponent active;
584         private final Set JavaDoc<TopComponent> opened;
585         private Node[] nodes;
586         private PropertyChangeSupport JavaDoc pcs;
587
588         public R() {
589             opened = new HashSet JavaDoc<TopComponent>();
590             nodes = new Node[0];
591         }
592
593         public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
594             if (pcs == null) {
595                 pcs = new PropertyChangeSupport JavaDoc(this);
596             }
597
598             pcs.addPropertyChangeListener(l);
599         }
600
601         public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
602             if (pcs != null) {
603                 pcs.removePropertyChangeListener(l);
604             }
605         }
606
607         synchronized void open(TopComponent tc) {
608             opened.add(tc);
609
610             if (pcs != null) {
611                 pcs.firePropertyChange(PROP_TC_OPENED, null, tc);
612                 pcs.firePropertyChange(PROP_OPENED, null, null);
613             }
614         }
615
616         synchronized void close(TopComponent tc) {
617             opened.remove(tc);
618
619             if (pcs != null) {
620                 pcs.firePropertyChange(PROP_TC_CLOSED, null, tc);
621                 pcs.firePropertyChange(PROP_OPENED, null, null);
622             }
623
624             if (active == tc) {
625                 setActive(null);
626             }
627         }
628
629         public synchronized Set JavaDoc<TopComponent> getOpened() {
630             return new HashSet JavaDoc<TopComponent>(opened);
631         }
632
633         synchronized void setActive(TopComponent tc) {
634             active = tc;
635
636             Node[] _nodes = (tc == null) ? new Node[0] : tc.getActivatedNodes();
637
638             if (_nodes != null) {
639                 nodes = _nodes;
640
641                 if (pcs != null) {
642                     pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null);
643                 }
644             }
645
646             if (pcs != null) {
647                 pcs.firePropertyChange(PROP_ACTIVATED, null, null);
648                 pcs.firePropertyChange(PROP_CURRENT_NODES, null, null);
649             }
650         }
651
652         synchronized void setActivatedNodes(TopComponent tc, Node[] _nodes) {
653             if (tc == active) {
654                 if (_nodes != null) {
655                     nodes = _nodes;
656
657                     if (pcs != null) {
658                         pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null);
659                     }
660                 }
661
662                 if (pcs != null) {
663                     pcs.firePropertyChange(PROP_CURRENT_NODES, null, null);
664                 }
665             }
666         }
667
668         public TopComponent getActivated() {
669             return active;
670         }
671
672         public Node[] getActivatedNodes() {
673             return nodes;
674         }
675
676         public synchronized Node[] getCurrentNodes() {
677             if (active != null) {
678                 return active.getActivatedNodes();
679             } else {
680                 return null;
681             }
682         }
683     }
684 }
685
Popular Tags