%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.swt.LayoutTagSupport |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
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 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 | 0 | private static final Log log = LogFactory.getLog(LayoutTagSupport.class); |
41 | ||
42 | private String var; |
|
43 | ||
44 | public LayoutTagSupport(Class layoutClass) { |
|
45 | 0 | super(layoutClass); |
46 | 0 | } |
47 | ||
48 | // Properties |
|
49 | //------------------------------------------------------------------------- |
|
50 | ||
51 | /** |
|
52 | * @return the parent widget which this widget will be added to. |
|
53 | */ |
|
54 | public Widget getParentWidget() { |
|
55 | 0 | WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class); |
56 | 0 | if (tag != null) { |
57 | 0 | return tag.getWidget(); |
58 | } |
|
59 | 0 | 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 | 0 | this.var = class="keyword">var; |
69 | 0 | } |
70 | ||
71 | // Implementation methods |
|
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 | 0 | if (var != null) { |
78 | 0 | context.setVariable(var, bean); |
79 | } |
|
80 | 0 | } |
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 | 0 | if (bean != null) { |
88 | 0 | Class theClass = bean.getClass(); |
89 | 0 | for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) { |
90 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
91 | 0 | String name = (String) entry.getKey(); |
92 | 0 | Object value = entry.getValue(); |
93 | ||
94 | 0 | value = convertValue(bean, name, value); |
95 | ||
96 | try { |
|
97 | // lets first see if there's a field available |
|
98 | 0 | Field field = theClass.getField(name); |
99 | 0 | if (field != null) { |
100 | 0 | if (value instanceof String) { |
101 | 0 | value = ConvertUtils.convert((String) value, field.getType()); |
102 | } |
|
103 | 0 | field.set(bean, value); |
104 | } else { |
|
105 | 0 | BeanUtils.setProperty(bean, name, value); |
106 | } |
|
107 | 0 | } catch (NoSuchFieldException e) { |
108 | 0 | throw new JellyTagException(e); |
109 | 0 | } catch (IllegalAccessException e) { |
110 | 0 | throw new JellyTagException(e); |
111 | 0 | } catch (InvocationTargetException e) { |
112 | 0 | throw new JellyTagException(e); |
113 | 0 | } |
114 | } |
|
115 | } |
|
116 | 0 | } |
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 | 0 | return value; |
130 | } |
|
131 | ||
132 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |