1 19 20 package org.netbeans.core.windows.view.dnd; 21 22 import java.awt.Component ; 23 import java.awt.Point ; 24 import java.awt.Window ; 25 import java.awt.event.WindowAdapter ; 26 import java.awt.event.WindowEvent ; 27 import java.awt.event.WindowListener ; 28 import java.lang.IllegalArgumentException ; 29 import java.lang.ref.WeakReference ; 30 import java.util.ArrayList ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Set ; 35 import java.util.logging.Logger ; 36 import javax.swing.JComponent ; 37 import javax.swing.RepaintManager ; 38 import javax.swing.RootPaneContainer ; 39 import javax.swing.SwingUtilities ; 40 import org.openide.windows.WindowManager; 41 42 52 public final class ZOrderManager extends WindowAdapter { 53 54 55 private static ZOrderManager instance; 56 57 private static Logger logger = Logger.getLogger("org.netbeans.core.windows.view.dnd"); 58 59 60 private List <WeakReference <RootPaneContainer >> zOrder = new ArrayList <WeakReference <RootPaneContainer >>(); 61 62 63 private Set <WeakReference <RootPaneContainer >> excludeSet = new HashSet <WeakReference <RootPaneContainer >>(); 64 65 66 67 private ZOrderManager() { 68 } 69 70 71 public static ZOrderManager getInstance () { 72 if (instance == null) { 73 instance = new ZOrderManager(); 74 } 75 return instance; 76 } 77 78 80 public void attachWindow (RootPaneContainer rpc) { 81 logger.entering(getClass().getName(), "attachWindow"); 82 83 if (!(rpc instanceof Window )) { 84 throw new IllegalArgumentException ("Argument must be subclas of java.awt.Window: " + rpc); } 86 if (getWeak(rpc) != null) { 87 throw new IllegalArgumentException ("Window already attached: " + rpc); } 89 90 zOrder.add(new WeakReference <RootPaneContainer >(rpc)); 91 ((Window )rpc).addWindowListener(this); 92 } 93 94 96 public boolean detachWindow (RootPaneContainer rpc) { 97 logger.entering(getClass().getName(), "detachWindow"); 98 99 if (!(rpc instanceof Window )) { 100 throw new IllegalArgumentException ("Argument must be subclas of java.awt.Window: " + rpc); } 102 103 WeakReference <RootPaneContainer > ww = getWeak(rpc); 104 if (ww == null) { 105 return false; 106 } 107 108 ((Window )rpc).removeWindowListener(this); 109 return zOrder.remove(ww); 110 } 111 112 122 public void setExcludeFromOrder (RootPaneContainer rpc, boolean exclude) { 123 if (exclude) { 124 excludeSet.add(new WeakReference <RootPaneContainer >(rpc)); 125 } else { 126 WeakReference <RootPaneContainer > ww = getExcludedWeak(rpc); 127 if (ww != null) { 128 excludeSet.remove(ww); 129 } 130 } 131 } 132 133 135 public void clear () { 136 RootPaneContainer rpc; 137 for (WeakReference <RootPaneContainer > elem : zOrder) { 138 rpc = elem.get(); 139 if (rpc != null) { 140 ((Window )rpc).removeWindowListener(this); 141 } 142 } 143 zOrder.clear(); 144 } 145 146 153 public boolean isOnTop (RootPaneContainer rpc, Point screenLoc) { 154 logger.entering(getClass().getName(), "isOnTop"); 155 156 161 162 int size = zOrder.size(); 163 WeakReference <RootPaneContainer > curWeakW = null; 164 RootPaneContainer 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 if (getExcludedWeak(curRpc) != null) { 173 continue; 174 } 175 if (curRpc == rpc) { 177 return true; 178 } 179 Window curW = (Window ) curRpc; 181 Point loc = new Point (screenLoc); 182 SwingUtilities.convertPointFromScreen(loc, curW); 183 if (curW.contains(loc)) { 184 return false; 186 } 187 } 188 189 if (rpc == WindowManager.getDefault().getMainWindow()) { 191 return true; 192 } 193 194 return false; 196 } 197 198 199 200 public void windowOpened(WindowEvent e) { 201 } 202 203 public void windowClosed(WindowEvent e) { 204 } 205 206 public void windowActivated(WindowEvent e) { 207 logger.entering(getClass().getName(), "windowActivated"); 208 209 WeakReference <RootPaneContainer > ww = getWeak((RootPaneContainer )e.getWindow()); 210 if (ww != null) { 211 zOrder.remove(ww); 213 zOrder.add(ww); 214 } else { 215 throw new IllegalArgumentException ("Window not attached: " + e.getWindow()); } 217 } 218 219 public void windowDeactivated(WindowEvent e) { 220 } 221 222 223 private WeakReference <RootPaneContainer > getWeak (RootPaneContainer rpc) { 224 for (WeakReference <RootPaneContainer > elem : zOrder) { 225 if (elem.get() == rpc) { 226 return elem; 227 } 228 } 229 return null; 230 } 231 232 private WeakReference <RootPaneContainer > getExcludedWeak (RootPaneContainer rpc) { 233 for (WeakReference <RootPaneContainer > elem : excludeSet) { 234 if (elem.get() == rpc) { 235 return elem; 236 } 237 } 238 return null; 239 } 240 241 } 242 243 | Popular Tags |