weka.classifiers.timeseries.gui.CustomPeriodicEditor Maven / Gradle / Ivy
/*
* 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 .
*/
/*
* CustomPeriodicEditor.java
* Copyright (C) 2010-2016 University of Waikato, Hamilton, New Zealand
*/
package weka.classifiers.timeseries.gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import weka.classifiers.timeseries.core.CustomPeriodicTest;
/**
* Provides a gui editor for a custom periodic test
*
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
* @version $Revision: 52593 $
*/
public class CustomPeriodicEditor extends JPanel {
/** For serialization */
private static final long serialVersionUID = 9027916898659519804L;
/** The list of tests to edit/add to */
protected List m_testsToEdit;
/** The lower bound editor */
protected CustomPeriodicTestEditor m_lowerBoundEditor =
new CustomPeriodicTestEditor(false);
/** The upper bound editor */
protected CustomPeriodicTestEditor m_upperBoundEditor =
new CustomPeriodicTestEditor(true);
/** The name of the custom periodic field */
protected JTextField m_fieldName = new JTextField("MyNewField");
/** The list of tests to edit/add to */
protected JList m_testsToEditList;
/** Button that adds a new interval to the custom periodic test */
protected JButton m_newBut = new JButton("New");
/** Button that deletes the selected interval from the test */
protected JButton m_deleteBut = new JButton("Delete");
/** field for defining labels for tests */
protected JTextField m_labelField = new JTextField();
public CustomPeriodicEditor() {
this(new ArrayList());
}
/**
* Constructor
*
* @param tests the list of test intervals to edit - may be an empty
* list for a new custom periodic test.
*/
public CustomPeriodicEditor(List tests) {
m_testsToEdit = tests;
//m_testsToEdit = tests;
final DefaultListModel testVec = new DefaultListModel();
for (CustomPeriodicTest t : tests) {
testVec.addElement(t);
}
m_testsToEditList = new JList(testVec);
JScrollPane js = new JScrollPane(m_testsToEditList);
js.setBorder(BorderFactory.createTitledBorder("Test list (rows are OR'ed)"));
/* JPanel scrollHolder = new JPanel();
scrollHolder.setLayout(new BorderLayout());
scrollHolder.setBorder(BorderFactory.createTitledBorder("Test list (rows are OR'ed)")); */
m_testsToEditList.setVisibleRowCount(5);
m_deleteBut.setEnabled(false);
setLayout(new BorderLayout());
JPanel fieldHolder = new JPanel();
fieldHolder.setLayout(new BorderLayout());
fieldHolder.setBorder(BorderFactory.createTitledBorder("Field name"));
fieldHolder.add(m_fieldName, BorderLayout.CENTER);
JPanel buttHolder = new JPanel();
buttHolder.setLayout(new BorderLayout());
buttHolder.setBorder(BorderFactory.createTitledBorder("Edit"));
JPanel tempH = new JPanel(); tempH.setLayout(new BorderLayout());
tempH.add(m_newBut, BorderLayout.WEST);
tempH.add(m_deleteBut, BorderLayout.EAST);
buttHolder.add(tempH, BorderLayout.WEST);
JPanel fieldAndButtHolder = new JPanel();
fieldAndButtHolder.setLayout(new BorderLayout());
fieldAndButtHolder.add(fieldHolder, BorderLayout.NORTH);
fieldAndButtHolder.add(buttHolder, BorderLayout.CENTER);
JPanel testEditorHolder = new JPanel();
testEditorHolder.setLayout(new BorderLayout());
JPanel tempP = new JPanel(); tempP.setLayout(new BorderLayout());
testEditorHolder.setBorder(BorderFactory.createTitledBorder("Test interval"));
tempP.add(m_lowerBoundEditor, BorderLayout.NORTH);
tempP.add(m_upperBoundEditor, BorderLayout.SOUTH);
JPanel labelHolder = new JPanel();
labelHolder.setLayout(new BorderLayout());
JLabel labelLabel = new JLabel("Label");
labelLabel.setToolTipText("The label for this test. If all tests have " +
"a label then a nominal attribute is created; otherwise it is binary");
labelHolder.add(labelLabel, BorderLayout.WEST);
labelHolder.add(m_labelField, BorderLayout.CENTER);
m_labelField.setToolTipText("The label for this test. If all tests have " +
"a label then a nominal attribute is created; otherwise it is binary");
testEditorHolder.add(tempP, BorderLayout.NORTH);
testEditorHolder.add(labelHolder, BorderLayout.SOUTH);
fieldAndButtHolder.add(testEditorHolder, BorderLayout.SOUTH);
add(fieldAndButtHolder, BorderLayout.NORTH);
add(js, BorderLayout.CENTER);
m_testsToEditList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
if (!m_deleteBut.isEnabled()) {
m_deleteBut.setEnabled(true);
}
Object test = m_testsToEditList.getSelectedValue();
if (test != null) {
CustomPeriodicTest t = (CustomPeriodicTest)test;
m_lowerBoundEditor.setTestToEdit(t);
m_upperBoundEditor.setTestToEdit(t);
if (t.getLabel() != null && t.getLabel().length() > 0) {
m_labelField.setText(t.getLabel());
} else {
m_labelField.setText("");
}
}
}
}
});
m_newBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CustomPeriodicTest newTest =
new CustomPeriodicTest(">*:*:*:*:*:*:*:*:*:* <*:*:*:*:*:*:*:*:*:*");
m_lowerBoundEditor.setTestToEdit(newTest);
m_upperBoundEditor.setTestToEdit(newTest);
testVec.addElement(newTest);
m_testsToEdit.add(newTest);
m_labelField.setText("");
m_testsToEditList.setSelectedIndex(m_testsToEdit.size() - 1);
}
});
m_deleteBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selected = m_testsToEditList.getSelectedIndex();
if (selected >= 0) {
testVec.removeElementAt(selected);
m_testsToEdit.remove(selected);
}
}
});
m_lowerBoundEditor.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
/*m_testsToEditList.revalidate();
m_testsToEditList.invalidate(); */
m_testsToEditList.repaint();
if (m_lowerBoundEditor.getTestBeingEdited().getLowerTest().m_boundOperator ==
CustomPeriodicTest.Operator.EQUALS) {
m_upperBoundEditor.setEnabled(false);
} else {
m_upperBoundEditor.setEnabled(true);
}
}
});
m_upperBoundEditor.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
/*m_testsToEditList.revalidate();
m_testsToEditList.invalidate(); */
m_testsToEditList.repaint();
}
});
m_labelField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
Object test = m_testsToEditList.getSelectedValue();
if (test != null) {
CustomPeriodicTest t = (CustomPeriodicTest)test;
t.setLabel(m_labelField.getText());
m_testsToEditList.repaint();
}
}
});
/*m_labelField.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
Object test = m_testsToEditList.getSelectedValue();
if (test != null) {
CustomPeriodicTest t = (CustomPeriodicTest)test;
t.setLabel(m_labelField.getText());
m_testsToEditList.repaint();
}
}
});*/
}
/**
* Get the name for this custom periodic test (will be used as the field name)
*
* @return the name to use for this custom periodic test
*/
public String getFieldName() {
return m_fieldName.getText();
}
/**
* Set the name for this custom periodic test
*
* @param name the name for this custom periodic test
*/
public void setFieldName(String name) {
m_fieldName.setText(name);
}
/**
* Main method for testing this class.
*
* @param args no command line arguments are needed.
*/
public static void main(String[] args) {
try {
final List testList =
new java.util.ArrayList();
CustomPeriodicEditor ed = new CustomPeriodicEditor(testList);
final javax.swing.JFrame jf =
new javax.swing.JFrame("Periodic test editor");
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(ed, BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
for (CustomPeriodicTest t : testList) {
System.out.println(t.toString());
}
jf.dispose();
System.exit(0);
}
});
jf.pack();
jf.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}