source: src/main/java/weka/gui/beans/ClassAssignerCustomizer.java @ 22

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

Import di weka.

File size: 5.8 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 *    ClassAssignerCustomizer.java
19 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.beans;
24
25import weka.core.Attribute;
26import weka.core.Instances;
27import weka.gui.PropertySheetPanel;
28
29import java.awt.BorderLayout;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.beans.Customizer;
33import java.beans.PropertyChangeListener;
34import java.beans.PropertyChangeSupport;
35
36import javax.swing.BorderFactory;
37import javax.swing.DefaultComboBoxModel;
38import javax.swing.JComboBox;
39import javax.swing.JPanel;
40
41/**
42 * GUI customizer for the class assigner bean
43 *
44 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
45 * @version $Revision: 1.7 $
46 */
47public class ClassAssignerCustomizer
48  extends JPanel
49  implements Customizer, CustomizerClosingListener, DataFormatListener {
50
51  /** for serialization */
52  private static final long serialVersionUID = 476539385765301907L;
53
54  private boolean m_displayColNames = false;
55
56  private ClassAssigner m_classAssigner;
57
58  private PropertyChangeSupport m_pcSupport = 
59    new PropertyChangeSupport(this);
60
61  private PropertySheetPanel m_caEditor = 
62    new PropertySheetPanel();
63
64  private JComboBox m_ClassCombo = new JComboBox();
65  private JPanel m_holderP = new JPanel();
66
67  public ClassAssignerCustomizer() {
68    setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 5, 5));
69
70    setLayout(new BorderLayout());
71    add(new javax.swing.JLabel("ClassAssignerCustomizer"), 
72        BorderLayout.NORTH);
73    m_holderP.setLayout(new BorderLayout());
74    m_holderP.setBorder(BorderFactory.createTitledBorder("Choose class attribute"));
75    m_holderP.add(m_ClassCombo, BorderLayout.CENTER);
76    m_ClassCombo.addActionListener(new ActionListener() {
77        public void actionPerformed(ActionEvent e) {
78          if (m_classAssigner != null && m_displayColNames == true) {
79            m_classAssigner.setClassColumn(""+(m_ClassCombo.getSelectedIndex()));
80          }
81        }
82      });
83    add(m_caEditor, BorderLayout.CENTER);
84  }
85
86  private void setUpStandardSelection() {
87    if (m_displayColNames == true) {
88      remove(m_holderP);
89      m_caEditor.setTarget(m_classAssigner);
90      add(m_caEditor, BorderLayout.CENTER);
91      m_displayColNames = false;
92    }
93    validate(); repaint();
94  }
95 
96  private void setUpColumnSelection(Instances format) {
97    if (m_displayColNames == false) {
98      remove(m_caEditor);
99    }
100    int existingClassCol = format.classIndex();
101    if (existingClassCol < 0) {
102      existingClassCol = 0;
103    }
104    String [] attribNames = new String [format.numAttributes()+1];
105    attribNames[0] = "NO CLASS";
106    for (int i = 1; i < attribNames.length; i++) {
107      String type = "";
108      switch (format.attribute(i-1).type()) {
109      case Attribute.NOMINAL:
110        type = "(Nom) ";
111        break;
112      case Attribute.NUMERIC:
113        type = "(Num) ";
114        break;
115      case Attribute.STRING:
116        type = "(Str) ";
117        break;
118      case Attribute.DATE:
119        type = "(Dat) ";
120        break;
121      case Attribute.RELATIONAL:
122        type = "(Rel) ";
123        break;
124      default:
125        type = "(???) ";
126      }
127      attribNames[i] = type + format.attribute(i-1).name();
128    }
129    m_ClassCombo.setModel(new DefaultComboBoxModel(attribNames));
130    if (attribNames.length > 0) {
131      m_ClassCombo.setSelectedIndex(existingClassCol+1);
132    }
133    if (m_displayColNames == false) {
134      add(m_holderP, BorderLayout.CENTER);
135      m_displayColNames = true;
136    }
137    validate(); repaint();
138  }
139
140  /**
141   * Set the bean to be edited
142   *
143   * @param object an <code>Object</code> value
144   */
145  public void setObject(Object object) {
146    if (m_classAssigner != (ClassAssigner)object) {
147      // remove ourselves as a listener from the old ClassAssigner (if necessary)
148      if (m_classAssigner != null) {
149        m_classAssigner.removeDataFormatListener(this);
150      }
151      m_classAssigner = (ClassAssigner)object;
152      // add ourselves as a data format listener
153      m_classAssigner.addDataFormatListener(this);
154      m_caEditor.setTarget(m_classAssigner);
155      if (m_classAssigner.getConnectedFormat() != null) {
156        setUpColumnSelection(m_classAssigner.getConnectedFormat());
157      }
158    }
159  }
160
161  public void customizerClosing() {
162    // remove ourselves as a listener from the ClassAssigner (if necessary)
163    if (m_classAssigner != null) {
164      System.err.println("Customizer deregistering with class assigner");
165      m_classAssigner.removeDataFormatListener(this);
166    }
167  }
168
169  public void newDataFormat(DataSetEvent dse) {
170    if (dse.getDataSet() != null) {
171      //      System.err.println("Setting up column selection.........");
172      setUpColumnSelection(m_classAssigner.getConnectedFormat());
173    } else {
174      setUpStandardSelection();
175    }
176  }
177
178  /**
179   * Add a property change listener
180   *
181   * @param pcl a <code>PropertyChangeListener</code> value
182   */
183  public void addPropertyChangeListener(PropertyChangeListener pcl) {
184    m_pcSupport.addPropertyChangeListener(pcl);
185  }
186 
187  /**
188   * Remove a property change listener
189   *
190   * @param pcl a <code>PropertyChangeListener</code> value
191   */
192  public void removePropertyChangeListener(PropertyChangeListener pcl) {
193    m_pcSupport.removePropertyChangeListener(pcl);
194  }
195}
Note: See TracBrowser for help on using the repository browser.