1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.binding.value.binder; |
17 | |
|
18 | |
import java.lang.reflect.Array; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.eclipse.jface.viewers.IBaseLabelProvider; |
23 | |
import org.eclipse.jface.viewers.IContentProvider; |
24 | |
import org.eclipse.jface.viewers.IStructuredSelection; |
25 | |
import org.eclipse.jface.viewers.StructuredSelection; |
26 | |
import org.eclipse.jface.viewers.StructuredViewer; |
27 | |
import org.eclipse.jface.viewers.Viewer; |
28 | |
import org.seasar.framework.beans.PropertyDesc; |
29 | |
import org.seasar.uruma.binding.value.ValueBinder; |
30 | |
import org.seasar.uruma.component.UIComponent; |
31 | |
import org.seasar.uruma.core.UrumaConstants; |
32 | |
import org.seasar.uruma.core.UrumaMessageCodes; |
33 | |
import org.seasar.uruma.exception.BindingException; |
34 | |
import org.seasar.uruma.log.UrumaLogger; |
35 | |
import org.seasar.uruma.util.AssertionUtil; |
36 | |
import org.seasar.uruma.viewer.ContentsHolder; |
37 | |
import org.seasar.uruma.viewer.TargetClassHoldingProvider; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public abstract class AbstractValueBinder<WIDGET_TYPE> implements ValueBinder { |
47 | 40 | private UrumaLogger logger = UrumaLogger.getLogger(getClass()); |
48 | |
|
49 | |
private Class<WIDGET_TYPE> widgetType; |
50 | |
|
51 | |
protected static final String IMPORT_VALUE = "[ImportValue]"; |
52 | |
|
53 | |
protected static final String EXPORT_VALUE = "[ExportValue]"; |
54 | |
|
55 | |
protected static final String IMPORT_SELECTION = "[ImportSelection]"; |
56 | |
|
57 | |
protected static final String EXPORT_SELECTION = "[ExportSelection]"; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 40 | public AbstractValueBinder(final Class<WIDGET_TYPE> widgetType) { |
66 | 40 | AssertionUtil.assertNotNull("widgetType", widgetType); |
67 | 40 | this.widgetType = widgetType; |
68 | 40 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void importValue(final Object widget, final Object formObj, |
75 | |
final PropertyDesc propDesc, final UIComponent uiComp) { |
76 | 0 | doImportValue(getWidgetType().cast(widget), formObj, propDesc, uiComp); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void exportValue(final Object widget, final Object formObj, |
84 | |
final PropertyDesc propDesc, final UIComponent uiComp) { |
85 | 32 | doExportValue(getWidgetType().cast(widget), formObj, propDesc, uiComp); |
86 | 32 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void importSelection(final Object widget, final Object formObj, |
93 | |
final PropertyDesc propDesc, final UIComponent uiComp) { |
94 | 0 | doImportSelection(getWidgetType().cast(widget), formObj, propDesc, |
95 | |
uiComp); |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void exportSelection(final Object widget, final Object formObj, |
103 | |
final PropertyDesc propDesc, final UIComponent uiComp) { |
104 | 4 | doExportSelection(getWidgetType().cast(widget), formObj, propDesc, |
105 | |
uiComp); |
106 | 4 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public Class<WIDGET_TYPE> getWidgetType() { |
112 | 76 | return this.widgetType; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected void doImportValue(final WIDGET_TYPE widget, |
130 | |
final Object formObj, final PropertyDesc propDesc, |
131 | |
final UIComponent uiComp) { |
132 | |
|
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
protected void doExportValue(final WIDGET_TYPE widget, |
152 | |
final Object formObj, final PropertyDesc propDesc, |
153 | |
final UIComponent uiComp) { |
154 | 20 | if (widget instanceof StructuredViewer) { |
155 | 20 | StructuredViewer viewer = StructuredViewer.class.cast(widget); |
156 | |
|
157 | 20 | Class<?> formFieldType = propDesc.getField().getType(); |
158 | 20 | Object contents = propDesc.getValue(formObj); |
159 | 20 | IContentProvider contentProvider = viewer.getContentProvider(); |
160 | 20 | IBaseLabelProvider labelProvider = viewer.getLabelProvider(); |
161 | |
|
162 | 20 | if (contentProvider != null |
163 | |
&& contentProvider instanceof ContentsHolder) { |
164 | 20 | ContentsHolder holder = (ContentsHolder) contentProvider; |
165 | 20 | if (formFieldType.isArray()) { |
166 | 4 | if (contents != null) { |
167 | 4 | holder.setContents((Object[]) contents); |
168 | |
} else { |
169 | 0 | holder.setContents(new Object[] {}); |
170 | |
} |
171 | 4 | setClassToProvider(labelProvider, formFieldType |
172 | |
.getComponentType()); |
173 | 16 | } else if (List.class.isAssignableFrom(formFieldType)) { |
174 | 12 | List<?> listContents = (List<?>) contents; |
175 | |
|
176 | 12 | if ((listContents != null) && (listContents.size() > 0)) { |
177 | 12 | holder.setContents(listContents); |
178 | |
|
179 | 12 | Object content = listContents.get(0); |
180 | 12 | setClassToProvider(labelProvider, content.getClass()); |
181 | 12 | } else { |
182 | 0 | holder.setContents(new ArrayList<Object>()); |
183 | 0 | setClassToProvider(labelProvider, Object.class); |
184 | |
} |
185 | 12 | } else { |
186 | 4 | if (contents != null) { |
187 | 4 | holder.setContents(new Object[] { contents }); |
188 | 4 | setClassToProvider(labelProvider, contents.getClass()); |
189 | |
} else { |
190 | 0 | holder.setContents(new Object[] {}); |
191 | 0 | setClassToProvider(labelProvider, Object.class); |
192 | |
} |
193 | |
} |
194 | |
|
195 | 20 | logBinding(EXPORT_VALUE, formObj, propDesc, widget, null, |
196 | |
contents); |
197 | |
|
198 | 20 | viewer.setInput(contents); |
199 | |
} |
200 | |
} |
201 | 20 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
protected void doImportSelection(final WIDGET_TYPE widget, |
221 | |
final Object formObj, final PropertyDesc propDesc, |
222 | |
final UIComponent uiComp) { |
223 | 0 | if (widget instanceof Viewer) { |
224 | 0 | Viewer viewer = Viewer.class.cast(widget); |
225 | |
|
226 | 0 | IStructuredSelection selection = (IStructuredSelection) viewer |
227 | |
.getSelection(); |
228 | 0 | int size = selection.size(); |
229 | 0 | if (size > 0) { |
230 | 0 | Class<?> propertyType = propDesc.getPropertyType(); |
231 | 0 | Object firstElement = selection.getFirstElement(); |
232 | 0 | if (propertyType.isArray()) { |
233 | 0 | Object[] selectedArray = selection.toArray(); |
234 | 0 | Object content = Array.newInstance(propertyType |
235 | |
.getComponentType(), selectedArray.length); |
236 | 0 | System.arraycopy(selectedArray, 0, content, 0, |
237 | |
selectedArray.length); |
238 | |
|
239 | 0 | logBinding(IMPORT_SELECTION, widget, null, formObj, |
240 | |
propDesc, content); |
241 | 0 | propDesc.setValue(formObj, content); |
242 | 0 | } else if (propertyType.isAssignableFrom(List.class)) { |
243 | 0 | List<?> list = selection.toList(); |
244 | 0 | logBinding(IMPORT_SELECTION, widget, null, formObj, |
245 | |
propDesc, list); |
246 | 0 | propDesc.setValue(formObj, list); |
247 | 0 | } else if (propertyType.isAssignableFrom(firstElement |
248 | |
.getClass())) { |
249 | 0 | logBinding(IMPORT_SELECTION, widget, null, formObj, |
250 | |
propDesc, firstElement); |
251 | 0 | propDesc.setValue(formObj, firstElement); |
252 | |
} else { |
253 | 0 | throw new BindingException( |
254 | |
UrumaMessageCodes.CLASS_NOT_MUTCH, formObj |
255 | |
.getClass().getName(), propDesc |
256 | |
.getPropertyName()); |
257 | |
} |
258 | |
} |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
protected void doExportSelection(final WIDGET_TYPE widget, |
279 | |
final Object formObj, final PropertyDesc propDesc, |
280 | |
final UIComponent uiComp) { |
281 | 4 | if (widget instanceof Viewer) { |
282 | 4 | Viewer viewer = Viewer.class.cast(widget); |
283 | 4 | Object selection = propDesc.getValue(formObj); |
284 | 4 | if (selection != null) { |
285 | 4 | logBinding(EXPORT_SELECTION, formObj, propDesc, viewer, null, |
286 | |
selection); |
287 | |
|
288 | 4 | viewer.setSelection(new StructuredSelection(selection), true); |
289 | |
} |
290 | |
} |
291 | 4 | } |
292 | |
|
293 | |
protected void setClassToProvider(final IBaseLabelProvider provider, |
294 | |
final Class<?> clazz) { |
295 | 20 | if ((provider != null) |
296 | |
&& (provider instanceof TargetClassHoldingProvider)) { |
297 | 20 | TargetClassHoldingProvider.class.cast(provider).setTargetClass( |
298 | |
clazz); |
299 | |
} |
300 | 20 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
protected void logBinding(final String command, final Object srcObj, |
319 | |
final PropertyDesc srcProp, final Object destObj, |
320 | |
final PropertyDesc destProp, final Object value) { |
321 | 36 | if (logger.isInfoEnabled()) { |
322 | 0 | String srcName = UrumaConstants.NULL_STRING; |
323 | 0 | if (srcProp != null) { |
324 | 0 | srcName = srcProp.getPropertyName(); |
325 | |
} |
326 | 0 | String destName = UrumaConstants.NULL_STRING; |
327 | 0 | if (destProp != null) { |
328 | 0 | destName = destProp.getPropertyName(); |
329 | |
} |
330 | 0 | logger.log(UrumaMessageCodes.DO_BINDING, command, UrumaLogger |
331 | |
.getObjectDescription(srcObj), srcName, UrumaLogger |
332 | |
.getObjectDescription(destObj), destName, value); |
333 | |
} |
334 | 36 | } |
335 | |
} |