alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.explorer;

import java.awt.Component;
import java.beans.*;
import java.io.*;
import java.util.*;
import java.text.MessageFormat;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.SwingUtilities;

import org.openide.nodes.*;
import org.openide.ErrorManager;
import org.openide.util.*;
import org.openide.util.datatransfer.*;
import org.openide.util.io.SafeException;

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

* To create a component that displays the content of an {@link ExplorerManager} you * should make your component implement {@link ExplorerManager.Provider} and * {@link org.openide.util.Lookup.Provider} and register actions in your component's {@link 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 {@link javax.swing.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();
    }
}
* @author Jaroslav Tulach * @since 4.14 */ public final class ExplorerUtils extends Object { /** Just package private subclass */ ExplorerUtils () { } // // Public factory methods // /** Creates copy action * @param em explorer manager the action should be attached to * @return action that invokes copy on the explorer */ public static Action actionCopy (ExplorerManager em) { return ExplorerManager.findExplorerActionsImpl (em).copyAction (); } /** Creates cut action * @param em explorer manager the action should be attached to * @return action that invokes cut on the explorer */ public static Action actionCut (ExplorerManager em) { return ExplorerManager.findExplorerActionsImpl (em).cutAction (); } /** Creates delete action * @param em explorer manager the action should be attached to * @param confirm true if a confirmation box should be displayed before actual deletion * @return action that invokes delete on the explorer */ public static Action actionDelete (ExplorerManager em, boolean confirm) { ExplorerActionsImpl impl = ExplorerManager.findExplorerActionsImpl (em); return impl.deleteAction (confirm); } /** Creates paste action * @param em explorer manager the action should be attached to * @return action that invokes paste on the explorer */ public static Action actionPaste (ExplorerManager em) { return ExplorerManager.findExplorerActionsImpl (em).pasteAction (); } /** 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. * * @param em the explorer manager * @param enable true if actions should be updated, false otherwise */ public static void activateActions (ExplorerManager em, boolean enable) { if (enable) { ExplorerManager.findExplorerActionsImpl (em).attach (em); } else { ExplorerManager.findExplorerActionsImpl (em).detach (); } } /** Creates new lookup containing selected nodes and their lookups. * @param em explorer manager which selection to follow * @param map additional map to be added into the lookup * * @return lookup that updates itself according the changes inside the ExplorerManager */ public static Lookup createLookup (ExplorerManager em, ActionMap map) { return new DefaultEMLookup (em, map); } /** 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 * @since 4.40 */ 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; } } } return result != null ? result : def; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.