source: src/main/java/weka/gui/beans/ClassifierCustomizer.java @ 15

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

Import di weka.

File size: 8.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 *    ClassifierCustomizer.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.classifiers.Classifier;
26import weka.classifiers.AbstractClassifier;
27import weka.gui.GenericObjectEditor;
28import weka.gui.PropertySheetPanel;
29
30import java.awt.BorderLayout;
31import java.awt.GridLayout;
32import java.awt.event.ActionEvent;
33import java.awt.event.ActionListener;
34import java.awt.event.FocusEvent;
35import java.awt.event.FocusListener;
36import java.beans.Customizer;
37import java.beans.PropertyChangeListener;
38import java.beans.PropertyChangeSupport;
39
40import javax.swing.BorderFactory;
41import javax.swing.JButton;
42import javax.swing.JCheckBox;
43import javax.swing.JFrame;
44import javax.swing.JLabel;
45import javax.swing.JPanel;
46import javax.swing.JTextField;
47import javax.swing.SwingConstants;
48
49/**
50 * GUI customizer for the classifier wrapper bean
51 *
52 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
53 * @version $Revision: 6077 $
54 */
55public class ClassifierCustomizer
56  extends JPanel
57  implements Customizer, CustomizerClosingListener, 
58  CustomizerCloseRequester {
59
60  /** for serialization */
61  private static final long serialVersionUID = -6688000820160821429L;
62
63  static {
64     GenericObjectEditor.registerEditors();
65  }
66
67  private PropertyChangeSupport m_pcSupport = 
68    new PropertyChangeSupport(this);
69 
70  private weka.gui.beans.Classifier m_dsClassifier;
71  /*  private GenericObjectEditor m_ClassifierEditor =
72      new GenericObjectEditor(true); */
73  private PropertySheetPanel m_ClassifierEditor = 
74    new PropertySheetPanel();
75
76  private JPanel m_incrementalPanel = new JPanel();
77  private JCheckBox m_updateIncrementalClassifier
78    = new JCheckBox("Update classifier on incoming instance stream");
79  private boolean m_panelVisible = false;
80 
81  private JPanel m_holderPanel = new JPanel();
82  private JTextField m_executionSlotsText = new JTextField();
83 
84  private JCheckBox m_blockOnLastFold = new JCheckBox("Block on last fold of last run");
85 
86  private JFrame m_parentFrame;
87 
88  /** Copy of the current classifier in case cancel is selected */
89  protected weka.classifiers.Classifier m_backup;
90
91  public ClassifierCustomizer() {
92   
93    m_ClassifierEditor.
94      setBorder(BorderFactory.createTitledBorder("Classifier options"));
95   
96    m_updateIncrementalClassifier.
97      setToolTipText("Train the classifier on "
98                     +"each individual incoming streamed instance.");
99    m_updateIncrementalClassifier.
100      addActionListener(new ActionListener() {
101          public void actionPerformed(ActionEvent e) {
102            if (m_dsClassifier != null) {
103              m_dsClassifier.
104                setUpdateIncrementalClassifier(m_updateIncrementalClassifier.
105                                               isSelected());
106            }
107          }
108        });
109    m_incrementalPanel.add(m_updateIncrementalClassifier);
110   
111    m_executionSlotsText.addActionListener(new ActionListener() {
112      public void actionPerformed(ActionEvent e) {
113        if (m_dsClassifier != null &&
114            m_executionSlotsText.getText().length() > 0) {
115          int newSlots = Integer.parseInt(m_executionSlotsText.getText());
116          m_dsClassifier.setExecutionSlots(newSlots);
117        }
118      }
119    });
120   
121    m_executionSlotsText.addFocusListener(new FocusListener() {
122      public void focusGained(FocusEvent e) {}
123     
124      public void focusLost(FocusEvent e) {
125        if (m_dsClassifier != null && 
126            m_executionSlotsText.getText().length() > 0) {
127          int newSlots = Integer.parseInt(m_executionSlotsText.getText());
128          m_dsClassifier.setExecutionSlots(newSlots);
129        }
130      }
131    });
132   
133    m_blockOnLastFold.addActionListener(new ActionListener() {
134      public void actionPerformed(ActionEvent e) {
135        if (m_dsClassifier != null) {
136          m_dsClassifier.setBlockOnLastFold(m_blockOnLastFold.isSelected());
137        }
138      }
139    });
140   
141    JPanel executionSlotsPanel = new JPanel();
142    executionSlotsPanel.
143      setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
144    JLabel executionSlotsLabel = new JLabel("Execution slots");
145    executionSlotsPanel.setLayout(new BorderLayout());
146    executionSlotsPanel.add(executionSlotsLabel, BorderLayout.WEST);
147    executionSlotsPanel.add(m_executionSlotsText, BorderLayout.CENTER);
148    m_holderPanel.
149      setBorder(BorderFactory.createTitledBorder("More options"));
150    m_holderPanel.setLayout(new BorderLayout());
151    m_holderPanel.add(executionSlotsPanel, BorderLayout.NORTH);
152//    m_blockOnLastFold.setHorizontalTextPosition(SwingConstants.RIGHT);
153    m_holderPanel.add(m_blockOnLastFold, BorderLayout.SOUTH);
154   
155    JPanel holder2 = new JPanel();
156    holder2.setLayout(new BorderLayout());
157    holder2.add(m_holderPanel, BorderLayout.NORTH);
158    JButton OKBut = new JButton("OK");
159    JButton CancelBut = new JButton("Cancel");
160    OKBut.addActionListener(new ActionListener() {
161      public void actionPerformed(ActionEvent e) {
162        m_parentFrame.dispose();
163      }
164    });
165   
166    CancelBut.addActionListener(new ActionListener() {
167      public void actionPerformed(ActionEvent e) {
168        // cancel requested, so revert to backup and then
169        // close the dialog
170        if (m_backup != null) {
171          m_dsClassifier.setClassifierTemplate(m_backup);
172        }
173        m_parentFrame.dispose();
174      }
175    });
176   
177    JPanel butHolder = new JPanel();
178    butHolder.setLayout(new GridLayout(1,2));
179    butHolder.add(OKBut);
180    butHolder.add(CancelBut);
181    holder2.add(butHolder, BorderLayout.SOUTH);
182   
183    setLayout(new BorderLayout());
184    add(m_ClassifierEditor, BorderLayout.CENTER);
185    add(holder2, BorderLayout.SOUTH);
186  }
187 
188  private void checkOnClassifierType() {
189    Classifier editedC = m_dsClassifier.getClassifierTemplate();
190    if (editedC instanceof weka.classifiers.UpdateableClassifier && 
191        m_dsClassifier.hasIncomingStreamInstances()) {
192      if (!m_panelVisible) {
193        m_holderPanel.add(m_incrementalPanel, BorderLayout.SOUTH);
194        m_panelVisible = true;
195        m_executionSlotsText.setEnabled(false);
196      }
197    } else {
198      if (m_panelVisible) {
199        m_holderPanel.remove(m_incrementalPanel);
200        m_executionSlotsText.setEnabled(true);
201        m_panelVisible = false;
202      }
203    }
204  }
205
206  /**
207   * Set the classifier object to be edited
208   *
209   * @param object an <code>Object</code> value
210   */
211  public void setObject(Object object) {
212    m_dsClassifier = (weka.gui.beans.Classifier)object;
213    //    System.err.println(Utils.joinOptions(((OptionHandler)m_dsClassifier.getClassifier()).getOptions()));
214    try {
215      m_backup = 
216        (weka.classifiers.Classifier)GenericObjectEditor.makeCopy(m_dsClassifier.getClassifierTemplate());
217    } catch (Exception ex) {
218      // ignore
219    }
220    m_ClassifierEditor.setTarget(m_dsClassifier.getClassifierTemplate());
221    m_updateIncrementalClassifier.
222      setSelected(m_dsClassifier.getUpdateIncrementalClassifier());
223    m_executionSlotsText.setText(""+m_dsClassifier.getExecutionSlots());
224    m_blockOnLastFold.setSelected(m_dsClassifier.getBlockOnLastFold());
225    checkOnClassifierType();
226  }
227 
228  /* (non-Javadoc)
229   * @see weka.gui.beans.CustomizerClosingListener#customizerClosing()
230   */
231  public void customizerClosing() {
232    if (m_executionSlotsText.getText().length() > 0) {
233      int newSlots = Integer.parseInt(m_executionSlotsText.getText());
234      m_dsClassifier.setExecutionSlots(newSlots);
235    }
236  }
237
238  /**
239   * Add a property change listener
240   *
241   * @param pcl a <code>PropertyChangeListener</code> value
242   */
243  public void addPropertyChangeListener(PropertyChangeListener pcl) {
244    m_pcSupport.addPropertyChangeListener(pcl);
245  }
246
247  /**
248   * Remove a property change listener
249   *
250   * @param pcl a <code>PropertyChangeListener</code> value
251   */
252  public void removePropertyChangeListener(PropertyChangeListener pcl) {
253    m_pcSupport.removePropertyChangeListener(pcl);
254  }
255
256  public void setParentFrame(JFrame parent) {
257    m_parentFrame = parent;   
258  }
259}
Note: See TracBrowser for help on using the repository browser.