package com.fiorano.openide.windows; import org.openide.explorer.ExplorerManager; import org.openide.explorer.ExplorerUtils; import org.openide.nodes.Node; import org.openide.util.HelpCtx; import org.openide.util.Lookup; import org.openide.windows.TopComponent; import javax.swing.*; import javax.swing.text.DefaultEditorKit; import java.awt.*; public class XExplorerPanel extends TopComponent implements ExplorerManager.Provider, Lookup.Provider { private ExplorerManager manager; public XExplorerPanel() { initExplorerManager(); setLayout(new BorderLayout()); } protected void initExplorerManager(){ 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)); //NOI18N map.put("forceDelete", ExplorerUtils.actionDelete(manager, false)); //NOI18N getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT). put(KeyStroke.getKeyStroke("shift DELETE"), "forceDelete"); //NOI18N //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); } /*-------------------------------------------------[ HelpCtx ]---------------------------------------------------*/ /** Get context help for an explorer window. * Looks at the manager's node selection. * @return the help context * @see #getHelpCtx(Node[],HelpCtx) */ public HelpCtx getHelpCtx () { return getHelpCtx (getExplorerManager ().getSelectedNodes (), super.getHelpCtx()); } public int getPersistenceType(){ return PERSISTENCE_NEVER; } /** 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. * @param sel a list of nodes to search for help in * @param def the default help to use if they have none or do not agree * @return a help context */ public static HelpCtx getHelpCtx (Node[] sel, HelpCtx def) { HelpCtx result = null; for (int i = 0; i < sel.length; i++) { HelpCtx attempt = sel[i].getHelpCtx (); if (attempt != null && ! attempt.equals (HelpCtx.DEFAULT_HELP)) { if (result == null || result.equals (attempt)) { result = attempt; } else { // More than one found, and they conflict. Get general help on the Explorer instead. result = null; break; } } } if (result != null) return result; else return def; } }