|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.examples.modules.shownodeactions;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import org.openide.nodes.*;
import org.openide.util.actions.*;
import org.openide.windows.TopComponent;
public class ShowNodeActionsMenu extends JMenu {
public ShowNodeActionsMenu () {
System.err.println("Creating ShowNodeActionsMenu...");
// [PENDING] really the list of things should be shared state, and only
// the physical menu part of instance state, but whatever...
addPropertyChangeListener (new PropertyChangeListener () {
public void propertyChange (PropertyChangeEvent ev) {
if ("ancestor".equals (ev.getPropertyName ()))
update ();
}
});
TopComponent.getRegistry ().addPropertyChangeListener (new PropertyChangeListener () {
public void propertyChange (PropertyChangeEvent ev) {
if (isShowing () && TopComponent.Registry.PROP_CURRENT_NODES.equals (ev.getPropertyName ()))
update ();
}
});
}
private void update () {
//System.err.println("update");
removeAll ();
Node[] nodes = TopComponent.getRegistry ().getCurrentNodes ();
if (nodes == null) nodes = new Node[] {};
// Adapted from org.openide.nodes.NodeOp algorithm:
// hashtable: SystemAction -> Integer
HashMap actions = new HashMap ();
// counts the number of occurences for each action
for (int n = 0; n < nodes.length; n++) {
SystemAction[] arr = nodes[n].getActions ();
if (arr == null) {
// use default actions
arr = NodeOp.getDefaultActions ();
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
Integer cntInt = (Integer)actions.get (arr[i]);
int cnt = cntInt == null ? 0 : cntInt.intValue ();
actions.put (arr[i], new Integer (cnt + 1));
}
}
}
// take all actions that are nodes.length number times
if (!actions.isEmpty ()) {
SystemAction[] arr = nodes[0].getActions ();
if (arr == null) {
// use default
arr = NodeOp.getDefaultActions ();
}
boolean canSep = false;
for (int i = 0; i < arr.length; i++) {
boolean addSep = true;
if (arr[i] != null) {
Integer cntInt = (Integer)actions.get (arr[i]);
int cnt = cntInt == null ? 0 : cntInt.intValue ();
if (cnt == nodes.length) {
addSep = false;
canSep = true;
JMenuItem item;
//System.err.println("adding " + arr[i]);
// Use popup presenters, not menu, because they are faster to display:
if (arr[i] instanceof Presenter.Popup) {
item = ((Presenter.Popup) arr[i]).getPopupPresenter ();
} else {
item = new JMenuItem (arr[i].getName ());
item.setEnabled (false);
}
add (item);
}
}
if (addSep && canSep) {
addSeparator ();
canSep = false;
}
}
}
setEnabled (getMenuComponentCount () > 0);
switch (nodes.length) {
case 0:
setText ("(No selection)");
break;
case 1:
setText ("Popup: " + nodes[0].getDisplayName ());
break;
default:
setText ("Popup: (" + nodes.length + " nodes)");
break;
}
}
}
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.