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.eclipse.swt.graphics.Drawable;
22  import org.eclipse.swt.graphics.GC;
23  
24  /***
25   * Class to create a {@link GC} instance within Jelly SWT.
26   *
27   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
28   * @version CVS $Id: GCTag.java 155420 2005-02-26 13:06:03Z dirkv $
29   */
30  public class GCTag extends TagSupport {
31  
32      /*** Drawable name */
33      private Drawable drawable;
34  
35      /*** Variable name */
36      private String var;
37  
38      /***
39       * Obtain the {@link Drawable} name for this {@link GC}.
40       *
41       * @return a {@link GC} {@link Drawable}
42       */
43      public Drawable getDrawable() {
44          return this.drawable;
45      }
46  
47      /***
48       * Set the {@link Drawable} name for this {@link GC}.
49       *
50       * @param drawable a {@link GC} {@link Drawable}
51       */
52      public void setDrawable(final Drawable drawable) {
53          this.drawable = drawable;
54      }
55  
56      /***
57       * Sets the variable name.
58       *
59       * @param var the variable name of this {@link GC} instance
60       */
61      public void setVar(final String var) {
62          this.var = var;
63      }
64  
65      /***
66       * Obtain the variable name.
67       *
68       * @return the variable name of this {@link GC} instance
69       */
70      public String getVar() {
71          return this.var;
72      }
73  
74      // Tag interface
75      //-------------------------------------------------------------------------
76  
77      /***
78       * Creates a {@link GC} instance and stores it in the Context under a
79       * particular variable name. Note, {@link GC} objects can only be created on
80       * {@link Drawable} objects.
81       *
82       * @param output {@link XMLOutput} reference
83       * @throws JellyTagException if an error occurs
84       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
85       */
86      public void doTag(final XMLOutput output) throws JellyTagException {
87          // invoke by body just in case some nested tag configures me
88          invokeBody(output);
89  
90          final boolean nullDrawable = drawable == null;
91          final boolean drawableParent = drawable instanceof Drawable;
92  
93          if (nullDrawable || !drawableParent) {
94              throw new JellyTagException(
95                  "This tag must specify a Drawable attribute (ie. Image or Control)"
96              );
97          }
98  
99          if (var == null) {
100             throw new JellyTagException("This tag requires a context variable name");
101         }
102 
103         // store the GC in the context
104         context.setVariable(var, new GC(drawable));
105     }
106 }