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.JComponent;
19
20 import net.sf.jguiraffe.gui.builder.components.model.ListComponentHandler;
21 import net.sf.jguiraffe.gui.builder.components.model.ListModel;
22
23 /**
24 * <p>
25 * An abstract base class for Swing component handlers that deal with list
26 * models.
27 * </p>
28 * <p>
29 * This class provides basic functionality for managing a list model. It will
30 * act as the base class for specific handler implementations that wrap Swing
31 * list-like components like <code>JList</code> or <code>JComboBox</code>.
32 * </p>
33 *
34 * @author Oliver Heger
35 * @version $Id: SwingListModelHandler.java 205 2012-01-29 18:29:57Z oheger $
36 */
37 abstract class SwingListModelHandler extends SwingComponentHandler<Object> implements
38 ListComponentHandler
39 {
40 /** Stores the special Swing list model. */
41 private SwingListModel model;
42
43 /**
44 * Creates a new instance of <code>SwingListModelHandler</code> and sets
45 * the managed component and the original list model.
46 *
47 * @param component the component to manage
48 * @param listModel the original list model; this class will copy this list
49 * model's data into a new list model that can also be used by Swing
50 * components
51 */
52 protected SwingListModelHandler(JComponent component, ListModel listModel)
53 {
54 super(component);
55 model = createSwingListModel(listModel);
56 initComponentModel(model);
57 }
58
59 /**
60 * Returns the list model for this component.
61 *
62 * @return the list model
63 */
64 public ListModel getListModel()
65 {
66 return model;
67 }
68
69 /**
70 * Adds an item to the list model of this component.
71 *
72 * @param index the index of the new item
73 * @param display the display object
74 * @param value the value object
75 */
76 public void addItem(int index, Object display, Object value)
77 {
78 model.insertItem(index, display, value);
79 }
80
81 /**
82 * Removes the item with the given index from the list model of this
83 * component.
84 *
85 * @param index the index
86 */
87 public void removeItem(int index)
88 {
89 model.removeElementAt(index);
90 }
91
92 /**
93 * Returns this component's data type. This type is directly derived from
94 * the list model.
95 *
96 * @return the data type of this component
97 */
98 public Class<?> getType()
99 {
100 return getListModel().getType();
101 }
102
103 /**
104 * Creates the Swing specific list model from the given source model.
105 *
106 * @param m the original list model
107 * @return the new Swing specific list model
108 */
109 protected SwingListModel createSwingListModel(ListModel m)
110 {
111 return new SwingListModel(m);
112 }
113
114 /**
115 * Initializes the list model for the managed component. This method is
116 * called after the Swing specific list model has been created. Derived
117 * classes should initialize the managed component with this model.
118 *
119 * @param model the Swing list model
120 */
121 protected abstract void initComponentModel(SwingListModel model);
122 }