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.JList;
19  import javax.swing.JScrollPane;
20  import javax.swing.event.ListSelectionEvent;
21  import javax.swing.event.ListSelectionListener;
22  
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 single
29   * selection lists.
30   * </p>
31   * <p>
32   * Single selection lists are quite similar to non editable combo boxes. Their
33   * data is based on the provided list model.
34   * </p>
35   *
36   * @author Oliver Heger
37   * @version $Id: SwingListBoxHandler.java 205 2012-01-29 18:29:57Z oheger $
38   */
39  class SwingListBoxHandler extends SwingListModelHandler implements
40          ListSelectionListener
41  {
42      /** Stores the list's scroll pane. */
43      private final JScrollPane scrollPane;
44  
45      /**
46       * Creates a new instance of {@code SwingListBoxHandler}.
47       *
48       * @param list the list component
49       * @param listModel the list model
50       * @param scrollWidth the preferred scroll width
51       * @param scrollHeight the preferred scroll height
52       */
53      public SwingListBoxHandler(JList list, ListModel listModel,
54              int scrollWidth, int scrollHeight)
55      {
56          super(list, listModel);
57          scrollPane = SwingComponentUtils.scrollPaneFor(list, scrollWidth,
58                  scrollHeight);
59      }
60  
61      /**
62       * Returns the managed list.
63       *
64       * @return the managed list component
65       */
66      public JList getList()
67      {
68          return (JList) getComponent();
69      }
70  
71      /**
72       * Callback for list selection changes. These events are fired as change
73       * events. Adjusting events are ignored.
74       *
75       * @param event the change event
76       */
77      public void valueChanged(ListSelectionEvent event)
78      {
79          if (!event.getValueIsAdjusting())
80          {
81              fireChangeEvent(event);
82          }
83      }
84  
85      /**
86       * Registers this component handler as a change listener at the managed
87       * component. This implementation registers a list selection listener.
88       */
89      @Override
90      protected void registerChangeListener()
91      {
92          getList().addListSelectionListener(this);
93      }
94  
95      /**
96       * Removes this component handler as change listener from the managed
97       * component.
98       */
99      @Override
100     protected void unregisterChangeListener()
101     {
102         getList().removeListSelectionListener(this);
103     }
104 
105     /**
106      * Initializes the list box with the given list model.
107      *
108      * @param model the list model
109      */
110     @Override
111     protected void initComponentModel(SwingListModel model)
112     {
113         getList().setModel(model);
114     }
115 
116     /**
117      * Returns this component's data. Based on the selected index an object from
118      * the list model will be returned.
119      *
120      * @return the component's data
121      */
122     public Object getData()
123     {
124         return ListModelUtils
125                 .getValue(getListModel(), getList().getSelectedIndex());
126     }
127 
128     /**
129      * Sets the data of this component.
130      *
131      * @param data the new data (which should be an item of the list model)
132      */
133     public void setData(Object data)
134     {
135         if (data == null)
136         {
137             getList().clearSelection();
138         }
139         else
140         {
141             int index = ListModelUtils.getIndex(getListModel(), data);
142             if (index >= 0)
143             {
144                 getList().setSelectedIndex(index);
145             }
146             else
147             {
148                 getList().clearSelection();
149             }
150         }
151     }
152 
153     /**
154      * Returns the outer most component. For lists this is a scroll pane.
155      *
156      * @return the outer most component
157      */
158     @Override
159     public Object getOuterComponent()
160     {
161         return scrollPane;
162     }
163 }