/*
* 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.maps.project.detector.model.inference.AbstractInferenceModel;
import de.fub.maps.project.detector.utils.DetectorUtils;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.Set;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.OutlineView;
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.WeakListeners;
@NbBundle.Messages({
"CLT_Inference_Models=Inference Model"
})
public final class InferenceModelSelectionVisualPanel extends JPanel implements ExplorerManager.Provider, PropertyChangeListener {
private static final long serialVersionUID = 1L;
private final ExplorerManager explorerManager = new ExplorerManager();
/**
* Creates new form InferenceModelSelectionVisualPanel
*/
public InferenceModelSelectionVisualPanel() {
initComponents();
outlineView.getOutline().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
outlineView.getOutline().setRootVisible(false);
explorerManager.setRootContext(new AbstractNode(Children.create(new InferenzModelNodeFactory(), true)));
explorerManager.addPropertyChangeListener(WeakListeners.propertyChange(InferenceModelSelectionVisualPanel.this, explorerManager));
}
@Override
public String getName() {
return Bundle.CLT_Inference_Models();
}
public JTextArea getDescription() {
return description;
}
public OutlineView getOutlineView() {
return outlineView;
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
outlineView = new org.openide.explorer.view.OutlineView(Bundle.CLT_Inference_Models());
jScrollPane1 = new javax.swing.JScrollPane();
description = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
setPreferredSize(new java.awt.Dimension(400, 300));
outlineView.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
jScrollPane1.setMinimumSize(new java.awt.Dimension(23, 58));
jScrollPane1.setPreferredSize(new java.awt.Dimension(166, 58));
description.setEditable(false);
description.setColumns(20);
description.setLineWrap(true);
description.setRows(2);
description.setWrapStyleWord(true);
description.setMinimumSize(new java.awt.Dimension(4, 0));
description.setOpaque(false);
description.setPreferredSize(new java.awt.Dimension(164, 0));
jScrollPane1.setViewportView(description);
org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(InferenceModelSelectionVisualPanel.class, "InferenceModelSelectionVisualPanel.jLabel1.text")); // NOI18N
jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(outlineView, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(outlineView, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1)
.addGap(0, 0, 0)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea description;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private org.openide.explorer.view.OutlineView outlineView;
// End of variables declaration//GEN-END:variables
@Override
public ExplorerManager getExplorerManager() {
return explorerManager;
}
public AbstractInferenceModel getSelectedInferenceModel() {
Node[] selectedNodes = getExplorerManager().getSelectedNodes();
return selectedNodes.length == 1
? selectedNodes[0].getLookup().lookup(AbstractInferenceModel.class)
: null;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
Node[] selectedNodes = explorerManager.getSelectedNodes();
if (selectedNodes.length == 1) {
AbstractInferenceModel inferenceModel = selectedNodes[0].getLookup().lookup(AbstractInferenceModel.class);
if (inferenceModel != null) {
getDescription().setText(inferenceModel.getDescription());
}
} else {
getDescription().setText(null);
}
}
}
private static class InferenzModelNodeFactory extends ChildFactory<AbstractInferenceModel> {
@Override
protected boolean createKeys(List<AbstractInferenceModel> list) {
Set<Class<? extends AbstractInferenceModel>> allClasses = Lookup.getDefault().lookupResult(AbstractInferenceModel.class).allClasses();
for (Class<? extends AbstractInferenceModel> clazz : allClasses) {
AbstractInferenceModel instance = DetectorUtils.createInstance(clazz, clazz.getName());
list.add(instance);
}
return true;
}
@Override
protected Node createNodeForKey(AbstractInferenceModel inferenceModel) {
return new InferenceModelFilterNode(inferenceModel.getNodeDelegate());
}
}
private static class InferenceModelFilterNode extends FilterNode {
public InferenceModelFilterNode(Node original) {
super(original);
}
@Override
public Action[] getActions(boolean context) {
return new Action[0];
}
@Override
public String getShortDescription() {
return null;
}
}
}