KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > actions > CollectSystemAction


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 package org.netbeans.modules.xml.core.actions;
20
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.util.*;
24
25 import javax.swing.JMenuItem JavaDoc;
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     /** Serial Version UID */
37     private static final long serialVersionUID = 6517322512481423122L;
38
39     /** All Actions Lookup Result. */
40     private Lookup.Result allActionsResult;
41
42     /** empty array of menu items */
43     static JMenuItem JavaDoc[] NONE = new JMenuItem JavaDoc[] {};
44
45
46     /** Which Class should be used for Lookup? */
47     protected abstract Class JavaDoc getActionLookClass ();
48
49     /** @return all instances of <code>getActionLookClass</code>.
50      */

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 JavaDoc[] createMenu () {
60         JMenuItem JavaDoc[] menu;
61
62         menu = createMenu (getPossibleActions());
63
64         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("--- CollectSystemAction.createMenu: menu = " + menu);//, new RuntimeException());
65

66         return menu;
67     }
68
69     private JMenuItem JavaDoc[] createMenu (Collection coll) {
70         if ( Util.THIS.isLoggable() ) /* then */ 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() ) /* then */ Util.THIS.debug ("-*- CollectSystemAction.createMenu: next action " + a +
79                              " -- " + ( a.isEnabled() ? "<enabled>" : "[disabled]" ) );
80             
81             if ( a.isEnabled() ) {
82                 JMenuItem JavaDoc item = null;
83                 if (a instanceof Presenter.Popup) {
84                     item = ((Presenter.Popup)a).getPopupPresenter ();
85                 }
86
87                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-*- CollectSystemAction.createMenu: menu item = " + item);
88
89                 // test if we obtained the item
90
if (item != null) {
91                     items.add (item);
92                 }
93             }
94         }
95
96         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("<-- CollectSystemAction.createMenu: all items = " + items + "\n");
97
98         JMenuItem JavaDoc[] array = new JMenuItem JavaDoc [items.size ()];
99         items.toArray (array);
100         return array;
101     }
102
103
104     /* @return popup presenter.
105      */

106     public JMenuItem JavaDoc getPopupPresenter () {
107         return new Menu();
108     }
109
110     /* Do nothing.
111     * This action itself does nothing, it only presents other actions.
112     * @param ev ignored
113     */

114     public void actionPerformed (java.awt.event.ActionEvent JavaDoc e) {
115     }
116
117
118
119     /** Presenter for this action.
120     */

121     private class Menu extends JInlineMenu {
122         private static final long serialVersionUID = -4962039848190160129L;
123
124         /** last registered items */
125         private JMenuItem JavaDoc[] last = NONE;
126         /** own property change listner */
127         private PropL propL = new PropL ();
128
129
130         /**
131          */

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         /** Changes the selection to new items.
143         * @param items the new items
144         */

145         synchronized void changeMenuItems (JMenuItem JavaDoc[] items) {
146             removeListeners (last);
147             addListeners (items);
148             last = items;
149             setMenuItems (items);
150         }
151
152
153         /** Add listeners to menu items.
154         * @param items the items
155         */

156         private void addListeners (JMenuItem JavaDoc[] items) {
157             int len = items.length;
158             for (int i = 0; i < len; i++) {
159                 items[i].addPropertyChangeListener (propL);
160             }
161         }
162
163         /** Remove all listeners from menu items.
164         * @param items the items
165         */

166         private void removeListeners (JMenuItem JavaDoc[] 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         /** Property listnener to watch changes of enable state.
190         */

191         private class PropL implements PropertyChangeListener JavaDoc {
192             public void propertyChange (PropertyChangeEvent JavaDoc ev) {
193                 String JavaDoc name = ev.getPropertyName ();
194                 if (
195                     name == null ||
196                     name.equals (SystemAction.PROP_ENABLED) ||
197                     name.equals (Registry.PROP_ACTIVATED_NODES)
198                     ) {
199                     // change items later
200
needsChange = true;
201                 }
202             }
203         }
204         
205     } // end: class Menu
206

207 }
208
Popular Tags