~ubuntu-branches/ubuntu/trusty/weka/trusty-proposed

« back to all changes in this revision

Viewing changes to weka/gui/experiment/OutputFormatDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
23
package weka.gui.experiment;
 
24
 
 
25
import weka.experiment.ResultMatrix;
 
26
import weka.experiment.ResultMatrixPlainText;
 
27
import weka.gui.GenericObjectEditor;
 
28
 
 
29
import java.awt.BorderLayout;
 
30
import java.awt.FlowLayout;
 
31
import java.awt.Frame;
 
32
import java.awt.GridLayout;
 
33
import java.awt.event.ActionEvent;
 
34
import java.awt.event.ActionListener;
 
35
import java.util.Vector;
 
36
 
 
37
import javax.swing.JButton;
 
38
import javax.swing.JCheckBox;
 
39
import javax.swing.JComboBox;
 
40
import javax.swing.JDialog;
 
41
import javax.swing.JLabel;
 
42
import javax.swing.JPanel;
 
43
import javax.swing.JSpinner;
 
44
import javax.swing.SpinnerNumberModel;
 
45
 
 
46
/** 
 
47
 * A dialog for setting various output format parameters.
 
48
 *
 
49
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
50
 * @version $Revision: 1.8 $
 
51
 */
 
52
public class OutputFormatDialog
 
53
  extends JDialog {
 
54
 
 
55
  /** for serialization. */
 
56
  private static final long serialVersionUID = 2169792738187807378L;
 
57
 
 
58
  /** Signifies an OK property selection. */
 
59
  public static final int APPROVE_OPTION = 0;
 
60
 
 
61
  /** Signifies a cancelled property selection. */
 
62
  public static final int CANCEL_OPTION = 1;
 
63
 
 
64
  /** the result of the user's action, either OK or CANCEL. */
 
65
  protected int m_Result = CANCEL_OPTION;
 
66
  
 
67
  /** the different classes for outputting the comparison tables. */
 
68
  protected static Vector m_OutputFormatClasses = null;
 
69
  
 
70
  /** the different names of matrices for outputting the comparison tables. */
 
71
  protected static Vector m_OutputFormatNames = null;
 
72
  
 
73
  /** determine all classes inheriting from the ResultMatrix (in the same
 
74
   * package!)
 
75
   * @see ResultMatrix
 
76
   * @see ClassDiscovery */
 
77
  static {
 
78
    Vector classes = GenericObjectEditor.getClassnames(ResultMatrix.class.getName());
 
79
 
 
80
    // set names and classes
 
81
    m_OutputFormatClasses = new Vector();
 
82
    m_OutputFormatNames   = new Vector();
 
83
    for (int i = 0; i < classes.size(); i++) {
 
84
      try {
 
85
        Class cls = Class.forName(classes.get(i).toString());
 
86
        ResultMatrix matrix = (ResultMatrix) cls.newInstance();
 
87
        m_OutputFormatClasses.add(cls);
 
88
        m_OutputFormatNames.add(matrix.getDisplayName());
 
89
      }
 
90
      catch (Exception e) {
 
91
        e.printStackTrace();
 
92
      }
 
93
    }
 
94
  }
 
95
 
 
96
  /** the output format specific matrix. */
 
97
  protected Class m_ResultMatrix = ResultMatrixPlainText.class;
 
98
 
 
99
  /** lets the user choose the format for the output. */
 
100
  protected JComboBox m_OutputFormatComboBox = new JComboBox(m_OutputFormatNames);
 
101
 
 
102
  /** the spinner to choose the precision for the mean from. */
 
103
  protected JSpinner m_MeanPrecSpinner = new JSpinner();
 
104
 
 
105
  /** the spinner to choose the precision for the std. deviation from */
 
106
  protected JSpinner m_StdDevPrecSpinner = new JSpinner();
 
107
 
 
108
  /** the checkbox for outputting the average. */
 
109
  protected JCheckBox m_ShowAverageCheckBox = new JCheckBox("");
 
110
 
 
111
  /** the checkbox for the removing of filter classnames. */
 
112
  protected JCheckBox m_RemoveFilterNameCheckBox = new JCheckBox("");
 
113
  
 
114
  /** Click to activate the current set parameters. */
 
115
  protected JButton m_OkButton = new JButton("OK");
 
116
 
 
117
  /** Click to cancel the dialog. */
 
118
  protected JButton m_CancelButton = new JButton("Cancel");
 
119
  
 
120
  /** the number of digits after the period (= precision) for printing the mean. */
 
121
  protected int m_MeanPrec = 2;
 
122
  
 
123
  /** the number of digits after the period (= precision) for printing the std.
 
124
   * deviation */
 
125
  protected int m_StdDevPrec = 2;
 
126
 
 
127
  /** whether to remove the filter names from the names. */
 
128
  protected boolean m_RemoveFilterName = false;
 
129
 
 
130
  /** whether to show the average too. */
 
131
  protected boolean m_ShowAverage = false;
 
132
 
 
133
  /**
 
134
   * initializes the dialog with the given parent frame.
 
135
   * 
 
136
   * @param parent the parent of this dialog
 
137
   */
 
138
  public OutputFormatDialog(Frame parent) {
 
139
    super(parent, "Output Format...", true);
 
140
    createDialog();
 
141
  }
 
142
  
 
143
  /**
 
144
   * performs the creation of the dialog and all its components.
 
145
   */
 
146
  protected void createDialog() {
 
147
    JPanel              panel;
 
148
    SpinnerNumberModel  model;
 
149
    JLabel              label;
 
150
    
 
151
    getContentPane().setLayout(new BorderLayout());
 
152
    
 
153
    panel = new JPanel(new GridLayout(5, 2));
 
154
    getContentPane().add(panel, BorderLayout.CENTER);
 
155
    
 
156
    // Precision
 
157
    model = (SpinnerNumberModel) m_MeanPrecSpinner.getModel();
 
158
    model.setMaximum(new Integer(20));
 
159
    model.setMinimum(new Integer(0));
 
160
    model = (SpinnerNumberModel) m_StdDevPrecSpinner.getModel();
 
161
    model.setMaximum(new Integer(20));
 
162
    model.setMinimum(new Integer(0));
 
163
    label = new JLabel("Mean Precision");
 
164
    label.setDisplayedMnemonic('M');
 
165
    label.setLabelFor(m_MeanPrecSpinner);
 
166
    panel.add(label);
 
167
    panel.add(m_MeanPrecSpinner);
 
168
    label = new JLabel("StdDev. Precision");
 
169
    label.setDisplayedMnemonic('S');
 
170
    label.setLabelFor(m_StdDevPrecSpinner);
 
171
    panel.add(label);
 
172
    panel.add(m_StdDevPrecSpinner);
 
173
    
 
174
    // Format
 
175
    label = new JLabel("Output Format");
 
176
    label.setDisplayedMnemonic('F');
 
177
    label.setLabelFor(m_OutputFormatComboBox);
 
178
    panel.add(label);
 
179
    panel.add(m_OutputFormatComboBox);
 
180
    m_OutputFormatComboBox.addActionListener(new ActionListener() {
 
181
        public void actionPerformed(ActionEvent e) {
 
182
          getData();
 
183
        }
 
184
      });
 
185
 
 
186
    // Average
 
187
    label = new JLabel("Show Average");
 
188
    label.setDisplayedMnemonic('A');
 
189
    label.setLabelFor(m_ShowAverageCheckBox);
 
190
    panel.add(label);
 
191
    panel.add(m_ShowAverageCheckBox);
 
192
 
 
193
    // Remove filter classname
 
194
    label = new JLabel("Remove filter classnames");
 
195
    label.setDisplayedMnemonic('R');
 
196
    label.setLabelFor(m_RemoveFilterNameCheckBox);
 
197
    panel.add(label);
 
198
    panel.add(m_RemoveFilterNameCheckBox);
 
199
    
 
200
    // Buttons
 
201
    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 
202
    getContentPane().add(panel, BorderLayout.SOUTH);
 
203
    m_CancelButton.setMnemonic('C');
 
204
    m_CancelButton.addActionListener(new ActionListener() {
 
205
      public void actionPerformed(ActionEvent e) {
 
206
        m_Result = CANCEL_OPTION;
 
207
        setVisible(false);
 
208
      }
 
209
    });
 
210
    m_OkButton.setMnemonic('O');
 
211
    m_OkButton.addActionListener(new ActionListener() {
 
212
      public void actionPerformed(ActionEvent e) {
 
213
        getData();
 
214
        m_Result = APPROVE_OPTION;
 
215
        setVisible(false);
 
216
      }
 
217
    });
 
218
    panel.add(m_OkButton);
 
219
    panel.add(m_CancelButton);
 
220
 
 
221
    // default button
 
222
    getRootPane().setDefaultButton(m_OkButton);
 
223
 
 
224
    pack();
 
225
  }
 
226
  
 
227
  /**
 
228
   * initializes the GUI components with the data.
 
229
   */
 
230
  private void setData() {
 
231
    // Precision
 
232
    m_MeanPrecSpinner.setValue(new Integer(m_MeanPrec));
 
233
    m_StdDevPrecSpinner.setValue(new Integer(m_StdDevPrec));
 
234
 
 
235
    // average
 
236
    m_ShowAverageCheckBox.setSelected(m_ShowAverage);
 
237
 
 
238
    // filter names
 
239
    m_RemoveFilterNameCheckBox.setSelected(m_RemoveFilterName);
 
240
    
 
241
    // format (must be last, since getData() will be called!)
 
242
    for (int i = 0; i < m_OutputFormatClasses.size(); i++) {
 
243
      if (m_OutputFormatClasses.get(i).equals(m_ResultMatrix)) {
 
244
        m_OutputFormatComboBox.setSelectedItem(m_OutputFormatNames.get(i));
 
245
        break;
 
246
      }
 
247
    }
 
248
  }    
 
249
  
 
250
  /**
 
251
   *  gets the data from GUI components.
 
252
   */
 
253
  private void getData() {
 
254
    // Precision
 
255
    m_MeanPrec   = Integer.parseInt(m_MeanPrecSpinner.getValue().toString());
 
256
    m_StdDevPrec = Integer.parseInt(m_StdDevPrecSpinner.getValue().toString());
 
257
 
 
258
    // average
 
259
    m_ShowAverage = m_ShowAverageCheckBox.isSelected();
 
260
 
 
261
    // filter names
 
262
    m_RemoveFilterName = m_RemoveFilterNameCheckBox.isSelected();
 
263
    
 
264
    // format
 
265
    m_ResultMatrix = (Class) m_OutputFormatClasses.get(
 
266
                        m_OutputFormatComboBox.getSelectedIndex());
 
267
  }
 
268
  
 
269
  /**
 
270
   * Sets the precision of the mean output.
 
271
   * 
 
272
   * @param precision the number of digits used in printing the mean
 
273
   */
 
274
  public void setMeanPrec(int precision) {
 
275
    m_MeanPrec = precision;
 
276
  }
 
277
 
 
278
  /**
 
279
   * Gets the precision used for printing the mean.
 
280
   * 
 
281
   * @return the number of digits used in printing the mean
 
282
   */
 
283
  public int getMeanPrec() {
 
284
    return m_MeanPrec;
 
285
  }
 
286
 
 
287
  /**
 
288
   * Sets the precision of the std. deviation output.
 
289
   * 
 
290
   * @param precision the number of digits used in printing the std. deviation
 
291
   */
 
292
  public void setStdDevPrec(int precision) {
 
293
    m_StdDevPrec = precision;
 
294
  }
 
295
 
 
296
  /**
 
297
   * Gets the precision used for printing the std. deviation
 
298
   * @return the number of digits used in printing the std. deviation
 
299
   */
 
300
  public int getStdDevPrec() {
 
301
    return m_StdDevPrec;
 
302
  }
 
303
 
 
304
  /**
 
305
   * Sets the matrix to use as initial selected output format.
 
306
   * 
 
307
   * @param matrix the matrix to use as initial selected output format
 
308
   */
 
309
  public void setResultMatrix(Class matrix) {
 
310
    m_ResultMatrix = matrix;
 
311
  }
 
312
 
 
313
  /**
 
314
   * Gets the currently selected output format result matrix.
 
315
   * 
 
316
   * @return the currently selected matrix to use as output
 
317
   */
 
318
  public Class getResultMatrix() {
 
319
    return m_ResultMatrix;
 
320
  }
 
321
 
 
322
  /**
 
323
   * sets whether to remove the filter classname from the dataset name.
 
324
   * 
 
325
   * @param remove if true then the filter classname is removed
 
326
   */
 
327
  public void setRemoveFilterName(boolean remove) {
 
328
    m_RemoveFilterName = remove;
 
329
  }
 
330
 
 
331
  /**
 
332
   * returns whether the filter classname is removed from the dataset name.
 
333
   * 
 
334
   * @return true if the filter classname is removed
 
335
   */
 
336
  public boolean getRemoveFilterName() {
 
337
    return m_RemoveFilterName;
 
338
  }
 
339
 
 
340
  /**
 
341
   * sets whether the average for each column is displayed.
 
342
   * 
 
343
   * @param show if true then an additional row with the average is printed.
 
344
   */
 
345
  public void setShowAverage(boolean show) {
 
346
    m_ShowAverage = show;
 
347
  }
 
348
 
 
349
  /**
 
350
   * returns whether the average for each column is displayed.
 
351
   * 
 
352
   * @return true if an additional row with the average is displayed
 
353
   */
 
354
  public boolean getShowAverage() {
 
355
    return m_ShowAverage;
 
356
  }
 
357
 
 
358
  /**
 
359
   * sets the class of the chosen result matrix.
 
360
   */
 
361
  protected void setFormat() {
 
362
    for (int i = 0; i < m_OutputFormatClasses.size(); i++) {
 
363
      if (m_OutputFormatNames.get(i).toString().equals(
 
364
            m_OutputFormatComboBox.getItemAt(i).toString())) {
 
365
        m_OutputFormatComboBox.setSelectedIndex(i);
 
366
        break;
 
367
      }
 
368
    }
 
369
  }
 
370
  
 
371
  /**
 
372
   * the result from the last display of the dialog, the same is returned
 
373
   * from <code>showDialog</code>.
 
374
   * 
 
375
   * @return the result from the last display of the dialog; 
 
376
   *         either APPROVE_OPTION, or CANCEL_OPTION
 
377
   * @see #showDialog()
 
378
   */
 
379
  public int getResult() {
 
380
    return m_Result;
 
381
  }
 
382
 
 
383
  /**
 
384
   * Pops up the modal dialog and waits for cancel or a selection.
 
385
   *
 
386
   * @return either APPROVE_OPTION, or CANCEL_OPTION
 
387
   */
 
388
  public int showDialog() {
 
389
    m_Result = CANCEL_OPTION;
 
390
    setData();
 
391
    setVisible(true);
 
392
    return m_Result;
 
393
  }
 
394
 
 
395
  /**
 
396
   * for testing only.
 
397
   * 
 
398
   * @param args ignored
 
399
   */
 
400
  public static void main(String[] args) {
 
401
    OutputFormatDialog      dialog;
 
402
    
 
403
    dialog = new OutputFormatDialog(null);
 
404
    if (dialog.showDialog() == APPROVE_OPTION)
 
405
      System.out.println("Accepted");
 
406
    else
 
407
      System.out.println("Aborted");
 
408
  }
 
409
}