package tr.gov.ulakbim.jDenetX.gui.clustertab;
import tr.gov.ulakbim.jDenetX.clusterers.AbstractClusterer;
import tr.gov.ulakbim.jDenetX.evaluation.MeasureCollection;
import tr.gov.ulakbim.jDenetX.gui.FileExtensionFilter;
import tr.gov.ulakbim.jDenetX.gui.TextViewerPanel;
import tr.gov.ulakbim.jDenetX.streams.clustering.ClusteringStream;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
/*
* ClusteringSetupTab.java
*
* Created on 20.03.2010, 09:41:54
*/
/**
* @author jansen
*/
public class ClusteringSetupTab extends javax.swing.JPanel {
private ClusteringTabPanel clusteringTab;
private String lastfile;
/**
* Creates new form ClusteringSetupTab
*/
public ClusteringSetupTab() {
initComponents();
clusteringAlgoPanel0.renderAlgoPanel(false);
clusteringAlgoPanel1.renderAlgoPanel(true);
}
public AbstractClusterer getClusterer0() {
return clusteringAlgoPanel0.getClusterer();
}
public AbstractClusterer getClusterer1() {
if (!clusteringAlgoPanel1.duplicateClusterer())
return clusteringAlgoPanel1.getClusterer();
else
return clusteringAlgoPanel0.getClusterer();
}
public boolean duplicateClusterer() {
return clusteringAlgoPanel1.duplicateClusterer();
}
public ClusteringStream getStream0() {
return clusteringAlgoPanel0.getStream();
}
public MeasureCollection[] getMeasures() {
return clusteringEvalPanel1.getSelectedMeasures();
}
public ClusteringStream getStream1() {
//if duplicate stream is active we return the duplicated stream,
//but for performance we should really use the same stream and only
//copy the generated points
if (!clusteringAlgoPanel1.duplicateStream())
return clusteringAlgoPanel1.getStream();
else
return clusteringAlgoPanel0.getStream();
}
public boolean duplicateStream() {
return clusteringAlgoPanel1.duplicateStream();
}
public TextViewerPanel getLogPanel() {
return logPanel;
}
public void addButtonActionListener(ActionListener l) {
buttonWeka.addActionListener(l);
buttonWeka.setActionCommand("weka export");
buttonExport.addActionListener(l);
buttonExport.setActionCommand("csv export");
}
/**
* 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() {
java.awt.GridBagConstraints gridBagConstraints;
clusteringAlgoPanel0 = new tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringAlgoPanel();
enableCompAlgo = new javax.swing.JCheckBox();
clusteringAlgoPanel1 = new tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringAlgoPanel();
clusteringEvalPanel1 = new tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringEvalPanel();
buttonStart = new javax.swing.JButton();
buttonStop = new javax.swing.JButton();
buttonExport = new javax.swing.JButton();
buttonWeka = new javax.swing.JButton();
buttonImportSettings = new javax.swing.JButton();
buttonExportSettings = new javax.swing.JButton();
logPanel = new tr.gov.ulakbim.jDenetX.gui.TextViewerPanel();
setLayout(new java.awt.GridBagLayout());
clusteringAlgoPanel0.setPreferredSize(new java.awt.Dimension(300, 60));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(clusteringAlgoPanel0, gridBagConstraints);
enableCompAlgo.setSelected(true);
enableCompAlgo.setText("use Comparison-Algorithm for visualization");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(21, 4, 0, 4);
add(enableCompAlgo, gridBagConstraints);
clusteringAlgoPanel1.setPreferredSize(new java.awt.Dimension(350, 150));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.gridheight = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(clusteringAlgoPanel1, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(clusteringEvalPanel1, gridBagConstraints);
buttonStart.setText("Start");
buttonStart.setPreferredSize(new java.awt.Dimension(80, 23));
buttonStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonStartActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(buttonStart, gridBagConstraints);
buttonStop.setText("Stop");
buttonStop.setEnabled(false);
buttonStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonStopActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(buttonStop, gridBagConstraints);
buttonExport.setText("Export CSV");
buttonExport.setEnabled(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 5;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(buttonExport, gridBagConstraints);
buttonWeka.setText("Weka Explorer");
buttonWeka.setEnabled(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(buttonWeka, gridBagConstraints);
buttonImportSettings.setText("Import");
buttonImportSettings.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonImportSettingsActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(4, 75, 4, 4);
add(buttonImportSettings, gridBagConstraints);
buttonExportSettings.setText("Export");
buttonExportSettings.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonExportSettingsActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(buttonExportSettings, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.gridwidth = 7;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
add(logPanel, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void buttonImportSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonImportSettingsActionPerformed
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.addChoosableFileFilter(new FileExtensionFilter("txt"));
if (lastfile != null)
fileChooser.setSelectedFile(new File(lastfile));
if (fileChooser.showOpenDialog(this.buttonImportSettings) == JFileChooser.APPROVE_OPTION) {
lastfile = fileChooser.getSelectedFile().getPath();
loadOptionsFromFile(fileChooser.getSelectedFile().getPath());
}
}//GEN-LAST:event_buttonImportSettingsActionPerformed
private void buttonExportSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExportSettingsActionPerformed
StringBuffer sb = new StringBuffer();
sb.append(clusteringAlgoPanel0.getStreamValueAsCLIString() + "\n");
sb.append(clusteringAlgoPanel0.getAlgorithmValueAsCLIString() + "\n");
if (clusteringAlgoPanel1.duplicateStream())
sb.append("1" + "\n");
else
sb.append(clusteringAlgoPanel1.getStreamValueAsCLIString() + "\n");
if (clusteringAlgoPanel1.duplicateClusterer())
sb.append("1" + "\n");
else
sb.append(clusteringAlgoPanel1.getAlgorithmValueAsCLIString() + "\n");
System.out.println(sb);
logPanel.addText(sb.toString());
}//GEN-LAST:event_buttonExportSettingsActionPerformed
private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStartActionPerformed
toggle(true);
}//GEN-LAST:event_buttonStartActionPerformed
private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopActionPerformed
stop(true);
}//GEN-LAST:event_buttonStopActionPerformed
private void loadOptionsFromFile(String filepath) {
try {
BufferedReader in = new BufferedReader(new FileReader(filepath));
String line = in.readLine();
String stream0 = line;
clusteringAlgoPanel0.setStreamValueAsCLIString(stream0);
line = in.readLine();
String algo0 = line;
clusteringAlgoPanel0.setAlgorithmValueAsCLIString(algo0);
line = in.readLine();
String stream1 = line;
if (stream1.startsWith("1"))
clusteringAlgoPanel1.setDuplicateStream(true);
else
clusteringAlgoPanel1.setStreamValueAsCLIString(stream1);
line = in.readLine();
String algo1 = line;
if (algo1.startsWith("1"))
clusteringAlgoPanel1.setDuplicateClusterer(true);
else
clusteringAlgoPanel1.setAlgorithmValueAsCLIString(algo1);
System.out.println("Loading settings from " + filepath);
logPanel.addText("Loading settings from " + filepath);
} catch (Exception e) {
System.out.println("Bad option file:" + e.getMessage());
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonExport;
private javax.swing.JButton buttonExportSettings;
private javax.swing.JButton buttonImportSettings;
private javax.swing.JButton buttonStart;
private javax.swing.JButton buttonStop;
private javax.swing.JButton buttonWeka;
private tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringAlgoPanel clusteringAlgoPanel0;
private tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringAlgoPanel clusteringAlgoPanel1;
private tr.gov.ulakbim.jDenetX.gui.clustertab.ClusteringEvalPanel clusteringEvalPanel1;
private javax.swing.JCheckBox enableCompAlgo;
private tr.gov.ulakbim.jDenetX.gui.TextViewerPanel logPanel;
// End of variables declaration//GEN-END:variables
void setClusteringTab(ClusteringTabPanel clusteringTab) {
this.clusteringTab = clusteringTab;
}
public void toggleRunMode() {
toggle(false);
}
public void stopRun() {
stop(false);
}
private void toggle(boolean internal) {
setStateConfigButtons(false);
if (buttonStart.getText().equals("Pause")) {
buttonStart.setText("Resume");
buttonWeka.setEnabled(true);
buttonExport.setEnabled(true);
} else {
buttonStart.setText("Pause");
buttonWeka.setEnabled(false);
buttonExport.setEnabled(false);
}
//push event forward to the cluster tab
if (internal)
clusteringTab.toggle();
}
private void stop(boolean internal) {
buttonStart.setEnabled(true);
buttonStart.setText("Start");
buttonStop.setEnabled(false);
buttonWeka.setEnabled(false);
buttonExport.setEnabled(false);
setStateConfigButtons(true);
//push event forward to the cluster tab
if (internal)
clusteringTab.stop();
}
private void setStateConfigButtons(boolean state) {
buttonStop.setEnabled(!state);
buttonExportSettings.setEnabled(state);
buttonImportSettings.setEnabled(state);
// clusteringAlgoPanel1.setEnabled(state);
// clusteringEvalPanel1.setEnabled(state);
enableCompAlgo.setEnabled(state);
}
}