1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.core;
17
18 import org.eclipse.jface.viewers.ContentViewer;
19 import org.eclipse.jface.viewers.IBaseLabelProvider;
20 import org.eclipse.jface.viewers.IContentProvider;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.StructuredViewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24 import org.seasar.framework.beans.BeanDesc;
25 import org.seasar.framework.beans.PropertyDesc;
26 import org.seasar.framework.container.ComponentDef;
27 import org.seasar.framework.container.S2Container;
28 import org.seasar.framework.util.StringUtil;
29 import org.seasar.uruma.annotation.Form;
30 import org.seasar.uruma.binding.context.ApplicationContextBinder;
31 import org.seasar.uruma.binding.widget.WidgetBinder;
32 import org.seasar.uruma.context.PartContext;
33 import org.seasar.uruma.context.WindowContext;
34 import org.seasar.uruma.desc.FormDesc;
35 import org.seasar.uruma.desc.FormDescFactory;
36 import org.seasar.uruma.desc.PartActionDesc;
37 import org.seasar.uruma.desc.PartActionDescFactory;
38 import org.seasar.uruma.exception.RenderException;
39 import org.seasar.uruma.log.UrumaLogger;
40 import org.seasar.uruma.util.AssertionUtil;
41 import org.seasar.uruma.util.ClassUtil;
42 import org.seasar.uruma.util.S2ContainerUtil;
43 import org.seasar.uruma.viewer.PojoLabelProvider;
44
45
46
47
48
49
50 public class ComponentUtil implements UrumaConstants, UrumaMessageCodes {
51 private static final UrumaLogger logger = UrumaLogger
52 .getLogger(ComponentUtil.class);
53
54 private static S2Container defaultContainer;
55
56 private ComponentUtil() {
57 }
58
59
60
61
62
63
64
65
66 public static void setS2Container(final S2Container container) {
67 AssertionUtil.assertNotNull("container", container);
68
69 defaultContainer = container;
70 }
71
72
73
74
75
76
77
78
79
80 public static Object setupWorkbenchAction(final WindowContext context,
81 final String id) {
82 String actionComponentName = StringUtil.decapitalize(id)
83 + PART_ACTION_SUFFIX;
84
85 ComponentDef cd = S2ContainerUtil
86 .getComponentDefNoException(actionComponentName);
87
88 Object workbenchActionComponent = S2ContainerUtil
89 .getComponentNoException(actionComponentName, defaultContainer);
90 if (workbenchActionComponent != null) {
91 S2ContainerUtil.injectDependency(workbenchActionComponent,
92 defaultContainer);
93
94 context.setPartActionObject(workbenchActionComponent);
95 PartActionDesc desc = PartActionDescFactory.getPartActionDesc(cd
96 .getComponentClass());
97 context.setPartActionDesc(desc);
98
99 logger.log(WORKBENCH_ACTION_CLASS_FOUND, id, actionComponentName,
100 workbenchActionComponent.getClass().getName());
101
102 return workbenchActionComponent;
103 } else {
104 logger.log(WORKBENCH_ACTION_CLASS_NOT_FOUND, id,
105 actionComponentName);
106 return null;
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public static Object setupPartAction(final PartContext context,
123 final String id, final S2Container container) {
124 String actionComponentName = StringUtil.decapitalize(id)
125 + PART_ACTION_SUFFIX;
126
127 ComponentDef cd = S2ContainerUtil
128 .getComponentDefNoException(actionComponentName);
129
130 Object partActionComponent = null;
131 if (cd != null) {
132
133
134 cd.setContainer(container);
135 partActionComponent = cd.getComponent();
136
137 S2ContainerUtil.injectDependency(partActionComponent, container);
138
139 context.setPartActionObject(partActionComponent);
140 PartActionDesc desc = PartActionDescFactory.getPartActionDesc(cd
141 .getComponentClass());
142 context.setPartActionDesc(desc);
143
144 logger.log(PART_ACTION_CLASS_FOUND, id, actionComponentName,
145 partActionComponent.getClass().getName());
146
147 return partActionComponent;
148 } else {
149 logger.log(PART_ACTION_CLASS_NOT_FOUND, id, actionComponentName);
150 return null;
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public static Object setupPartAction(final PartContext context,
165 final String id) {
166 return setupPartAction(context, id, defaultContainer);
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public static Object setupFormComponent(final PartContext context,
188 final String id) {
189 Object formObject = null;
190 FormDesc formDesc = null;
191 Class<?> formClass = null;
192 Object actionObject = context.getPartActionObject();
193 if (actionObject != null) {
194 Form formAnnotation = context.getPartActionDesc()
195 .getPartActionClass().getAnnotation(Form.class);
196
197 if (formAnnotation != null) {
198 formClass = formAnnotation.value();
199 if (formClass == actionObject.getClass()) {
200 formObject = actionObject;
201 } else {
202 formObject = S2ContainerUtil.getComponent(formClass,
203 defaultContainer);
204 }
205 }
206
207
208 if (formObject == null) {
209 String formComponentName = StringUtil.decapitalize(id)
210 + FORM_SUFFIX;
211 formObject = S2ContainerUtil.getComponentNoException(
212 formComponentName, defaultContainer);
213 if (formObject != null) {
214
215 ComponentDef cd = S2ContainerUtil
216 .getComponentDef(formComponentName);
217 formClass = cd.getComponentClass();
218 } else {
219
220 formObject = actionObject;
221 formClass = formObject.getClass();
222 }
223 }
224
225 formDesc = FormDescFactory.getFormDesc(formClass);
226 }
227
228 if (formObject != null && formDesc != null) {
229 logger.log(FORM_CLASS_FOUND, id, formObject.getClass().getName());
230
231 context.setFormDesc(formDesc);
232 context.setFormObject(formObject);
233 injectFormToAction(context);
234 return formObject;
235 } else {
236 logger.log(FORM_CLASS_NOT_FOUND, context.getName(), id);
237 return null;
238 }
239 }
240
241
242
243
244
245 protected static void injectFormToAction(final PartContext context) {
246 Object partActionObject = context.getPartActionObject();
247 Object formObject = context.getFormObject();
248
249 String formObjectName = formObject.getClass().getSimpleName();
250 BeanDesc beanDesc = context.getPartActionDesc().getBeanDesc();
251 if (beanDesc.hasPropertyDesc(formObjectName)) {
252 PropertyDesc pd = beanDesc.getPropertyDesc(formObjectName);
253 pd.setValue(partActionObject, formObject);
254 }
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public static void setupContentProvider(final ContentViewer viewer,
282 final String id, final IContentProvider defaultProvider) {
283 IContentProvider provider = null;
284 if (!StringUtil.isEmpty(id)) {
285 Object defined = S2ContainerUtil.getComponentNoException(id
286 + CONTENT_PROVIDER_SUFFIX, defaultContainer);
287 if (defined != null) {
288 if (defined instanceof IContentProvider) {
289 provider = IContentProvider.class.cast(defined);
290 } else {
291 throw new RenderException(UNSUPPORTED_TYPE_ERROR, provider,
292 IContentProvider.class.getName());
293 }
294 }
295 }
296
297 if (provider == null) {
298 provider = defaultProvider;
299 }
300
301 if (provider != null) {
302 viewer.setContentProvider(provider);
303
304 logger.log(CONTENT_PROVIDER_FOUND, id, provider.getClass());
305 }
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static void setupLabelProvider(final StructuredViewer viewer,
339 final String id, final IBaseLabelProvider defaultProvider,
340 final Class<? extends IBaseLabelProvider> providerClass,
341 final Class<? extends PojoLabelProvider> pojoProviderClass) {
342 IBaseLabelProvider provider = null;
343 if (!StringUtil.isEmpty(id)) {
344 Object defined = S2ContainerUtil.getComponentNoException(id
345 + LABEL_PROVIDER_SUFFIX, defaultContainer);
346 if (defined != null) {
347 if (providerClass.isAssignableFrom(defined.getClass())) {
348 provider = providerClass.cast(defined);
349 } else {
350 AssertionUtil.assertNotNull("pojoProviderClass",
351 pojoProviderClass);
352 PojoLabelProvider pojoProvider = ClassUtil
353 .<PojoLabelProvider> newInstance(pojoProviderClass);
354 pojoProvider.setPojo(defined);
355 provider = pojoProvider;
356 }
357 }
358 }
359
360 if (provider == null) {
361 provider = defaultProvider;
362 }
363
364 if (provider != null) {
365 viewer.setLabelProvider(provider);
366
367 logger.log(LABEL_PROVIDER_FOUND, id, provider.getClass());
368 }
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 public static void setupComparator(final StructuredViewer viewer,
394 final String id, final ViewerComparator defaultComparator) {
395 if (!StringUtil.isEmpty(id)) {
396 Object comparator = S2ContainerUtil.getComponentNoException(id
397 + SORTER_SUFFIX, defaultContainer);
398 if (comparator != null) {
399 if (comparator instanceof ViewerComparator) {
400 viewer.setComparator(ViewerComparator.class
401 .cast(comparator));
402
403 logger.log(COMPARATOR_FOUND, id, comparator.getClass());
404 } else {
405 throw new RenderException(UNSUPPORTED_TYPE_ERROR,
406 comparator, ViewerComparator.class.getName());
407 }
408 } else {
409 viewer.setComparator(defaultComparator);
410
411 String className = "(null)";
412 if (defaultComparator != null) {
413 className = defaultComparator.getClass().getName();
414 logger.log(COMPARATOR_FOUND, id, className);
415 }
416 }
417 }
418 }
419
420
421
422
423
424
425
426 public static void invokeInitMethodOnAction(final WindowContext context) {
427 Object workbenchActionObject = context.getPartActionObject();
428 if (workbenchActionObject != null) {
429
430 PartActionDesc desc = context.getPartActionDesc();
431 ApplicationContextBinder.importObjects(workbenchActionObject, desc
432 .getApplicationContextDefList(), context
433 .getApplicationContext());
434
435 desc.invokeInitializeMethod(workbenchActionObject);
436 }
437 }
438
439
440
441
442
443
444
445
446
447 public static void invokeInitMethodOnAction(final Object partAction,
448 final PartContext context) {
449 if (partAction != null) {
450 WidgetBinder.bindWidgets(partAction, context);
451
452
453 PartActionDesc desc = context.getPartActionDesc();
454 ApplicationContextBinder.importObjects(partAction, desc
455 .getApplicationContextDefList(), context.getWindowContext()
456 .getApplicationContext());
457
458 desc.invokeInitializeMethod(partAction);
459 }
460 }
461
462
463
464
465
466
467
468
469
470 public static void invokePostOpenMethodOnAction(final Object partAction,
471 final PartContext context) {
472 if (partAction != null) {
473 WidgetBinder.bindWidgets(partAction, context);
474
475
476 PartActionDesc desc = context.getPartActionDesc();
477 ApplicationContextBinder.importObjects(partAction, desc
478 .getApplicationContextDefList(), context.getWindowContext()
479 .getApplicationContext());
480
481 desc.invokePostOpenMethod(partAction);
482 }
483 }
484 }