1 19 package org.netbeans.modules.xml.core.actions; 20 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyChangeEvent ; 23 import java.util.*; 24 25 import javax.swing.JMenuItem ; 26 27 import org.openide.awt.JInlineMenu; 28 import org.openide.windows.TopComponent.Registry; 29 import org.openide.windows.WindowManager; 30 import org.openide.util.actions.SystemAction; 31 import org.openide.util.actions.Presenter; 32 import org.openide.util.Lookup; 33 34 35 public abstract class CollectSystemAction extends SystemAction implements Presenter.Popup { 36 37 private static final long serialVersionUID = 6517322512481423122L; 38 39 40 private Lookup.Result allActionsResult; 41 42 43 static JMenuItem [] NONE = new JMenuItem [] {}; 44 45 46 47 protected abstract Class getActionLookClass (); 48 49 51 protected synchronized Collection getPossibleActions () { 52 if ( allActionsResult == null ) { 53 allActionsResult = Lookup.getDefault().lookup (new Lookup.Template (getActionLookClass())); 54 } 55 return allActionsResult.allInstances(); 56 } 57 58 59 private JMenuItem [] createMenu () { 60 JMenuItem [] menu; 61 62 menu = createMenu (getPossibleActions()); 63 64 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("--- CollectSystemAction.createMenu: menu = " + menu); 66 return menu; 67 } 68 69 private JMenuItem [] createMenu (Collection coll) { 70 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("\n--> CollectSystemAction.createMenu: ( " + coll + " )"); 71 72 ArrayList items = new ArrayList (); 73 74 Iterator it = coll.iterator(); 75 while (it.hasNext ()) { 76 SystemAction a = (SystemAction) it.next(); 77 78 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("-*- CollectSystemAction.createMenu: next action " + a + 79 " -- " + ( a.isEnabled() ? "<enabled>" : "[disabled]" ) ); 80 81 if ( a.isEnabled() ) { 82 JMenuItem item = null; 83 if (a instanceof Presenter.Popup) { 84 item = ((Presenter.Popup)a).getPopupPresenter (); 85 } 86 87 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("-*- CollectSystemAction.createMenu: menu item = " + item); 88 89 if (item != null) { 91 items.add (item); 92 } 93 } 94 } 95 96 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("<-- CollectSystemAction.createMenu: all items = " + items + "\n"); 97 98 JMenuItem [] array = new JMenuItem [items.size ()]; 99 items.toArray (array); 100 return array; 101 } 102 103 104 106 public JMenuItem getPopupPresenter () { 107 return new Menu(); 108 } 109 110 114 public void actionPerformed (java.awt.event.ActionEvent e) { 115 } 116 117 118 119 121 private class Menu extends JInlineMenu { 122 private static final long serialVersionUID = -4962039848190160129L; 123 124 125 private JMenuItem [] last = NONE; 126 127 private PropL propL = new PropL (); 128 129 130 132 Menu () { 133 changeMenuItems (createMenu()); 134 135 Registry r = WindowManager.getDefault().getRegistry (); 136 137 r.addPropertyChangeListener ( 138 org.openide.util.WeakListeners.propertyChange (propL, r) 139 ); 140 } 141 142 145 synchronized void changeMenuItems (JMenuItem [] items) { 146 removeListeners (last); 147 addListeners (items); 148 last = items; 149 setMenuItems (items); 150 } 151 152 153 156 private void addListeners (JMenuItem [] items) { 157 int len = items.length; 158 for (int i = 0; i < len; i++) { 159 items[i].addPropertyChangeListener (propL); 160 } 161 } 162 163 166 private void removeListeners (JMenuItem [] items) { 167 int len = items.length; 168 for (int i = 0; i < len; i++) { 169 items[i].removePropertyChangeListener (propL); 170 } 171 } 172 173 boolean needsChange = false; 174 175 public void addNotify() { 176 if (needsChange) { 177 changeMenuItems (createMenu()); 178 needsChange = false; 179 } 180 super.addNotify(); 181 } 182 183 public void removeNotify() { 184 removeListeners (last); 185 last = NONE; 186 } 187 188 189 191 private class PropL implements PropertyChangeListener { 192 public void propertyChange (PropertyChangeEvent ev) { 193 String name = ev.getPropertyName (); 194 if ( 195 name == null || 196 name.equals (SystemAction.PROP_ENABLED) || 197 name.equals (Registry.PROP_ACTIVATED_NODES) 198 ) { 199 needsChange = true; 201 } 202 } 203 } 204 205 } 207 } 208 | Popular Tags |