Source code: com/virtuosotechnologies/lib/basiccommand/builder/AbstractElementBuilderNode.java
1 /*
2 ================================================================================
3
4 FILE: AbstractElementBuilderNode.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 A skeletal builder that creates a single leaf object with no children.
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.lib.basiccommand.builder;
43
44
45 import javax.swing.JComponent;
46
47 import com.virtuosotechnologies.lib.command.CommandNode;
48
49
50 /**
51 * A skeletal builder that creates a single leaf object with no children.
52 * This needs to be subclassed to implement createInitialElement(), and/or
53 * to pass an element into the constructor. Also note that this builder
54 * registers itself as a PropertySetListener on the CommandNode, so
55 * subclasses do not need to do so again.
56 * <p>
57 * A lot of this implementaiton is identical to
58 * AbstractContainerElementBuilderNode, but alas, Java lacks multiple
59 * implementation inheritance.
60 */
61 public abstract class AbstractElementBuilderNode
62 extends AbstractBuilderNode
63 {
64 private Object element_;
65
66
67 /**
68 * Constructor
69 */
70 protected AbstractElementBuilderNode(
71 CommandNode commandNode,
72 AbstractBranchBuilderNode parent,
73 int index)
74 {
75 this(commandNode, parent, index, null);
76 }
77
78
79 /**
80 * Constructor
81 */
82 protected AbstractElementBuilderNode(
83 CommandNode commandNode,
84 AbstractBranchBuilderNode parent,
85 int index,
86 Object element)
87 {
88 super(commandNode, parent, index);
89 if (element != null)
90 {
91 element_ = element;
92 }
93 else
94 {
95 element_ = createInitialElement();
96 }
97 if (parent != null && !isHidden())
98 {
99 if (index == END_POSITION)
100 {
101 index = parent.getNumChildren()-1;
102 }
103 parent.addElementAt(parent.getPosition(index), element_);
104 }
105 if (isDisabled() && element_ instanceof JComponent)
106 {
107 ((JComponent)element_).setEnabled(false);
108 }
109 }
110
111
112 /**
113 * Get the cardinality (number of swing objects this node represents).
114 * Most things have a cardinality of 1. Groups have variable cardinality.
115 */
116 protected final int getCardinality()
117 {
118 return isHidden() ? 0 : 1;
119 }
120
121
122 /**
123 * Change the element
124 */
125 protected final void setElement(
126 Object element)
127 {
128 element_ = element;
129 if (!isHidden())
130 {
131 AbstractBranchBuilderNode parent = getParent();
132 if (parent != null)
133 {
134 parent.setElementAt(parent.getPosition(this), element);
135 }
136 }
137 }
138
139
140 /**
141 * Get the element
142 */
143 protected final Object getElement()
144 {
145 return element_;
146 }
147
148
149 /**
150 * The hidden state has changed.
151 */
152 protected void hiddenStateChanged(
153 boolean nowHidden)
154 {
155 AbstractBranchBuilderNode parent = getParent();
156 if (parent != null)
157 {
158 if (nowHidden)
159 {
160 parent.removeElementAt(parent.getPosition(this));
161 }
162 else
163 {
164 parent.addElementAt(parent.getPosition(this), element_);
165 }
166 }
167 }
168
169
170 /**
171 * The disabled state has changed.
172 */
173 protected void disabledStateChanged(
174 boolean nowDisabled)
175 {
176 if (element_ instanceof JComponent)
177 {
178 ((JComponent)element_).setEnabled(!nowDisabled);
179 }
180 }
181
182
183 /**
184 * Override this method to create the initial element object.
185 */
186 protected abstract Object createInitialElement();
187 }