View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
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.xulux.guilayer.swing.widgets;
17  
18  import java.awt.Component;
19  
20  import javax.swing.JPopupMenu;
21  
22  import org.xulux.api.gui.IWidget;
23  import org.xulux.api.gui.IXuluxListener;
24  import org.xulux.gui.ContainerWidget;
25  
26  /**
27   * A popopmenu
28   *
29   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
30   * @version $Id: PopupMenu.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
31   */
32  public class PopupMenu extends ContainerWidget {
33  
34      /**
35       * the native popupmenu
36       */
37      protected JPopupMenu menu;
38  
39      /**
40       * @param name the popupmenu name
41       */
42      public PopupMenu(String name) {
43          super(name);
44      }
45  
46      /**
47       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
48       */
49      public Object getNativeWidget() {
50          if (!initialized) {
51              initialize();
52          }
53          return this.menu;
54      }
55  
56      /**
57       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
58       */
59      public void initialize() {
60          if (initialized) {
61              return;
62          }
63          this.menu = new JPopupMenu();
64          setVisible(false);
65          initialized = true;
66          initializeChildren();
67          refresh();
68          processInit();
69      }
70  
71      /**
72       * @see org.xulux.nyx.gui.XuluxWidget#refresh()
73       */
74      public void refresh() {
75          initialize();
76          if (isVisible()) {
77              // check if the parent is showing, else an exception will be
78              // thrown
79              if (((Component) getParent().getNativeWidget()).isShowing()) {
80                  menu.show((Component) getParent().getNativeWidget(), getRectangle().getX(), getRectangle().getY());
81              }
82          }
83      }
84  
85      /**
86       * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
87       */
88      public Object getGuiValue() {
89          return null;
90      }
91  
92      /**
93       * @see org.xulux.nyx.gui.XuluxWidget#focus()
94       */
95      public void focus() {
96          menu.requestFocus();
97      }
98  
99      /**
100      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
101      */
102     public boolean isValueEmpty() {
103         return false;
104     }
105 
106     /**
107      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
108      */
109     public boolean canContainValue() {
110         return false;
111     }
112 
113     /**
114      * @see org.xulux.nyx.gui.ContainerWidget#addToParent(org.xulux.nyx.gui.XuluxWidget)
115      */
116     public void addToParent(IWidget widget) {
117         if (widget instanceof MenuItem) {
118             menu.add((Component) widget.getNativeWidget());
119         } else {
120             System.out.println("Widgets of type " + widget.getWidgetType() + " is not supported in a popumen");
121         }
122     }
123     /**
124      * @see org.xulux.nyx.gui.XuluxWidget#setVisible(boolean)
125      */
126     public void setVisible(boolean visible) {
127         this.visible = true;
128         if (initialized) {
129             refresh();
130         }
131     }
132 
133     /**
134      * @see org.xulux.nyx.gui.XuluxWidget#addXuluxListener(org.xulux.nyx.gui.XuluxListener)
135      */
136     public void addXuluxListener(IXuluxListener listener) {
137     }
138 
139 }