1
2
3
4 package org.asyrinx.joey.gui.impl;
5
6 import java.awt.Component;
7 import java.io.Serializable;
8 import java.util.List;
9 import java.util.Map;
10 import org.asyrinx.brownie.core.lang.ClassUtils;
11 import org.asyrinx.brownie.swing.SwingUtils;
12 import org.asyrinx.joey.gui.EntityEditView;
13 import org.asyrinx.joey.gui.EntityGuiRuntimeException;
14 import org.asyrinx.joey.gui.EntityListView;
15 import org.asyrinx.joey.gui.EntityView;
16 import org.asyrinx.joey.gui.EntityViewManager;
17 import org.asyrinx.joey.gui.EntityViewManagerUser;
18 import org.asyrinx.joey.om.Entity;
19 import org.asyrinx.joey.om.EntityService;
20 import org.asyrinx.joey.om.EntityServiceManager;
21 import org.asyrinx.joey.om.SearchCondition;
22 import org.seasar.framework.container.S2Container;
23
24 /***
25 * @author akima
26 */
27 public abstract class AbstractEntityViewManager implements EntityViewManager {
28 /***
29 *
30 */
31 public AbstractEntityViewManager() {
32 super();
33 editViewMap = initEditViewMap();
34 listViewMap = initListViewMap();
35 }
36 protected final Map editViewMap;
37 protected final Map listViewMap;
38
39 /***
40 * @return
41 */
42 protected abstract Map initListViewMap();
43
44 /***
45 * @return
46 */
47 protected abstract Map initEditViewMap();
48
49 protected EntityListView newListView(Class entityClass, Object invoker) {
50 return (EntityListView) newEntityView(
51 entityClass,
52 listViewMap,
53 EntityListView.class,
54 invoker);
55 }
56
57 protected EntityEditView newEditView(Class entityClass, Object invoker) {
58 return (EntityEditView) newEntityView(
59 entityClass,
60 editViewMap,
61 EntityEditView.class,
62 invoker);
63 }
64
65 protected EntityView newEntityView(
66 Class entityClass,
67 Map classMap,
68 Class baseViewClass,
69 Object invoker) {
70 final Class class1 =
71 getClassFromMap(entityClass, classMap, baseViewClass);
72 try {
73 final EntityView viewObj =
74 (EntityView) ClassUtils.newObject(class1, baseViewClass);
75
76 if (invoker instanceof EntityViewManagerUser) {
77 EntityViewManagerUser user = (EntityViewManagerUser) invoker;
78 viewObj.setEntityViewManager(user.getEntityViewManager());
79 } else {
80 viewObj.setEntityViewManager(this);
81 }
82 return viewObj;
83 } catch (InstantiationException e) {
84 throw new EntityGuiRuntimeException(e);
85 }
86 }
87
88 /***
89 * @param entityClass
90 * @param viewMap
91 * @return
92 */
93 private Class getClassFromMap(
94 Class entityClass,
95 Map viewMap,
96 Class baseClass) {
97 final Object object = viewMap.get(entityClass);
98 if (!(object instanceof Class)) {
99 throw new EntityGuiRuntimeException("Illegal class");
100 }
101 final Class class1 = (Class) object;
102 if (!baseClass.isAssignableFrom(class1))
103 throw new EntityGuiRuntimeException(
104 "Illegal class - "
105 + class1.getName()
106 + " must extends "
107 + baseClass);
108 return class1;
109 }
110
111
112
113
114
115
116
117 public EntityEditView getDetailEditView(
118 Object invoker,
119 Class entityClass) {
120 return newEditView(entityClass, invoker);
121 }
122
123
124
125
126
127
128
129 public EntityListView getSelectionListView(
130 Object invoker,
131 Class entityClass) {
132 return newListView(entityClass, invoker);
133 }
134
135
136
137
138
139
140
141 public EntityEditView showDetailEditView(
142 Object invoker,
143 Class entityClass,
144 Serializable entityKey) {
145
146 final EntityService finder =
147 getEntityServiceManager().getEntityService(entityClass);
148 return showDetailEditView(invoker, entityClass, entityKey, finder);
149 }
150
151
152
153
154
155
156
157
158 public EntityEditView showDetailEditView(
159 Object invoker,
160 Class entityClass,
161 Serializable entityKey,
162 EntityService finder) {
163 final EntityEditView editView = getDetailEditView(invoker, entityClass);
164 editView.setService(finder);
165 editView.load(entityKey);
166 showView(editView, invoker);
167 return editView;
168 }
169
170
171
172
173
174
175
176 public EntityEditView showDetailEditView(Object invoker, Object entity) {
177 final EntityEditView editView =
178 getDetailEditView(invoker, entity.getClass());
179 editView.setService(null);
180 if (entity instanceof Entity)
181 editView.setEntity((Entity) entity);
182 editView.loadFrom(entity);
183 showView(editView, invoker);
184 return editView;
185 }
186
187
188
189
190
191
192
193
194 public EntityListView showSelectionListView(
195 Object invoker,
196 Class entityClass,
197 SearchCondition condition,
198 Serializable selectedEntityKey) {
199 final EntityService finder =
200 getEntityServiceManager().getEntityService(entityClass);
201 return showSelectionListView(
202 invoker,
203 entityClass,
204 condition,
205 selectedEntityKey,
206 finder);
207 }
208
209
210
211
212
213
214
215
216 public EntityListView showSelectionListView(
217 Object invoker,
218 Class entityClass,
219 SearchCondition condition,
220 Serializable selectedEntityKey,
221 EntityService finder) {
222 final EntityListView listView =
223 getSelectionListView(invoker, entityClass);
224 listView.setCondition(condition);
225 listView.setService(finder);
226 listView.refresh();
227 listView.setPointedKey(selectedEntityKey);
228 showView(listView, invoker);
229 return listView;
230 }
231
232
233
234
235
236
237
238 public EntityListView showSelectionListView(
239 Object invoker,
240 Class entityClass,
241 List entities,
242 Serializable selectedEntityKey) {
243 final EntityListView listView =
244 getSelectionListView(invoker, entityClass);
245 listView.setCondition(null);
246 listView.setService(null);
247 listView.load(entities);
248 listView.setPointedKey(selectedEntityKey);
249 showView(listView, invoker);
250 return listView;
251 }
252
253 protected void showView(EntityView view, Object invoker) {
254 view.setInvoker(invoker);
255 if ((view instanceof Component) && (invoker instanceof Component)) {
256 SwingUtils.showComponent(
257 (Component) view,
258 (Component) invoker,
259 800,
260 600,
261 "",
262 true);
263 } else {
264 throw new EntityGuiRuntimeException(
265 view.getClass().getName()
266 + " can't be shown because it doesn't extends java.awt.Component.");
267 }
268 }
269 private S2Container container = null;
270
271 /***
272 * @return Returns the container.
273 */
274 public S2Container getContainer() {
275 return container;
276 }
277
278 /***
279 * @param container
280 * The container to set.
281 */
282 public void setContainer(S2Container container) {
283 this.container = container;
284 }
285 private EntityServiceManager entityServiceManager = null;
286
287 /***
288 * @return Returns the entityServiceManager.
289 */
290 public EntityServiceManager getEntityServiceManager() {
291 return entityServiceManager;
292 }
293
294 /***
295 * @param entityServiceManager
296 * The entityServiceManager to set.
297 */
298 public void setEntityServiceManager(EntityServiceManager entityServiceManager) {
299 this.entityServiceManager = entityServiceManager;
300 }
301 }