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.forms;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 /**
23 * <p>
24 * A fully functional default implementation of the <code>ComponentStore</code>
25 * interface.
26 * </p>
27 * <p>
28 * This implementation keeps the managed components and handlers in maps where
29 * they can directly be accessed. The components of the stored component
30 * handlers are also put in the map for the components, so the name spaces of
31 * these entities are not disjunct.
32 * </p>
33 * <p>
34 * For field handlers situation is similar: The component handlers associated to
35 * the added field handlers will be added to the map with the component
36 * handlers, too (and their components will in turn be added to the component
37 * map). So access to the stored entities is somewhat hierarchical.
38 * </p>
39 *
40 * @author Oliver Heger
41 * @version $Id: ComponentStoreImpl.java 205 2012-01-29 18:29:57Z oheger $
42 */
43 public class ComponentStoreImpl implements ComponentStore
44 {
45 /** Stores the components. */
46 private Map<String, Object> components;
47
48 /** Stores the component handlers. */
49 private Map<String, ComponentHandler<?>> componentHandlers;
50
51 /** Stores the field handlers. */
52 private Map<String, FieldHandler> fieldHandlers;
53
54 /**
55 * Creates a new instance of <code>ComponentStoreImpl</code> and
56 * initializes it.
57 */
58 public ComponentStoreImpl()
59 {
60 components = new HashMap<String, Object>();
61 componentHandlers = new HashMap<String, ComponentHandler<?>>();
62 fieldHandlers = new HashMap<String, FieldHandler>();
63 }
64
65 /**
66 * Adds a new component to this store.
67 *
68 * @param name the name of the component
69 * @param component the component to be added
70 * @throws IllegalArgumentException if either name or component are <b>null</b>
71 */
72 public void add(String name, Object component)
73 {
74 if (component == null)
75 {
76 throw new IllegalArgumentException(
77 "Component to be added must not be null!");
78 }
79 checkName(name);
80 components.put(name, component);
81 }
82
83 /**
84 * Adds a new component handler to this store.
85 *
86 * @param name the name of the handler
87 * @param handler the handler to be added
88 * @throws IllegalArgumentException if either name or component handler are
89 * <b>null</b>
90 */
91 public void addComponentHandler(String name, ComponentHandler<?> handler)
92 {
93 if (handler == null)
94 {
95 throw new IllegalArgumentException(
96 "Component handler must not be null!");
97 }
98 checkName(name);
99 componentHandlers.put(name, handler);
100 Object comp = handler.getComponent();
101 if (comp != null)
102 {
103 add(name, comp);
104 }
105 }
106
107 /**
108 * Adds a new field handler to this store.
109 *
110 * @param name the name of the field handler
111 * @param fldHandler the handler to be added
112 * @throws IllegalArgumentException if either name or handler are <b>null</b>
113 */
114 public void addFieldHandler(String name, FieldHandler fldHandler)
115 {
116 if (fldHandler == null)
117 {
118 throw new IllegalArgumentException(
119 "Field handler must not be null!");
120 }
121 checkName(name);
122 fieldHandlers.put(name, fldHandler);
123 ComponentHandler<?> compHandler = fldHandler.getComponentHandler();
124 if (compHandler != null)
125 {
126 addComponentHandler(name, compHandler);
127 }
128 }
129
130 /**
131 * Searches the component with the specified name.
132 *
133 * @param name the name
134 * @return the component with this name or <b>null</b> if it cannot be
135 * found
136 */
137 public Object findComponent(String name)
138 {
139 return components.get(name);
140 }
141
142 /**
143 * Searches the component handler with the specified name.
144 *
145 * @param name the name
146 * @return the component handler with this name or <b>null</b> if it cannot
147 * be found
148 */
149 public ComponentHandler<?> findComponentHandler(String name)
150 {
151 return componentHandlers.get(name);
152 }
153
154 /**
155 * Searches the field handler with the specified name.
156 *
157 * @param name the name
158 * @return the field handler with this name or <b>null</b> if it cannot be
159 * found
160 */
161 public FieldHandler findFieldHandler(String name)
162 {
163 return fieldHandlers.get(name);
164 }
165
166 /**
167 * Returns a set with the names of all stored component handlers.
168 *
169 * @return the names of the stored component handlers
170 */
171 public Set<String> getComponentHandlerNames()
172 {
173 return componentHandlers.keySet();
174 }
175
176 /**
177 * Returns a set with the names of all stored components.
178 *
179 * @return the names of the stored components
180 */
181 public Set<String> getComponentNames()
182 {
183 return components.keySet();
184 }
185
186 /**
187 * Returns a set with the names of all stored field handlers.
188 *
189 * @return the names of the stored field handlers
190 */
191 public Set<String> getFieldHandlerNames()
192 {
193 return fieldHandlers.keySet();
194 }
195
196 /**
197 * Clears the content of this store. After that this object is exactly like
198 * a newly created one.
199 */
200 public void clear()
201 {
202 components.clear();
203 componentHandlers.clear();
204 fieldHandlers.clear();
205 }
206
207 /**
208 * Checks the name of a newly added entity. The name must not be <b>null</b>.
209 *
210 * @param name the name to check
211 * @throws IllegalArgumentException if the name is <b>null</b>
212 */
213 private void checkName(String name)
214 {
215 if (name == null)
216 {
217 throw new IllegalArgumentException("Name must not be null!");
218 }
219 }
220 }