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 / searchpanel / SearchFieldController.java @ 44259
History | View | Annotate | Download (11.7 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.event.ItemEvent; |
5 |
import java.awt.event.ItemListener; |
6 |
import java.awt.event.MouseAdapter; |
7 |
import java.awt.event.MouseEvent; |
8 |
import java.util.ArrayList; |
9 |
import java.util.HashMap; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import java.util.Objects; |
13 |
import javax.swing.ComboBoxModel; |
14 |
import javax.swing.DefaultComboBoxModel; |
15 |
import javax.swing.JComboBox; |
16 |
import javax.swing.JLabel; |
17 |
import org.apache.commons.lang3.ObjectUtils; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
20 |
import org.gvsig.fmap.dal.feature.Feature; |
21 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
22 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
23 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
24 |
import org.gvsig.fmap.dal.feature.FeatureType; |
25 |
import org.gvsig.tools.dataTypes.CoercionException; |
26 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
27 |
import org.gvsig.tools.exception.BaseException; |
28 |
import org.gvsig.tools.swing.api.DropDown; |
29 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
30 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
31 |
import org.gvsig.tools.util.LabeledValue; |
32 |
import org.gvsig.tools.util.LabeledValueImpl; |
33 |
import org.gvsig.tools.visitor.VisitCanceledException; |
34 |
import org.gvsig.tools.visitor.Visitor; |
35 |
|
36 |
/**
|
37 |
*
|
38 |
* @author jjdelcerro
|
39 |
*/
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class SearchFieldController { |
42 |
|
43 |
private static class FeatureAttribute extends LabeledValueImpl<String> { |
44 |
|
45 |
FeatureAttributeDescriptor attrdesc; |
46 |
|
47 |
public FeatureAttribute(FeatureAttributeDescriptor attrdesc) {
|
48 |
this(attrdesc, null, null); |
49 |
} |
50 |
|
51 |
public FeatureAttribute(FeatureAttributeDescriptor attrdesc, String label, String value) { |
52 |
super(
|
53 |
ObjectUtils.defaultIfNull(label, attrdesc.getLabel()), |
54 |
ObjectUtils.defaultIfNull(value, attrdesc.getName()) |
55 |
); |
56 |
this.attrdesc = attrdesc;
|
57 |
} |
58 |
|
59 |
public FeatureAttributeDescriptor getDescriptor() {
|
60 |
return this.attrdesc; |
61 |
} |
62 |
|
63 |
public boolean isExpression() { |
64 |
FeatureType type = this.attrdesc.getFeatureType();
|
65 |
if( type == null ) { |
66 |
return false; |
67 |
} |
68 |
Object x = type.get(this.getValue()); |
69 |
return x==null; |
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
private FeatureStore store;
|
75 |
private final JLabel lblFields; |
76 |
private final JLabel lblExtraFields; |
77 |
private final JLabel lblLogicalOperators; |
78 |
private final JLabel lblRelationalOperators; |
79 |
private final JComboBox cboValue; |
80 |
|
81 |
private DropDown ddnFields;
|
82 |
private DropDown ddnLogicalOperators;
|
83 |
private DropDown ddnRelationalOperators;
|
84 |
|
85 |
|
86 |
|
87 |
private final LabeledValue[] relationalOperators = { |
88 |
new LabeledValueImpl("Equals to", ExpressionBuilder.OPERATOR_EQ), |
89 |
new LabeledValueImpl("Like to", ExpressionBuilder.OPERATOR_ILIKE), |
90 |
new LabeledValueImpl("Not equals to", ExpressionBuilder.OPERATOR_NE), |
91 |
new LabeledValueImpl("Greater than", ExpressionBuilder.OPERATOR_GT), |
92 |
new LabeledValueImpl("Greater or equal to", ExpressionBuilder.OPERATOR_GE), |
93 |
new LabeledValueImpl("Less than", ExpressionBuilder.OPERATOR_LT), |
94 |
new LabeledValueImpl("Less or equal to", ExpressionBuilder.OPERATOR_LE) |
95 |
}; |
96 |
private final LabeledValue[] logicalOperators = { |
97 |
new LabeledValueImpl("Or", ExpressionBuilder.OPERATOR_OR), |
98 |
new LabeledValueImpl("And", ExpressionBuilder.OPERATOR_AND) |
99 |
}; |
100 |
|
101 |
public SearchFieldController(
|
102 |
FeatureStore store, |
103 |
JLabel lblFields,
|
104 |
JLabel lblExtraFields,
|
105 |
JLabel lblRelationalOperators,
|
106 |
JComboBox cboValue,
|
107 |
JLabel lblLogicalOperators
|
108 |
) { |
109 |
this.store = store;
|
110 |
this.lblFields = lblFields;
|
111 |
this.lblExtraFields = lblExtraFields;
|
112 |
this.lblRelationalOperators = lblRelationalOperators;
|
113 |
this.cboValue = cboValue;
|
114 |
this.lblLogicalOperators = lblLogicalOperators;
|
115 |
this.initComponents();
|
116 |
} |
117 |
|
118 |
private void initComponents() { |
119 |
try {
|
120 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
121 |
this.lblExtraFields.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
122 |
|
123 |
this.ddnFields = toolsSwingManager.createDropDown(lblFields);
|
124 |
this.ddnRelationalOperators = toolsSwingManager.createDropDown(lblRelationalOperators);
|
125 |
if( lblLogicalOperators!=null ) { |
126 |
this.ddnLogicalOperators = toolsSwingManager.createDropDown(lblLogicalOperators);
|
127 |
} |
128 |
|
129 |
DefaultComboBoxModel modelRelationalOperators = new DefaultComboBoxModel(); |
130 |
for (LabeledValue op : relationalOperators) {
|
131 |
modelRelationalOperators.addElement(op); |
132 |
} |
133 |
this.ddnRelationalOperators.setModel(modelRelationalOperators);
|
134 |
|
135 |
if( this.ddnLogicalOperators!=null ) { |
136 |
DefaultComboBoxModel modelLogicalOperators = new DefaultComboBoxModel(); |
137 |
for (LabeledValue op : logicalOperators) {
|
138 |
modelLogicalOperators.addElement(op); |
139 |
} |
140 |
this.ddnLogicalOperators.setModel(modelLogicalOperators);
|
141 |
} |
142 |
|
143 |
List<FeatureAttributeDescriptor> orderedAttributes = SearchUtils.getOrderedAttributes(
|
144 |
this.store,
|
145 |
SearchUtils.BASIC_TYPES_FILTER, |
146 |
SearchUtils.STR_INT_LONG_LABEL_ORDER, |
147 |
20
|
148 |
); |
149 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
150 |
for (FeatureAttributeDescriptor attrdesc : orderedAttributes) {
|
151 |
model.addElement(new FeatureAttribute(attrdesc));
|
152 |
} |
153 |
|
154 |
this.ddnFields.setModel(model);
|
155 |
this.ddnFields.addItemListener(new ItemListener() { |
156 |
@Override
|
157 |
public void itemStateChanged(ItemEvent e) { |
158 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
159 |
doUpdateValuesList(); |
160 |
} |
161 |
|
162 |
} |
163 |
}); |
164 |
|
165 |
this.lblExtraFields.addMouseListener(new MouseAdapter() { |
166 |
@Override
|
167 |
public void mouseClicked(MouseEvent e) { |
168 |
doSelectExtraField(); |
169 |
} |
170 |
}); |
171 |
clear(); |
172 |
} catch (Exception ex) { |
173 |
throw new RuntimeException(ex); |
174 |
} |
175 |
} |
176 |
|
177 |
private void doSelectExtraField() { |
178 |
|
179 |
} |
180 |
|
181 |
public void clear() { |
182 |
this.ddnRelationalOperators.setSelectedIndex(0); |
183 |
if( this.ddnLogicalOperators!=null ) { |
184 |
this.ddnLogicalOperators.setSelectedIndex(0); |
185 |
} |
186 |
this.cboValue.setSelectedIndex(-1); |
187 |
} |
188 |
|
189 |
private void doUpdateValuesList() { |
190 |
final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
191 |
if( attribute==null ) { |
192 |
return;
|
193 |
} |
194 |
final List<Object> values = new ArrayList<>(); |
195 |
final int limit = 60; |
196 |
final long timeLimit = System.currentTimeMillis() + limit * 1000; |
197 |
try {
|
198 |
FeatureSet set = this.store.getFeatureSet();
|
199 |
set.accept(new Visitor() {
|
200 |
@Override
|
201 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
202 |
Object value = ((Feature) o).get(attribute.getDescriptor().getName());
|
203 |
if (!values.contains(value)) {
|
204 |
values.add(value); |
205 |
} |
206 |
if (System.currentTimeMillis() > timeLimit) { |
207 |
throw new VisitCanceledException(); |
208 |
} |
209 |
if (values.size()>1000 ) { |
210 |
throw new VisitCanceledException(); |
211 |
} |
212 |
} |
213 |
}); |
214 |
} catch (VisitCanceledException ex) {
|
215 |
|
216 |
} catch (Exception ex) { |
217 |
// FIXME
|
218 |
} |
219 |
List<LabeledValue> elements = new ArrayList<>(); |
220 |
if (!values.isEmpty()) {
|
221 |
LabeledValue[] availableValues = attribute.getDescriptor().getAvailableValues();
|
222 |
Map<String, String> availableValuesMap = new HashMap<>(); |
223 |
if (availableValues != null) { |
224 |
for (LabeledValue availableValue : availableValues) {
|
225 |
availableValuesMap.put( |
226 |
Objects.toString(availableValue.getValue()), |
227 |
availableValue.getLabel() |
228 |
); |
229 |
} |
230 |
} |
231 |
elements.add(new LabeledValueImpl("", null)); |
232 |
for (Object value : values) { |
233 |
String key = Objects.toString(value);
|
234 |
String label = availableValuesMap.getOrDefault(key, key);
|
235 |
elements.add(new LabeledValueImpl(label, value));
|
236 |
} |
237 |
} |
238 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
239 |
for (LabeledValue element : elements) {
|
240 |
model.addElement(element); |
241 |
} |
242 |
this.cboValue.setModel(model);
|
243 |
} |
244 |
|
245 |
public String getRelationalOperator() { |
246 |
LabeledValue<String> op = (LabeledValue) this.ddnRelationalOperators.getSelectedItem(); |
247 |
if( op == null ) { |
248 |
return null; |
249 |
} |
250 |
return op.getValue();
|
251 |
} |
252 |
|
253 |
public String getLogicalOperator() { |
254 |
if( this.ddnLogicalOperators==null ) { |
255 |
return null; |
256 |
} |
257 |
LabeledValue<String> rel = (LabeledValue) this.ddnLogicalOperators.getSelectedItem(); |
258 |
if( rel == null ) { |
259 |
return null; |
260 |
} |
261 |
return rel.getValue();
|
262 |
} |
263 |
|
264 |
public Object getValue() { |
265 |
final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
266 |
if( attribute==null ) { |
267 |
return null; |
268 |
} |
269 |
Object v = this.cboValue.getSelectedItem(); |
270 |
if( v == null ) { |
271 |
return null; |
272 |
} |
273 |
if( v instanceof LabeledValue ) { |
274 |
v = ((LabeledValue)v).getValue(); |
275 |
if( v == null ) { |
276 |
return null; |
277 |
} |
278 |
} |
279 |
if( v instanceof CharSequence ) { |
280 |
if( StringUtils.isBlank((CharSequence) v)) { |
281 |
return null; |
282 |
} |
283 |
} |
284 |
DataTypesManager.Coercion coercion = attribute.getDescriptor().getDataType().getCoercion(); |
285 |
try {
|
286 |
return coercion.coerce(v);
|
287 |
} catch (CoercionException ex) {
|
288 |
return null; |
289 |
} |
290 |
} |
291 |
|
292 |
public boolean isAttributeAnExpression() { |
293 |
final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
294 |
if( attribute==null ) { |
295 |
return false; |
296 |
} |
297 |
return attribute.isExpression();
|
298 |
} |
299 |
|
300 |
public String getAttribute() { |
301 |
final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
302 |
if( attribute==null ) { |
303 |
return null; |
304 |
} |
305 |
SearchUtils.getRecentUseds().add(attribute.getDescriptor()); |
306 |
return attribute.getValue();
|
307 |
} |
308 |
|
309 |
public void setAttribute(String name) { |
310 |
ComboBoxModel<FeatureAttribute> model = this.ddnFields.getModel(); |
311 |
for (int i = 0; i < model.getSize(); i++) { |
312 |
FeatureAttribute x = model.getElementAt(i); |
313 |
if( StringUtils.equalsIgnoreCase(name, x.getLabel())) {
|
314 |
this.ddnFields.setSelectedIndex(i);
|
315 |
return;
|
316 |
} |
317 |
} |
318 |
this.ddnFields.setSelectedIndex(-1); |
319 |
} |
320 |
|
321 |
public void setAttribute(int index) { |
322 |
try {
|
323 |
this.ddnFields.setSelectedIndex(index);
|
324 |
} catch(Exception ex) { |
325 |
this.ddnFields.setSelectedIndex(-1); |
326 |
} |
327 |
} |
328 |
|
329 |
} |