1 19 20 package org.netbeans.core.windows; 21 22 import org.openide.nodes.Node; 23 import org.openide.util.WeakSet; 24 import org.openide.windows.TopComponent; 25 26 import javax.swing.*; 27 import java.awt.*; 28 import java.beans.PropertyChangeListener ; 29 import java.beans.PropertyChangeSupport ; 30 import java.util.Arrays ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 34 40 public final class RegistryImpl extends Object implements TopComponent.Registry { 41 42 44 private TopComponent activatedTopComponent; 45 46 private final Set<TopComponent> openSet = new WeakSet<TopComponent>(30); 47 48 private Node[] currentNodes; 49 51 private Node[] activatedNodes; 52 53 private final PropertyChangeSupport support = new PropertyChangeSupport (this); 54 55 56 private static final boolean DEBUG = Debug.isLoggable(RegistryImpl.class); 57 58 59 public RegistryImpl() { 60 } 61 62 66 public synchronized Set<TopComponent> getOpened() { 67 return java.util.Collections.unmodifiableSet(openSet); 68 } 69 70 73 public TopComponent getActivated() { 74 return activatedTopComponent; 75 } 76 77 79 public Node[] getCurrentNodes() { 80 return currentNodes; 81 } 82 83 89 public Node[] getActivatedNodes() { 90 return activatedNodes == null ? new Node[0] : activatedNodes; 91 } 92 93 96 public void addPropertyChangeListener(PropertyChangeListener l) { 97 support.addPropertyChangeListener(l); 98 } 99 100 103 public void removePropertyChangeListener(PropertyChangeListener l) { 104 support.removePropertyChangeListener(l); 105 } 106 107 108 114 void topComponentActivated(TopComponent tc) { 115 if(activatedTopComponent == tc 116 && activatedNodes != null) { return; 118 } 119 120 final TopComponent old = activatedTopComponent; 121 activatedTopComponent = tc; 122 123 Window w = tc == null ? null : SwingUtilities.windowForComponent(tc); 124 cancelMenu(w); 125 126 136 SwingUtilities.invokeLater(new Runnable () { 137 public void run() { 138 doFirePropertyChange(PROP_ACTIVATED, old, activatedTopComponent); 139 } 140 }); 141 142 selectedNodesChanged(activatedTopComponent, 143 activatedTopComponent == null ? null : activatedTopComponent.getActivatedNodes()); 144 } 145 146 147 148 149 synchronized void topComponentOpened(TopComponent tc) { 150 if (openSet.contains(tc)) { 151 return; 152 } 153 Set<TopComponent> old = new HashSet <TopComponent>(openSet); 154 openSet.add(tc); 155 doFirePropertyChange(PROP_TC_OPENED, null, tc); 156 doFirePropertyChange(PROP_OPENED, old, new HashSet <TopComponent>(openSet)); 157 } 158 159 160 synchronized void topComponentClosed(TopComponent tc) { 161 if (!openSet.contains(tc)) { 162 return; 163 } 164 165 Set<TopComponent> old = new HashSet <TopComponent>(openSet); 166 openSet.remove(tc); 167 doFirePropertyChange(PROP_TC_CLOSED, null, tc); 168 doFirePropertyChange(PROP_OPENED, old, new HashSet <TopComponent>(openSet)); 169 170 if (activatedNodes != null) { 171 Node[] closedNodes = tc.getActivatedNodes(); 172 if (closedNodes != null && Arrays.equals(closedNodes, activatedNodes)) { 173 activatedNodes = null; 175 doFirePropertyChange(PROP_ACTIVATED_NODES, closedNodes, null); 176 } 177 } 178 } 179 180 181 public void selectedNodesChanged(TopComponent tc, Node[] newNodes) { 182 Node[] oldNodes = currentNodes; 183 184 if(tc != activatedTopComponent 189 && activatedNodes != null) { return; 191 } 192 194 if(Arrays.equals(oldNodes, newNodes) 195 && activatedNodes != null) { return; 197 } 198 199 currentNodes = newNodes == null ? null : newNodes.clone(); 200 tryFireChanges(oldNodes, currentNodes); 202 } 203 206 210 212 public static void cancelMenu(Window window) { 213 MenuSelectionManager msm = MenuSelectionManager.defaultManager(); 214 MenuElement[] path = msm.getSelectedPath(); 215 216 for (int i = 0; i < path.length; i++) { 217 java.awt.Window w = SwingUtilities.windowForComponent( 219 path[i].getComponent() 220 ); 221 222 if ((w != null) && (w == window || w.getOwner() == window)) { 224 return; 226 } 227 228 } 229 230 msm.clearSelectedPath(); 231 } 232 233 234 236 private void tryFireChanges(Node[] oldNodes, Node[] newNodes) { 237 doFirePropertyChange(PROP_CURRENT_NODES, oldNodes, newNodes); 238 239 if(newNodes == null && activatedNodes == null) { 240 newNodes = new Node[0]; 242 } 243 244 if (newNodes != null) { 245 oldNodes = activatedNodes; 246 activatedNodes = newNodes; 247 support.firePropertyChange(PROP_ACTIVATED_NODES, oldNodes, activatedNodes); 248 } 249 } 250 251 252 private void doFirePropertyChange(final String propName, 253 final Object oldValue, final Object newValue) { 254 if(DEBUG) { 255 debugLog(""); debugLog("Scheduling event firing: propName=" + propName); debugLog("\toldValue=" + (oldValue instanceof Object [] ? Arrays.asList((Object [])oldValue) : oldValue)); debugLog("\tnewValue=" + (newValue instanceof Object [] ? Arrays.asList((Object [])newValue) : newValue)); } 260 if(SwingUtilities.isEventDispatchThread()) { 264 support.firePropertyChange(propName, oldValue, newValue); 265 } else { 266 SwingUtilities.invokeLater(new Runnable () { 267 public void run() { 268 support.firePropertyChange(propName, oldValue, newValue); 269 } 270 }); 271 } 272 } 273 274 void clear() { 275 activatedTopComponent = null; 276 openSet.clear(); 277 currentNodes = null; 278 activatedNodes = null; 279 } 280 281 private static void debugLog(String message) { 282 Debug.log(RegistryImpl.class, message); 283 } 284 285 } 286 | Popular Tags |