1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swt;
17
18 import java.lang.reflect.Field;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.apache.commons.beanutils.ConvertUtils;
25 import org.apache.commons.jelly.JellyTagException;
26 import org.apache.commons.jelly.tags.core.UseBeanTag;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.eclipse.swt.widgets.Widget;
30
31 /***
32 * An abstract base class for Layout or LayoutData tags.
33 *
34 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35 * @version 1.1
36 */
37 public abstract class LayoutTagSupport extends UseBeanTag {
38
39 /*** The Log to which logging calls will be made. */
40 private static final Log log = LogFactory.getLog(LayoutTagSupport.class);
41
42 private String var;
43
44 public LayoutTagSupport(Class layoutClass) {
45 super(layoutClass);
46 }
47
48
49
50
51 /***
52 * @return the parent widget which this widget will be added to.
53 */
54 public Widget getParentWidget() {
55 WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
56 if (tag != null) {
57 return tag.getWidget();
58 }
59 return null;
60 }
61
62 /***
63 * Sets the name of the variable to use to expose the new Layout object.
64 * If this attribute is not set then the parent widget tag will have its
65 * layout property set.
66 */
67 public void setVar(String var) {
68 this.var = var;
69 }
70
71
72
73 /***
74 * Either defines a variable or adds the current component to the parent
75 */
76 protected void processBean(String var, Object bean) throws JellyTagException {
77 if (var != null) {
78 context.setVariable(var, bean);
79 }
80 }
81
82 /***
83 * @see org.apache.commons.jelly.tags.core.UseBeanTag#setBeanProperties(java.lang.Object, java.util.Map)
84 */
85 protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException {
86
87 if (bean != null) {
88 Class theClass = bean.getClass();
89 for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
90 Map.Entry entry = (Map.Entry) iter.next();
91 String name = (String) entry.getKey();
92 Object value = entry.getValue();
93
94 value = convertValue(bean, name, value);
95
96 try {
97
98 Field field = theClass.getField(name);
99 if (field != null) {
100 if (value instanceof String) {
101 value = ConvertUtils.convert((String) value, field.getType());
102 }
103 field.set(bean, value);
104 } else {
105 BeanUtils.setProperty(bean, name, value);
106 }
107 } catch (NoSuchFieldException e) {
108 throw new JellyTagException(e);
109 } catch (IllegalAccessException e) {
110 throw new JellyTagException(e);
111 } catch (InvocationTargetException e) {
112 throw new JellyTagException(e);
113 }
114 }
115 }
116 }
117
118 /***
119 * Provides a strategy method that allows values to be converted,
120 * particularly to support integer enumerations and String representations.
121 *
122 * @param bean is the bean on which the property is to be set
123 * @param name is the name of the property
124 * @param value the value of the property
125 * @return the new value
126 */
127 protected Object convertValue(Object bean, String name, Object value)
128 throws JellyTagException {
129 return value;
130 }
131
132 }