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.jelly.TagSupport;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.tags.swt.converters.ColorConverter;
22  import org.eclipse.swt.graphics.Color;
23  import org.eclipse.swt.widgets.Widget;
24  
25  /***
26   * Class to create a {@link Color} instance within Jelly SWT.
27   *
28   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
29   * @author Written with much help thanks to the ImageTag class
30   * @version CVS $Id: ColorTag.java 155420 2005-02-26 13:06:03Z dirkv $
31   */
32  public class ColorTag extends TagSupport {
33  
34      /*** RGB value */
35      private String rgb;
36  
37      /*** Variable name */
38      private String var;
39  
40      /***
41       * Sets the RGB value for this {@link Color} instance
42       *
43       * @param rgb value (eg. #666666);
44       */
45      public void setRgb(final String rgb) {
46          this.rgb = rgb;
47      }
48  
49      /***
50       * Obtain the RGB value for this {@link Color} instance
51       *
52       * @return the RGB value (eg. #666666)
53       */
54      public String getRgb() {
55          return this.rgb;
56      }
57  
58      /***
59       * Sets the variable name
60       *
61       * @param var the variable name of this {@link Color} instance
62       */
63      public void setVar(final String var) {
64          this.var = var;
65      }
66  
67      /***
68       * Obtain the variable name.
69       *
70       * @return the variable name of this {@link Color} instance
71       */
72      public String getVar() {
73          return this.var;
74      }
75  
76      /***
77       * @return the parent widget which this widget will be added to.
78       */
79      public Widget getParentWidget() {
80          final WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
81          if (tag != null) {
82              return tag.getWidget();
83          }
84          return null;
85      }
86  
87      // Tag interface
88      //-------------------------------------------------------------------------
89  
90      /***
91       * Creates a {@link Color} instance as defined by the RGB attribute.
92       * Stores this {@link Color} instance in the Context so that it can be
93       * referenced in the Jelly script.
94       *
95       * @param output {@link XMLOutput} reference
96       * @throws JellyTagException if an error occurs
97       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
98       */
99      public void doTag(final XMLOutput output) throws JellyTagException {
100         // invoke by body just in case some nested tag configures me
101         invokeBody(output);
102 
103         final Widget parent = getParentWidget();
104 
105         if (parent == null) {
106             throw new JellyTagException(
107                 "This tag must be nested within a Widget or a Window"
108             );
109         }
110 
111         if (var == null) {
112             throw new JellyTagException("This tag requires a context variable name");
113         }
114 
115         final Color color =
116             new Color(
117                 parent.getDisplay(),
118                 ColorConverter.getInstance().parse(getRgb())
119             );
120 
121         // store the Color in the context
122         context.setVariable(var, color);
123     }
124 }