source: src/main/java/weka/gui/experiment/RunNumberPanel.java @ 25

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

Import di weka.

File size: 5.1 KB
RevLine 
[4]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 *    RunNumberPanel.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.gui.experiment;
25
26import weka.experiment.Experiment;
27
28import java.awt.BorderLayout;
29import java.awt.GridLayout;
30import java.awt.event.FocusAdapter;
31import java.awt.event.FocusEvent;
32import java.awt.event.KeyAdapter;
33import java.awt.event.KeyEvent;
34import java.awt.event.WindowAdapter;
35import java.awt.event.WindowEvent;
36
37import javax.swing.BorderFactory;
38import javax.swing.Box;
39import javax.swing.BoxLayout;
40import javax.swing.JFrame;
41import javax.swing.JLabel;
42import javax.swing.JPanel;
43import javax.swing.JTextField;
44import javax.swing.SwingConstants;
45
46
47
48/**
49 * This panel controls configuration of lower and upper run numbers
50 * in an experiment.
51 *
52 * @author Len Trigg (trigg@cs.waikato.ac.nz)
53 * @version $Revision: 1.6 $
54 */
55public class RunNumberPanel
56  extends JPanel {
57
58  /** for serialization */
59  private static final long serialVersionUID = -1644336658426067852L;
60
61  /** Configures the lower run number */
62  protected JTextField m_LowerText = new JTextField("1");
63
64  /** Configures the upper run number */
65  protected JTextField m_UpperText = new JTextField("10");
66
67  /** The experiment being configured */
68  protected Experiment m_Exp;
69 
70  /**
71   * Creates the panel with no initial experiment.
72   */
73  public RunNumberPanel() {
74   
75    // Updates occur to the values in exp whenever enter is pressed
76    // or the component loses focus
77    m_LowerText.addKeyListener(new KeyAdapter() {
78      public void keyReleased(KeyEvent e) {
79        m_Exp.setRunLower(getLower());
80      }
81    });
82    m_LowerText.addFocusListener(new FocusAdapter() {
83      public void focusLost(FocusEvent e) {
84        m_Exp.setRunLower(getLower());
85      }
86    });
87    m_UpperText.addKeyListener(new KeyAdapter() {
88      public void keyReleased(KeyEvent e) {
89        m_Exp.setRunUpper(getUpper());
90      }
91    });
92    m_UpperText.addFocusListener(new FocusAdapter() {
93      public void focusLost(FocusEvent e) {
94        m_Exp.setRunUpper(getUpper());
95      }
96    });
97    m_LowerText.setEnabled(false);
98    m_UpperText.setEnabled(false);
99
100    // Set the GUI layout
101    setLayout(new GridLayout(1,2));
102    setBorder(BorderFactory.createTitledBorder("Runs"));
103    Box b1 = new Box(BoxLayout.X_AXIS);
104    b1.add(Box.createHorizontalStrut(10));
105    b1.add(new JLabel("From:", SwingConstants.RIGHT));
106    b1.add(Box.createHorizontalStrut(5));
107    b1.add(m_LowerText);
108    add(b1);
109    Box b2 = new Box(BoxLayout.X_AXIS);
110    b2.add(Box.createHorizontalStrut(10));
111    b2.add(new JLabel("To:", SwingConstants.RIGHT));
112    b2.add(Box.createHorizontalStrut(5));
113    b2.add(m_UpperText);
114    add(b2);
115  }
116
117  /**
118   * Creates the panel with the supplied initial experiment.
119   *
120   * @param exp a value of type 'Experiment'
121   */
122  public RunNumberPanel(Experiment exp) {
123
124    this();
125    setExperiment(exp);
126  }
127
128  /**
129   * Sets the experiment to be configured.
130   *
131   * @param exp a value of type 'Experiment'
132   */
133  public void setExperiment(Experiment exp) {
134   
135    m_Exp = exp;
136    m_LowerText.setText("" + m_Exp.getRunLower());
137    m_UpperText.setText("" + m_Exp.getRunUpper());
138    m_LowerText.setEnabled(true);
139    m_UpperText.setEnabled(true);
140  }
141 
142  /**
143   * Gets the current lower run number.
144   *
145   * @return the lower run number.
146   */
147  public int getLower() {
148
149    int result = 1;
150    try {
151      result = Integer.parseInt(m_LowerText.getText());
152    } catch (Exception ex) {
153    }
154    return Math.max(1, result);
155  }
156 
157  /**
158   * Gets the current upper run number.
159   *
160   * @return the upper run number.
161   */
162  public int getUpper() {
163
164    int result = 1;
165    try {
166      result = Integer.parseInt(m_UpperText.getText());
167    } catch (Exception ex) {
168    }
169    return Math.max(1, result);
170  }
171 
172  /**
173   * Tests out the panel from the command line.
174   *
175   * @param args ignored.
176   */
177  public static void main(String [] args) {
178
179    try {
180      final JFrame jf = new JFrame("Dataset List Editor");
181      jf.getContentPane().setLayout(new BorderLayout());
182      jf.getContentPane().add(new RunNumberPanel(new Experiment()),
183                              BorderLayout.CENTER);
184      jf.addWindowListener(new WindowAdapter() {
185        public void windowClosing(WindowEvent e) {
186          jf.dispose();
187          System.exit(0);
188        }
189      });
190      jf.pack();
191      jf.setVisible(true);
192    } catch (Exception ex) {
193      ex.printStackTrace();
194      System.err.println(ex.getMessage());
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.