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 org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.eclipse.swt.SWT;
22  import org.eclipse.swt.layout.FillLayout;
23  import org.eclipse.swt.widgets.Composite;
24  import org.eclipse.swt.widgets.Layout;
25  import org.eclipse.swt.widgets.Widget;
26  
27  /***
28   * Creates a new Layout implementations and adds it to the parent Widget.
29   *
30   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31   * @version 1.1
32   */
33  public class LayoutTag extends LayoutTagSupport {
34  
35      /*** The Log to which logging calls will be made. */
36      private static final Log log = LogFactory.getLog(LayoutTag.class);
37  
38      public LayoutTag(Class layoutClass) {
39          super(layoutClass);
40      }
41  
42      // Properties
43      //-------------------------------------------------------------------------
44  
45      /***
46       * @return the Layout if there is one otherwise null
47       */
48      public Layout getLayout() {
49          Object bean = getBean();
50          if (bean instanceof Layout) {
51              return (Layout) bean;
52          }
53          return null;
54      }
55  
56      // Implementation methods
57      //-------------------------------------------------------------------------
58  
59      /***
60       * Either defines a variable or adds the current component to the parent
61       */
62      protected void processBean(String var, Object bean)
63          throws JellyTagException {
64          super.processBean(var, bean);
65  
66          Widget parent = getParentWidget();
67  
68          if (parent instanceof Composite) {
69              Composite composite = (Composite) parent;
70              composite.setLayout(getLayout());
71  
72          } else {
73              throw new JellyTagException("This tag must be nested within a composite widget tag");
74          }
75      }
76  
77      /***
78       * @see org.apache.commons.jelly.tags.swt.LayoutTagSupport#convertValue(java.lang.Object, java.lang.String, java.lang.Object)
79       */
80      protected Object convertValue(Object bean, String name, Object value)
81          throws JellyTagException {
82  
83          if (bean instanceof FillLayout
84              && name.equals("type")
85              && value instanceof String) {
86              int style = SwtHelper.parseStyle(SWT.class, (String) value);
87              return new Integer(style);
88          }
89          return super.convertValue(bean, name, value);
90      }
91  
92  }