1 | |
package org.jtheque.core.managers.view.impl.components.model; |
2 | |
|
3 | |
import org.jtheque.utils.collections.CollectionUtils; |
4 | |
|
5 | |
import javax.swing.DefaultComboBoxModel; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.Arrays; |
8 | |
import java.util.Collection; |
9 | |
import java.util.List; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class SimpleComboBoxModel<T> extends DefaultComboBoxModel { |
35 | |
private final List<T> objects; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public SimpleComboBoxModel(){ |
41 | 0 | this(10); |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public SimpleComboBoxModel(int capacity){ |
50 | 0 | super(); |
51 | |
|
52 | 0 | objects = new ArrayList<T>(capacity); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public SimpleComboBoxModel(Collection<T> objects){ |
61 | 0 | super(); |
62 | |
|
63 | 0 | this.objects = CollectionUtils.copyOf(objects); |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public SimpleComboBoxModel(T[] objects){ |
72 | 0 | super(); |
73 | |
|
74 | 0 | this.objects = Arrays.asList(objects); |
75 | 0 | } |
76 | |
|
77 | |
@Override |
78 | |
public final Object getElementAt(int index){ |
79 | 0 | return objects.get(index); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public final int getSize(){ |
84 | 0 | return objects.size(); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public final void addElement(Object obj){ |
89 | 0 | objects.add((T) obj); |
90 | 0 | fireIntervalAdded(this, getSize(), getSize()); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void addElements(Iterable<T> elements){ |
99 | 0 | int index = objects.size(); |
100 | |
|
101 | 0 | for (T category : elements){ |
102 | 0 | objects.add(category); |
103 | |
} |
104 | |
|
105 | 0 | fireIntervalAdded(this, index, getSize()); |
106 | 0 | } |
107 | |
|
108 | |
@Override |
109 | |
public final void removeElement(Object obj){ |
110 | 0 | T category = (T) obj; |
111 | |
|
112 | 0 | int index = objects.indexOf(category); |
113 | |
|
114 | 0 | objects.remove(category); |
115 | |
|
116 | 0 | fireIntervalRemoved(this, index, index); |
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
public final void removeAllElements(){ |
121 | 0 | objects.clear(); |
122 | 0 | fireContentsChanged(this, 0, getSize()); |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void setElements(Collection<T> elements){ |
131 | 0 | objects.clear(); |
132 | 0 | objects.addAll(elements); |
133 | 0 | fireContentsChanged(this, 0, getSize()); |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void setElements(T[] elements){ |
142 | 0 | objects.clear(); |
143 | 0 | objects.addAll(Arrays.asList(elements)); |
144 | 0 | fireContentsChanged(this, 0, getSize()); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public Iterable<T> getObjects(){ |
153 | 0 | return objects; |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public final T getSelectedItem(){ |
158 | 0 | return (T)super.getSelectedItem(); |
159 | |
} |
160 | |
} |