1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.gui.platform.swing.builder.components;
17
18 import java.awt.Component;
19
20 import javax.swing.JTextField;
21 import javax.swing.JTree;
22 import javax.swing.tree.DefaultTreeCellEditor;
23
24 /**
25 * <p>
26 * An internally used editor class for editing the nodes of a tree view.
27 * </p>
28 * <p>
29 * This class is needed to ensure that the editor field used for editing a
30 * node's label is initialized correctly. It has to be initialized with the name
31 * of the {@code ConfigurationNode} representing the tree node. The actual
32 * extraction of the node name is handled by the {@link SwingTreeNodeFormatter}
33 * passed to the constructor.
34 * </p>
35 * <p>
36 * Implementation note: This class is used internally only. Therefore, no
37 * sophisticated parameter checks are performed.
38 * </p>
39 *
40 * @author Oliver Heger
41 * @version $Id: $
42 */
43 class SwingTreeCellEditor extends DefaultTreeCellEditor
44 {
45 /**
46 * Creates a new instance of {@code SwingTreeCellEditor} and initializes it.
47 *
48 * @param tree the associated tree component
49 * @param renderer the renderer component
50 */
51 public SwingTreeCellEditor(JTree tree, SwingTreeCellRenderer renderer)
52 {
53 super(tree, renderer);
54 }
55
56 /**
57 * Returns the {@code SwingTreeNodeFormatter} used by this editor.
58 *
59 * @return the {@code SwingTreeNodeFormatter}
60 */
61 public SwingTreeNodeFormatter getNodeFormatter()
62 {
63 return ((SwingTreeCellRenderer) renderer).getNodeFormatter();
64 }
65
66 @Override
67 public Component getTreeCellEditorComponent(JTree tree, Object value,
68 boolean isSelected, boolean expanded, boolean leaf, int row)
69 {
70 Component comp =
71 super.getTreeCellEditorComponent(tree, value, isSelected,
72 expanded, leaf, row);
73 initEditorComponentText(value);
74 return comp;
75 }
76
77 /**
78 * Sets the correct text for the editor component.
79 *
80 * @param value the value to be displayed in the editor component
81 */
82 private void initEditorComponentText(Object value)
83 {
84 ((JTextField) editingComponent).setText(getNodeFormatter().textForNode(
85 value));
86 }
87 }