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.Tag;
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.jface.wizard.WizardPageTag;
23  import org.apache.commons.jelly.tags.swt.WidgetTag;
24  import org.eclipse.jface.window.Window;
25  import org.eclipse.swt.widgets.Composite;
26  import org.eclipse.swt.widgets.Widget;
27  
28  /***
29   * Implementation of SWT WidgetTag
30   *
31   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
32   */
33  public class JFaceWidgetTag extends WidgetTag implements Tag {
34  
35      /***
36       * @param widgetClass
37       */
38      public JFaceWidgetTag(Class widgetClass) {
39          super(widgetClass);
40      }
41  
42      /***
43       * @param widgetClass
44       * @param style
45       */
46      public JFaceWidgetTag(Class widgetClass, int style) {
47          super(widgetClass, style);
48      }
49  
50      /*
51       * @see org.apache.commons.jelly.tags.swt.WidgetTag#attachWidgets(java.lang.Object, org.eclipse.swt.widgets.Widget)
52       */
53      protected void attachWidgets(Object parent, Widget widget) throws JellyTagException {
54          super.attachWidgets(parent, widget);
55  
56          // set Parent composite of wizard page
57          if (getParent() instanceof WizardPageTag) {
58              WizardPageTag tag = (WizardPageTag) getParent();
59              if (tag.getWizardPageImpl().getParentControl() == null) {
60                  if (widget instanceof Composite) {
61                      tag.getWizardPageImpl().setParentComposite((Composite) widget);
62                  } else {
63                      throw new JellyTagException("First child of a <wizardPage> must be of type Composite");
64                  }
65              }
66          }
67      }
68  
69      /*
70       * @see org.apache.commons.jelly.tags.swt.WidgetTag#getParentWidget()
71       */
72      public Widget getParentWidget() {
73          parent = super.getParentWidget();
74  
75          if (parent == null && getParent() instanceof WizardPageTag) {
76              WizardPageTag tag = (WizardPageTag) getParent();
77              if (tag != null) {
78                  WizardPageTag.WizardPageImpl page = tag.getWizardPageImpl();
79                  return page.getControl();
80              }
81          }
82  
83          if (parent == null) {
84              ApplicationWindowTag tag =
85                  (ApplicationWindowTag) findAncestorWithClass(ApplicationWindowTag.class);
86              if (tag != null) {
87                  Window window = tag.getWindow();
88                  if (window != null && window instanceof ApplicationWindowImpl) {
89                      return ((ApplicationWindowImpl) window).getContents();
90                  }
91              }
92          }
93  
94          return parent;
95      }
96  
97  }