/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package nl.fontys.sofa.limo.view.graphs;
import java.awt.event.ActionEvent;
import java.util.Collection;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.BarChart;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import javafx.scene.chart.Chart;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javax.swing.ImageIcon;
import nl.fontys.sofa.limo.view.topcomponent.ResultTopComponent;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.WindowManager;
/**
* Top component which displays something.
*/
@TopComponent.Description(
preferredID = "GraphSwitchTopComponent",
iconBase = "icons/gui/system_task.png",
persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "properties", openAtStartup = false)
@ActionID(category = "Window", id = "nl.fontys.sofa.limo.view.graphs.GraphSwitchTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_GraphSwitchAction",
preferredID = "GraphSwitchTopComponent"
)
@Messages({
"CTL_GraphSwitchAction=GraphSwitch",
"CTL_GraphSwitchTopComponent=GraphSwitch Window",
"HINT_GraphSwitchTopComponent=This is a GraphSwitch window"
})
public final class GraphSwitchTopComponent extends TopComponent implements LookupListener {
private Lookup.Result<ResultTopComponent.GraphChangeListener> result = null;
private final ProxyLookup lkp;
private final InstanceContent ic = new InstanceContent();
public GraphSwitchTopComponent() {
initComponents();
setName(Bundle.CTL_GraphSwitchTopComponent());
setToolTipText(Bundle.HINT_GraphSwitchTopComponent());
putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
result = Utilities.actionsGlobalContext().lookupResult(ResultTopComponent.GraphChangeListener.class);
result.addLookupListener(this);
lkp = new ProxyLookup(Lookups.singleton(this), new AbstractLookup(ic));
}
private Class<? extends Chart> chartClass;
/**
* 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.
*/
private void initComponents() {
pieSelect = new javax.swing.JButton();
barSelect = new javax.swing.JButton();
lineSelect = new javax.swing.JButton();
areaSelect = new javax.swing.JButton();
setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));
pieSelect.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/gui/Pie_Chart.png")));
org.openide.awt.Mnemonics.setLocalizedText(pieSelect, org.openide.util.NbBundle.getMessage(GraphSwitchTopComponent.class, "GraphSwitchTopComponent.text")); // NOI18N
pieSelect.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
pieSelect.setIconTextGap(25);
pieSelect.setMargin(new java.awt.Insets(25, 25, 25, 25));
pieSelect.setMaximumSize(new java.awt.Dimension(500, 23));
pieSelect.setName(""); // NOI18N
pieSelect.setOpaque(false);
pieSelect.addActionListener((evt) -> {
pieSelectActionPerformed(evt);
});
add(pieSelect);
barSelect.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/gui/Bar_Chart.png")));
org.openide.awt.Mnemonics.setLocalizedText(barSelect, org.openide.util.NbBundle.getMessage(GraphSwitchTopComponent.class, "GraphSwitchTopComponent.barSelect.text")); // NOI18N
barSelect.setAutoscrolls(true);
barSelect.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
barSelect.setIconTextGap(25);
barSelect.setMargin(new java.awt.Insets(25, 25, 25, 25));
barSelect.setMaximumSize(new java.awt.Dimension(500, 23));
barSelect.addActionListener((evt)
-> {
barSelectActionPerformed(evt);
});
add(barSelect);
lineSelect.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/gui/Line_Chart.png")));
org.openide.awt.Mnemonics.setLocalizedText(lineSelect, org.openide.util.NbBundle.getMessage(GraphSwitchTopComponent.class, "GraphSwitchTopComponent.lineSelect.text")); // NOI18N
lineSelect.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
lineSelect.setIconTextGap(25);
lineSelect.setMargin(new java.awt.Insets(25, 25, 25, 25));
lineSelect.setMaximumSize(new java.awt.Dimension(500, 23));
lineSelect.addActionListener((evt) -> {
lineSelectActionPerformed(evt);
});
add(lineSelect);
areaSelect.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/gui/Area_Chart.png")));
org.openide.awt.Mnemonics.setLocalizedText(areaSelect, org.openide.util.NbBundle.getMessage(GraphSwitchTopComponent.class, "GraphSwitchTopComponent.areaSelect.text")); // NOI18N
areaSelect.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
areaSelect.setIconTextGap(25);
areaSelect.setMargin(new java.awt.Insets(25, 25, 25, 25));
areaSelect.setMaximumSize(new java.awt.Dimension(500, 23));
areaSelect.addActionListener((evt) -> {
areaSelectActionPerformed(evt);
});
add(areaSelect);
}// </editor-fold>
private void barSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_barSelectActionPerformed
if (chartClass != BarChart.class) {
chartClass = BarChart.class;
notifyGraphTypeChangeListener();
}
}//GEN-LAST:event_barSelectActionPerformed
private void pieSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pieSelectActionPerformed
if (chartClass != PieChart.class) {
chartClass = PieChart.class;
notifyGraphTypeChangeListener();
}
}//GEN-LAST:event_pieSelectActionPerformed
private void lineSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lineSelectActionPerformed
if (chartClass != LineChart.class) {
chartClass = LineChart.class;
notifyGraphTypeChangeListener();
}
}//GEN-LAST:event_lineSelectActionPerformed
private void areaSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_areaSelectActionPerformed
if (chartClass != AreaChart.class) {
chartClass = AreaChart.class;
notifyGraphTypeChangeListener();
}
}//GEN-LAST:event_areaSelectActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton areaSelect;
private javax.swing.JButton barSelect;
private javax.swing.JButton lineSelect;
private javax.swing.JButton pieSelect;
// End of variables declaration//GEN-END:variables
@Override
public void componentOpened() {
// TODO add custom code on component opening
}
ResultTopComponent.GraphChangeListener graphTypeChangeListener = null;
private void notifyGraphTypeChangeListener() {
ActionEvent event = new ActionEvent(chartClass, ActionEvent.ACTION_FIRST, "Chart class changed");
graphTypeChangeListener.actionPerformed(event);
}
@Override
public void componentClosed() {
// TODO add custom code on component closing
}
@Override
public Lookup getLookup() {
return lkp;
}
@Override
public void resultChanged(LookupEvent le) {
Collection<? extends ResultTopComponent.GraphChangeListener> allInstances = result.allInstances();
GraphSwitchTopComponent lookup = Utilities.actionsGlobalContext().lookup(GraphSwitchTopComponent.class);
if (!allInstances.isEmpty()) {
WindowManager.getDefault().findMode("properties").dockInto(this);
this.open();
this.requestVisible();
if (graphTypeChangeListener != null) {
if (lookup == null) {
graphTypeChangeListener = allInstances.iterator().next();
}
} else {
graphTypeChangeListener = allInstances.iterator().next();
ic.add(graphTypeChangeListener);
}
} else {
this.close();
}
}
}