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.extensions;
17  
18  import java.awt.Component;
19  import java.awt.event.FocusListener;
20  
21  import javax.swing.ComboBoxModel;
22  import javax.swing.JComboBox;
23  
24  import org.xulux.gui.XuluxListener;
25  
26  /**
27   * This overrides the default JComboBox.
28   * The problem it solves is that when setting
29   * a new model in the NyxCombo class, it would fire
30   * an action event, which would trigger another
31   * firing of all pre requests (and possibly nulling values!)
32   * This prevents that situation.
33   *
34   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
35   * @version $Id: NyxJComboBox.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
36   */
37  public class NyxJComboBox extends JComboBox {
38  
39      /**
40       * is a new model set or not ?
41       */
42      private boolean newModelIsSet = false;
43  
44      /**
45       * Constructor for NyxComboBox.
46       */
47      public NyxJComboBox() {
48          super();
49      }
50  
51      /**
52       * Set a setting so that we now a new model is set
53       * and no action events should be fired
54       * @param model - the model
55       */
56      public void setModel(ComboBoxModel model) {
57          newModelIsSet = true;
58          super.setModel(model);
59          newModelIsSet = false;
60      }
61      /**
62       * Only calls the selectedItem when we are not
63       * setting a new model
64       * @param object - the selectedItem
65       */
66      public void setSelectedItem(Object object) {
67          if (!newModelIsSet) {
68              super.setSelectedItem(object);
69          }
70      }
71  
72      /**
73       * Adds a nyx focuslistener to the currently known children
74       *
75       * @see java.awt.Component#addFocusListener(java.awt.event.FocusListener)
76       */
77      public synchronized void addFocusListener(FocusListener l) {
78          if (l instanceof XuluxListener) {
79              int childCount = getComponentCount();
80              for (int i = 0; i < childCount; i++) {
81                  Component comp = getComponent(i);
82                  comp.addFocusListener(l);
83              }
84          }
85          super.addFocusListener(l);
86      }
87  
88      /**
89       * Removes a nyx focuslistener from the currently known children
90       * @see java.awt.Component#removeFocusListener(java.awt.event.FocusListener)
91       */
92      public synchronized void removeFocusListener(FocusListener l) {
93          if (l instanceof XuluxListener) {
94              int childCount = getComponentCount();
95              for (int i = 0; i < childCount; i++) {
96                  Component comp = getComponent(i);
97                  comp.removeFocusListener(l);
98              }
99          }
100         super.removeFocusListener(l);
101     }
102 
103     /**
104      * @return if the new model is set
105      */
106     public boolean isNewModelIsSet() {
107         return newModelIsSet;
108     }
109 
110 }