Revision 44215 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
DefaultAttributeDescriptorPickerController.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
2 | 2 |
|
3 |
import java.awt.event.ActionEvent; |
|
4 |
import java.awt.event.ActionListener; |
|
3 | 5 |
import org.gvsig.fmap.dal.swing.featuretype.AttributeDescriptorPickerController; |
4 | 6 |
import java.awt.event.ItemEvent; |
5 | 7 |
import java.awt.event.ItemListener; |
... | ... | |
8 | 10 |
import java.util.List; |
9 | 11 |
import java.util.function.Predicate; |
10 | 12 |
import javax.swing.DefaultComboBoxModel; |
13 |
import javax.swing.DefaultListModel; |
|
11 | 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; |
|
12 | 19 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
13 | 20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.tools.swing.api.Component; |
|
14 | 22 |
import org.gvsig.tools.swing.api.ListElement; |
15 | 23 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
16 | 24 |
|
... | ... | |
23 | 31 |
implements AttributeDescriptorPickerController |
24 | 32 |
{ |
25 | 33 |
|
26 |
private final JComboBox combo; |
|
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; |
|
27 | 153 |
private FeatureType featureType; |
28 | 154 |
private final Predicate<FeatureAttributeDescriptor> filter; |
29 | 155 |
|
30 | 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) { |
|
31 | 165 |
this.combo = combo; |
32 |
this.combo.setEditable(false); |
|
33 |
this.combo.addItemListener(new ItemListener() { |
|
166 |
this.combo.addSelectionListener(new ActionListener() { |
|
34 | 167 |
@Override |
35 |
public void itemStateChanged(ItemEvent e) {
|
|
168 |
public void actionPerformed(ActionEvent e) {
|
|
36 | 169 |
fireChangeEvent(); |
37 | 170 |
} |
38 | 171 |
}); |
39 | 172 |
this.filter = filter; |
40 | 173 |
} |
41 | 174 |
|
175 |
@Override |
|
42 | 176 |
public void setFeatureType(FeatureType featureType) { |
43 | 177 |
this.featureType = featureType; |
178 |
this.combo.clear(); |
|
179 |
if( this.featureType==null ) { |
|
180 |
return; |
|
181 |
} |
|
44 | 182 |
List<FeatureAttributeDescriptor> attributes = new ArrayList<>(); |
45 | 183 |
for (FeatureAttributeDescriptor descriptor : featureType) { |
46 | 184 |
if( filter==null || filter.test(descriptor) ) { |
... | ... | |
53 | 191 |
return o1.getName().compareToIgnoreCase(o2.getName()); |
54 | 192 |
} |
55 | 193 |
}); |
56 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
|
57 | 194 |
for (FeatureAttributeDescriptor descriptor : attributes) { |
58 | 195 |
String nameDescription = descriptor.getName() + " [" + descriptor.getDataTypeName() + "]"; |
59 |
model.addElement(new ListElement<>(nameDescription, descriptor));
|
|
196 |
this.combo.add(nameDescription, descriptor);
|
|
60 | 197 |
} |
61 |
this.combo.setModel(model); |
|
62 |
if( this.combo.getModel().getSize()>0 ) { |
|
198 |
if( this.combo.getSize()>0 ) { |
|
63 | 199 |
this.combo.setSelectedIndex(0); |
64 | 200 |
} else { |
65 | 201 |
this.combo.setSelectedIndex(-1); |
... | ... | |
68 | 204 |
|
69 | 205 |
@Override |
70 | 206 |
public FeatureAttributeDescriptor get() { |
71 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) ListElement.getSelected(this.combo);
|
|
207 |
FeatureAttributeDescriptor descriptor = this.combo.getSelectedValue();
|
|
72 | 208 |
return descriptor; |
73 | 209 |
} |
74 | 210 |
|
75 | 211 |
@Override |
76 | 212 |
public void set(FeatureAttributeDescriptor descriptor) { |
77 |
ListElement.setSelected(this.combo, descriptor);
|
|
213 |
this.combo.setSelectedValue(descriptor);
|
|
78 | 214 |
} |
79 | 215 |
|
80 | 216 |
@Override |
Also available in: Unified diff