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.io.InputStream;
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  import org.apache.commons.jelly.util.ClassLoaderUtils;
23  import org.eclipse.swt.graphics.Image;
24  import org.eclipse.swt.widgets.Button;
25  import org.eclipse.swt.widgets.Decorations;
26  import org.eclipse.swt.widgets.Display;
27  import org.eclipse.swt.widgets.Item;
28  import org.eclipse.swt.widgets.Label;
29  import org.eclipse.swt.widgets.Widget;
30  
31  /***
32   * This creates an image on the parent Widget.
33   *
34   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
35   * @version CVS ImageTag.java,v 1.5 2004/09/07 02:41:40 dion Exp
36   */
37  public class ImageTag extends TagSupport {
38  
39      /*** path to file */
40      private String src;
41  
42      /*** variable name, if specified */
43      private String var;
44  
45      /*** resource name, if specified */
46      private String resource;
47  
48      /***
49       * Sets the resource
50       * @param resource image resource location
51       */
52      public void setResource(String resource) {
53         this.resource = resource;
54      }
55  
56      /***
57       * Obtains the resource
58       * @return the image resource
59       */
60      public String getResource() {
61          return resource;
62      }
63  
64      /***
65       * Sets the variable name
66       */
67      public void setVar(String var) {
68          this.var = var;
69      }
70  
71      /***
72       * Obtain the variable name.
73       * @return String the variable name
74       */
75      public String getVar() {
76        return this.var;
77      }
78  
79      /***
80       * Sets the src.
81       * @param src The src to set
82       */
83      public void setSrc(String src) {
84          this.src = src;
85      }
86  
87      /***
88       * Method getSrc.
89       * @return String
90       */
91      public String getSrc() {
92          return src;
93      }
94  
95      /***
96       * @return the parent widget which this widget will be added to.
97       */
98      public Widget getParentWidget() {
99          WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
100         if (tag != null) {
101             return tag.getWidget();
102         }
103         return null;
104     }
105 
106     // Tag interface
107     //-------------------------------------------------------------------------
108 
109     /***
110      * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
111      */
112     public void doTag(XMLOutput output) throws JellyTagException {
113         // invoke by body just in case some nested tag configures me
114         invokeBody(output);
115 
116         Widget parent = getParentWidget();
117 
118         if (parent == null) {
119             throw new JellyTagException("This tag must be nested within a Widget or a Window");
120         }
121 
122         Image image = null;
123 
124         if (getSrc() != null) {
125            image = loadLocalImage(parent.getDisplay());
126         } else if (getResource() != null) {
127            image = loadResourceImage(parent.getDisplay());
128         } else {
129             throw new JellyTagException("Either an image location or a resource must be specified");
130         }
131 
132         setWidgetImage(parent, image);
133 
134         // store the image as a context variable if specified
135         if (var != null) {
136             context.setVariable(var, image);
137         }
138     }
139 
140     /***
141      * Creates an Image, loaded from the local disk
142      */
143     private Image loadLocalImage(Display display) {
144         return new Image(display, getSrc());
145     }
146 
147     /***
148      * Creates an Image, loaded from a specified resource.
149      */
150     private Image loadResourceImage(Display display) {
151         ClassLoader loader = ClassLoaderUtils.getClassLoader(null, getContext().getUseContextClassLoader(), getClass());
152         InputStream stream = loader.getResourceAsStream(getResource());
153         return new Image(display, stream);
154     }
155 
156     /***
157      * Add image to a widget
158      * @param parent
159      * @param image
160      * @throws JellyTagException
161      */
162     protected void setWidgetImage(Widget parent, Image image) throws JellyTagException {
163         if (parent instanceof Label) {
164             Label label = (Label) parent;
165             label.setImage(image);
166 
167         } else if (parent instanceof Button) {
168             Button button = (Button) parent;
169             button.setImage(image);
170 
171         } else if (parent instanceof Item) {
172             Item item = (Item) parent;
173             item.setImage(image);
174 
175         } else if (parent instanceof Decorations) {
176             Decorations item = (Decorations) parent;
177             item.setImage(image);
178 
179         } else {
180             throw new JellyTagException("This tag must be nested inside a <label>, <button>, <shell> or <item> tag");
181         }
182     }
183 }