Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

File Browser

$
0
0
import java.awt.BorderLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ActionMap;
import javax.swing.text.DefaultEditorKit;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;

@TopComponent.Description(
        preferredID = "FileBrowserTopComponent",        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(        mode = "explorer",         openAtStartup = true)
@ActionID(        category = "Window",         id = "com.mycompany.FileBrowserTopComponent")
@ActionReference(        path = "Menu/Window")
@TopComponent.OpenActionRegistration(        displayName = "#CTL_FileBrowserTopComponentAction",        preferredID = "FileBrowserTopComponent")
@NbBundle.Messages({    "CTL_FileBrowserTopComponentAction=Open File Browser",    "CTL_FileBrowserTopComponent=File Browser",
})
public class FileBrowserTopComponent extends TopComponent implements ExplorerManager.Provider  {    private ExplorerManager mgr = new ExplorerManager();    public FileBrowserTopComponent() throws DataObjectNotFoundException {        setDisplayName(Bundle.CTL_FileBrowserTopComponent());        setLayout(new BorderLayout());        add(new BeanTreeView(), BorderLayout.CENTER);        File file = new File(System.getProperty("user.home"));        FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(file));        DataObject dob = DataObject.find(fo);        Node rootNode = dob.getNodeDelegate();        mgr.setRootContext(new FileFilterNode(rootNode));        ActionMap map = this.getActionMap();        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(mgr));        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(mgr));        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(mgr));        map.put("delete", ExplorerUtils.actionDelete(mgr, true)); // or false        associateLookup(ExplorerUtils.createLookup(mgr, map));    }    @Override    public ExplorerManager getExplorerManager() {        return mgr;    }    public class FileFilterNode extends FilterNode {        public FileFilterNode(Node original) {            super(original, new FileFilterNodeChildren(original));        }        //Override e.g., icons here...    }    public class FileFilterNodeChildren extends FilterNode.Children {        public FileFilterNodeChildren(Node original) {            super(original);        }        @Override        protected Node[] createNodes(Node object) {            List<Node> result = new ArrayList<Node>();            for (Node node : super.createNodes(object)) {                if (accept(node)) {                    result.add(node);                }            }            return result.toArray(new Node[0]);        }        //Example filter for children, e.g., don't include if folder:        private boolean accept(Node node) {            return node.getLookup().lookup(DataFolder.class) == null;        }    }
}

Thanks to Tim Boudreau for help with the above.


Viewing all articles
Browse latest Browse all 19780

Trending Articles