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.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.eclipse.swt.widgets.Control;
22  import org.eclipse.swt.widgets.Decorations;
23  import org.eclipse.swt.widgets.Menu;
24  import org.eclipse.swt.widgets.MenuItem;
25  import org.eclipse.swt.widgets.Widget;
26  
27  /***
28   * This tag creates an SWT Menu
29   * </p>
30   *
31   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32   * @version 1.1
33   */
34  public class MenuTag extends WidgetTag {
35  
36      /*** The Log to which logging calls will be made. */
37      private static final Log log = LogFactory.getLog(MenuTag.class);
38  
39      public MenuTag() {
40          super(Menu.class);
41      }
42  
43      public MenuTag(int style) {
44          super(Menu.class, style);
45      }
46  
47      // Implementation methods
48      //-------------------------------------------------------------------------
49  
50      /***
51       * Provides a strategy method to allow a new child widget to be attached to
52       * its parent
53       *
54       * @param parent is the parent widget which is never null
55       * @param widget is the new child widget to be attached to the parent
56       */
57      protected void attachWidgets(Object parent, Widget widget) {
58          Menu menu = (Menu) widget;
59          if (parent instanceof Decorations) {
60              Decorations shell = (Decorations) parent;
61              shell.setMenuBar(menu);
62          }
63          else if (parent instanceof Control) {
64              Control control = (Control) parent;
65              control.setMenu(menu);
66          }
67          else if (parent instanceof MenuItem) {
68              MenuItem menuItem = (MenuItem) parent;
69              menuItem.setMenu(menu);
70          }
71      }
72  
73      /***
74       * @see org.apache.commons.jelly.tags.swt.WidgetTag#createWidget(java.lang.Class, org.eclipse.swt.widgets.Widget, int)
75       */
76      protected Object createWidget(Class theClass, Widget parent, int style)
77          throws JellyTagException {
78  
79          if (parent instanceof Decorations) {
80              return super.createWidget(theClass, parent, style);
81          }
82          else {
83              if (parent instanceof Menu) {
84                  return new Menu((Menu) parent);
85              }
86              else if (parent instanceof MenuItem) {
87                  return new Menu((MenuItem) parent);
88              }
89              else if (parent instanceof Control) {
90                  return new Menu((Control) parent);
91              }
92              else {
93                  throw new JellyTagException("This tag must be nested inside a <shell>, <menu>, <menuItem> or control tag");
94              }
95          }
96      }
97  
98  }