source: src/main/java/weka/gui/experiment/GeneratorPropertyIteratorPanel.java @ 13

Last change on this file since 13 was 4, checked in by gnappo, 14 years ago

Import di weka.

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