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.jface;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.XMLOutput;
20  import org.apache.commons.jelly.tags.jface.window.ApplicationWindowImpl;
21  import org.apache.commons.jelly.tags.jface.window.ApplicationWindowTag;
22  import org.apache.commons.jelly.tags.swt.ImageTag;
23  import org.eclipse.jface.window.Window;
24  import org.eclipse.swt.graphics.Image;
25  import org.eclipse.swt.widgets.Widget;
26  
27  /***
28   * Implementation of SWT ImageTag
29   *
30   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
31   */
32  public class JFaceImageTag extends ImageTag {
33  
34      /***
35       * @return the parent window
36       */
37      public Window getParentWindow() {
38          ApplicationWindowTag tag =
39              (ApplicationWindowTag) findAncestorWithClass(ApplicationWindowTag.class);
40          if (tag != null) {
41              return tag.getWindow();
42          }
43          return null;
44      }
45  
46      /***
47       * Set default image Window
48       * @param window
49       * @param image
50       */
51      private void setWindowImage(Window window, Image image) {
52          window.getShell().setImage(image);
53      }
54  
55      /*
56       * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
57       */
58      public void doTag(XMLOutput output) throws JellyTagException {
59  
60          // invoke by body just in case some nested tag configures me
61          invokeBody(output);
62  
63          Widget parent = getParentWidget();
64          Window window = null;
65          if (parent == null) {
66              window = getParentWindow();
67              if (window != null && window instanceof ApplicationWindowImpl) {
68                  parent = ((ApplicationWindowImpl) window).getContents();
69              }
70          }
71  
72          if (parent == null && window == null) {
73              throw new JellyTagException("This tag must be nested within a Widget or a Window");
74          }
75  
76          Image image = new Image(parent.getDisplay(), getSrc());
77          if (window != null) {
78              setWindowImage(window, image);
79          } else {
80              setWidgetImage(parent, image);
81          }
82  
83      }
84  
85  }