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 @ 45815
History | View | Annotate | Download (9.94 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.addItemListener((ItemEvent e) -> { |
67 |
if( e.getStateChange()==ItemEvent.SELECTED ) { |
68 |
listener.actionPerformed(new ActionEvent(combo, 0, "selected")); |
69 |
} |
70 |
}); |
71 |
} |
72 |
} |
73 |
|
74 |
public void clear() { |
75 |
if( this.combo == null ) { |
76 |
DefaultListModel model = new DefaultListModel(); |
77 |
this.list.setModel(model);
|
78 |
} else {
|
79 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
80 |
this.combo.setModel(model);
|
81 |
} |
82 |
} |
83 |
|
84 |
public void add(String label, T value) { |
85 |
ListElement<T> element = new ListElement<>(label,value);
|
86 |
if( this.combo == null ) { |
87 |
DefaultListModel model = (DefaultListModel) this.list.getModel(); |
88 |
model.addElement(element); |
89 |
} else {
|
90 |
DefaultComboBoxModel model = (DefaultComboBoxModel) this.combo.getModel(); |
91 |
model.addElement(element); |
92 |
} |
93 |
} |
94 |
|
95 |
public int getSize() { |
96 |
if( this.combo == null ) { |
97 |
return this.list.getModel().getSize(); |
98 |
} else {
|
99 |
return this.combo.getModel().getSize(); |
100 |
} |
101 |
} |
102 |
|
103 |
public void setSelectedIndex(int i) { |
104 |
if( this.combo == null ) { |
105 |
this.list.setSelectedIndex(i);
|
106 |
} else {
|
107 |
this.combo.setSelectedIndex(i);
|
108 |
} |
109 |
} |
110 |
|
111 |
public void setEnabled(boolean enabled) { |
112 |
if( this.combo == null ) { |
113 |
this.list.setEnabled(enabled);
|
114 |
} else {
|
115 |
this.combo.setEnabled(enabled);
|
116 |
} |
117 |
} |
118 |
|
119 |
public boolean isEnabled() { |
120 |
if( this.combo == null ) { |
121 |
return this.list.isEnabled(); |
122 |
} else {
|
123 |
return this.combo.isEnabled(); |
124 |
} |
125 |
} |
126 |
|
127 |
public void setEditable(boolean editable){ |
128 |
if( this.combo != null ) { |
129 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
130 |
if(editable){
|
131 |
this.combo.setEditable(editable);
|
132 |
toolsSwingManager.addClearButton(this.combo, (ActionEvent e) -> { |
133 |
this.combo.setSelectedIndex(-1); |
134 |
}); |
135 |
toolsSwingManager.setDefaultPopupMenu(this.combo);
|
136 |
} else {
|
137 |
toolsSwingManager.removeClearButton(this.combo);
|
138 |
this.combo.setEditable(editable);
|
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
public void setSelectedValue(T value) { |
144 |
if( this.combo == null ) { |
145 |
ListElement.setSelected(this.list, value);
|
146 |
} else {
|
147 |
ListElement.setSelected(this.combo, value);
|
148 |
} |
149 |
} |
150 |
|
151 |
public T getSelectedValue() {
|
152 |
T value; |
153 |
if( this.combo == null ) { |
154 |
value = (T) ListElement.getSelected(this.list);
|
155 |
} else {
|
156 |
value = (T) ListElement.getSelected(this.combo);
|
157 |
} |
158 |
return value;
|
159 |
} |
160 |
} |
161 |
|
162 |
|
163 |
private final ComboOrList<FeatureAttributeDescriptor> combo; |
164 |
private FeatureType featureType;
|
165 |
private final Predicate<FeatureAttributeDescriptor> filter; |
166 |
|
167 |
public DefaultAttributeDescriptorPickerController(JComboBox combo, Predicate<FeatureAttributeDescriptor> filter) { |
168 |
this(new ComboOrList(combo), filter); |
169 |
combo.setRenderer(new FeatureAttributeListCellRendererImpl());
|
170 |
} |
171 |
|
172 |
public DefaultAttributeDescriptorPickerController(JList list, Predicate<FeatureAttributeDescriptor> filter) { |
173 |
this(new ComboOrList(list), filter); |
174 |
list.setCellRenderer(new FeatureAttributeListCellRendererImpl());
|
175 |
} |
176 |
|
177 |
private DefaultAttributeDescriptorPickerController(ComboOrList combo, Predicate<FeatureAttributeDescriptor> filter) { |
178 |
this.combo = combo;
|
179 |
this.combo.addSelectionListener((ActionEvent e) -> { |
180 |
fireChangeEvent(); |
181 |
}); |
182 |
this.filter = filter;
|
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void setFeatureType(FeatureType featureType) { |
187 |
this.featureType = featureType;
|
188 |
this.combo.clear();
|
189 |
if( this.featureType==null ) { |
190 |
return;
|
191 |
} |
192 |
List<FeatureAttributeDescriptor> attributes = new ArrayList<>(); |
193 |
for (FeatureAttributeDescriptor descriptor : featureType) {
|
194 |
if( filter==null || filter.test(descriptor) ) { |
195 |
attributes.add(descriptor); |
196 |
} |
197 |
} |
198 |
int useLabels = DataManager.USE_LABELS_BOTH; //FIXME |
199 |
attributes.sort((FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) -> { |
200 |
String theLabel1;
|
201 |
String theLabel2;
|
202 |
switch(useLabels) {
|
203 |
case DataManager.USE_LABELS_NO:
|
204 |
theLabel1 = o1.getName(); |
205 |
theLabel2 = o2.getName(); |
206 |
break;
|
207 |
case DataManager.USE_LABELS_YES:
|
208 |
theLabel1 = (o1.hasLabel())? o1.getLocalizedLabel() : o1.getName(); |
209 |
theLabel2 = (o2.hasLabel())? o2.getLocalizedLabel() : o2.getName(); |
210 |
break;
|
211 |
case DataManager.USE_LABELS_BOTH:
|
212 |
default:
|
213 |
theLabel1 = (o1.hasLabel())?String.format("%s [%s]", o1.getLocalizedLabel(), o1.getName()): o1.getName(); |
214 |
theLabel2 = (o2.hasLabel())?String.format("%s [%s]", o2.getLocalizedLabel(), o2.getName()): o2.getName(); |
215 |
break;
|
216 |
} |
217 |
return theLabel1.compareToIgnoreCase(theLabel2);
|
218 |
}); |
219 |
for (FeatureAttributeDescriptor descriptor : attributes) {
|
220 |
String nameDescription = descriptor.getName();// + " [" + descriptor.getDataTypeName() + "]"; |
221 |
this.combo.add(nameDescription, descriptor);
|
222 |
} |
223 |
if( this.combo.getSize()>0 ) { |
224 |
this.combo.setSelectedIndex(0); |
225 |
} else {
|
226 |
this.combo.setSelectedIndex(-1); |
227 |
} |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public FeatureAttributeDescriptor get() {
|
232 |
FeatureAttributeDescriptor descriptor = this.combo.getSelectedValue();
|
233 |
return descriptor;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public void set(FeatureAttributeDescriptor descriptor) { |
238 |
this.combo.setSelectedValue(descriptor);
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void set(String name) { |
243 |
FeatureAttributeDescriptor descriptor = this.featureType.getAttributeDescriptor(name);
|
244 |
if( descriptor == null ) { |
245 |
// throw new IllegalArgumentException("The field '"+name+" don't exists.");
|
246 |
this.combo.setSelectedIndex(-1); |
247 |
return;
|
248 |
} |
249 |
this.set(descriptor);
|
250 |
} |
251 |
|
252 |
@Override
|
253 |
public void coerceAndSet(Object descriptor) { |
254 |
this.set((FeatureAttributeDescriptor) descriptor);
|
255 |
} |
256 |
|
257 |
@Override
|
258 |
public void setEnabled(boolean enabled) { |
259 |
this.combo.setEnabled(enabled);
|
260 |
} |
261 |
|
262 |
@Override
|
263 |
public boolean isEnabled() { |
264 |
return this.combo.isEnabled(); |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public String getName() { |
269 |
Object selected = this.combo.getSelectedValue(); |
270 |
if( selected == null ) { |
271 |
return null; |
272 |
} |
273 |
if(selected instanceof String){ |
274 |
String name = (String)selected; |
275 |
if(StringUtils.isBlank(name)){
|
276 |
return null; |
277 |
} |
278 |
return name;
|
279 |
} |
280 |
|
281 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) selected; |
282 |
return descriptor.getName();
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void clean() { |
287 |
this.combo.setSelectedIndex(-1); |
288 |
} |
289 |
|
290 |
@Override
|
291 |
public void setEditable(boolean editable) { |
292 |
super.setEditable(editable);
|
293 |
this.combo.setEditable(editable);
|
294 |
} |
295 |
|
296 |
} |