1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.awt.Component ; 25 import java.awt.EventQueue ; 26 import java.awt.event.ActionEvent ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import javax.swing.Action ; 33 import javax.swing.Icon ; 34 import javax.swing.JMenu ; 35 import javax.swing.JMenuItem ; 36 import javax.swing.JPopupMenu ; 37 import javax.swing.JToolBar ; 38 39 import org.netbeans.modules.i18n.wizard.I18nWizardAction; 40 import org.netbeans.modules.i18n.wizard.I18nTestWizardAction; 41 42 import org.openide.awt.Actions; 43 import org.openide.util.ContextAwareAction; 44 import org.openide.util.Lookup; 45 import org.openide.util.WeakListeners; 46 import org.openide.util.actions.Presenter; 47 import org.openide.util.actions.SystemAction; 48 import org.openide.util.HelpCtx; 49 50 51 57 public final class I18nGroupAction extends SystemAction 58 implements ContextAwareAction, 59 Presenter.Menu, 60 Presenter.Popup, 61 Presenter.Toolbar { 62 63 public I18nGroupAction() { 64 65 68 putValue("noIconInMenu", Boolean.TRUE); 69 } 70 71 72 protected static final SystemAction[] i18nActions = new SystemAction[] { 73 SystemAction.get(I18nAction.class), 74 SystemAction.get(InsertI18nStringAction.class), 75 null, 76 SystemAction.get(I18nWizardAction.class), 77 SystemAction.get(I18nTestWizardAction.class), 78 }; 79 80 83 public void actionPerformed(ActionEvent e) { 84 assert false; 85 } 86 87 88 public String getName() { 89 return I18nUtil.getBundle().getString("LBL_I18nGroupActionName"); 90 } 91 92 93 protected String iconResource () { 94 return "org/netbeans/modules/i18n/i18nAction.gif"; } 96 97 98 public HelpCtx getHelpCtx () { 99 return new HelpCtx(I18nUtil.HELP_ID_I18N); 100 } 101 102 104 public JMenuItem getMenuPresenter() { 105 return LazyPopup.createLazyPopup(true, this); 106 } 107 108 110 public JMenuItem getPopupPresenter() { 111 return LazyPopup.createLazyPopup(false, this); 112 } 113 114 116 public Component getToolbarPresenter() { 117 JToolBar toolbar = new JToolBar (getName()); 119 120 for(int i=0; i<i18nActions.length; i++) { 121 SystemAction action = i18nActions[i]; 122 123 if(action == null) { 124 toolbar.addSeparator(); 125 } else if(action instanceof Presenter.Toolbar) { 126 toolbar.add(((Presenter.Toolbar)action).getToolbarPresenter()); 127 } 128 } 129 130 return toolbar; 131 } 132 133 135 public Action createContextAwareInstance(Lookup actionContext) { 136 return new ContextAwareClass(this, actionContext); 137 } 138 139 140 142 static class LazyPopup extends JMenu { 143 144 145 private boolean isMenu; 146 147 148 private boolean created = false; 149 150 151 152 private LazyPopup(boolean isMenu, SystemAction action) { 153 Actions.setMenuText(this, action.getName(), isMenu); 154 155 this.isMenu = isMenu; 156 if (isMenu) { 157 createMenuItems(); 159 } 160 } 161 162 163 static JMenuItem createLazyPopup(boolean isMenu, SystemAction action) { 164 return new LazyPopup(isMenu, action); 165 } 166 167 168 public JPopupMenu getPopupMenu() { 169 if(!created) 170 createMenuItems(); 171 172 return super.getPopupMenu(); 173 } 174 175 176 private void createMenuItems() { 177 created = true; 178 removeAll(); 179 180 for(int i=0; i<i18nActions.length; i++) { 181 SystemAction action = i18nActions[i]; 182 183 if(action == null) { 184 addSeparator(); 185 } else if(!isMenu && action instanceof Presenter.Popup) { 186 add(((Presenter.Popup)action).getPopupPresenter()); 187 } else if(isMenu && action instanceof Presenter.Menu) { 188 add(((Presenter.Menu)action).getMenuPresenter()); 189 } 190 } 191 } 192 } 194 199 private final class ContextAwareClass implements Action , 200 PropertyChangeListener , 201 Presenter.Menu, 202 Presenter.Popup, 203 Presenter.Toolbar { 204 205 206 207 private final I18nGroupAction delegate; 208 209 private final Lookup context; 210 211 private final Action [] contextActions; 212 213 private List propListeners; 214 215 private volatile Boolean enabled = null; 217 219 ContextAwareClass(I18nGroupAction delegate, Lookup actionContext) { 220 this.delegate = delegate; 221 this.context = actionContext; 222 this.contextActions = new Action [i18nActions.length]; 223 224 228 delegate.addPropertyChangeListener( 229 WeakListeners.propertyChange(this, delegate)); 230 231 for (int i = 0; i < i18nActions.length; i++) { 232 final SystemAction action = i18nActions[i]; 233 if (action == null) { 234 contextActions[i] = null; 235 } else { 236 contextActions[i] = (action instanceof ContextAwareAction) 237 ? ((ContextAwareAction) action) 238 .createContextAwareInstance(context) 239 : action; 240 241 245 contextActions[i].addPropertyChangeListener( 246 WeakListeners.propertyChange(this, 247 contextActions[i])); 248 } 249 } 250 } 251 252 254 public void propertyChange(PropertyChangeEvent e) { 255 if (e.getSource() == delegate) { 256 firePropertyChange(e.getPropertyName(), 257 e.getOldValue(), 258 e.getNewValue()); 259 return; 260 } 261 262 263 final String propertyName = e.getPropertyName(); 264 if (PROP_ENABLED.equals(propertyName)) { 265 updateEnabled(); 266 } 267 } 268 269 private synchronized void updateEnabled() { 270 assert EventQueue.isDispatchThread(); 271 272 if (propListeners == null) { 273 enabled = null; return; 275 } 276 277 Boolean wasEnabled = enabled; 278 enabled = Boolean.valueOf(shouldBeEnabled()); 279 if (enabled != wasEnabled) { 280 fireEnabledChanged(); 281 } 282 } 283 284 286 private void fireEnabledChanged() { 287 firePropertyChange(PROP_ENABLED, null, null); 288 } 289 290 298 private boolean shouldBeEnabled() { 299 for (int i = 0; i < i18nActions.length; i++) { 300 if ((contextActions[i] != null) 301 && contextActions[i].isEnabled()) { 302 return true; 303 } 304 } 305 return false; 306 } 307 308 310 public boolean isEnabled() { 311 assert EventQueue.isDispatchThread(); 312 313 if (enabled == null) { 314 enabled = Boolean.valueOf(shouldBeEnabled()); 315 } 316 return enabled.booleanValue(); 317 } 318 319 322 public void setEnabled(boolean enabled) { } 323 324 326 public void actionPerformed(ActionEvent e) { 327 delegate.actionPerformed(e); 328 } 329 330 332 public JMenuItem getMenuPresenter() { 333 return delegate.getMenuPresenter(); 334 } 335 336 338 public JMenuItem getPopupPresenter() { 339 return delegate.getPopupPresenter(); 340 } 341 342 344 public Component getToolbarPresenter() { 345 return delegate.getToolbarPresenter(); 346 } 347 348 350 public Object getValue(String name) { 351 return delegate.getValue(name); 352 } 353 354 356 public void putValue(String name, Object value) { 357 delegate.putValue(name, value); 358 } 359 360 362 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 363 if (propListeners == null) { 364 propListeners = new ArrayList (4); 365 } 366 propListeners.add(l); 367 } 368 369 371 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 372 if ((propListeners != null) 373 && propListeners.remove(l) 374 && propListeners.isEmpty()) { 375 propListeners = null; 376 } 377 } 378 379 381 private void firePropertyChange(String propName, 382 Object oldValue, 383 Object newValue) { 384 if ((newValue != null) && newValue.equals(oldValue)) { 385 return; 386 } 387 388 if (propListeners != null) { 389 final PropertyChangeEvent e = new PropertyChangeEvent ( 390 this, 391 propName, 392 oldValue, 393 newValue); 394 for (Iterator i = propListeners.iterator(); i.hasNext(); ) { 395 ((PropertyChangeListener ) i.next()).propertyChange(e); 396 } 397 } 398 } 399 400 } 401 402 } 403 | Popular Tags |