public final class ExplorerUtils extends Object
ExplorerManager
s 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(); } }
Modifier and Type | Method and Description |
---|---|
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.
|
public static Action actionCopy(ExplorerManager em)
em
- explorer manager the action should be attached topublic static Action actionCut(ExplorerManager em)
em
- explorer manager the action should be attached topublic static Action actionDelete(ExplorerManager em, boolean confirm)
em
- explorer manager the action should be attached toconfirm
- true if a confirmation box should be displayed before actual deletionpublic static Action actionPaste(ExplorerManager em)
em
- explorer manager the action should be attached topublic static void activateActions(ExplorerManager em, boolean enable)
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.em
- the explorer managerenable
- true if actions should be updated, false otherwisepublic static Lookup createLookup(ExplorerManager em, ActionMap map)
em
- explorer manager which selection to followmap
- additional map to be added into the lookuppublic static HelpCtx getHelpCtx(Node[] sel, HelpCtx def)
sel
- a list of nodes to search for help indef
- the default help to use if they have none or do not agree