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 | * ModelTreeNodeEditor.java |
---|
19 | * Copyright (C) 2006 Robert Jung |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.ensembleLibraryEditor.tree; |
---|
24 | |
---|
25 | import weka.gui.EnsembleLibraryEditor; |
---|
26 | import weka.gui.GenericObjectEditor; |
---|
27 | |
---|
28 | import java.awt.Component; |
---|
29 | import java.awt.event.ActionEvent; |
---|
30 | import java.awt.event.ActionListener; |
---|
31 | import java.awt.event.ItemEvent; |
---|
32 | import java.awt.event.ItemListener; |
---|
33 | import java.awt.event.MouseEvent; |
---|
34 | import java.beans.PropertyChangeEvent; |
---|
35 | import java.beans.PropertyChangeListener; |
---|
36 | import java.beans.PropertyEditor; |
---|
37 | import java.util.EventObject; |
---|
38 | |
---|
39 | import javax.swing.AbstractCellEditor; |
---|
40 | import javax.swing.JComponent; |
---|
41 | import javax.swing.JLabel; |
---|
42 | import javax.swing.JTree; |
---|
43 | import javax.swing.tree.DefaultMutableTreeNode; |
---|
44 | import javax.swing.tree.TreeCellEditor; |
---|
45 | import javax.swing.tree.TreePath; |
---|
46 | |
---|
47 | /** |
---|
48 | * This class is in charge of dynamically creating editor GUI objects on |
---|
49 | * demand for the main JTree class that will display our Classifier tree |
---|
50 | * model of parameters. This is in fact the CellEditor class that is |
---|
51 | * registered with our tree. |
---|
52 | * <p/> |
---|
53 | * Basically it delegates much of the work to the various NodeEditor |
---|
54 | * classes found in this package. All it really has to do is detect |
---|
55 | * what of node it is and then instantiate an editor of the appropriate |
---|
56 | * type. |
---|
57 | * |
---|
58 | * @author Robert Jung (mrbobjung@gmail.com) |
---|
59 | * @version $Revision: 1.1 $ |
---|
60 | */ |
---|
61 | public class ModelTreeNodeEditor |
---|
62 | extends AbstractCellEditor |
---|
63 | implements TreeCellEditor, ItemListener, PropertyChangeListener, ActionListener { |
---|
64 | |
---|
65 | /** for serialization */ |
---|
66 | private static final long serialVersionUID = 7057924814405386358L; |
---|
67 | |
---|
68 | /** |
---|
69 | * This is the underlying tree holding all our parameter nodes in the GUI. |
---|
70 | */ |
---|
71 | private JTree m_Tree; |
---|
72 | |
---|
73 | /** |
---|
74 | * default Constructor |
---|
75 | * |
---|
76 | * @param tree the tree to use |
---|
77 | */ |
---|
78 | public ModelTreeNodeEditor(JTree tree) { |
---|
79 | m_Tree = tree; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * I'm supposed to implemnent this as part of the TreeCellEDitor |
---|
84 | * interface. however, it's not necessary for this class so it |
---|
85 | * returns null. |
---|
86 | * |
---|
87 | * @return always null |
---|
88 | */ |
---|
89 | public Object getCellEditorValue() { |
---|
90 | return null; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * This tells the JTree whether or not to let nodes in the tree be |
---|
95 | * edited. Basically all this does is return true for all nodes that |
---|
96 | * aren't PropertyNodes - which don't have any interactive widgets |
---|
97 | * |
---|
98 | * @param event the event |
---|
99 | * @return true if editable |
---|
100 | */ |
---|
101 | public boolean isCellEditable(EventObject event) { |
---|
102 | boolean returnValue = false; |
---|
103 | if (event instanceof MouseEvent) { |
---|
104 | MouseEvent mouseEvent = (MouseEvent) event; |
---|
105 | TreePath path = m_Tree.getPathForLocation(mouseEvent.getX(), |
---|
106 | mouseEvent.getY()); |
---|
107 | if (path != null) { |
---|
108 | Object node = path.getLastPathComponent(); |
---|
109 | if ((node != null) && (node instanceof DefaultMutableTreeNode)) { |
---|
110 | DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node; |
---|
111 | returnValue = (!(treeNode instanceof PropertyNode)); |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | return returnValue; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * This method uses the ModelTreeNodeRenderer class to get the individual |
---|
120 | * editors and then registers this classes editing event listeners with |
---|
121 | * them |
---|
122 | * |
---|
123 | * @param tree the associated tree |
---|
124 | * @param value the value |
---|
125 | * @param selected true if item is selected |
---|
126 | * @param expanded true if it is expanded |
---|
127 | * @param leaf true if it is a leaf |
---|
128 | * @param row the row in the tree |
---|
129 | * @return the rendering component |
---|
130 | */ |
---|
131 | public Component getTreeCellEditorComponent(JTree tree, Object value, |
---|
132 | boolean selected, boolean expanded, boolean leaf, int row) { |
---|
133 | |
---|
134 | //make the renderer to all the "painting" work, then add all of the |
---|
135 | //necessary action listeners to the objects. |
---|
136 | //Component customEditor = m_Renderer.getTreeCellRendererComponent(tree, |
---|
137 | // value,selected,expanded,leaf,row,false); |
---|
138 | |
---|
139 | JComponent customRenderer = null; |
---|
140 | |
---|
141 | if (value instanceof GenericObjectNode) { |
---|
142 | GenericObjectNode node = (GenericObjectNode) value; |
---|
143 | customRenderer = new GenericObjectNodeEditor(node); |
---|
144 | customRenderer.setToolTipText(node.getToolTipText()); |
---|
145 | ((GenericObjectNodeEditor) customRenderer) |
---|
146 | .setPropertyChangeListener(this); |
---|
147 | |
---|
148 | } else if (value instanceof PropertyNode) { |
---|
149 | PropertyNode node = (PropertyNode) value; |
---|
150 | JLabel label = new JLabel(node.getName()); |
---|
151 | label.setToolTipText(node.getToolTipText()); |
---|
152 | customRenderer = label; |
---|
153 | customRenderer.setToolTipText(node.getToolTipText()); |
---|
154 | //do nothing, these nodes aren't editable |
---|
155 | |
---|
156 | } else if (value instanceof CheckBoxNode) { |
---|
157 | CheckBoxNode node = (CheckBoxNode) value; |
---|
158 | customRenderer = new CheckBoxNodeEditor(node); |
---|
159 | customRenderer.setToolTipText(node.getToolTipText()); |
---|
160 | ((CheckBoxNodeEditor) customRenderer).setItemListener(this); |
---|
161 | |
---|
162 | } else if (value instanceof NumberNode) { |
---|
163 | NumberNode node = (NumberNode) value; |
---|
164 | customRenderer = new NumberNodeEditor(node); |
---|
165 | customRenderer.setToolTipText(node.getToolTipText()); |
---|
166 | ((NumberNodeEditor) customRenderer).setPropertyChangeListener(this); |
---|
167 | ((NumberNodeEditor) customRenderer).setItemListener(this); |
---|
168 | ((NumberNodeEditor) customRenderer).setActionListener(this); |
---|
169 | |
---|
170 | } else if (value instanceof DefaultNode) { |
---|
171 | |
---|
172 | DefaultNode node = (DefaultNode) value; |
---|
173 | PropertyEditor nodeEditor = node.getEditor(); |
---|
174 | customRenderer = (JComponent) EnsembleLibraryEditor |
---|
175 | .getDefaultRenderer(nodeEditor); |
---|
176 | ((JComponent) customRenderer).setToolTipText(node.getToolTipText()); |
---|
177 | nodeEditor.addPropertyChangeListener(this); |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | return customRenderer; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * The item Listener that gets registered with all node editors that |
---|
186 | * have a widget that had itemStateChangeg events. It just fires the |
---|
187 | * editing stopped event. |
---|
188 | * |
---|
189 | * @param e the event |
---|
190 | */ |
---|
191 | public void itemStateChanged(ItemEvent e) { |
---|
192 | if (stopCellEditing()) { |
---|
193 | fireEditingStopped(); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * The prtopertyListener that gets registered with all node editors that |
---|
199 | * have a widget that had propertyStateChangeg events. It just fires the |
---|
200 | * editing stopped event. |
---|
201 | * |
---|
202 | * @param evt the event |
---|
203 | */ |
---|
204 | public void propertyChange(PropertyChangeEvent evt) { |
---|
205 | |
---|
206 | if (evt.getSource() instanceof GenericObjectEditor |
---|
207 | || evt.getSource() instanceof GenericObjectNodeEditor) { |
---|
208 | |
---|
209 | if (stopCellEditing()) { |
---|
210 | fireEditingStopped(); |
---|
211 | } |
---|
212 | |
---|
213 | } else if (evt.getPropertyName() != null) { |
---|
214 | if (evt.getPropertyName().equals("value")) { |
---|
215 | |
---|
216 | if (stopCellEditing()) { |
---|
217 | fireEditingStopped(); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * The item Listener that gets registered with all node editors that |
---|
225 | * have a widget that had actionPerformed events. It just fires the |
---|
226 | * editing stopped event. |
---|
227 | * |
---|
228 | * @param e the event |
---|
229 | */ |
---|
230 | public void actionPerformed(ActionEvent e) { |
---|
231 | if (stopCellEditing()) { |
---|
232 | fireEditingStopped(); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|