| 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 | * DefaultNode.java |
|---|
| 19 | * Copyright (C) 2006 Robert Jung |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.gui.ensembleLibraryEditor.tree; |
|---|
| 24 | |
|---|
| 25 | import weka.gui.EnsembleLibraryEditor; |
|---|
| 26 | |
|---|
| 27 | import java.beans.PropertyChangeEvent; |
|---|
| 28 | import java.beans.PropertyChangeListener; |
|---|
| 29 | import java.beans.PropertyEditor; |
|---|
| 30 | |
|---|
| 31 | import javax.swing.tree.DefaultMutableTreeNode; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * This class is responsible for representing objects that we haven't explicitly |
|---|
| 35 | * written custom tree node editors for. In other words - Objects that are |
|---|
| 36 | * "weird". It didn't make sense for us to try to come up with a nice tree |
|---|
| 37 | * representation for absolutely everything, e.g. CostMatrixes or ArrayLists. |
|---|
| 38 | * This class is responsible for representing Classifier parameter values that |
|---|
| 39 | * are not numbers, an enumeration of values (e.g. true/false), or Objects that |
|---|
| 40 | * have their own GenericObjectEditors (like other Classifiers). So in these |
|---|
| 41 | * cases we can just use the default editor that came with the object. |
|---|
| 42 | * |
|---|
| 43 | * @author Robert Jung (mrbobjung@gmail.com) |
|---|
| 44 | * @version $Revision: 1.1 $ |
|---|
| 45 | */ |
|---|
| 46 | public class DefaultNode |
|---|
| 47 | extends DefaultMutableTreeNode |
|---|
| 48 | implements PropertyChangeListener { |
|---|
| 49 | |
|---|
| 50 | /** for serialization */ |
|---|
| 51 | private static final long serialVersionUID = -2182147677358461880L; |
|---|
| 52 | |
|---|
| 53 | /** the name of this node */ |
|---|
| 54 | private String m_Name; |
|---|
| 55 | |
|---|
| 56 | /** the tip text for our node editor to display */ |
|---|
| 57 | private String m_ToolTipText; |
|---|
| 58 | |
|---|
| 59 | /** The default PropertyEditor that was supplied for this node */ |
|---|
| 60 | private PropertyEditor m_PropertyEditor; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * The constructor initializes the members of this node. |
|---|
| 64 | * |
|---|
| 65 | * @param name |
|---|
| 66 | * the name of the value represented by this node |
|---|
| 67 | * @param toolTipText |
|---|
| 68 | * the tool tip text to be displayed |
|---|
| 69 | * @param value |
|---|
| 70 | * the intial value |
|---|
| 71 | * @param propertyEditor |
|---|
| 72 | * the editor provided for this node |
|---|
| 73 | */ |
|---|
| 74 | public DefaultNode(String name, String toolTipText, Object value, |
|---|
| 75 | PropertyEditor propertyEditor) { |
|---|
| 76 | |
|---|
| 77 | super(value); |
|---|
| 78 | |
|---|
| 79 | this.m_Name = name; |
|---|
| 80 | this.m_ToolTipText = toolTipText; |
|---|
| 81 | this.m_PropertyEditor = propertyEditor; |
|---|
| 82 | |
|---|
| 83 | m_PropertyEditor.addPropertyChangeListener(this); |
|---|
| 84 | |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * this returns the property editor that was provided for this object. This |
|---|
| 89 | * propertyEditor object is initially chosen inside of the GenericObjectNode |
|---|
| 90 | * updateTree() method if you are interested in where it comes from. |
|---|
| 91 | * |
|---|
| 92 | * @return the default editor for this node |
|---|
| 93 | */ |
|---|
| 94 | public PropertyEditor getEditor() { |
|---|
| 95 | m_PropertyEditor.setValue(this.getUserObject()); |
|---|
| 96 | return m_PropertyEditor; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * getter for the tooltip text |
|---|
| 101 | * |
|---|
| 102 | * @return tooltip text |
|---|
| 103 | */ |
|---|
| 104 | public String getToolTipText() { |
|---|
| 105 | return m_ToolTipText; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * gets the name of the parameter value represented by this node |
|---|
| 110 | * |
|---|
| 111 | * @return the name of this parameter |
|---|
| 112 | */ |
|---|
| 113 | public String getName() { |
|---|
| 114 | return m_Name; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * this is a simple filter for the setUserObject method. We basically don't |
|---|
| 119 | * want null values to be passed in. |
|---|
| 120 | * |
|---|
| 121 | * @param o the user object |
|---|
| 122 | */ |
|---|
| 123 | public void setUserObject(Object o) { |
|---|
| 124 | if (o != null) |
|---|
| 125 | super.setUserObject(o); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | * ToString method simply prints out the user object toString for this node |
|---|
| 130 | * |
|---|
| 131 | * @return a string representation |
|---|
| 132 | */ |
|---|
| 133 | public String toString() { |
|---|
| 134 | return getClass().getName() + "[" + getUserObject().toString() + "]"; |
|---|
| 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 | } |
|---|