Source code: com/virtuosotechnologies/lib/basiccommand/builder/AbstractContainerElementBuilderNode.java
1 /*
2 ================================================================================
3
4 FILE: AbstractContainerElementBuilderNode.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 A branch builder that creates an element that can contain other elements.
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 branch builder that creates an element that can contain other elements.
52 * Subclasses need to implement getChildNode(), createInitialElement(), and
53 * the add/set/removeElements methods.
54 * <p>
55 * A lot of this implementation is identical to AbstractElementBuilderNode,
56 * but alas, Java lacks multiple implementation inheritance.
57 */
58 public abstract class AbstractContainerElementBuilderNode
59 extends AbstractBranchBuilderNode
60 {
61 private Object element_;
62
63
64 /**
65 * Constructor implementation
66 */
67 protected AbstractContainerElementBuilderNode(
68 CommandNode commandNode,
69 AbstractBranchBuilderNode parent,
70 int index)
71 {
72 this(commandNode, parent, index, null);
73 }
74
75
76 /**
77 * Constructor implementation
78 */
79 protected AbstractContainerElementBuilderNode(
80 CommandNode commandNode,
81 AbstractBranchBuilderNode parent,
82 int index,
83 Object element)
84 {
85 super(commandNode, parent, index);
86 if (element != null)
87 {
88 element_ = element;
89 }
90 else
91 {
92 element_ = createInitialElement();
93 }
94 buildChildren();
95 if (parent != null && !isHidden())
96 {
97 if (index == END_POSITION)
98 {
99 index = parent.getNumChildren()-1;
100 }
101 parent.addElementAt(parent.getPosition(index), element_);
102 }
103 if (isDisabled() && element_ instanceof JComponent)
104 {
105 ((JComponent)element_).setEnabled(false);
106 }
107 }
108
109
110 /**
111 * Get the cardinality (number of swing objects this node represents).
112 * Most things have a cardinality of 1. Groups have variable cardinality.
113 */
114 protected int getCardinality()
115 {
116 return isHidden() ? 0 : 1;
117 }
118
119
120 /**
121 * Get the element
122 */
123 protected Object getElement()
124 {
125 return element_;
126 }
127
128
129 /**
130 * The hidden state has changed.
131 */
132 protected void hiddenStateChanged(
133 boolean nowHidden)
134 {
135 AbstractBranchBuilderNode parent = getParent();
136 if (parent != null)
137 {
138 if (nowHidden)
139 {
140 parent.removeElementAt(parent.getPosition(this));
141 }
142 else
143 {
144 parent.addElementAt(parent.getPosition(this), element_);
145 }
146 }
147 }
148
149
150 /**
151 * The disabled state has changed.
152 */
153 protected void disabledStateChanged(
154 boolean nowDisabled)
155 {
156 if (element_ instanceof JComponent)
157 {
158 ((JComponent)element_).setEnabled(!nowDisabled);
159 }
160 }
161
162
163 /**
164 * Override this method to create the initial element object.
165 */
166 protected abstract Object createInitialElement();
167 }