001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * DefaultNode.java
019: * Copyright (C) 2006 Robert Jung
020: *
021: */
022:
023: package weka.gui.ensembleLibraryEditor.tree;
024:
025: import weka.gui.EnsembleLibraryEditor;
026:
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.beans.PropertyEditor;
030:
031: import javax.swing.tree.DefaultMutableTreeNode;
032:
033: /**
034: * This class is responsible for representing objects that we haven't explicitly
035: * written custom tree node editors for. In other words - Objects that are
036: * "weird". It didn't make sense for us to try to come up with a nice tree
037: * representation for absolutely everything, e.g. CostMatrixes or ArrayLists.
038: * This class is responsible for representing Classifier parameter values that
039: * are not numbers, an enumeration of values (e.g. true/false), or Objects that
040: * have their own GenericObjectEditors (like other Classifiers). So in these
041: * cases we can just use the default editor that came with the object.
042: *
043: * @author Robert Jung (mrbobjung@gmail.com)
044: * @version $Revision: 1.1 $
045: */
046: public class DefaultNode extends DefaultMutableTreeNode implements
047: PropertyChangeListener {
048:
049: /** for serialization */
050: private static final long serialVersionUID = -2182147677358461880L;
051:
052: /** the name of this node */
053: private String m_Name;
054:
055: /** the tip text for our node editor to display */
056: private String m_ToolTipText;
057:
058: /** The default PropertyEditor that was supplied for this node */
059: private PropertyEditor m_PropertyEditor;
060:
061: /**
062: * The constructor initializes the members of this node.
063: *
064: * @param name
065: * the name of the value represented by this node
066: * @param toolTipText
067: * the tool tip text to be displayed
068: * @param value
069: * the intial value
070: * @param propertyEditor
071: * the editor provided for this node
072: */
073: public DefaultNode(String name, String toolTipText, Object value,
074: PropertyEditor propertyEditor) {
075:
076: super (value);
077:
078: this .m_Name = name;
079: this .m_ToolTipText = toolTipText;
080: this .m_PropertyEditor = propertyEditor;
081:
082: m_PropertyEditor.addPropertyChangeListener(this );
083:
084: }
085:
086: /**
087: * this returns the property editor that was provided for this object. This
088: * propertyEditor object is initially chosen inside of the GenericObjectNode
089: * updateTree() method if you are interested in where it comes from.
090: *
091: * @return the default editor for this node
092: */
093: public PropertyEditor getEditor() {
094: m_PropertyEditor.setValue(this .getUserObject());
095: return m_PropertyEditor;
096: }
097:
098: /**
099: * getter for the tooltip text
100: *
101: * @return tooltip text
102: */
103: public String getToolTipText() {
104: return m_ToolTipText;
105: }
106:
107: /**
108: * gets the name of the parameter value represented by this node
109: *
110: * @return the name of this parameter
111: */
112: public String getName() {
113: return m_Name;
114: }
115:
116: /**
117: * this is a simple filter for the setUserObject method. We basically don't
118: * want null values to be passed in.
119: *
120: * @param o the user object
121: */
122: public void setUserObject(Object o) {
123: if (o != null)
124: super .setUserObject(o);
125: }
126:
127: /**
128: * ToString method simply prints out the user object toString for this node
129: *
130: * @return a string representation
131: */
132: public String toString() {
133: return getClass().getName() + "[" + getUserObject().toString()
134: + "]";
135: }
136:
137: /**
138: * This implements the PropertyChangeListener for this node that gets
139: * registered with its Editor. All we really have to do is change the Object
140: * value stored internally at this node when its editor says the value
141: * changed.
142: *
143: * @param evt the event
144: */
145: public void propertyChange(PropertyChangeEvent evt) {
146:
147: Object source = evt.getSource();
148:
149: Object value = EnsembleLibraryEditor.getEditorValue(source);
150:
151: /*
152: * //was useful for debugging when we encountered some strange value
153: * types //these printouts tell you the classes that the editor is
154: * supplying System.out.println("prop name: " + evt.getPropertyName() +
155: * "new value: " + evt.getNewValue() + "old value: " +
156: * evt.getOldValue()); System.out.println("prop val: " +
157: * source.toString() + " expected val: " + value);
158: */
159: setUserObject(value);
160: }
161: }
|