Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DataContainerCachedComboBoxModel |
|
| 1.1428571428571428;1.143 |
1 | package org.jtheque.primary.view.impl.models; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import org.jtheque.core.managers.persistence.able.DataContainer; | |
20 | import org.jtheque.core.managers.persistence.able.DataListener; | |
21 | import org.jtheque.core.managers.persistence.able.Entity; | |
22 | import org.jtheque.primary.od.able.Data; | |
23 | ||
24 | import javax.swing.DefaultComboBoxModel; | |
25 | import java.util.ArrayList; | |
26 | import java.util.Collection; | |
27 | import java.util.List; | |
28 | ||
29 | /** | |
30 | * A combo box model with the data in a cache. | |
31 | * | |
32 | * @author Baptiste Wicht | |
33 | * @param <T> The entity class on the model. | |
34 | */ | |
35 | public final class DataContainerCachedComboBoxModel<T extends Data> extends DefaultComboBoxModel implements DataListener { | |
36 | private final DataContainer<T> container; | |
37 | ||
38 | private List<T> datas; | |
39 | ||
40 | /** | |
41 | * Construct a new DataContainerCachedComboBoxModel with a specific container. | |
42 | * | |
43 | * @param container The data container. | |
44 | */ | |
45 | public DataContainerCachedComboBoxModel(DataContainer<T> container){ | |
46 | 0 | super(); |
47 | ||
48 | 0 | this.container = container; |
49 | 0 | datas = new ArrayList<T>(container.getDatas()); |
50 | ||
51 | 0 | container.addDataListener(this); |
52 | 0 | } |
53 | ||
54 | @Override | |
55 | public Object getElementAt(int index){ | |
56 | 0 | return datas.get(index); |
57 | } | |
58 | ||
59 | @Override | |
60 | public int getSize(){ | |
61 | 0 | return getDatas().size(); |
62 | } | |
63 | ||
64 | /** | |
65 | * Return the selected data in the model. | |
66 | * | |
67 | * @return The data who's selected. | |
68 | */ | |
69 | @SuppressWarnings("unchecked") | |
70 | public T getSelectedData(){ | |
71 | 0 | return (T) getSelectedItem(); |
72 | } | |
73 | ||
74 | @Override | |
75 | public void dataChanged(){ | |
76 | 0 | datas = new ArrayList<T>(container.getDatas()); |
77 | ||
78 | 0 | fireContentsChanged(this, 0, getSize()); |
79 | 0 | } |
80 | ||
81 | /** | |
82 | * Return the datas. | |
83 | * | |
84 | * @return The datas. | |
85 | */ | |
86 | private Collection<? extends Entity> getDatas(){ | |
87 | 0 | return datas; |
88 | } | |
89 | ||
90 | /** | |
91 | * Select the first element. If there is no element, nothing will be done. | |
92 | */ | |
93 | public void selectFirst(){ | |
94 | 0 | if (!datas.isEmpty()){ |
95 | 0 | setSelectedItem(datas.get(0)); |
96 | } | |
97 | 0 | } |
98 | } |