/* * Copyright (C) 2013 Serdar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.fub.maps.project.detector.wizards.detector; import de.fub.utilsmodule.Collections.ObservableArrayList; import de.fub.utilsmodule.Collections.ObservableList; import de.fub.utilsmodule.icons.IconRegister; import java.awt.Image; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import javax.swing.Action; import javax.swing.JPanel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.openide.explorer.ExplorerManager; import org.openide.explorer.ExplorerUtils; import org.openide.loaders.DataObject; import org.openide.nodes.AbstractNode; import org.openide.nodes.ChildFactory; import org.openide.nodes.Children; import org.openide.nodes.FilterNode; import org.openide.nodes.Node; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.Utilities; import org.openide.util.WeakListeners; import org.openide.util.lookup.AbstractLookup; import org.openide.util.lookup.InstanceContent; /** * * @author Serdar */ @NbBundle.Messages({ "CLT_InferenceDataset_Name=Inference Data Set" }) public class InferenceDataSetSelectionVisualPanel extends JPanel implements Lookup.Provider, ExplorerManager.Provider { private static final long serialVersionUID = 1L; private final ExplorerManager explorerManager = new ExplorerManager(); private Lookup lookup = ExplorerUtils.createLookup(explorerManager, getActionMap()); /** * Creates new form InferenceDataSetSelectionVisualPanel */ public InferenceDataSetSelectionVisualPanel() { initComponents(); beanTreeView1.setRootVisible(true); explorerManager.setRootContext(new RootNode()); } @Override public String getName() { return Bundle.CLT_InferenceDataset_Name(); } public List<Node> getDataNodes() { return Arrays.asList(getExplorerManager().getRootContext().getChildren().getNodes(true)); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { beanTreeView1 = new org.openide.explorer.view.BeanTreeView(); setPreferredSize(new java.awt.Dimension(400, 300)); setLayout(new java.awt.BorderLayout()); beanTreeView1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204))); beanTreeView1.setPreferredSize(new java.awt.Dimension(400, 300)); add(beanTreeView1, java.awt.BorderLayout.CENTER); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private org.openide.explorer.view.BeanTreeView beanTreeView1; // End of variables declaration//GEN-END:variables @Override public ExplorerManager getExplorerManager() { return explorerManager; } @Override public Lookup getLookup() { return lookup; } private static class DataNodeFactory extends ChildFactory<DataObject> implements ChangeListener { private final ObservableList<DataObject> list; public DataNodeFactory(ObservableList<DataObject> list) { this.list = list; this.list.addChangeListener(WeakListeners.change(DataNodeFactory.this, this.list)); } @Override protected boolean createKeys(List<DataObject> toPopulate) { toPopulate.addAll(list); return true; } @Override protected Node createNodeForKey(DataObject key) { return new FilterNode(key.getNodeDelegate()) { @Override public Action[] getActions(boolean context) { return new Action[0]; } }; } @Override public void stateChanged(ChangeEvent e) { refresh(true); } } private static class RootNode extends AbstractNode { public static final String ACTION_PATH = "mapsforge/Detector/Wizard/Inferenceset/Actions"; private InstanceContent content; public RootNode() { this(new ObservableArrayList<DataObject>(), new InstanceContent()); } private RootNode(final ObservableList<DataObject> list, InstanceContent instanceContent) { super(Children.createLazy(new Callable<Children>() { @Override public Children call() throws Exception { return Children.create(new DataNodeFactory(list), true); } }), new AbstractLookup(instanceContent)); setDisplayName("Inference Dataset"); //NON18N content = instanceContent; content.add(list); } @Override public Action[] getActions(boolean context) { List<? extends Action> actionsForPath = Utilities.actionsForPath(ACTION_PATH); return actionsForPath.toArray(new Action[actionsForPath.size()]); } @Override public Image getIcon(int type) { Image image = IconRegister.getFolderIcon(); if (image == null) { image = super.getIcon(type); } return image; } @Override public Image getOpenedIcon(int type) { return getIcon(type); } } }