%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.swt.LayoutDataTag |
|
|
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.Constructor; |
|
19 | import java.lang.reflect.InvocationTargetException; |
|
20 | import java.util.Map; |
|
21 | ||
22 | import org.apache.commons.jelly.JellyTagException; |
|
23 | import org.apache.commons.jelly.XMLOutput; |
|
24 | import org.apache.commons.logging.Log; |
|
25 | import org.apache.commons.logging.LogFactory; |
|
26 | import org.eclipse.swt.layout.GridData; |
|
27 | import org.eclipse.swt.widgets.Control; |
|
28 | import org.eclipse.swt.widgets.Widget; |
|
29 | ||
30 | /** |
|
31 | * Creates a LayoutData object and sets it on the parent Widget. |
|
32 | * |
|
33 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
34 | * @version 1.1 |
|
35 | */ |
|
36 | public class LayoutDataTag extends LayoutTagSupport { |
|
37 | ||
38 | /** The Log to which logging calls will be made. */ |
|
39 | 0 | private static final Log log = LogFactory.getLog(LayoutDataTag.class); |
40 | ||
41 | public LayoutDataTag(Class layoutDataClass) { |
|
42 | 0 | super(layoutDataClass); |
43 | 0 | } |
44 | ||
45 | // Implementation methods |
|
46 | //------------------------------------------------------------------------- |
|
47 | ||
48 | /** |
|
49 | * Either defines a variable or adds the current component to the parent |
|
50 | */ |
|
51 | protected void processBean(String var, Object bean) |
|
52 | throws JellyTagException { |
|
53 | 0 | super.processBean(var, bean); |
54 | ||
55 | 0 | Widget parent = getParentWidget(); |
56 | ||
57 | 0 | if (parent instanceof Control) { |
58 | 0 | Control control = (Control) parent; |
59 | 0 | control.setLayoutData(getBean()); |
60 | } else { |
|
61 | 0 | throw new JellyTagException("This tag must be nested within a control widget tag"); |
62 | } |
|
63 | 0 | } |
64 | ||
65 | /** |
|
66 | * @see org.apache.commons.jelly.tags.core.UseBeanTag#newInstance(java.lang.Class, java.util.Map, org.apache.commons.jelly.XMLOutput) |
|
67 | */ |
|
68 | protected Object newInstance( |
|
69 | Class theClass, |
|
70 | Map attributes, |
|
71 | XMLOutput output) |
|
72 | throws JellyTagException { |
|
73 | ||
74 | 0 | String text = (String) attributes.remove("style"); |
75 | 0 | if (text != null) { |
76 | 0 | int style = SwtHelper.parseStyle(theClass, text); |
77 | ||
78 | // now lets try invoke a constructor |
|
79 | 0 | Class[] types = { int.class }; |
80 | ||
81 | try { |
|
82 | 0 | Constructor constructor = theClass.getConstructor(types); |
83 | 0 | if (constructor != null) { |
84 | 0 | Object[] values = { new Integer(style)}; |
85 | 0 | return constructor.newInstance(values); |
86 | } |
|
87 | 0 | } catch (NoSuchMethodException e) { |
88 | 0 | throw new JellyTagException(e); |
89 | 0 | } catch (InstantiationException e) { |
90 | 0 | throw new JellyTagException(e); |
91 | 0 | } catch (IllegalAccessException e) { |
92 | 0 | throw new JellyTagException(e); |
93 | 0 | } catch (InvocationTargetException e) { |
94 | 0 | throw new JellyTagException(e); |
95 | 0 | } |
96 | } |
|
97 | 0 | return super.newInstance(theClass, attributes, output); |
98 | } |
|
99 | ||
100 | /** |
|
101 | * @see org.apache.commons.jelly.tags.swt.LayoutTagSupport#convertValue(java.lang.Object, java.lang.String, java.lang.Object) |
|
102 | */ |
|
103 | protected Object convertValue(Object bean, String name, Object value) |
|
104 | throws JellyTagException { |
|
105 | ||
106 | 0 | if (bean instanceof GridData) { |
107 | 0 | if (name.endsWith("Alignment") && value instanceof String) { |
108 | 0 | int style = |
109 | SwtHelper.parseStyle(bean.getClass(), (String) value); |
|
110 | 0 | return new Integer(style); |
111 | } |
|
112 | } |
|
113 | 0 | return super.convertValue(bean, name, value); |
114 | } |
|
115 | ||
116 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |