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 java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.DefaultComboBoxModel;
22  
23  import net.sf.jguiraffe.gui.builder.components.model.ListModel;
24  
25  /**
26   * <p>
27   * A specialized list model used internally by list-like components.
28   * </p>
29   * <p>
30   * This class is a hybrid: It is both a Swing list model and a form builder list
31   * model.
32   * </p>
33   *
34   * @author Oliver Heger
35   * @version $Id: SwingListModel.java 205 2012-01-29 18:29:57Z oheger $
36   */
37  class SwingListModel extends DefaultComboBoxModel implements ListModel
38  {
39      /**
40       * A default serial version UID.
41       */
42      private static final long serialVersionUID = 20131003L;
43  
44      /** Stores the data type of this model. */
45      private final Class<?> type;
46  
47      /** The collection with value objects. */
48      private final List<Object> valueObjects;
49  
50      /**
51       * Creates a new instance of {@code SwingListModel}.
52       *
53       * @param model the underlying model
54       */
55      public SwingListModel(ListModel model)
56      {
57          type = model.getType();
58          valueObjects = new ArrayList<Object>(model.size());
59          initFromModel(model);
60      }
61  
62      /**
63       * Returns the display object with the given index.
64       *
65       * @param index the index (0 based)
66       * @return the display object at this index
67       */
68      public Object getDisplayObject(int index)
69      {
70          return getElementAt(index);
71      }
72  
73      /**
74       * Returns the data object of this list model.
75       *
76       * @return the data type of this model
77       */
78      public Class<?> getType()
79      {
80          return type;
81      }
82  
83      /**
84       * Returns the value object with the given index.
85       *
86       * @param index the index (0 based)
87       * @return the value object for this index
88       */
89      public Object getValueObject(int index)
90      {
91          return valueObjects.get(index);
92      }
93  
94      /**
95       * Returns the number of entries in this model.
96       *
97       * @return the number of entries
98       */
99      public int size()
100     {
101         return getSize();
102     }
103 
104     /**
105      * Inserts the given data at the specified index.
106      *
107      * @param index the index
108      * @param display the display object
109      * @param value the value object
110      */
111     public void insertItem(int index, Object display, Object value)
112     {
113         insertElementAt(display, index);
114         valueObjects.add(index, value);
115     }
116 
117     /**
118      * {@inheritDoc} This implementation also updates the specific state of
119      * this model.
120      */
121     @Override
122     public void removeElementAt(int index)
123     {
124         super.removeElementAt(index);
125         valueObjects.remove(index);
126     }
127 
128     /**
129      * Fills this model with the data from the passed in original object.
130      *
131      * @param model the original model
132      */
133     private void initFromModel(ListModel model)
134     {
135         for (int idx = 0; idx < model.size(); idx++)
136         {
137             addElement(model.getDisplayObject(idx));
138             valueObjects.add(model.getValueObject(idx));
139         }
140     }
141 }