KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > dnd > ZOrderManager


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.view.dnd;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Window JavaDoc;
25 import java.awt.event.WindowAdapter JavaDoc;
26 import java.awt.event.WindowEvent JavaDoc;
27 import java.awt.event.WindowListener JavaDoc;
28 import java.lang.IllegalArgumentException JavaDoc;
29 import java.lang.ref.WeakReference JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.RepaintManager JavaDoc;
38 import javax.swing.RootPaneContainer JavaDoc;
39 import javax.swing.SwingUtilities JavaDoc;
40 import org.openide.windows.WindowManager;
41
42 /** Holds and manages z-order of attached windows.
43  *
44  * Note, manager is NetBeans specific, not general. It automatically attaches
45  * main window and expects that all registered zOrder are always above
46  * this main window.
47  *
48  * Not thread safe, must be called from EQT.
49  *
50  * @author Dafe Simonek
51  */

52 public final class ZOrderManager extends WindowAdapter JavaDoc {
53
54     /** Singleton instance */
55     private static ZOrderManager instance;
56
57     private static Logger JavaDoc logger = Logger.getLogger("org.netbeans.core.windows.view.dnd");
58
59     /** Z-order list of pane containers (windows) */
60     private List JavaDoc<WeakReference JavaDoc<RootPaneContainer JavaDoc>> zOrder = new ArrayList JavaDoc<WeakReference JavaDoc<RootPaneContainer JavaDoc>>();
61
62     /** Set of pane containers to temporarily exclude from z-ordering. */
63     private Set JavaDoc<WeakReference JavaDoc<RootPaneContainer JavaDoc>> excludeSet = new HashSet JavaDoc<WeakReference JavaDoc<RootPaneContainer JavaDoc>>();
64
65
66     /** Creates a new instance of ZOrderManager */
67     private ZOrderManager() {
68     }
69
70     /** Returns singleton instance of ZOrderManager */
71     public static ZOrderManager getInstance () {
72         if (instance == null) {
73             instance = new ZOrderManager();
74         }
75         return instance;
76     }
77
78     /** Adds given window (RootPaneContainer) to the set of windows which are tracked.
79      */

80     public void attachWindow (RootPaneContainer JavaDoc rpc) {
81         logger.entering(getClass().getName(), "attachWindow");
82
83         if (!(rpc instanceof Window JavaDoc)) {
84             throw new IllegalArgumentException JavaDoc("Argument must be subclas of java.awt.Window: " + rpc); //NOI18N
85
}
86         if (getWeak(rpc) != null) {
87             throw new IllegalArgumentException JavaDoc("Window already attached: " + rpc); //NOI18N
88
}
89
90         zOrder.add(new WeakReference JavaDoc<RootPaneContainer JavaDoc>(rpc));
91         ((Window JavaDoc)rpc).addWindowListener(this);
92     }
93
94     /** Stops to track given window (RootPaneContainer).
95      */

96     public boolean detachWindow (RootPaneContainer JavaDoc rpc) {
97         logger.entering(getClass().getName(), "detachWindow");
98
99         if (!(rpc instanceof Window JavaDoc)) {
100             throw new IllegalArgumentException JavaDoc("Argument must be subclas of java.awt.Window: " + rpc); //NOI18N
101
}
102
103         WeakReference JavaDoc<RootPaneContainer JavaDoc> ww = getWeak(rpc);
104         if (ww == null) {
105             return false;
106         }
107
108         ((Window JavaDoc)rpc).removeWindowListener(this);
109         return zOrder.remove(ww);
110     }
111
112     /** Excludes/reincludes given RootPaneContainer from z-ordering. Excluded RootPaneContainer
113      * never returns true from isOnTop call, even if it is on top of window stack.
114      * RootPaneContainer that is second on top is returned in such situation.
115      *
116      * Used to distinguish RootPaneContainer that is being dragged.
117      *
118      * @param rpc Pane container to exlude or include back into rthe z-ordering.
119      * @param exclude true when exclusion is needed, false when normal default
120      * behaviour is desirable.
121      */

122     public void setExcludeFromOrder (RootPaneContainer JavaDoc rpc, boolean exclude) {
123         if (exclude) {
124             excludeSet.add(new WeakReference JavaDoc<RootPaneContainer JavaDoc>(rpc));
125         } else {
126             WeakReference JavaDoc<RootPaneContainer JavaDoc> ww = getExcludedWeak(rpc);
127             if (ww != null) {
128                 excludeSet.remove(ww);
129             }
130         }
131     }
132
133     /* Stops to track all windows registered before.
134      */

135     public void clear () {
136         RootPaneContainer JavaDoc rpc;
137         for (WeakReference JavaDoc<RootPaneContainer JavaDoc> elem : zOrder) {
138             rpc = elem.get();
139             if (rpc != null) {
140                 ((Window JavaDoc)rpc).removeWindowListener(this);
141             }
142         }
143         zOrder.clear();
144     }
145
146     /** Finds out whether given pane container (window) is not under any other
147      * window registered in this manager at given screen point.
148      *
149      * @param rpc Pane container (window)
150      * @param screenLoc point relative to screen
151      * @return true when given window is on top of other registered windows at given point
152      */

153     public boolean isOnTop (RootPaneContainer JavaDoc rpc, Point JavaDoc screenLoc) {
154         logger.entering(getClass().getName(), "isOnTop");
155         
156         /*JComponent cp = (JComponent) rpc.getContentPane();
157         // false if point in dirty region - probably overlapped by other window
158         if (RepaintManager.currentManager(cp).getDirtyRegion(cp).contains(screenLoc)) {
159             return false;
160         }*/

161
162         int size = zOrder.size();
163         WeakReference JavaDoc<RootPaneContainer JavaDoc> curWeakW = null;
164         RootPaneContainer JavaDoc curRpc = null;
165         for (int i = size - 1; i >= 0; i--) {
166             curWeakW = zOrder.get(i);
167             if (curWeakW == null) {
168                 continue;
169             }
170             curRpc = curWeakW.get();
171             // ignore excluded ones
172
if (getExcludedWeak(curRpc) != null) {
173                 continue;
174             }
175             // return top one
176
if (curRpc == rpc) {
177                 return true;
178             }
179             // safe cast, assured by checks in attachWindow method
180
Window JavaDoc curW = (Window JavaDoc) curRpc;
181             Point JavaDoc loc = new Point JavaDoc(screenLoc);
182             SwingUtilities.convertPointFromScreen(loc, curW);
183             if (curW.contains(loc)) {
184                     // && !RepaintManager.currentManager(curComp).getDirtyRegion(curComp).contains(screenLoc)) {
185
return false;
186             }
187         }
188
189         // take main window automatically as last window to check
190
if (rpc == WindowManager.getDefault().getMainWindow()) {
191             return true;
192         }
193
194         // not found
195
return false;
196     }
197
198     /*** Implementation of WindowListener ******/
199
200     public void windowOpened(WindowEvent JavaDoc e) {
201     }
202
203     public void windowClosed(WindowEvent JavaDoc e) {
204     }
205
206     public void windowActivated(WindowEvent JavaDoc e) {
207         logger.entering(getClass().getName(), "windowActivated");
208
209         WeakReference JavaDoc<RootPaneContainer JavaDoc> ww = getWeak((RootPaneContainer JavaDoc)e.getWindow());
210         if (ww != null) {
211             // place as last item in zOrder list
212
zOrder.remove(ww);
213             zOrder.add(ww);
214         } else {
215             throw new IllegalArgumentException JavaDoc("Window not attached: " + e.getWindow()); //NOI18N
216
}
217     }
218
219     public void windowDeactivated(WindowEvent JavaDoc e) {
220     }
221
222
223     private WeakReference JavaDoc<RootPaneContainer JavaDoc> getWeak (RootPaneContainer JavaDoc rpc) {
224         for (WeakReference JavaDoc<RootPaneContainer JavaDoc> elem : zOrder) {
225             if (elem.get() == rpc) {
226                 return elem;
227             }
228         }
229         return null;
230     }
231
232     private WeakReference JavaDoc<RootPaneContainer JavaDoc> getExcludedWeak (RootPaneContainer JavaDoc rpc) {
233         for (WeakReference JavaDoc<RootPaneContainer JavaDoc> elem : excludeSet) {
234             if (elem.get() == rpc) {
235                 return elem;
236             }
237         }
238         return null;
239     }
240
241 }
242
243
Popular Tags