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.
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.
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.
18
* OutputFormatDialog.java
19
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
23
package weka.gui.experiment;
25
import weka.experiment.ResultMatrix;
26
import weka.experiment.ResultMatrixPlainText;
27
import weka.gui.GenericObjectEditor;
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;
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;
47
* A dialog for setting various output format parameters.
49
* @author FracPete (fracpete at waikato dot ac dot nz)
50
* @version $Revision: 1.8 $
52
public class OutputFormatDialog
55
/** for serialization. */
56
private static final long serialVersionUID = 2169792738187807378L;
58
/** Signifies an OK property selection. */
59
public static final int APPROVE_OPTION = 0;
61
/** Signifies a cancelled property selection. */
62
public static final int CANCEL_OPTION = 1;
64
/** the result of the user's action, either OK or CANCEL. */
65
protected int m_Result = CANCEL_OPTION;
67
/** the different classes for outputting the comparison tables. */
68
protected static Vector m_OutputFormatClasses = null;
70
/** the different names of matrices for outputting the comparison tables. */
71
protected static Vector m_OutputFormatNames = null;
73
/** determine all classes inheriting from the ResultMatrix (in the same
76
* @see ClassDiscovery */
78
Vector classes = GenericObjectEditor.getClassnames(ResultMatrix.class.getName());
80
// set names and classes
81
m_OutputFormatClasses = new Vector();
82
m_OutputFormatNames = new Vector();
83
for (int i = 0; i < classes.size(); i++) {
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());
96
/** the output format specific matrix. */
97
protected Class m_ResultMatrix = ResultMatrixPlainText.class;
99
/** lets the user choose the format for the output. */
100
protected JComboBox m_OutputFormatComboBox = new JComboBox(m_OutputFormatNames);
102
/** the spinner to choose the precision for the mean from. */
103
protected JSpinner m_MeanPrecSpinner = new JSpinner();
105
/** the spinner to choose the precision for the std. deviation from */
106
protected JSpinner m_StdDevPrecSpinner = new JSpinner();
108
/** the checkbox for outputting the average. */
109
protected JCheckBox m_ShowAverageCheckBox = new JCheckBox("");
111
/** the checkbox for the removing of filter classnames. */
112
protected JCheckBox m_RemoveFilterNameCheckBox = new JCheckBox("");
114
/** Click to activate the current set parameters. */
115
protected JButton m_OkButton = new JButton("OK");
117
/** Click to cancel the dialog. */
118
protected JButton m_CancelButton = new JButton("Cancel");
120
/** the number of digits after the period (= precision) for printing the mean. */
121
protected int m_MeanPrec = 2;
123
/** the number of digits after the period (= precision) for printing the std.
125
protected int m_StdDevPrec = 2;
127
/** whether to remove the filter names from the names. */
128
protected boolean m_RemoveFilterName = false;
130
/** whether to show the average too. */
131
protected boolean m_ShowAverage = false;
134
* initializes the dialog with the given parent frame.
136
* @param parent the parent of this dialog
138
public OutputFormatDialog(Frame parent) {
139
super(parent, "Output Format...", true);
144
* performs the creation of the dialog and all its components.
146
protected void createDialog() {
148
SpinnerNumberModel model;
151
getContentPane().setLayout(new BorderLayout());
153
panel = new JPanel(new GridLayout(5, 2));
154
getContentPane().add(panel, BorderLayout.CENTER);
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);
167
panel.add(m_MeanPrecSpinner);
168
label = new JLabel("StdDev. Precision");
169
label.setDisplayedMnemonic('S');
170
label.setLabelFor(m_StdDevPrecSpinner);
172
panel.add(m_StdDevPrecSpinner);
175
label = new JLabel("Output Format");
176
label.setDisplayedMnemonic('F');
177
label.setLabelFor(m_OutputFormatComboBox);
179
panel.add(m_OutputFormatComboBox);
180
m_OutputFormatComboBox.addActionListener(new ActionListener() {
181
public void actionPerformed(ActionEvent e) {
187
label = new JLabel("Show Average");
188
label.setDisplayedMnemonic('A');
189
label.setLabelFor(m_ShowAverageCheckBox);
191
panel.add(m_ShowAverageCheckBox);
193
// Remove filter classname
194
label = new JLabel("Remove filter classnames");
195
label.setDisplayedMnemonic('R');
196
label.setLabelFor(m_RemoveFilterNameCheckBox);
198
panel.add(m_RemoveFilterNameCheckBox);
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;
210
m_OkButton.setMnemonic('O');
211
m_OkButton.addActionListener(new ActionListener() {
212
public void actionPerformed(ActionEvent e) {
214
m_Result = APPROVE_OPTION;
218
panel.add(m_OkButton);
219
panel.add(m_CancelButton);
222
getRootPane().setDefaultButton(m_OkButton);
228
* initializes the GUI components with the data.
230
private void setData() {
232
m_MeanPrecSpinner.setValue(new Integer(m_MeanPrec));
233
m_StdDevPrecSpinner.setValue(new Integer(m_StdDevPrec));
236
m_ShowAverageCheckBox.setSelected(m_ShowAverage);
239
m_RemoveFilterNameCheckBox.setSelected(m_RemoveFilterName);
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));
251
* gets the data from GUI components.
253
private void getData() {
255
m_MeanPrec = Integer.parseInt(m_MeanPrecSpinner.getValue().toString());
256
m_StdDevPrec = Integer.parseInt(m_StdDevPrecSpinner.getValue().toString());
259
m_ShowAverage = m_ShowAverageCheckBox.isSelected();
262
m_RemoveFilterName = m_RemoveFilterNameCheckBox.isSelected();
265
m_ResultMatrix = (Class) m_OutputFormatClasses.get(
266
m_OutputFormatComboBox.getSelectedIndex());
270
* Sets the precision of the mean output.
272
* @param precision the number of digits used in printing the mean
274
public void setMeanPrec(int precision) {
275
m_MeanPrec = precision;
279
* Gets the precision used for printing the mean.
281
* @return the number of digits used in printing the mean
283
public int getMeanPrec() {
288
* Sets the precision of the std. deviation output.
290
* @param precision the number of digits used in printing the std. deviation
292
public void setStdDevPrec(int precision) {
293
m_StdDevPrec = precision;
297
* Gets the precision used for printing the std. deviation
298
* @return the number of digits used in printing the std. deviation
300
public int getStdDevPrec() {
305
* Sets the matrix to use as initial selected output format.
307
* @param matrix the matrix to use as initial selected output format
309
public void setResultMatrix(Class matrix) {
310
m_ResultMatrix = matrix;
314
* Gets the currently selected output format result matrix.
316
* @return the currently selected matrix to use as output
318
public Class getResultMatrix() {
319
return m_ResultMatrix;
323
* sets whether to remove the filter classname from the dataset name.
325
* @param remove if true then the filter classname is removed
327
public void setRemoveFilterName(boolean remove) {
328
m_RemoveFilterName = remove;
332
* returns whether the filter classname is removed from the dataset name.
334
* @return true if the filter classname is removed
336
public boolean getRemoveFilterName() {
337
return m_RemoveFilterName;
341
* sets whether the average for each column is displayed.
343
* @param show if true then an additional row with the average is printed.
345
public void setShowAverage(boolean show) {
346
m_ShowAverage = show;
350
* returns whether the average for each column is displayed.
352
* @return true if an additional row with the average is displayed
354
public boolean getShowAverage() {
355
return m_ShowAverage;
359
* sets the class of the chosen result matrix.
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);
372
* the result from the last display of the dialog, the same is returned
373
* from <code>showDialog</code>.
375
* @return the result from the last display of the dialog;
376
* either APPROVE_OPTION, or CANCEL_OPTION
379
public int getResult() {
384
* Pops up the modal dialog and waits for cancel or a selection.
386
* @return either APPROVE_OPTION, or CANCEL_OPTION
388
public int showDialog() {
389
m_Result = CANCEL_OPTION;
398
* @param args ignored
400
public static void main(String[] args) {
401
OutputFormatDialog dialog;
403
dialog = new OutputFormatDialog(null);
404
if (dialog.showDialog() == APPROVE_OPTION)
405
System.out.println("Accepted");
407
System.out.println("Aborted");