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

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

Import di weka.

File size: 5.3 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 *    ClassValuePickerCustomizer.java
19 *    Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.core.Instances;
26
27import java.awt.BorderLayout;
28import java.awt.event.ActionEvent;
29import java.awt.event.ActionListener;
30import java.beans.Customizer;
31import java.beans.PropertyChangeListener;
32import java.beans.PropertyChangeSupport;
33
34import javax.swing.BorderFactory;
35import javax.swing.DefaultComboBoxModel;
36import javax.swing.JComboBox;
37import javax.swing.JLabel;
38import javax.swing.JPanel;
39
40/**
41 * @author Mark Hall
42 * @version $Revision: 1.6 $
43 */
44public class ClassValuePickerCustomizer
45  extends JPanel
46  implements Customizer, CustomizerClosingListener, DataFormatListener {
47
48  /** for serialization */
49  private static final long serialVersionUID = 8213423053861600469L;
50
51  private boolean m_displayValNames = false;
52
53  private ClassValuePicker m_classValuePicker;
54 
55  private PropertyChangeSupport m_pcSupport = 
56    new PropertyChangeSupport(this);
57
58  private JComboBox m_ClassValueCombo = new JComboBox();
59  private JPanel m_holderP = new JPanel();
60
61  private JLabel m_messageLabel = new JLabel("No customization possible at present.");
62
63  public ClassValuePickerCustomizer() {
64    setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 5, 5));
65   
66    setLayout(new BorderLayout());
67    add(new javax.swing.JLabel("ClassValuePickerCustomizer"), 
68        BorderLayout.NORTH);
69    m_holderP.setLayout(new BorderLayout());
70    m_holderP.setBorder(BorderFactory.createTitledBorder("Choose class value"));
71    m_holderP.add(m_ClassValueCombo, BorderLayout.CENTER);
72    m_ClassValueCombo.addActionListener(new ActionListener() {
73        public void actionPerformed(ActionEvent e) {
74          if (m_classValuePicker != null) {
75            m_classValuePicker.setClassValueIndex(m_ClassValueCombo.getSelectedIndex());
76          }
77        }
78      });
79
80    add(m_messageLabel, BorderLayout.CENTER);
81  }
82
83  private void setUpNoCustPossible() {
84    if (m_displayValNames == true) {
85      remove(m_holderP);
86      add(m_messageLabel, BorderLayout.CENTER);
87      m_displayValNames = false;
88    }
89    validate(); repaint();
90  }
91
92  private void setUpValueSelection(Instances format) {
93    if (format.classIndex() < 0 || format.classAttribute().isNumeric()) {
94      // cant do anything in this case
95      return;
96    }
97    if (m_displayValNames == false) {
98      remove(m_messageLabel);
99    }
100   
101    int existingClassVal = m_classValuePicker.getClassValueIndex();
102    String [] attribValNames = new String [format.classAttribute().numValues()];
103    for (int i = 0; i < attribValNames.length; i++) {
104      attribValNames[i] = format.classAttribute().value(i);
105    }
106    m_ClassValueCombo.setModel(new DefaultComboBoxModel(attribValNames));
107    if (attribValNames.length > 0) {
108      if (existingClassVal < attribValNames.length) {
109        m_ClassValueCombo.setSelectedIndex(existingClassVal);
110      }
111    }
112    if (m_displayValNames == false) {
113      add(m_holderP, BorderLayout.CENTER);
114      m_displayValNames = true;
115    }
116    validate(); repaint();
117  }
118
119  /**
120   * Set the bean to be edited
121   *
122   * @param object an <code>Object</code> value
123   */
124  public void setObject(Object object) {
125    if (m_classValuePicker != (ClassValuePicker)object) {
126      // remove ourselves as a listener from the old ClassvaluePicker (if necessary)
127      if (m_classValuePicker != null) {
128        m_classValuePicker.removeDataFormatListener(this);
129      }
130      m_classValuePicker = (ClassValuePicker)object;
131      // add ourselves as a data format listener
132      m_classValuePicker.addDataFormatListener(this);
133      if (m_classValuePicker.getConnectedFormat() != null) {
134        setUpValueSelection(m_classValuePicker.getConnectedFormat());   
135      }
136    }
137  }
138 
139  public void customizerClosing() {
140    // remove ourselves as a listener from the ClassValuePicker (if necessary)
141    if (m_classValuePicker != null) {
142      System.err.println("Customizer deregistering with class value picker");
143      m_classValuePicker.removeDataFormatListener(this);
144    }
145  }
146
147  public void newDataFormat(DataSetEvent dse) {
148    if (dse.getDataSet() != null) {
149      setUpValueSelection(m_classValuePicker.getConnectedFormat());
150    } else {
151      setUpNoCustPossible();
152    }
153  }
154 
155  /**
156   * Add a property change listener
157   *
158   * @param pcl a <code>PropertyChangeListener</code> value
159   */
160  public void addPropertyChangeListener(PropertyChangeListener pcl) {
161    m_pcSupport.addPropertyChangeListener(pcl);
162  }
163 
164  /**
165   * Remove a property change listener
166   *
167   * @param pcl a <code>PropertyChangeListener</code> value
168   */
169  public void removePropertyChangeListener(PropertyChangeListener pcl) {
170    m_pcSupport.removePropertyChangeListener(pcl);
171  }
172}
Note: See TracBrowser for help on using the repository browser.