KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > WindowTable


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.execution;
21
22 import java.awt.Window JavaDoc;
23 import java.awt.event.WindowAdapter JavaDoc;
24 import java.awt.event.WindowEvent JavaDoc;
25 import java.awt.event.WindowListener JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31 *
32 * @author Ales Novak
33 */

34 final class WindowTable extends HashMap JavaDoc<Window JavaDoc,TaskThreadGroup> {
35     /** generated Serialized Version UID */
36     static final long serialVersionUID = -1494996298725028533L;
37
38     /** window listener */
39     private WindowListener JavaDoc winListener;
40
41     /** maps ThreadGroup:ArrayList, ArrayList keeps windows */
42     private HashMap JavaDoc<ThreadGroup JavaDoc,ArrayList JavaDoc<Window JavaDoc>> windowMap;
43
44     /**
45     *default constructor
46     */

47     public WindowTable () {
48         super(16);
49         windowMap = new HashMap JavaDoc<ThreadGroup JavaDoc,ArrayList JavaDoc<Window JavaDoc>>(16);
50         winListener = new WindowAdapter JavaDoc() {
51                           public void windowClosed(WindowEvent JavaDoc ev) {
52                               Window JavaDoc win;
53                               removeWindow(win = (Window JavaDoc)ev.getSource());
54                               win.removeWindowListener(this);
55                           }
56                       };
57     }
58
59     public synchronized void putTaskWindow(Window JavaDoc win, TaskThreadGroup grp) {
60         ArrayList JavaDoc<Window JavaDoc> vec;
61         if ((vec = windowMap.get(grp)) == null) {
62             vec = new ArrayList JavaDoc<Window JavaDoc>();
63             windowMap.put(grp, vec);
64         }
65         vec.add(win);
66         win.addWindowListener(winListener);
67         super.put(win, grp);
68     }
69
70     public TaskThreadGroup getThreadGroup(Window JavaDoc win) {
71         return super.get(win);
72     }
73
74     /** closes windows opened by grp ThreadGroup */
75     void closeGroup(ThreadGroup JavaDoc grp) {
76         Window JavaDoc win;
77         ArrayList JavaDoc<Window JavaDoc> vec = windowMap.get(grp);
78         if (vec == null) return;
79         Iterator JavaDoc<Window JavaDoc> ee = vec.iterator();
80         while (ee.hasNext()) {
81             (win = ee.next()).setVisible(false);
82             remove(win);
83             if (win != getSharedOwnerFrame()) {
84                 win.dispose();
85             }
86         }
87         windowMap.remove(grp);
88     }
89     
90     // XXX todo nasty hack into Swing
91
private static java.awt.Frame JavaDoc shOwnerFrame;
92     private static java.awt.Frame JavaDoc getSharedOwnerFrame() {
93         if (shOwnerFrame != null) {
94             return shOwnerFrame;
95         }
96         
97         try {
98             Class JavaDoc swUtil = Class.forName("javax.swing.SwingUtilities"); // NOI18N
99
java.lang.reflect.Method JavaDoc getter = swUtil.getDeclaredMethod("getSharedOwnerFrame", new Class JavaDoc[] {}); // NOI18N
100
getter.setAccessible(true);
101             
102             shOwnerFrame = (java.awt.Frame JavaDoc) getter.invoke(null, new Object JavaDoc[] {});
103         } catch (Exception JavaDoc e) {
104             // do nothing
105
}
106         
107         return shOwnerFrame;
108     }
109
110     /** return true if the ThreadGroup has any windows */
111     boolean hasWindows(ThreadGroup JavaDoc grp) {
112         ArrayList JavaDoc<Window JavaDoc> vec = windowMap.get(grp);
113         if ((vec == null) || (vec.size() == 0) || hiddenWindows(vec)) {
114             return false;
115         }
116         return true;
117     }
118
119     /**
120     * @param vec is a ArrayList of windows
121     * @param grp is a ThreadGroup that belongs to the ArrayList
122     * @return true if all windows in the ArrayList vec are invisible
123     */

124     private boolean hiddenWindows(ArrayList JavaDoc<Window JavaDoc> vec) {
125         Iterator JavaDoc<Window JavaDoc> ee = vec.iterator();
126         Window JavaDoc win;
127         while (ee.hasNext()) {
128             win = ee.next();
129             if (win.isVisible()) return false;
130         }
131         // windows will be removed later
132
return true;
133     }
134
135     /** removes given window */
136     private void removeWindow(Window JavaDoc win) {
137         Object JavaDoc obj = get(win); // obj is threadgroup
138
if (obj == null) return;
139         remove(win);
140         ArrayList JavaDoc<Window JavaDoc> vec = windowMap.get(obj);
141         if (vec == null) return;
142         vec.remove(win);
143     }
144 }
145
Popular Tags