站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > NetBeans API Javadoc 5.0.0

ExplorerUtils (NetBeans Explorer API) - NetBeans API Javadoc 5.0.0

 

org.openide.explorer
Class ExplorerUtils

java.lang.Object
  extended byorg.openide.explorer.ExplorerUtils

public final class ExplorerUtils
extends Object

Helper methods to embed ExplorerManagers and explorer views into Swing component trees.

To create a component that displays the content of an ExplorerManager you should make your component implement ExplorerManager.Provider and Lookup.Provider and register actions in your component's ActionMap:

public class YourComponent extends TopComponent
implements ExplorerManager.Provider, Lookup.Provider {
    private ExplorerManager manager;
    public YourComponent() {
        this.manager = new ExplorerManager();
        ActionMap map = this.getActionMap ();
        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
        map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false

        // following line tells the top component which lookup should be associated with it
        associateLookup (ExplorerUtils.createLookup (manager, map));
    }
    public ExplorerManager getExplorerManager() {
        return manager;
    }
    // It is good idea to switch all listeners on and off when the
    // component is shown or hidden. In the case of TopComponent use:
    protected void componentActivated() {
        ExplorerUtils.activateActions(manager, true);
    }
    protected void componentDeactivated() {
        ExplorerUtils.activateActions(manager, false);
    }
}
The above code will work in a NetBeans module. For a standalone NetBeans-based application you will need to set up your InputMap and use different triggers to turn the listeners on and off:
public class YourComponent extends JPanel
implements ExplorerManager.Provider, Lookup.Provider {
    private ExplorerManager manager;
    private Lookup lookup;
    public YourComponent() {
        // same as before...
        manager = new ExplorerManager();
        ActionMap map = getActionMap();
        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
        map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false

        // ...but add e.g.:
        InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        keys.put(KeyStroke.getKeyStroke("control C"), DefaultEditorKit.copyAction);
        keys.put(KeyStroke.getKeyStroke("control X"), DefaultEditorKit.cutAction);
        keys.put(KeyStroke.getKeyStroke("control V"), DefaultEditorKit.pasteAction);
        keys.put(KeyStroke.getKeyStroke("DELETE"), "delete");

        // ...and initialization of lookup variable
        lookup = ExplorerUtils.createLookup (manager, map);
    }
    // ...method as before and getLookup
    public ExplorerManager getExplorerManager() {
        return manager;
    }
    public Lookup getLookup() {
        return lookup;
    }
    // ...methods as before, but replace componentActivated and
    // componentDeactivated with e.g.:
    public void addNotify() {
        super.addNotify();
        ExplorerUtils.activateActions(manager, true);
    }
    public void removeNotify() {
        ExplorerUtils.activateActions(manager, false);
        super.removeNotify();
    }
}

Since:
4.14

Method Summary
static Action actionCopy(ExplorerManager em)
          Creates copy action
static Action actionCut(ExplorerManager em)
          Creates cut action
static Action actionDelete(ExplorerManager em, boolean confirm)
          Creates delete action
static Action actionPaste(ExplorerManager em)
          Creates paste action
static void activateActions(ExplorerManager em, boolean enable)
          Activates or deactivates updates of actions for given ExplorerManager.
static Lookup createLookup(ExplorerManager em, ActionMap map)
          Creates new lookup containing selected nodes and their lookups.
static HelpCtx getHelpCtx(Node[] sel, HelpCtx def)
          Utility method to get context help from a node selection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

actionCopy

public static Action actionCopy(ExplorerManager em)
Creates copy action

Parameters:
em - explorer manager the action should be attached to
Returns:
action that invokes copy on the explorer

actionCut

public static Action actionCut(ExplorerManager em)
Creates cut action

Parameters:
em - explorer manager the action should be attached to
Returns:
action that invokes cut on the explorer

actionDelete

public static Action actionDelete(ExplorerManager em,
                                  boolean confirm)
Creates delete action

Parameters:
em - explorer manager the action should be attached to
confirm - true if a confirmation box should be displayed before actual deletion
Returns:
action that invokes delete on the explorer

actionPaste

public static Action actionPaste(ExplorerManager em)
Creates paste action

Parameters:
em - explorer manager the action should be attached to
Returns:
action that invokes paste on the explorer

activateActions

public static void activateActions(ExplorerManager em,
                                   boolean enable)
Activates or deactivates updates of actions for given ExplorerManager. By default actions created by actionXXX factory methods are started and update itself according to changes in external environment (the explorer manager itself, clipboard content, etc.). This might not be necessary and a bit of resource consuming in case when the component showing the ExplorerManager is not visible. In such case the implementation can disable and then again reenable refresh by calling this method.

Parameters:
em - the explorer manager
enable - true if actions should be updated, false otherwise

createLookup

public static Lookup createLookup(ExplorerManager em,
                                  ActionMap map)
Creates new lookup containing selected nodes and their lookups.

Parameters:
em - explorer manager which selection to follow
map - additional map to be added into the lookup
Returns:
lookup that updates itself according the changes inside the ExplorerManager

getHelpCtx

public static HelpCtx getHelpCtx(Node[] sel,
                                 HelpCtx def)
Utility method to get context help from a node selection. Tries to find context helps for selected nodes. If there are some, and they all agree, uses that. In all other cases, uses the supplied generic help.

Parameters:
sel - a list of nodes to search for help in
def - the default help to use if they have none or do not agree
Returns:
a help context
Since:
4.40

 

Built on May 3 2007.  |  Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.