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.Container;
19  import java.awt.event.ActionListener;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.swing.JMenuItem;
25  import javax.swing.JSeparator;
26  
27  import org.xulux.api.gui.IWidgetInitializer;
28  import org.xulux.api.gui.IXuluxListener;
29  import org.xulux.gui.XuluxWidget;
30  import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
31  
32  /**
33   * Creates a menuitem or a seperator, based on the type of
34   * menuitem
35   *
36   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
37   * @version $Id: MenuItem.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
38   */
39  public class MenuItem extends XuluxWidget {
40  
41      /**
42       * The native menuitem
43       */
44      protected JMenuItem item;
45      /**
46       * the native seperator
47       */
48      protected JSeparator separator;
49      /**
50       * the actionlistener
51       */
52      protected ActionListener actionListener;
53      /**
54       * the nyx listeners
55       */
56      protected List listenerList;
57  
58      /**
59       * @param name the name of the menuitem
60       */
61      public MenuItem(String name) {
62          super(name);
63      }
64  
65      /**
66       * @see org.xulux.gui.XuluxWidget#destroy()
67       */
68      public void destroy() {
69          removeAllRules();
70          if (item != null && this.actionListener != null) {
71              this.item.removeActionListener(this.actionListener);
72          }
73          this.actionListener = null;
74          if (listenerList != null) {
75              for (Iterator it = listenerList.iterator(); it.hasNext();) {
76                  this.item.removeActionListener((ActionListener) it.next());
77              }
78          }
79          if (item != null && item.getParent() != null) {
80              Container container = item.getParent();
81              container.remove(item);
82          }
83          item = null;
84          processDestroy();
85      }
86  
87      /**
88       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
89       */
90      public Object getNativeWidget() {
91          if (!initialized) {
92              initialize();
93          }
94          if (item != null) {
95              return item;
96          } else {
97              return separator;
98          }
99      }
100 
101     /**
102      * @see org.xulux.nyx.gui.XuluxWidget#initialize()
103      */
104     public void initialize() {
105         if (initialized) {
106             return;
107         }
108         String type = getProperty("type");
109         if (type == null || (type != null && !type.equals("separator"))) {
110             item = new JMenuItem();
111             // allways add an actionlistener..
112             this.actionListener = new PrePostFieldListener(this);
113             item.addActionListener(this.actionListener);
114         } else {
115             // not other things are needed..
116             separator = new JSeparator();
117         }
118         initialized = true;
119         refresh();
120         processInit();
121     }
122 
123     /**
124      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
125      */
126     public void refresh() {
127         if (isRefreshing()) {
128             return;
129         }
130         isRefreshing = true;
131         if (!initialized) {
132             initialize();
133         }
134         if (item != null) {
135             refreshItem();
136         } else if (separator != null) {
137             refreshSeperator();
138         }
139         isRefreshing = false;
140     }
141 
142     /**
143      * Refreshes the menuitem
144      */
145     private void refreshItem() {
146         String text = getProperty("text");
147         if (text != null) {
148             item.setText(text);
149         }
150         item.setEnabled(isEnabled());
151         item.setVisible(isVisible());
152     }
153 
154     /**
155      * Refreshes the separator (no code yet..)
156      *
157      */
158     private void refreshSeperator() {
159         // do nothing..
160     }
161 
162     /**
163      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
164      */
165     public Object getGuiValue() {
166         return null;
167     }
168 
169     /**
170      * @see org.xulux.nyx.gui.XuluxWidget#focus()
171      */
172     public void focus() {
173         item.requestFocus();
174     }
175 
176     /**
177      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
178      */
179     public boolean isValueEmpty() {
180         return false;
181     }
182 
183     /**
184      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
185      */
186     public boolean canContainValue() {
187         return false;
188     }
189 
190     /**
191      * @see org.xulux.nyx.gui.XuluxWidget#addXuluxListener(org.xulux.nyx.gui.XuluxListener)
192      */
193     public void addXuluxListener(IXuluxListener listener) {
194         if (listener instanceof ActionListener) {
195             if (listenerList == null) {
196                 listenerList = new ArrayList();
197             }
198             listenerList.add(listener);
199             initialize();
200             this.item.addActionListener((ActionListener) listener);
201         }
202     }
203 
204     public IWidgetInitializer getWidgetInitializer() {
205         return null;
206     }
207 
208 }