1 19 20 package org.netbeans.core; 21 22 import java.awt.event.ActionEvent ; 23 import java.util.*; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.Action ; 27 28 import org.openide.actions.ActionManager; 29 import org.openide.util.actions.SystemAction; 30 import org.openide.util.Lookup; 31 32 import org.netbeans.core.startup.ManifestSection; 33 34 35 39 public class ModuleActions extends ActionManager 40 { 41 42 43 private static SystemAction[] array; 44 45 private static Map<Object ,List<ManifestSection.ActionSection>> map = new HashMap<Object ,List<ManifestSection.ActionSection>> (8); 46 47 private static Object module = null; 48 49 private Map<ActionEvent ,Action > runningActions = new HashMap<ActionEvent ,Action >(); 50 51 public static ModuleActions getDefaultInstance() { 52 ActionManager mgr = ActionManager.getDefault(); 53 assert mgr instanceof ModuleActions : "Got wrong ActionManager instance: " + mgr + " from " + Lookup.getDefault(); 54 return (ModuleActions)mgr; 55 } 56 57 60 public SystemAction[] getContextActions () { 61 SystemAction[] a = array; 62 if (a != null) { 63 return a; 64 } 65 array = a = createActions (); 66 return a; 67 } 68 69 72 @SuppressWarnings ("deprecation") 73 public void invokeAction(final Action a, final ActionEvent e) { 74 try { 75 org.openide.util.Mutex.EVENT.readAccess (new Runnable () { 76 public void run() { 77 showWaitCursor(e); 78 } 79 }); 80 addRunningAction(a, e); 81 82 a.actionPerformed (e); 83 } finally { 84 removeRunningAction(e); 85 org.openide.util.Mutex.EVENT.readAccess (new Runnable () { 86 public void run() { 87 hideWaitCursor(e); 88 } 89 }); 90 } 91 } 92 93 96 private void fireChange () { 97 firePropertyChange(PROP_CONTEXT_ACTIONS, null, null); 98 } 99 100 104 private void addRunningAction(Action action, ActionEvent evt) { 105 synchronized(runningActions) { 106 runningActions.put(evt, action); 107 } 108 } 109 110 112 private void removeRunningAction(ActionEvent evt) { 113 synchronized(runningActions) { 114 runningActions.remove(evt); 115 } 116 } 117 118 119 public Collection<Action > getRunningActions() { 120 synchronized(runningActions) { 121 return new ArrayList<Action >(runningActions.values()); 122 } 123 } 124 125 133 134 140 public static synchronized void attachTo (Object m) { 141 module = m; 142 } 143 144 146 public synchronized static void add (ManifestSection.ActionSection a) { 147 List<ManifestSection.ActionSection> list = map.get (module); 148 if (list == null) { 149 list = new ArrayList<ManifestSection.ActionSection> (); 150 map.put (module, list); 151 } 152 list.add (a); 153 155 array = null; 156 getDefaultInstance().fireChange (); } 158 159 161 public synchronized static void remove (ManifestSection.ActionSection a) { 162 List<ManifestSection.ActionSection> list = map.get (module); 163 if (list == null) { 164 return; 165 } 166 list.remove (a); 167 169 if (list.isEmpty ()) { 170 map.remove (module); 171 } 172 173 array = null; 174 getDefaultInstance().fireChange (); } 176 177 179 private synchronized static SystemAction[] createActions () { 180 Iterator<List<ManifestSection.ActionSection>> it = map.values ().iterator (); 181 182 ArrayList<Object > arr = new ArrayList<Object > (map.size () * 5); 183 184 while (it.hasNext ()) { 185 List<ManifestSection.ActionSection> l = it.next (); 186 187 Iterator<ManifestSection.ActionSection> actions = l.iterator (); 188 while (actions.hasNext()) { 189 ManifestSection.ActionSection s = actions.next(); 190 191 try { 192 arr.add (s.getInstance ()); 193 } catch (Exception ex) { 194 Logger.getLogger(ModuleActions.class.getName()).log(Level.WARNING, null, ex); 195 } 196 } 197 198 199 if (it.hasNext ()) { 200 arr.add (null); 202 } 203 204 } 205 206 return (SystemAction[])arr.toArray (new SystemAction[arr.size ()]); 207 } 208 209 210 private static final Logger err = Logger.getLogger("org.openide.util.actions.MouseCursorUtils"); 212 218 private static final Map<Object ,java.awt.Component > glassPaneUses = new HashMap<Object ,java.awt.Component >(); 219 220 224 private static java.awt.Component activeGlassPane() { 225 java.awt.Window w = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 226 if (w instanceof javax.swing.RootPaneContainer ) { 227 return ((javax.swing.RootPaneContainer )w).getGlassPane(); 228 } else { 229 return null; 230 } 231 } 232 233 237 public static void showWaitCursor(Object key) { 238 assert java.awt.EventQueue.isDispatchThread(); 239 assert !glassPaneUses.containsKey(key); 240 java.awt.Component c = activeGlassPane(); 241 if (c == null) { 242 err.warning("showWaitCursor could not find a suitable glass pane; key=" + key); 243 return; 244 } 245 if (glassPaneUses.values().contains(c)) { 246 err.fine("wait cursor already displayed on " + c); 247 } else { 248 err.fine("wait cursor will be displayed on " + c); 249 c.setCursor(org.openide.util.Utilities.createProgressCursor(c)); 250 c.setVisible(true); 251 } 252 glassPaneUses.put(key, c); 253 } 254 255 259 public static void hideWaitCursor(Object key) { 260 assert java.awt.EventQueue.isDispatchThread(); 261 java.awt.Component c = glassPaneUses.get(key); 262 if (c == null) { 263 return; 264 } 265 glassPaneUses.remove(key); 266 if (glassPaneUses.values().contains(c)) { 267 err.fine("wait cursor still displayed on " + c); 268 } else { 269 err.fine("wait cursor will be hidden on " + c); 270 c.setVisible(false); 271 c.setCursor(null); 272 } 273 } 274 275 276 } 277 278 | Popular Tags |