GeneratorPropertyIteratorPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 9k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    GeneratorPropertyIteratorPanel.java
  18.  *    Copyright (C) 1999 Len Trigg, Mark Hall
  19.  *
  20.  */
  21. package weka.gui.experiment;
  22. import weka.core.FastVector;
  23. import weka.gui.GenericArrayEditor;
  24. import weka.gui.PropertySelectorDialog;
  25. import weka.experiment.PropertyNode;
  26. import weka.experiment.Experiment;
  27. import weka.experiment.ResultProducer;
  28. import java.awt.Component;
  29. import java.awt.BorderLayout;
  30. import java.awt.GridLayout;
  31. import java.awt.event.WindowAdapter;
  32. import java.awt.event.WindowEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.GridBagLayout;
  35. import java.awt.GridBagConstraints;
  36. import java.awt.Insets;
  37. import javax.swing.JPanel;
  38. import javax.swing.JLabel;
  39. import javax.swing.JFrame;
  40. import javax.swing.SwingConstants;
  41. import javax.swing.JTextField;
  42. import javax.swing.BorderFactory;
  43. import javax.swing.DefaultListModel;
  44. import javax.swing.JScrollPane;
  45. import javax.swing.JButton;
  46. import java.awt.FlowLayout;
  47. import javax.swing.Box;
  48. import javax.swing.BoxLayout;
  49. import javax.swing.JComboBox;
  50. import javax.swing.ComboBoxModel;
  51. import javax.swing.DefaultComboBoxModel;
  52. import java.awt.event.ActionEvent;
  53. import java.awt.Dimension;
  54. import java.beans.PropertyDescriptor;
  55. import java.lang.reflect.Array;
  56. import java.beans.PropertyChangeListener;
  57. import java.beans.PropertyChangeEvent;
  58. /** 
  59.  * This panel controls setting a list of values for an arbitrary
  60.  * resultgenerator property for an experiment to iterate over.
  61.  *
  62.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  63.  * @version $Revision: 1.8 $
  64.  */
  65. public class GeneratorPropertyIteratorPanel extends JPanel
  66.   implements ActionListener {
  67.   /** Click to select the property to iterate over */
  68.   protected JButton m_ConfigureBut = new JButton("Select property...");
  69.   /** Controls whether the custom iterator is used or not */
  70.   protected JComboBox m_StatusBox = new JComboBox();
  71.   /** Allows editing of the custom property values */
  72.   protected GenericArrayEditor m_ArrayEditor = new GenericArrayEditor();
  73.   /** The experiment this all applies to */
  74.   protected Experiment m_Exp;
  75.   /** Listeners who want to be notified about editing status of this
  76.       panel */
  77.   protected FastVector m_Listeners = new FastVector();
  78.   
  79.   /**
  80.    * Creates the property iterator panel initially disabled.
  81.    */
  82.   public GeneratorPropertyIteratorPanel() {
  83.     String [] options = {"Disabled", "Enabled"};
  84.     ComboBoxModel cbm = new DefaultComboBoxModel(options);
  85.     m_StatusBox.setModel(cbm);
  86.     m_StatusBox.setSelectedIndex(0);
  87.     m_StatusBox.addActionListener(this);
  88.     m_StatusBox.setEnabled(false);
  89.     m_ConfigureBut.setEnabled(false);
  90.     m_ConfigureBut.addActionListener(this);
  91.     JPanel buttons = new JPanel();
  92.     GridBagLayout gb = new GridBagLayout();
  93.     GridBagConstraints constraints = new GridBagConstraints();
  94.     buttons.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  95.     //    buttons.setLayout(new GridLayout(1, 2));
  96.     buttons.setLayout(gb);
  97.     constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
  98.     constraints.fill = GridBagConstraints.HORIZONTAL;
  99.     constraints.gridwidth=1;constraints.gridheight=1;
  100.     constraints.insets = new Insets(0,2,0,2);
  101.     buttons.add(m_StatusBox,constraints);
  102.     constraints.gridx=1;constraints.gridy=0;constraints.weightx=5;
  103.     constraints.gridwidth=1;constraints.gridheight=1;
  104.     buttons.add(m_ConfigureBut,constraints);
  105.     buttons.setMaximumSize(new Dimension(buttons.getMaximumSize().width,
  106.    buttons.getMinimumSize().height));
  107.     setBorder(BorderFactory.createTitledBorder("Generator properties"));
  108.     setLayout(new BorderLayout());
  109.     add(buttons, BorderLayout.NORTH);
  110.     //    add(Box.createHorizontalGlue());
  111.     m_ArrayEditor.setBorder(BorderFactory.createEtchedBorder());
  112.     m_ArrayEditor.addPropertyChangeListener(new PropertyChangeListener() {
  113.       public void propertyChange(PropertyChangeEvent e) {
  114. System.err.println("Updating experiment property iterator array");
  115. m_Exp.setPropertyArray(m_ArrayEditor.getValue());
  116.       }
  117.     });
  118.     add(m_ArrayEditor, BorderLayout.CENTER);
  119.   }
  120.   /**
  121.    * Creates the property iterator panel and sets the experiment.
  122.    *
  123.    * @param exp a value of type 'Experiment'
  124.    */
  125.   public GeneratorPropertyIteratorPanel(Experiment exp) {
  126.     this();
  127.     setExperiment(exp);
  128.   }
  129.   /**
  130.    * Returns true if the editor is currently in an active status---that
  131.    * is the array is active and able to be edited.
  132.    * @return true if editor is active
  133.    */
  134.   public boolean getEditorActive() {
  135.     if (m_StatusBox.getSelectedIndex() == 0) {
  136.       return false;
  137.     }
  138.     return true;
  139.   }
  140.   /**
  141.    * Sets the experiment which will have the custom properties edited.
  142.    *
  143.    * @param exp a value of type 'Experiment'
  144.    */
  145.   public void setExperiment(Experiment exp) {
  146.     m_Exp = exp;
  147.     m_StatusBox.setEnabled(true);
  148.     m_ArrayEditor.setValue(m_Exp.getPropertyArray());
  149.     if (m_Exp.getPropertyArray() == null) {
  150.       m_StatusBox.setSelectedIndex(0);
  151.       m_ConfigureBut.setEnabled(false);
  152.     } else {
  153.       m_StatusBox.setSelectedIndex(m_Exp.getUsePropertyIterator() ? 1 : 0);
  154.       m_ConfigureBut.setEnabled(m_Exp.getUsePropertyIterator());
  155.     }
  156.     validate();
  157.   }
  158.   /**
  159.    * Gets the user to select a property of the current resultproducer.
  160.    *
  161.    * @return APPROVE_OPTION if the selection went OK, otherwise the selection
  162.    * was cancelled.
  163.    */
  164.   protected int selectProperty() {
  165.     
  166.     final PropertySelectorDialog jd = new PropertySelectorDialog(null,
  167.   m_Exp.getResultProducer());
  168.     jd.setLocationRelativeTo(this);
  169.     int result = jd.showDialog();
  170.     if (result == PropertySelectorDialog.APPROVE_OPTION) {
  171.       System.err.println("Property Selected");
  172.       PropertyNode [] path = jd.getPath();
  173.       Object value = path[path.length - 1].value;
  174.       PropertyDescriptor property = path[path.length - 1].property;
  175.       // Make an array containing the propertyValue
  176.       Class propertyClass = property.getPropertyType();
  177.       m_Exp.setPropertyPath(path);
  178.       m_Exp.setPropertyArray(Array.newInstance(propertyClass, 1));
  179.       Array.set(m_Exp.getPropertyArray(), 0, value);
  180.       // Pass it to the arrayeditor
  181.       m_ArrayEditor.setValue(m_Exp.getPropertyArray());
  182.       m_ArrayEditor.repaint();
  183.       System.err.println("Set new array to array editor");
  184.     } else {
  185.       System.err.println("Cancelled");
  186.     }
  187.     return result;
  188.   }
  189.   /**
  190.    * Handles the various button clicking type activities.
  191.    *
  192.    * @param e a value of type 'ActionEvent'
  193.    */
  194.   public void actionPerformed(ActionEvent e) {
  195.     if (e.getSource() == m_ConfigureBut) {
  196.       selectProperty();
  197.     } else if (e.getSource() == m_StatusBox) {
  198.       // notify any listeners
  199.       for (int i = 0; i < m_Listeners.size(); i++) {
  200. ActionListener temp = ((ActionListener)m_Listeners.elementAt(i));
  201. temp.actionPerformed(new ActionEvent(this, 
  202.      ActionEvent.ACTION_PERFORMED, 
  203.      "Editor status change"));
  204.       }
  205.       // Toggles whether the custom property is used
  206.       if (m_StatusBox.getSelectedIndex() == 0) {
  207. m_Exp.setUsePropertyIterator(false);
  208. m_ConfigureBut.setEnabled(false);
  209. m_ArrayEditor.setEnabled(false);
  210. m_ArrayEditor.setValue(null);
  211. validate();
  212.       } else {
  213. if (m_Exp.getPropertyArray() == null) {
  214.   selectProperty();
  215. }
  216. if (m_Exp.getPropertyArray() == null) {
  217.   m_StatusBox.setSelectedIndex(0);
  218. } else {
  219.   m_Exp.setUsePropertyIterator(true);
  220.   m_ConfigureBut.setEnabled(true);
  221.   m_ArrayEditor.setEnabled(true);
  222. }
  223. validate();
  224.       }
  225.     }
  226.   }
  227.   /**
  228.    * Add a listener interested in kowing about editor status changes
  229.    * @param newA an listener to add
  230.    */
  231.   public void addActionListener(ActionListener newA) {
  232.     m_Listeners.addElement(newA);
  233.   }
  234.   /**
  235.    * Tests out the panel from the command line.
  236.    *
  237.    * @param args ignored.
  238.    */
  239.   public static void main(String [] args) {
  240.     try {
  241.       final JFrame jf = new JFrame("Generator Property Iterator");
  242.       jf.getContentPane().setLayout(new BorderLayout());
  243.       GeneratorPropertyIteratorPanel gp = new GeneratorPropertyIteratorPanel();
  244.       jf.getContentPane().add(gp, BorderLayout.CENTER);
  245.       jf.addWindowListener(new WindowAdapter() {
  246. public void windowClosing(WindowEvent e) {
  247.   jf.dispose();
  248.   System.exit(0);
  249. }
  250.       });
  251.       jf.pack();
  252.       jf.setVisible(true);
  253.       System.err.println("Short nap");
  254.       Thread.currentThread().sleep(3000);
  255.       System.err.println("Done");
  256.       gp.setExperiment(new Experiment());
  257.     } catch (Exception ex) {
  258.       ex.printStackTrace();
  259.       System.err.println(ex.getMessage());
  260.     }
  261.   }
  262. }