svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretype / DefaultAttributeDescriptorPickerController.java @ 44215
History | View | Annotate | Download (8.17 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import org.gvsig.fmap.dal.swing.featuretype.AttributeDescriptorPickerController; |
6 |
import java.awt.event.ItemEvent; |
7 |
import java.awt.event.ItemListener; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Comparator; |
10 |
import java.util.List; |
11 |
import java.util.function.Predicate; |
12 |
import javax.swing.DefaultComboBoxModel; |
13 |
import javax.swing.DefaultListModel; |
14 |
import javax.swing.JComboBox; |
15 |
import javax.swing.JComponent; |
16 |
import javax.swing.JList; |
17 |
import javax.swing.event.ListSelectionEvent; |
18 |
import javax.swing.event.ListSelectionListener; |
19 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.tools.swing.api.Component; |
22 |
import org.gvsig.tools.swing.api.ListElement; |
23 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
public class DefaultAttributeDescriptorPickerController |
30 |
extends AbstractPickerController<FeatureAttributeDescriptor>
|
31 |
implements AttributeDescriptorPickerController
|
32 |
{ |
33 |
|
34 |
private static class ComboOrList<T> implements Component { |
35 |
|
36 |
private JComboBox combo; |
37 |
private JList list; |
38 |
|
39 |
public ComboOrList(JComboBox combo) { |
40 |
this.combo = combo;
|
41 |
this.combo.setEditable(false); |
42 |
} |
43 |
|
44 |
public ComboOrList(JList list) { |
45 |
this.list = list;
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public JComponent asJComponent() { |
50 |
if( this.combo == null ) { |
51 |
return this.list; |
52 |
} |
53 |
return this.combo; |
54 |
} |
55 |
|
56 |
private void addSelectionListener(final ActionListener listener) { |
57 |
if( this.combo == null ) { |
58 |
this.list.addListSelectionListener(new ListSelectionListener() { |
59 |
@Override
|
60 |
public void valueChanged(ListSelectionEvent e) { |
61 |
if( e.getValueIsAdjusting() ) {
|
62 |
return;
|
63 |
} |
64 |
listener.actionPerformed(new ActionEvent(list, 0, "selected")); |
65 |
} |
66 |
}); |
67 |
} else {
|
68 |
this.combo.addItemListener(new ItemListener() { |
69 |
@Override
|
70 |
public void itemStateChanged(ItemEvent e) { |
71 |
if( e.getStateChange()==ItemEvent.SELECTED ) { |
72 |
listener.actionPerformed(new ActionEvent(combo, 0, "selected")); |
73 |
} |
74 |
} |
75 |
}); |
76 |
} |
77 |
} |
78 |
|
79 |
public void clear() { |
80 |
if( this.combo == null ) { |
81 |
DefaultListModel model = new DefaultListModel(); |
82 |
this.list.setModel(model);
|
83 |
} else {
|
84 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
85 |
this.combo.setModel(model);
|
86 |
} |
87 |
} |
88 |
|
89 |
public void add(String label, T value) { |
90 |
ListElement<T> element = new ListElement<>(label,value);
|
91 |
if( this.combo == null ) { |
92 |
DefaultListModel model = (DefaultListModel) this.list.getModel(); |
93 |
model.addElement(element); |
94 |
} else {
|
95 |
DefaultComboBoxModel model = (DefaultComboBoxModel) this.combo.getModel(); |
96 |
model.addElement(element); |
97 |
} |
98 |
} |
99 |
|
100 |
public int getSize() { |
101 |
if( this.combo == null ) { |
102 |
return this.list.getModel().getSize(); |
103 |
} else {
|
104 |
return this.combo.getModel().getSize(); |
105 |
} |
106 |
} |
107 |
|
108 |
public void setSelectedIndex(int i) { |
109 |
if( this.combo == null ) { |
110 |
this.list.setSelectedIndex(i);
|
111 |
} else {
|
112 |
this.combo.setSelectedIndex(i);
|
113 |
} |
114 |
} |
115 |
|
116 |
public void setEnabled(boolean enabled) { |
117 |
if( this.combo == null ) { |
118 |
this.list.setEnabled(enabled);
|
119 |
} else {
|
120 |
this.combo.setEnabled(enabled);
|
121 |
} |
122 |
} |
123 |
|
124 |
public boolean isEnabled() { |
125 |
if( this.combo == null ) { |
126 |
return this.list.isEnabled(); |
127 |
} else {
|
128 |
return this.combo.isEnabled(); |
129 |
} |
130 |
} |
131 |
|
132 |
public void setSelectedValue(T value) { |
133 |
if( this.combo == null ) { |
134 |
ListElement.setSelected(this.list, value);
|
135 |
} else {
|
136 |
ListElement.setSelected(this.combo, value);
|
137 |
} |
138 |
} |
139 |
|
140 |
public T getSelectedValue() {
|
141 |
T value; |
142 |
if( this.combo == null ) { |
143 |
value = (T) ListElement.getSelected(this.list);
|
144 |
} else {
|
145 |
value = (T) ListElement.getSelected(this.combo);
|
146 |
} |
147 |
return value;
|
148 |
} |
149 |
} |
150 |
|
151 |
|
152 |
private final ComboOrList<FeatureAttributeDescriptor> combo; |
153 |
private FeatureType featureType;
|
154 |
private final Predicate<FeatureAttributeDescriptor> filter; |
155 |
|
156 |
public DefaultAttributeDescriptorPickerController(JComboBox combo, Predicate<FeatureAttributeDescriptor> filter) { |
157 |
this(new ComboOrList(combo), filter); |
158 |
} |
159 |
|
160 |
public DefaultAttributeDescriptorPickerController(JList list, Predicate<FeatureAttributeDescriptor> filter) { |
161 |
this(new ComboOrList(list), filter); |
162 |
} |
163 |
|
164 |
private DefaultAttributeDescriptorPickerController(ComboOrList combo, Predicate<FeatureAttributeDescriptor> filter) { |
165 |
this.combo = combo;
|
166 |
this.combo.addSelectionListener(new ActionListener() { |
167 |
@Override
|
168 |
public void actionPerformed(ActionEvent e) { |
169 |
fireChangeEvent(); |
170 |
} |
171 |
}); |
172 |
this.filter = filter;
|
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void setFeatureType(FeatureType featureType) { |
177 |
this.featureType = featureType;
|
178 |
this.combo.clear();
|
179 |
if( this.featureType==null ) { |
180 |
return;
|
181 |
} |
182 |
List<FeatureAttributeDescriptor> attributes = new ArrayList<>(); |
183 |
for (FeatureAttributeDescriptor descriptor : featureType) {
|
184 |
if( filter==null || filter.test(descriptor) ) { |
185 |
attributes.add(descriptor); |
186 |
} |
187 |
} |
188 |
attributes.sort(new Comparator<FeatureAttributeDescriptor>() { |
189 |
@Override
|
190 |
public int compare(FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) { |
191 |
return o1.getName().compareToIgnoreCase(o2.getName());
|
192 |
} |
193 |
}); |
194 |
for (FeatureAttributeDescriptor descriptor : attributes) {
|
195 |
String nameDescription = descriptor.getName() + " [" + descriptor.getDataTypeName() + "]"; |
196 |
this.combo.add(nameDescription, descriptor);
|
197 |
} |
198 |
if( this.combo.getSize()>0 ) { |
199 |
this.combo.setSelectedIndex(0); |
200 |
} else {
|
201 |
this.combo.setSelectedIndex(-1); |
202 |
} |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public FeatureAttributeDescriptor get() {
|
207 |
FeatureAttributeDescriptor descriptor = this.combo.getSelectedValue();
|
208 |
return descriptor;
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public void set(FeatureAttributeDescriptor descriptor) { |
213 |
this.combo.setSelectedValue(descriptor);
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public void set(String name) { |
218 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(name);
|
219 |
if( descriptor == null ) { |
220 |
// throw new IllegalArgumentException("The field '"+name+" don't exists.");
|
221 |
this.combo.setSelectedIndex(-1); |
222 |
return;
|
223 |
} |
224 |
this.set(descriptor);
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void coerceAndSet(Object descriptor) { |
229 |
this.set((FeatureAttributeDescriptor) descriptor);
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void setEnabled(boolean enabled) { |
234 |
this.combo.setEnabled(enabled);
|
235 |
} |
236 |
|
237 |
@Override
|
238 |
public boolean isEnabled() { |
239 |
return this.combo.isEnabled(); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public String getName() { |
244 |
FeatureAttributeDescriptor descriptor = this.get();
|
245 |
if( descriptor==null ) { |
246 |
return null; |
247 |
} |
248 |
return descriptor.getName();
|
249 |
} |
250 |
|
251 |
@Override
|
252 |
public void clean() { |
253 |
this.combo.setSelectedIndex(-1); |
254 |
} |
255 |
|
256 |
|
257 |
} |