2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.xml.core.actions;
43
import java.beans.PropertyChangeListener;
44
import java.beans.PropertyChangeEvent;
47
import javax.swing.JMenuItem;
49
import org.openide.awt.JInlineMenu;
50
import org.openide.windows.TopComponent.Registry;
51
import org.openide.windows.WindowManager;
52
import org.openide.util.actions.SystemAction;
53
import org.openide.util.actions.Presenter;
54
import org.openide.util.Lookup;
57
public abstract class CollectSystemAction extends SystemAction implements Presenter.Popup {
58
/** Serial Version UID */
59
private static final long serialVersionUID = 6517322512481423122L;
61
/** All Actions Lookup Result. */
62
private Lookup.Result allActionsResult;
64
/** empty array of menu items */
65
static JMenuItem[] NONE = new JMenuItem[] {};
68
/** Which Class should be used for Lookup? */
69
protected abstract Class getActionLookClass ();
71
/** @return all instances of <code>getActionLookClass</code>.
73
protected synchronized Collection getPossibleActions () {
74
if ( allActionsResult == null ) {
75
allActionsResult = Lookup.getDefault().lookup (new Lookup.Template (getActionLookClass()));
77
return allActionsResult.allInstances();
81
private JMenuItem[] createMenu () {
84
menu = createMenu (getPossibleActions());
86
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("--- CollectSystemAction.createMenu: menu = " + menu);//, new RuntimeException());
91
private JMenuItem[] createMenu (Collection coll) {
92
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n--> CollectSystemAction.createMenu: ( " + coll + " )");
94
ArrayList items = new ArrayList ();
96
Iterator it = coll.iterator();
97
while (it.hasNext ()) {
98
SystemAction a = (SystemAction) it.next();
100
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-*- CollectSystemAction.createMenu: next action " + a +
101
" -- " + ( a.isEnabled() ? "<enabled>" : "[disabled]" ) );
103
if ( a.isEnabled() ) {
104
JMenuItem item = null;
105
if (a instanceof Presenter.Popup) {
106
item = ((Presenter.Popup)a).getPopupPresenter ();
109
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-*- CollectSystemAction.createMenu: menu item = " + item);
111
// test if we obtained the item
118
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("<-- CollectSystemAction.createMenu: all items = " + items + "\n");
120
JMenuItem[] array = new JMenuItem [items.size ()];
121
items.toArray (array);
126
/* @return popup presenter.
128
public JMenuItem getPopupPresenter () {
133
* This action itself does nothing, it only presents other actions.
136
public void actionPerformed (java.awt.event.ActionEvent e) {
141
/** Presenter for this action.
143
private class Menu extends JInlineMenu {
144
private static final long serialVersionUID = -4962039848190160129L;
146
/** last registered items */
147
private JMenuItem[] last = NONE;
148
/** own property change listner */
149
private PropL propL = new PropL ();
155
changeMenuItems (createMenu());
157
Registry r = WindowManager.getDefault().getRegistry ();
159
r.addPropertyChangeListener (
160
org.openide.util.WeakListeners.propertyChange (propL, r)
164
/** Changes the selection to new items.
165
* @param items the new items
167
synchronized void changeMenuItems (JMenuItem[] items) {
168
removeListeners (last);
169
addListeners (items);
171
setMenuItems (items);
175
/** Add listeners to menu items.
176
* @param items the items
178
private void addListeners (JMenuItem[] items) {
179
int len = items.length;
180
for (int i = 0; i < len; i++) {
181
items[i].addPropertyChangeListener (propL);
185
/** Remove all listeners from menu items.
186
* @param items the items
188
private void removeListeners (JMenuItem[] items) {
189
int len = items.length;
190
for (int i = 0; i < len; i++) {
191
items[i].removePropertyChangeListener (propL);
195
boolean needsChange = false;
197
public void addNotify() {
199
changeMenuItems (createMenu());
205
public void removeNotify() {
206
removeListeners (last);
211
/** Property listnener to watch changes of enable state.
213
private class PropL implements PropertyChangeListener {
214
public void propertyChange (PropertyChangeEvent ev) {
215
String name = ev.getPropertyName ();
218
name.equals (SystemAction.PROP_ENABLED) ||
219
name.equals (Registry.PROP_ACTIVATED_NODES)
221
// change items later