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

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

Import di weka.

File size: 15.5 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 *    OutputFormatDialog.java
19 *    Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.experiment;
24
25import weka.experiment.ResultMatrix;
26import weka.experiment.ResultMatrixPlainText;
27import weka.gui.GenericObjectEditor;
28import weka.gui.PropertyPanel;
29
30import java.awt.BorderLayout;
31import java.awt.Dimension;
32import java.awt.FlowLayout;
33import java.awt.Frame;
34import java.awt.GridLayout;
35import java.awt.event.ActionEvent;
36import java.awt.event.ActionListener;
37import java.beans.PropertyChangeEvent;
38import java.beans.PropertyChangeListener;
39import java.util.Vector;
40
41import javax.swing.BorderFactory;
42import javax.swing.JButton;
43import javax.swing.JCheckBox;
44import javax.swing.JComboBox;
45import javax.swing.JDialog;
46import javax.swing.JLabel;
47import javax.swing.JPanel;
48import javax.swing.JSpinner;
49import javax.swing.SpinnerNumberModel;
50import javax.swing.event.ChangeEvent;
51import javax.swing.event.ChangeListener;
52
53/**
54 * A dialog for setting various output format parameters.
55 *
56 * @author FracPete (fracpete at waikato dot ac dot nz)
57 * @version $Revision: 5346 $
58 */
59public class OutputFormatDialog
60  extends JDialog {
61
62  /** for serialization. */
63  private static final long serialVersionUID = 2169792738187807378L;
64
65  /** Signifies an OK property selection. */
66  public static final int APPROVE_OPTION = 0;
67
68  /** Signifies a cancelled property selection. */
69  public static final int CANCEL_OPTION = 1;
70
71  /** the result of the user's action, either OK or CANCEL. */
72  protected int m_Result;
73 
74  /** the different classes for outputting the comparison tables. */
75  protected static Vector<Class> m_OutputFormatClasses;
76 
77  /** the different names of matrices for outputting the comparison tables. */
78  protected static Vector<String> m_OutputFormatNames;
79 
80  /** Lets the user configure the result matrix. */
81  protected GenericObjectEditor m_ResultMatrixEditor;
82
83  /** the panel for the GOE. */
84  protected PropertyPanel m_ResultMatrixPanel;
85
86  /** the label for the GOE. */
87  protected JLabel m_ResultMatrixLabel;
88 
89  /** the current result matrix. */
90  protected ResultMatrix m_ResultMatrix;
91
92  /** lets the user choose the format for the output. */
93  protected JComboBox m_OutputFormatComboBox;
94
95  /** the label for the format. */
96  protected JLabel m_OutputFormatLabel;
97
98  /** the spinner to choose the precision for the mean from. */
99  protected JSpinner m_MeanPrecSpinner;
100
101  /** the label for the mean precision. */
102  protected JLabel m_MeanPrecLabel;
103
104  /** the spinner to choose the precision for the std. deviation from */
105  protected JSpinner m_StdDevPrecSpinner;
106
107  /** the label for the std dev precision. */
108  protected JLabel m_StdDevPrecLabel;
109
110  /** the checkbox for outputting the average. */
111  protected JCheckBox m_ShowAverageCheckBox;
112
113  /** the label for showing the average. */
114  protected JLabel m_ShowAverageLabel;
115
116  /** the checkbox for the removing of filter classnames. */
117  protected JCheckBox m_RemoveFilterNameCheckBox;
118
119  /** the label for the removing the filter classnames. */
120  protected JLabel m_RemoveFilterNameLabel;
121 
122  /** Click to activate the current set parameters. */
123  protected JButton m_OkButton;
124
125  /** Click to cancel the dialog. */
126  protected JButton m_CancelButton;
127
128  /** whether to ignore updates in the GUI momentarily. */
129  protected boolean m_IgnoreChanges;
130 
131  /**
132   * initializes the dialog with the given parent frame.
133   *
134   * @param parent the parent of this dialog
135   */
136  public OutputFormatDialog(Frame parent) {
137    super(parent, "Output Format...", true);
138
139    m_IgnoreChanges = true;
140   
141    initialize();
142    initGUI();
143
144    m_IgnoreChanges = false;
145  }
146 
147  /**
148   * initializes the member variables.
149   */
150  protected void initialize() {
151    Vector              classes;
152    int                 i;
153    Class               cls;
154    ResultMatrix        matrix;
155   
156    m_Result = CANCEL_OPTION;
157
158    if (m_OutputFormatClasses == null) {
159      classes = GenericObjectEditor.getClassnames(ResultMatrix.class.getName());
160
161      // set names and classes
162      m_OutputFormatClasses = new Vector<Class>();
163      m_OutputFormatNames   = new Vector<String>();
164      for (i = 0; i < classes.size(); i++) {
165        try {
166          cls    = Class.forName(classes.get(i).toString());
167          matrix = (ResultMatrix) cls.newInstance();
168          m_OutputFormatClasses.add(cls);
169          m_OutputFormatNames.add(matrix.getDisplayName());
170        }
171        catch (Exception e) {
172          e.printStackTrace();
173        }
174      }
175    }
176  }
177 
178  /**
179   * performs the creation of the dialog and all its components.
180   */
181  protected void initGUI() {
182    JPanel              panel;
183    SpinnerNumberModel  model;
184    JPanel              panel2;
185   
186    getContentPane().setLayout(new BorderLayout());
187   
188    panel = new JPanel(new GridLayout(6, 1));
189    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
190    getContentPane().add(panel, BorderLayout.CENTER);
191   
192    // mean precision
193    m_MeanPrecSpinner = new JSpinner();
194    m_MeanPrecSpinner.addChangeListener(new ChangeListener() {
195      public void stateChanged(ChangeEvent e) {
196        getData();
197      }
198    });
199    model = (SpinnerNumberModel) m_MeanPrecSpinner.getModel();
200    model.setMaximum(new Integer(20));
201    model.setMinimum(new Integer(0));
202    m_MeanPrecLabel = new JLabel("Mean Precision");
203    m_MeanPrecLabel.setDisplayedMnemonic('M');
204    m_MeanPrecLabel.setLabelFor(m_MeanPrecSpinner);
205    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
206    panel2.add(m_MeanPrecLabel);
207    panel2.add(m_MeanPrecSpinner);
208    panel.add(panel2);
209   
210    // stddev precision
211    m_StdDevPrecSpinner = new JSpinner();
212    m_StdDevPrecSpinner.addChangeListener(new ChangeListener() {
213      public void stateChanged(ChangeEvent e) {
214        getData();
215      }
216    });
217    model = (SpinnerNumberModel) m_StdDevPrecSpinner.getModel();
218    model.setMaximum(new Integer(20));
219    model.setMinimum(new Integer(0));
220    m_StdDevPrecLabel = new JLabel("StdDev. Precision");
221    m_StdDevPrecLabel.setDisplayedMnemonic('S');
222    m_StdDevPrecLabel.setLabelFor(m_StdDevPrecSpinner);
223    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
224    panel2.add(m_StdDevPrecLabel);
225    panel2.add(m_StdDevPrecSpinner);
226    panel.add(panel2);
227   
228    // Format
229    m_OutputFormatComboBox = new JComboBox(m_OutputFormatNames);
230    m_OutputFormatComboBox.addActionListener(new ActionListener() {
231      public void actionPerformed(ActionEvent e) {
232        getData();
233      }
234    });
235    m_OutputFormatLabel = new JLabel("Output Format");
236    m_OutputFormatLabel.setDisplayedMnemonic('F');
237    m_OutputFormatLabel.setLabelFor(m_OutputFormatComboBox);
238    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
239    panel2.add(m_OutputFormatLabel);
240    panel2.add(m_OutputFormatComboBox);
241    panel.add(panel2);
242
243    // Average
244    m_ShowAverageCheckBox = new JCheckBox("");
245    m_ShowAverageCheckBox.addChangeListener(new ChangeListener() {
246      public void stateChanged(ChangeEvent e) {
247        getData();
248      }
249    });
250    m_ShowAverageLabel = new JLabel("Show Average");
251    m_ShowAverageLabel.setDisplayedMnemonic('A');
252    m_ShowAverageLabel.setLabelFor(m_ShowAverageCheckBox);
253    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
254    panel2.add(m_ShowAverageLabel);
255    panel2.add(m_ShowAverageCheckBox);
256    panel.add(panel2);
257
258    // Remove filter classname
259    m_RemoveFilterNameCheckBox = new JCheckBox("");
260    m_RemoveFilterNameCheckBox.addChangeListener(new ChangeListener() {
261      public void stateChanged(ChangeEvent e) {
262        getData();
263      }
264    });
265    m_RemoveFilterNameLabel = new JLabel("Remove filter classnames");
266    m_RemoveFilterNameLabel.setDisplayedMnemonic('R');
267    m_RemoveFilterNameLabel.setLabelFor(m_RemoveFilterNameCheckBox);
268    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
269    panel2.add(m_RemoveFilterNameLabel);
270    panel2.add(m_RemoveFilterNameCheckBox);
271    panel.add(panel2);
272
273    // Advanced setup
274    m_ResultMatrix       = ExperimenterDefaults.getOutputFormat();
275    m_ResultMatrixEditor = new GenericObjectEditor(true);
276    m_ResultMatrixEditor.setClassType(ResultMatrix.class);
277    m_ResultMatrixEditor.setValue(m_ResultMatrix);
278    m_ResultMatrixEditor.addPropertyChangeListener(new PropertyChangeListener() {
279        public void propertyChange(PropertyChangeEvent e) {
280          // user selected different class?
281          if (!m_ResultMatrix.getClass().equals(m_ResultMatrixEditor.getValue().getClass())) {
282            // if it's the preferred class, then automaticallly use the Experimenter defaults
283            if (m_ResultMatrixEditor.getValue().getClass().equals(ExperimenterDefaults.getOutputFormat().getClass())) {
284              m_ResultMatrix = ExperimenterDefaults.getOutputFormat();
285              m_ResultMatrixEditor.setValue(ExperimenterDefaults.getOutputFormat());
286            }
287            else {
288              m_ResultMatrix = (ResultMatrix) m_ResultMatrixEditor.getValue();
289            }
290            setData();
291          }
292          repaint();
293        }
294      });
295    ((GenericObjectEditor.GOEPanel) m_ResultMatrixEditor.getCustomEditor()).addOkListener(new ActionListener() {
296        public void actionPerformed(ActionEvent e) {
297          m_ResultMatrix = (ResultMatrix) m_ResultMatrixEditor.getValue();
298          setData();
299        }
300      });
301    m_ResultMatrixPanel = new PropertyPanel(m_ResultMatrixEditor, false);
302    m_ResultMatrixLabel = new JLabel("Advanced setup");
303    panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
304    panel2.add(m_ResultMatrixLabel);
305    panel2.add(m_ResultMatrixPanel);
306    panel.add(panel2);
307   
308    // Buttons
309    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
310    getContentPane().add(panel, BorderLayout.SOUTH);
311    m_CancelButton = new JButton("Cancel");
312    m_CancelButton.setMnemonic('C');
313    m_CancelButton.addActionListener(new ActionListener() {
314      public void actionPerformed(ActionEvent e) {
315        m_Result = CANCEL_OPTION;
316        setVisible(false);
317      }
318    });
319    m_OkButton = new JButton("OK");
320    m_OkButton.setMnemonic('O');
321    m_OkButton.addActionListener(new ActionListener() {
322      public void actionPerformed(ActionEvent e) {
323        getData();
324        m_Result = APPROVE_OPTION;
325        setVisible(false);
326      }
327    });
328    panel.add(m_OkButton);
329    panel.add(m_CancelButton);
330
331    // default button
332    getRootPane().setDefaultButton(m_OkButton);
333   
334    // initial layout (to get widths and heights)
335    pack();
336   
337    // adjust dimensions
338    m_MeanPrecLabel.setPreferredSize(new Dimension(m_RemoveFilterNameLabel.getWidth(), m_MeanPrecLabel.getHeight()));
339    m_MeanPrecSpinner.setPreferredSize(new Dimension(m_MeanPrecSpinner.getWidth() * 3, m_MeanPrecSpinner.getHeight()));
340    m_StdDevPrecLabel.setPreferredSize(new Dimension(m_RemoveFilterNameLabel.getWidth(), m_StdDevPrecLabel.getHeight()));
341    m_StdDevPrecSpinner.setPreferredSize(new Dimension(m_StdDevPrecSpinner.getWidth() * 3, m_StdDevPrecSpinner.getHeight()));
342    m_OutputFormatLabel.setPreferredSize(new Dimension(m_RemoveFilterNameLabel.getWidth(), m_OutputFormatLabel.getHeight()));
343    m_ShowAverageLabel.setPreferredSize(new Dimension(m_RemoveFilterNameLabel.getWidth(), m_ShowAverageLabel.getHeight()));
344    m_ResultMatrixLabel.setPreferredSize(new Dimension(m_RemoveFilterNameLabel.getWidth(), m_ResultMatrixLabel.getHeight()));
345    m_ResultMatrixPanel.setPreferredSize(new Dimension((int) (m_ResultMatrixPanel.getWidth() * 1.5), m_ResultMatrixPanel.getHeight()));
346   
347    // final layout
348    pack();
349  }
350 
351  /**
352   * initializes the GUI components with the data.
353   */
354  private void setData() {
355    m_IgnoreChanges = true;
356   
357    // Precision
358    m_MeanPrecSpinner.setValue(m_ResultMatrix.getMeanPrec());
359    m_StdDevPrecSpinner.setValue(m_ResultMatrix.getStdDevPrec());
360   
361    // format
362    for (int i = 0; i < m_OutputFormatClasses.size(); i++) {
363      if (m_OutputFormatClasses.get(i).equals(m_ResultMatrix.getClass())) {
364        m_OutputFormatComboBox.setSelectedItem(m_OutputFormatNames.get(i));
365        break;
366      }
367    }
368
369    // average
370    m_ShowAverageCheckBox.setSelected(m_ResultMatrix.getShowAverage());
371
372    // filter names
373    m_RemoveFilterNameCheckBox.setSelected(m_ResultMatrix.getRemoveFilterName());
374
375    // GOE
376    m_ResultMatrixEditor.setValue(m_ResultMatrix);
377   
378    m_IgnoreChanges = false;
379  }   
380 
381  /**
382   *  gets the data from GUI components.
383   */
384  private void getData() {
385    if (m_IgnoreChanges)
386      return;
387   
388    // format
389    try {
390      if (!m_ResultMatrix.getClass().equals(m_OutputFormatClasses.get(m_OutputFormatComboBox.getSelectedIndex()))) {
391        if (m_OutputFormatClasses.get(m_OutputFormatComboBox.getSelectedIndex()).equals(ExperimenterDefaults.getOutputFormat().getClass()))
392          m_ResultMatrix = ExperimenterDefaults.getOutputFormat();
393        else
394          m_ResultMatrix = (ResultMatrix) ((Class) m_OutputFormatClasses.get(m_OutputFormatComboBox.getSelectedIndex())).newInstance();
395      }
396    }
397    catch (Exception e) {
398      e.printStackTrace();
399      m_ResultMatrix = new ResultMatrixPlainText();
400    }
401   
402    // Precision
403    m_ResultMatrix.setMeanPrec(Integer.parseInt(m_MeanPrecSpinner.getValue().toString()));
404    m_ResultMatrix.setStdDevPrec(Integer.parseInt(m_StdDevPrecSpinner.getValue().toString()));
405
406    // average
407    m_ResultMatrix.setShowAverage(m_ShowAverageCheckBox.isSelected());
408
409    // filter names
410    m_ResultMatrix.setRemoveFilterName(m_RemoveFilterNameCheckBox.isSelected());
411   
412    // update GOE
413    m_ResultMatrixEditor.setValue(m_ResultMatrix);
414  }
415
416  /**
417   * Sets the matrix to use as initial selected output format.
418   *
419   * @param matrix the matrix to use as initial selected output format
420   */
421  public void setResultMatrix(ResultMatrix matrix) {
422    m_ResultMatrix = matrix;
423    setData();
424  }
425
426  /**
427   * Gets the currently selected output format result matrix.
428   *
429   * @return the currently selected matrix to use as output
430   */
431  public ResultMatrix getResultMatrix() {
432    return m_ResultMatrix;
433  }
434
435  /**
436   * sets the class of the chosen result matrix.
437   */
438  protected void setFormat() {
439    for (int i = 0; i < m_OutputFormatClasses.size(); i++) {
440      if (m_OutputFormatNames.get(i).equals(
441            m_OutputFormatComboBox.getItemAt(i).toString())) {
442        m_OutputFormatComboBox.setSelectedIndex(i);
443        break;
444      }
445    }
446  }
447 
448  /**
449   * the result from the last display of the dialog, the same is returned
450   * from <code>showDialog</code>.
451   *
452   * @return the result from the last display of the dialog;
453   *         either APPROVE_OPTION, or CANCEL_OPTION
454   * @see #showDialog()
455   */
456  public int getResult() {
457    return m_Result;
458  }
459
460  /**
461   * Pops up the modal dialog and waits for cancel or a selection.
462   *
463   * @return either APPROVE_OPTION, or CANCEL_OPTION
464   */
465  public int showDialog() {
466    m_Result = CANCEL_OPTION;
467    setData();
468    setVisible(true);
469    return m_Result;
470  }
471
472  /**
473   * for testing only.
474   *
475   * @param args ignored
476   */
477  public static void main(String[] args) {
478    OutputFormatDialog      dialog;
479   
480    dialog = new OutputFormatDialog(null);
481    if (dialog.showDialog() == APPROVE_OPTION)
482      System.out.println("Accepted");
483    else
484      System.out.println("Aborted");
485  }
486}
Note: See TracBrowser for help on using the repository browser.