KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > I18nGroupAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n;
22
23
24 import java.awt.Component JavaDoc;
25 import java.awt.EventQueue JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.JMenu JavaDoc;
35 import javax.swing.JMenuItem JavaDoc;
36 import javax.swing.JPopupMenu JavaDoc;
37 import javax.swing.JToolBar JavaDoc;
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 /**
52  * Pseudo-action representing the Internationalization submenu.
53  *
54  * @author Peter Zavadsky
55  * @author Marian Petras
56  */

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         /*
66          * Caution! Calling putValue(...) triggers the initialize() method!
67          */

68         putValue("noIconInMenu", Boolean.TRUE);
69     }
70     
71     /** Array of i18n actions. */
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     /**
81      * Does nothing, possibly except throwing an <code>AssertionError</code>.
82      */

83     public void actionPerformed(ActionEvent JavaDoc e) {
84         assert false;
85     }
86     
87     /** Gets localized name of action. Implements superclass abstract method. */
88     public String JavaDoc getName() {
89         return I18nUtil.getBundle().getString("LBL_I18nGroupActionName");
90     }
91
92     /** Gets icon resource. Overrides suprclass method. */
93      protected String JavaDoc iconResource () {
94          return "org/netbeans/modules/i18n/i18nAction.gif"; // NOI18N
95
}
96
97     /** Gets help context. Implements abstract superclass method. */
98     public HelpCtx getHelpCtx () {
99         return new HelpCtx(I18nUtil.HELP_ID_I18N);
100     }
101     
102     /**
103      */

104     public JMenuItem JavaDoc getMenuPresenter() {
105         return LazyPopup.createLazyPopup(true, this);
106     }
107     
108     /**
109      */

110     public JMenuItem JavaDoc getPopupPresenter() {
111         return LazyPopup.createLazyPopup(false, this);
112     }
113
114     /**
115      */

116     public Component JavaDoc getToolbarPresenter() {
117         // In jdk1.3 could be used new JToolBar(getName()).
118
JToolBar JavaDoc toolbar = new JToolBar JavaDoc (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     /**
134      */

135     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
136         return new ContextAwareClass(this, actionContext);
137     }
138     
139     
140     /** Menu item which will create its items lazilly when the popup will becomming visible.
141      * Performance savings.*/

142     static class LazyPopup extends JMenu JavaDoc {
143
144         /** Indicates if is part of menu, i.e. if should have icons. */
145         private boolean isMenu;
146         
147         /** Indicates whether menu items were created. */
148         private boolean created = false;
149
150         
151         /** Constructor. */
152         private LazyPopup(boolean isMenu, SystemAction action) {
153             Actions.setMenuText(this, action.getName(), isMenu);
154             
155             this.isMenu = isMenu;
156             if (isMenu) {
157                 // mkleint doens't seem to be dynamic at all, just set it for main menu..
158
createMenuItems();
159             }
160         }
161
162         /** Creates <code>LazyPopup</code> menu item. */
163         static JMenuItem JavaDoc createLazyPopup(boolean isMenu, SystemAction action) {
164             return new LazyPopup(isMenu, action);
165         }
166         
167         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
168         public JPopupMenu JavaDoc getPopupMenu() {
169             if(!created)
170                 createMenuItems();
171             
172             return super.getPopupMenu();
173         }
174
175         /** Creates items when actually needed. */
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     } // End of class LazyPopup.
193

194     /**
195      * Context-aware variant of the <code>I18nGroupAction</code>.
196      *
197      * @see org.openide.util.ContextAwareAction
198      */

199     private final class ContextAwareClass implements Action JavaDoc,
200                                                      PropertyChangeListener JavaDoc,
201                                                      Presenter.Menu,
202                                                      Presenter.Popup,
203                                                      Presenter.Toolbar {
204         /* The class is final only for performance reasons. */
205         
206         /** */
207         private final I18nGroupAction delegate;
208         /** */
209         private final Lookup context;
210         /** */
211         private final Action JavaDoc[] contextActions;
212         /** */
213         private List JavaDoc/*<PropertyChangeListener>*/ propListeners;
214         /** is this action enabled? */
215         private volatile Boolean JavaDoc enabled = null; //unknown
216

217         /**
218          */

219         ContextAwareClass(I18nGroupAction delegate, Lookup actionContext) {
220             this.delegate = delegate;
221             this.context = actionContext;
222             this.contextActions = new Action JavaDoc[i18nActions.length];
223             
224             /*
225              * Listen on the delegate's property changes so that we can
226              * propagate them to this action:
227              */

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                     /*
242                      * Listen on the property changes in order to detect
243                      * that one of the I18n actions was enabled/disabled:
244                      */

245                     contextActions[i].addPropertyChangeListener(
246                             WeakListeners.propertyChange(this,
247                                                          contextActions[i]));
248                 }
249             }
250         }
251         
252         /**
253          */

254         public void propertyChange(PropertyChangeEvent JavaDoc e) {
255             if (e.getSource() == delegate) {
256                 firePropertyChange(e.getPropertyName(),
257                                    e.getOldValue(),
258                                    e.getNewValue());
259                 return;
260             }
261             
262             /* Source is one of the I18n actions. */
263             final String JavaDoc 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; //unknown
274
return;
275             }
276             
277             Boolean JavaDoc wasEnabled = enabled;
278             enabled = Boolean.valueOf(shouldBeEnabled());
279             if (enabled != wasEnabled) {
280                 fireEnabledChanged();
281             }
282         }
283         
284         /**
285          */

286         private void fireEnabledChanged() {
287             firePropertyChange(PROP_ENABLED, null, null);
288         }
289         
290         /**
291          * Determines whether the <em>Internationalize</em> submenu should
292          * be enabled or not.
293          *
294          * @return <code>true</code> if at least one I18n action displayed
295          * in the submenu should be enabled;
296          * <code>false</code> otherwise
297          */

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         /**
309          */

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         /**
320          * Has no effect.
321          */

322         public void setEnabled(boolean enabled) { }
323         
324         /**
325          */

326         public void actionPerformed(ActionEvent JavaDoc e) {
327             delegate.actionPerformed(e);
328         }
329         
330         /**
331          */

332         public JMenuItem JavaDoc getMenuPresenter() {
333             return delegate.getMenuPresenter();
334         }
335         
336         /**
337          */

338         public JMenuItem JavaDoc getPopupPresenter() {
339             return delegate.getPopupPresenter();
340         }
341         
342         /**
343          */

344         public Component JavaDoc getToolbarPresenter() {
345             return delegate.getToolbarPresenter();
346         }
347         
348         /**
349          */

350         public Object JavaDoc getValue(String JavaDoc name) {
351             return delegate.getValue(name);
352         }
353         
354         /**
355          */

356         public void putValue(String JavaDoc name, Object JavaDoc value) {
357             delegate.putValue(name, value);
358         }
359         
360         /**
361          */

362         public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
363             if (propListeners == null) {
364                 propListeners = new ArrayList JavaDoc(4);
365             }
366             propListeners.add(l);
367         }
368         
369         /**
370          */

371         public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
372             if ((propListeners != null)
373                     && propListeners.remove(l)
374                     && propListeners.isEmpty()) {
375                 propListeners = null;
376             }
377         }
378         
379         /**
380          */

381         private void firePropertyChange(String JavaDoc propName,
382                                         Object JavaDoc oldValue,
383                                         Object JavaDoc newValue) {
384             if ((newValue != null) && newValue.equals(oldValue)) {
385                 return;
386             }
387             
388             if (propListeners != null) {
389                 final PropertyChangeEvent JavaDoc e = new PropertyChangeEvent JavaDoc(
390                                                     this,
391                                                     propName,
392                                                     oldValue,
393                                                     newValue);
394                 for (Iterator JavaDoc i = propListeners.iterator(); i.hasNext(); ) {
395                     ((PropertyChangeListener JavaDoc) i.next()).propertyChange(e);
396                 }
397             }
398         }
399     
400     }
401
402 }
403
Popular Tags