View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
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 net.sf.jguiraffe.gui.platform.swing.builder.components;
17  
18  import javax.swing.JComboBox;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  
22  import net.sf.jguiraffe.gui.builder.components.model.EditableComboBoxModel;
23  import net.sf.jguiraffe.gui.builder.components.model.ListModel;
24  import net.sf.jguiraffe.gui.builder.components.tags.ListModelUtils;
25  
26  /**
27   * <p>
28   * A specific Swing component handler implementation that deals with combo
29   * boxes.
30   * </p>
31   * <p>
32   * This class manages a <code>JComboBox</code> component, which is not
33   * editable. The specified <code>ListModel</code> is used to transform the
34   * combo box's selected index into a value object.
35   * </p>
36   *
37   * @author Oliver Heger
38   * @version $Id: SwingComboBoxHandler.java 205 2012-01-29 18:29:57Z oheger $
39   */
40  class SwingComboBoxHandler extends SwingListModelHandler implements
41          ActionListener
42  {
43      /**
44       * A reference to an editable combo box model for dealing with values not
45       * found in the data model.
46       */
47      private final EditableComboBoxModel editModel;
48  
49      /**
50       * Creates a new instance of <code>SwingComboBoxHandler</code> and
51       * initializes it.
52       *
53       * @param combo the combo box
54       * @param m the list model for the combo box
55       */
56      public SwingComboBoxHandler(JComboBox combo, ListModel m)
57      {
58          super(combo, m);
59          editModel = ListModelUtils.fetchEditableComboBoxModel(m);
60      }
61  
62      /**
63       * Returns the managed combo box.
64       *
65       * @return the managed combo box component
66       */
67      public JComboBox getComboBox()
68      {
69          return (JComboBox) getComponent();
70      }
71  
72      /**
73       * Registers this component handler as a change listener at the managed
74       * component. This implementation registers an action listener.
75       */
76      @Override
77      protected void registerChangeListener()
78      {
79          getComboBox().addActionListener(this);
80      }
81  
82      /**
83       * Unregisters this component as change listener from the managed component.
84       */
85      @Override
86      protected void unregisterChangeListener()
87      {
88          getComboBox().removeActionListener(this);
89      }
90  
91      /**
92       * Returns this component's data. Based on the selected index an object from
93       * the list model will be returned.
94       *
95       * @return the component's data
96       */
97      public Object getData()
98      {
99          Object selectedItem = getComboBox().getSelectedItem();
100         if (selectedItem == null)
101         {
102             return null;
103         }
104 
105         int index = ListModelUtils.getDisplayIndex(getListModel(), selectedItem);
106         if (index != ListModelUtils.IDX_UNDEFINED)
107         {
108             return ListModelUtils.getValue(getListModel(), index);
109         }
110 
111         return editModel.toValue(selectedItem);
112     }
113 
114     /**
115      * Sets the data of this component.
116      *
117      * @param data the new data (which should be an item of the list model)
118      */
119     public void setData(Object data)
120     {
121         Object display;
122         if (data == null)
123         {
124             display = null;
125         }
126         else
127         {
128             int index = ListModelUtils.getIndex(getListModel(), data);
129             if (index != ListModelUtils.IDX_UNDEFINED)
130             {
131                 display = getListModel().getDisplayObject(index);
132             }
133             else
134             {
135                 display = editModel.toDisplay(data);
136                 getComboBox().setSelectedIndex(-1);
137             }
138         }
139         getComboBox().setSelectedItem(display);
140     }
141 
142     /**
143      * Callback for action events. An action event indicates a change in the
144      * combobox's selection.
145      *
146      * @param event the event
147      */
148     public void actionPerformed(ActionEvent event)
149     {
150         fireChangeEvent(event);
151     }
152 
153     /**
154      * Initializes the combo box with the given list model.
155      *
156      * @param model the list model
157      */
158     @Override
159     protected void initComponentModel(SwingListModel model)
160     {
161         getComboBox().setModel(model);
162     }
163 }