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 @ 46485
History | View | Annotate | Download (10.6 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 java.awt.event.ItemEvent; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import java.util.function.Predicate; |
9 |
import javax.swing.DefaultComboBoxModel; |
10 |
import javax.swing.DefaultListModel; |
11 |
import javax.swing.JComboBox; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JList; |
14 |
import javax.swing.event.ListSelectionEvent; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.fmap.dal.DataManager; |
17 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
18 |
import org.gvsig.fmap.dal.feature.FeatureType; |
19 |
import org.gvsig.fmap.dal.swing.featuretype.AttributeDescriptorPickerController; |
20 |
import org.gvsig.tools.swing.api.Component; |
21 |
import org.gvsig.tools.swing.api.ListElement; |
22 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
24 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author jjdelcerro
|
29 |
*/
|
30 |
public class DefaultAttributeDescriptorPickerController |
31 |
extends AbstractPickerController<FeatureAttributeDescriptor>
|
32 |
implements AttributeDescriptorPickerController
|
33 |
{ |
34 |
|
35 |
private static class ComboOrList<T> implements Component { |
36 |
|
37 |
private JComboBox combo; |
38 |
private JList list; |
39 |
|
40 |
public ComboOrList(JComboBox combo) { |
41 |
this.combo = combo;
|
42 |
this.combo.setEditable(false); |
43 |
} |
44 |
|
45 |
public ComboOrList(JList list) { |
46 |
this.list = list;
|
47 |
} |
48 |
|
49 |
@Override
|
50 |
public JComponent asJComponent() { |
51 |
if( this.combo == null ) { |
52 |
return this.list; |
53 |
} |
54 |
return this.combo; |
55 |
} |
56 |
|
57 |
private void addSelectionListener(final ActionListener listener) { |
58 |
if( this.combo == null ) { |
59 |
this.list.addListSelectionListener((ListSelectionEvent e) -> { |
60 |
if( e.getValueIsAdjusting() ) {
|
61 |
return;
|
62 |
} |
63 |
listener.actionPerformed(new ActionEvent(list, 0, "selected")); |
64 |
}); |
65 |
} else {
|
66 |
this.combo.addActionListener((ActionEvent e) -> { |
67 |
listener.actionPerformed(new ActionEvent(combo, 0, "selected")); |
68 |
}); |
69 |
|
70 |
// .addItemListener((ItemEvent e) -> {
|
71 |
// if( e.getStateChange()==ItemEvent.SELECTED ) {
|
72 |
// listener.actionPerformed(new ActionEvent(combo, 0, "selected"));
|
73 |
// }
|
74 |
// });
|
75 |
} |
76 |
} |
77 |
|
78 |
public void clear() { |
79 |
if( this.combo == null ) { |
80 |
DefaultListModel model = new DefaultListModel(); |
81 |
this.list.setModel(model);
|
82 |
} else {
|
83 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
84 |
this.combo.setModel(model);
|
85 |
} |
86 |
} |
87 |
|
88 |
public void add(String label, T value) { |
89 |
ListElement<T> element = new ListElement<>(label,value);
|
90 |
if( this.combo == null ) { |
91 |
DefaultListModel model = (DefaultListModel) this.list.getModel(); |
92 |
model.addElement(element); |
93 |
} else {
|
94 |
DefaultComboBoxModel model = (DefaultComboBoxModel) this.combo.getModel(); |
95 |
model.addElement(element); |
96 |
} |
97 |
} |
98 |
|
99 |
public int getSize() { |
100 |
if( this.combo == null ) { |
101 |
return this.list.getModel().getSize(); |
102 |
} else {
|
103 |
return this.combo.getModel().getSize(); |
104 |
} |
105 |
} |
106 |
|
107 |
public void setSelectedIndex(int i) { |
108 |
if( this.combo == null ) { |
109 |
this.list.setSelectedIndex(i);
|
110 |
} else {
|
111 |
this.combo.setSelectedIndex(i);
|
112 |
} |
113 |
} |
114 |
|
115 |
public void setEnabled(boolean enabled) { |
116 |
if( this.combo == null ) { |
117 |
this.list.setEnabled(enabled);
|
118 |
} else {
|
119 |
this.combo.setEnabled(enabled);
|
120 |
} |
121 |
} |
122 |
|
123 |
public boolean isEnabled() { |
124 |
if( this.combo == null ) { |
125 |
return this.list.isEnabled(); |
126 |
} else {
|
127 |
return this.combo.isEnabled(); |
128 |
} |
129 |
} |
130 |
|
131 |
public void setEditable(boolean editable){ |
132 |
if( this.combo != null ) { |
133 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
134 |
if(editable){
|
135 |
this.combo.setEditable(editable);
|
136 |
toolsSwingManager.addClearButton(this.combo, (ActionEvent e) -> { |
137 |
this.combo.setSelectedIndex(-1); |
138 |
}); |
139 |
toolsSwingManager.setDefaultPopupMenu(this.combo);
|
140 |
} else {
|
141 |
toolsSwingManager.removeClearButton(this.combo);
|
142 |
this.combo.setEditable(editable);
|
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
public void setSelectedValue(T value) { |
148 |
if( this.combo == null ) { |
149 |
ListElement.setSelected(this.list, value);
|
150 |
} else {
|
151 |
ListElement.setSelected(this.combo, value);
|
152 |
} |
153 |
} |
154 |
|
155 |
public T getSelectedValue() {
|
156 |
T value; |
157 |
if( this.combo == null ) { |
158 |
value = (T) ListElement.getSelected(this.list);
|
159 |
} else {
|
160 |
value = (T) ListElement.getSelected(this.combo);
|
161 |
} |
162 |
return value;
|
163 |
} |
164 |
} |
165 |
|
166 |
|
167 |
private final ComboOrList<FeatureAttributeDescriptor> combo; |
168 |
private FeatureType featureType;
|
169 |
private final Predicate<FeatureAttributeDescriptor> filter; |
170 |
private boolean allowNull; |
171 |
|
172 |
public DefaultAttributeDescriptorPickerController(JComboBox combo, Predicate<FeatureAttributeDescriptor> filter) { |
173 |
this(new ComboOrList(combo), filter); |
174 |
combo.setRenderer(new FeatureAttributeListCellRendererImpl());
|
175 |
} |
176 |
|
177 |
public DefaultAttributeDescriptorPickerController(JList list, Predicate<FeatureAttributeDescriptor> filter) { |
178 |
this(new ComboOrList(list), filter); |
179 |
list.setCellRenderer(new FeatureAttributeListCellRendererImpl());
|
180 |
} |
181 |
|
182 |
private DefaultAttributeDescriptorPickerController(ComboOrList combo, Predicate<FeatureAttributeDescriptor> filter) { |
183 |
this.combo = combo;
|
184 |
this.filter = filter;
|
185 |
this.allowNull = false; |
186 |
this.combo.addSelectionListener((ActionEvent e) -> { |
187 |
fireChangeEvent(); |
188 |
}); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void setFeatureType(FeatureType featureType) { |
193 |
this.featureType = featureType;
|
194 |
this.combo.clear();
|
195 |
if( this.featureType==null ) { |
196 |
return;
|
197 |
} |
198 |
List<FeatureAttributeDescriptor> attributes = new ArrayList<>(); |
199 |
for (FeatureAttributeDescriptor descriptor : featureType) {
|
200 |
if( filter==null || filter.test(descriptor) ) { |
201 |
attributes.add(descriptor); |
202 |
} |
203 |
} |
204 |
int useLabels = DataManager.USE_LABELS_BOTH; //FIXME |
205 |
attributes.sort((FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) -> { |
206 |
String theLabel1;
|
207 |
String theLabel2;
|
208 |
switch(useLabels) {
|
209 |
case DataManager.USE_LABELS_NO:
|
210 |
theLabel1 = o1.getName(); |
211 |
theLabel2 = o2.getName(); |
212 |
break;
|
213 |
case DataManager.USE_LABELS_YES:
|
214 |
theLabel1 = (o1.hasLabel())? o1.getLocalizedLabel() : o1.getName(); |
215 |
theLabel2 = (o2.hasLabel())? o2.getLocalizedLabel() : o2.getName(); |
216 |
break;
|
217 |
case DataManager.USE_LABELS_BOTH:
|
218 |
default:
|
219 |
theLabel1 = (o1.hasLabel())?String.format("%s [%s]", o1.getLocalizedLabel(), o1.getName()): o1.getName(); |
220 |
theLabel2 = (o2.hasLabel())?String.format("%s [%s]", o2.getLocalizedLabel(), o2.getName()): o2.getName(); |
221 |
break;
|
222 |
} |
223 |
return theLabel1.compareToIgnoreCase(theLabel2);
|
224 |
}); |
225 |
if(this.allowNull) { |
226 |
this.combo.add("", null); |
227 |
} |
228 |
for (FeatureAttributeDescriptor descriptor : attributes) {
|
229 |
String nameDescription = descriptor.getName();// + " [" + descriptor.getDataTypeName() + "]"; |
230 |
this.combo.add(nameDescription, descriptor);
|
231 |
} |
232 |
if( this.combo.getSize()>0 ) { |
233 |
this.combo.setSelectedIndex(0); |
234 |
} else {
|
235 |
this.combo.setSelectedIndex(-1); |
236 |
} |
237 |
} |
238 |
|
239 |
@Override
|
240 |
public FeatureAttributeDescriptor get() {
|
241 |
FeatureAttributeDescriptor descriptor = this.combo.getSelectedValue();
|
242 |
return descriptor;
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public void set(FeatureAttributeDescriptor descriptor) { |
247 |
this.combo.setSelectedValue(descriptor);
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
public void set(String name) { |
252 |
if( this.featureType==null ) { |
253 |
this.combo.setSelectedIndex(-1); |
254 |
return;
|
255 |
} |
256 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(name);
|
257 |
if( descriptor == null ) { |
258 |
// throw new IllegalArgumentException("The field '"+name+" don't exists.");
|
259 |
this.combo.setSelectedIndex(-1); |
260 |
return;
|
261 |
} |
262 |
this.set(descriptor);
|
263 |
} |
264 |
|
265 |
@Override
|
266 |
public void coerceAndSet(Object descriptor) { |
267 |
this.set((FeatureAttributeDescriptor) descriptor);
|
268 |
} |
269 |
|
270 |
@Override
|
271 |
public void setEnabled(boolean enabled) { |
272 |
this.combo.setEnabled(enabled);
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
public boolean isEnabled() { |
277 |
return this.combo.isEnabled(); |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public String getName() { |
282 |
Object selected = this.combo.getSelectedValue(); |
283 |
if( selected == null ) { |
284 |
return null; |
285 |
} |
286 |
if(selected instanceof String){ |
287 |
String name = (String)selected; |
288 |
if(StringUtils.isBlank(name)){
|
289 |
return null; |
290 |
} |
291 |
return name;
|
292 |
} |
293 |
|
294 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) selected; |
295 |
return descriptor.getName();
|
296 |
} |
297 |
|
298 |
@Override
|
299 |
public void clean() { |
300 |
this.combo.setSelectedIndex(-1); |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public void setEditable(boolean editable) { |
305 |
super.setEditable(editable);
|
306 |
this.combo.setEditable(editable);
|
307 |
} |
308 |
|
309 |
@Override
|
310 |
public void setAllowNull(boolean allowNull) { |
311 |
this.allowNull = allowNull;
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public boolean getAllowNull() { |
316 |
return this.allowNull; |
317 |
} |
318 |
|
319 |
|
320 |
} |