View Javadoc

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      private static final Log log = LogFactory.getLog(LayoutDataTag.class);
40  
41      public LayoutDataTag(Class layoutDataClass) {
42          super(layoutDataClass);
43      }
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          super.processBean(var, bean);
54  
55          Widget parent = getParentWidget();
56  
57          if (parent instanceof Control) {
58              Control control = (Control) parent;
59              control.setLayoutData(getBean());
60          } else {
61              throw new JellyTagException("This tag must be nested within a control widget tag");
62          }
63      }
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          String text = (String) attributes.remove("style");
75          if (text != null) {
76              int style = SwtHelper.parseStyle(theClass, text);
77  
78              // now lets try invoke a constructor
79              Class[] types = { int.class };
80  
81              try {
82                  Constructor constructor = theClass.getConstructor(types);
83                  if (constructor != null) {
84                      Object[] values = { new Integer(style)};
85                      return constructor.newInstance(values);
86                  }
87              } catch (NoSuchMethodException e) {
88                  throw new JellyTagException(e);
89              } catch (InstantiationException e) {
90                  throw new JellyTagException(e);
91              } catch (IllegalAccessException e) {
92                  throw new JellyTagException(e);
93              } catch (InvocationTargetException e) {
94                  throw new JellyTagException(e);
95              }
96          }
97          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         if (bean instanceof GridData) {
107             if (name.endsWith("Alignment") && value instanceof String) {
108                 int style =
109                     SwtHelper.parseStyle(bean.getClass(), (String) value);
110                 return new Integer(style);
111             }
112         }
113         return super.convertValue(bean, name, value);
114     }
115 
116 }