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 @ 44669
History | View | Annotate | Download (21.1 KB)
1 | 44259 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|---|---|
2 | |||
3 | import java.awt.Cursor; |
||
4 | 44262 | jjdelcerro | import java.awt.Dimension; |
5 | import java.awt.event.ActionEvent; |
||
6 | import java.awt.event.ActionListener; |
||
7 | 44259 | jjdelcerro | import java.awt.event.ItemEvent; |
8 | import java.awt.event.ItemListener; |
||
9 | import java.awt.event.MouseAdapter; |
||
10 | import java.awt.event.MouseEvent; |
||
11 | import java.util.ArrayList; |
||
12 | import java.util.HashMap; |
||
13 | import java.util.List; |
||
14 | import java.util.Map; |
||
15 | import java.util.Objects; |
||
16 | import javax.swing.ComboBoxModel; |
||
17 | import javax.swing.DefaultComboBoxModel; |
||
18 | 44263 | jjdelcerro | import javax.swing.ImageIcon; |
19 | 44259 | jjdelcerro | import javax.swing.JComboBox; |
20 | import javax.swing.JLabel; |
||
21 | 44262 | jjdelcerro | import javax.swing.JScrollPane; |
22 | import javax.swing.JTree; |
||
23 | import javax.swing.SwingUtilities; |
||
24 | import javax.swing.tree.TreePath; |
||
25 | 44259 | jjdelcerro | import org.apache.commons.lang3.ObjectUtils; |
26 | import org.apache.commons.lang3.StringUtils; |
||
27 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
28 | 44262 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
29 | import org.gvsig.fmap.dal.DataManager; |
||
30 | import org.gvsig.fmap.dal.complements.Search; |
||
31 | import org.gvsig.fmap.dal.exception.DataException; |
||
32 | 44259 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
33 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
34 | 44374 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
35 | 44259 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSet; |
36 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
37 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
38 | 44262 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.searchpanel.AdvancedAttributeSelectionTreeModel.Node; |
39 | 44351 | jjdelcerro | import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel; |
40 | 44262 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
41 | 44669 | jjdelcerro | import org.gvsig.tools.dataTypes.Coercion; |
42 | 44259 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
43 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
44 | import org.gvsig.tools.exception.BaseException; |
||
45 | import org.gvsig.tools.swing.api.DropDown; |
||
46 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
47 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
48 | 44262 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
49 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
50 | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
||
51 | 44263 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
52 | 44259 | jjdelcerro | import org.gvsig.tools.util.LabeledValue; |
53 | import org.gvsig.tools.util.LabeledValueImpl; |
||
54 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
55 | import org.gvsig.tools.visitor.Visitor; |
||
56 | 44262 | jjdelcerro | import org.slf4j.Logger; |
57 | import org.slf4j.LoggerFactory; |
||
58 | 44259 | jjdelcerro | |
59 | /**
|
||
60 | *
|
||
61 | * @author jjdelcerro
|
||
62 | */
|
||
63 | @SuppressWarnings("UseSpecificCatch") |
||
64 | public class SearchFieldController { |
||
65 | |||
66 | 44262 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(SearchFieldController.class); |
67 | |||
68 | 44259 | jjdelcerro | private static class FeatureAttribute extends LabeledValueImpl<String> { |
69 | |||
70 | FeatureAttributeDescriptor attrdesc; |
||
71 | 44262 | jjdelcerro | private final FeatureStore store; |
72 | 44337 | jjdelcerro | private final int type; |
73 | 44351 | jjdelcerro | private final boolean showStoreName; |
74 | 44259 | jjdelcerro | |
75 | 44337 | jjdelcerro | public FeatureAttribute(FeatureStore store, FeatureAttributeDescriptor attrdesc, int type) { |
76 | 44351 | jjdelcerro | this(store, attrdesc, null, type, false); |
77 | 44259 | jjdelcerro | } |
78 | 44262 | jjdelcerro | |
79 | public FeatureAttribute(
|
||
80 | FeatureStore store, |
||
81 | FeatureAttributeDescriptor attrdesc, |
||
82 | 44337 | jjdelcerro | String value,
|
83 | 44351 | jjdelcerro | int type,
|
84 | boolean showStoreName
|
||
85 | 44262 | jjdelcerro | ) { |
86 | 44259 | jjdelcerro | super(
|
87 | 44351 | jjdelcerro | getAttributeDescriptorLabel(attrdesc, store.getName()), |
88 | 44259 | jjdelcerro | ObjectUtils.defaultIfNull(value, attrdesc.getName()) |
89 | ); |
||
90 | 44262 | jjdelcerro | this.store = store;
|
91 | 44259 | jjdelcerro | this.attrdesc = attrdesc;
|
92 | 44337 | jjdelcerro | this.type = type;
|
93 | 44351 | jjdelcerro | this.showStoreName = showStoreName;
|
94 | 44259 | jjdelcerro | } |
95 | 44262 | jjdelcerro | |
96 | 44337 | jjdelcerro | @Override
|
97 | public String getLabel() { |
||
98 | 44351 | jjdelcerro | String theLabel = getAttributeDescriptorLabel(attrdesc, showStoreName? store.getName():null); |
99 | 44337 | jjdelcerro | switch(this.type) { |
100 | case Search.OrderedAttribute.TYPE_REGURAL:
|
||
101 | break;
|
||
102 | case Search.OrderedAttribute.TYPE_FAVORITE:
|
||
103 | 44338 | jjdelcerro | theLabel = "<html><b>"+theLabel+"</b></html>"; |
104 | 44337 | jjdelcerro | break;
|
105 | case Search.OrderedAttribute.TYPE_RECENT:
|
||
106 | 44338 | jjdelcerro | theLabel = "<html><i><b>"+theLabel+"</b></i></html>"; |
107 | 44337 | jjdelcerro | break;
|
108 | } |
||
109 | return theLabel;
|
||
110 | } |
||
111 | |||
112 | 44259 | jjdelcerro | public FeatureAttributeDescriptor getDescriptor() {
|
113 | return this.attrdesc; |
||
114 | } |
||
115 | 44262 | jjdelcerro | |
116 | public FeatureStore getFeatureStore() {
|
||
117 | return this.store; |
||
118 | } |
||
119 | 44259 | jjdelcerro | |
120 | public boolean isExpression() { |
||
121 | FeatureType type = this.attrdesc.getFeatureType();
|
||
122 | 44262 | jjdelcerro | if (type == null) { |
123 | 44259 | jjdelcerro | return false; |
124 | } |
||
125 | Object x = type.get(this.getValue()); |
||
126 | 44262 | jjdelcerro | return x == null; |
127 | 44259 | jjdelcerro | } |
128 | |||
129 | } |
||
130 | |||
131 | private FeatureStore store;
|
||
132 | private final JLabel lblFields; |
||
133 | private final JLabel lblExtraFields; |
||
134 | private final JLabel lblLogicalOperators; |
||
135 | private final JLabel lblRelationalOperators; |
||
136 | private final JComboBox cboValue; |
||
137 | 44292 | jjdelcerro | private Object valueAssigned = null; |
138 | 44259 | jjdelcerro | |
139 | private DropDown ddnFields;
|
||
140 | private DropDown ddnLogicalOperators;
|
||
141 | private DropDown ddnRelationalOperators;
|
||
142 | |||
143 | private final LabeledValue[] relationalOperators = { |
||
144 | new LabeledValueImpl("Equals to", ExpressionBuilder.OPERATOR_EQ), |
||
145 | new LabeledValueImpl("Like to", ExpressionBuilder.OPERATOR_ILIKE), |
||
146 | new LabeledValueImpl("Not equals to", ExpressionBuilder.OPERATOR_NE), |
||
147 | new LabeledValueImpl("Greater than", ExpressionBuilder.OPERATOR_GT), |
||
148 | new LabeledValueImpl("Greater or equal to", ExpressionBuilder.OPERATOR_GE), |
||
149 | new LabeledValueImpl("Less than", ExpressionBuilder.OPERATOR_LT), |
||
150 | new LabeledValueImpl("Less or equal to", ExpressionBuilder.OPERATOR_LE) |
||
151 | }; |
||
152 | private final LabeledValue[] logicalOperators = { |
||
153 | new LabeledValueImpl("Or", ExpressionBuilder.OPERATOR_OR), |
||
154 | new LabeledValueImpl("And", ExpressionBuilder.OPERATOR_AND) |
||
155 | }; |
||
156 | |||
157 | public SearchFieldController(
|
||
158 | FeatureStore store, |
||
159 | JLabel lblFields,
|
||
160 | JLabel lblExtraFields,
|
||
161 | JLabel lblRelationalOperators,
|
||
162 | JComboBox cboValue,
|
||
163 | JLabel lblLogicalOperators
|
||
164 | ) { |
||
165 | this.store = store;
|
||
166 | this.lblFields = lblFields;
|
||
167 | this.lblExtraFields = lblExtraFields;
|
||
168 | this.lblRelationalOperators = lblRelationalOperators;
|
||
169 | this.cboValue = cboValue;
|
||
170 | this.lblLogicalOperators = lblLogicalOperators;
|
||
171 | this.initComponents();
|
||
172 | } |
||
173 | |||
174 | 44292 | jjdelcerro | public boolean isAValidRelationOperator(String name) { |
175 | for (LabeledValue relationalOperator : relationalOperators) {
|
||
176 | if( StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
||
177 | return true; |
||
178 | } |
||
179 | } |
||
180 | return false; |
||
181 | } |
||
182 | |||
183 | 44259 | jjdelcerro | private void initComponents() { |
184 | try {
|
||
185 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
186 | this.lblExtraFields.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
||
187 | |||
188 | this.ddnFields = toolsSwingManager.createDropDown(lblFields);
|
||
189 | 44338 | jjdelcerro | this.ddnFields.setVisibleDropdownArrow(false); |
190 | 44259 | jjdelcerro | this.ddnRelationalOperators = toolsSwingManager.createDropDown(lblRelationalOperators);
|
191 | 44338 | jjdelcerro | this.ddnRelationalOperators.setVisibleDropdownArrow(false); |
192 | 44262 | jjdelcerro | if (lblLogicalOperators != null) { |
193 | 44259 | jjdelcerro | this.ddnLogicalOperators = toolsSwingManager.createDropDown(lblLogicalOperators);
|
194 | 44338 | jjdelcerro | this.ddnLogicalOperators.setVisibleDropdownArrow(false); |
195 | 44259 | jjdelcerro | } |
196 | |||
197 | DefaultComboBoxModel modelRelationalOperators = new DefaultComboBoxModel(); |
||
198 | for (LabeledValue op : relationalOperators) {
|
||
199 | modelRelationalOperators.addElement(op); |
||
200 | } |
||
201 | this.ddnRelationalOperators.setModel(modelRelationalOperators);
|
||
202 | |||
203 | 44262 | jjdelcerro | if (this.ddnLogicalOperators != null) { |
204 | 44259 | jjdelcerro | DefaultComboBoxModel modelLogicalOperators = new DefaultComboBoxModel(); |
205 | for (LabeledValue op : logicalOperators) {
|
||
206 | modelLogicalOperators.addElement(op); |
||
207 | } |
||
208 | this.ddnLogicalOperators.setModel(modelLogicalOperators);
|
||
209 | } |
||
210 | 44262 | jjdelcerro | FeatureType featureType = store.getDefaultFeatureType(); |
211 | Search search = (Search) ToolsLocator.getComplementsManager().get( |
||
212 | Search.COMPLEMENT_MANE, featureType |
||
213 | ); |
||
214 | 44337 | jjdelcerro | List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
215 | 44262 | jjdelcerro | Search.BASIC_TYPES_FILTER, |
216 | Search.STR_INT_LONG_LABEL_ORDER, |
||
217 | 44259 | jjdelcerro | 20
|
218 | ); |
||
219 | 44263 | jjdelcerro | List<ImageIcon>icons = new ArrayList<>(); |
220 | 44338 | jjdelcerro | // DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
|
221 | 44263 | jjdelcerro | IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
222 | 44259 | jjdelcerro | DefaultComboBoxModel model = new DefaultComboBoxModel(); |
223 | 44337 | jjdelcerro | for (Search.OrderedAttribute attr : orderedAttributes) {
|
224 | FeatureAttributeDescriptor attrdesc = attr.getDescriptor(); |
||
225 | model.addElement(new FeatureAttribute(this.store, attrdesc, attr.getType())); |
||
226 | 44263 | jjdelcerro | String iconName = attrdesc.getDataType().getIconName();
|
227 | if( iconTheme.exists(iconName) ) {
|
||
228 | icons.add(iconTheme.get(iconName)); |
||
229 | } else {
|
||
230 | icons.add(null);
|
||
231 | } |
||
232 | 44259 | jjdelcerro | } |
233 | 44263 | jjdelcerro | this.ddnFields.setIcons(icons);
|
234 | 44259 | jjdelcerro | this.ddnFields.setModel(model);
|
235 | this.ddnFields.addItemListener(new ItemListener() { |
||
236 | @Override
|
||
237 | public void itemStateChanged(ItemEvent e) { |
||
238 | if (e.getStateChange() == ItemEvent.SELECTED) { |
||
239 | doUpdateValuesList(); |
||
240 | } |
||
241 | |||
242 | } |
||
243 | }); |
||
244 | 44262 | jjdelcerro | |
245 | 44259 | jjdelcerro | this.lblExtraFields.addMouseListener(new MouseAdapter() { |
246 | @Override
|
||
247 | public void mouseClicked(MouseEvent e) { |
||
248 | doSelectExtraField(); |
||
249 | } |
||
250 | }); |
||
251 | clear(); |
||
252 | } catch (Exception ex) { |
||
253 | throw new RuntimeException(ex); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | 44262 | jjdelcerro | private FeatureType getFeatureType() {
|
258 | try {
|
||
259 | return this.store.getDefaultFeatureType(); |
||
260 | } catch (DataException ex) {
|
||
261 | return null; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | 44259 | jjdelcerro | private void doSelectExtraField() { |
266 | 44262 | jjdelcerro | FeatureType featureType = this.getFeatureType();
|
267 | AdvancedAttributeSelectionTreeModel treeModel = new AdvancedAttributeSelectionTreeModel(
|
||
268 | this.store,
|
||
269 | 44263 | jjdelcerro | Search.BASIC_TYPES_FILTER |
270 | 44262 | jjdelcerro | ); |
271 | final JTree tree = new JTree(); |
||
272 | tree.setCellRenderer(new AdvancedAttributeSelectionTreeCellRenderer());
|
||
273 | tree.setModel(treeModel); |
||
274 | 44338 | jjdelcerro | try {
|
275 | tree.setSelectionRow(1);
|
||
276 | } catch(Throwable th) { |
||
277 | } |
||
278 | 44262 | jjdelcerro | JScrollPane scrollpane = new JScrollPane(tree); |
279 | scrollpane.setPreferredSize(new Dimension(400, 300)); |
||
280 | scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||
281 | scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
||
282 | |||
283 | WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
284 | final Dialog dialog = winManager.createDialog( |
||
285 | scrollpane, |
||
286 | "Select attribute",
|
||
287 | null,
|
||
288 | WindowManager_v2.BUTTONS_OK_CANCEL |
||
289 | ); |
||
290 | dialog.addActionListener(new ActionListener() { |
||
291 | @Override
|
||
292 | public void actionPerformed(ActionEvent e) { |
||
293 | if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) {
|
||
294 | TreePath path = tree.getSelectionPath();
|
||
295 | doAddAndSelect(path.getPath()); |
||
296 | } |
||
297 | } |
||
298 | }); |
||
299 | dialog.show(WindowManager.MODE.DIALOG); |
||
300 | |||
301 | 44259 | jjdelcerro | } |
302 | 44262 | jjdelcerro | |
303 | private void doAddAndSelect(Object[] nodes) { |
||
304 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
305 | 44376 | jjdelcerro | List<String> list = new ArrayList<>(); |
306 | 44262 | jjdelcerro | for (int i = 1; i < nodes.length; i++) { |
307 | Node node = (Node) nodes[i]; |
||
308 | FeatureAttributeDescriptor attrdesc = node.getValue(); |
||
309 | 44376 | jjdelcerro | list.add(attrdesc.getName()); |
310 | 44262 | jjdelcerro | } |
311 | Node node = (Node) nodes[nodes.length - 1];
|
||
312 | FeatureStore theStore = node.getFeatureStore(); |
||
313 | FeatureAttributeDescriptor attrdesc = node.getValue(); |
||
314 | String storeFullName = theStore.getFullName();
|
||
315 | DefaultComboBoxModel<FeatureAttribute> model = (DefaultComboBoxModel) this.ddnFields.getModel(); |
||
316 | for (int i = 0; i < model.getSize(); i++) { |
||
317 | FeatureAttribute attr = model.getElementAt(i); |
||
318 | FeatureAttributeDescriptor attrdescN = attr.getDescriptor(); |
||
319 | if (StringUtils.equalsIgnoreCase(storeFullName, attrdescN.getStore().getFullName())
|
||
320 | && StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdescN.getName())) { |
||
321 | this.setAttribute(i);
|
||
322 | return;
|
||
323 | } |
||
324 | } |
||
325 | 44351 | jjdelcerro | String formula;
|
326 | boolean showStoreName;
|
||
327 | 44262 | jjdelcerro | if (StringUtils.equalsIgnoreCase(storeFullName, this.store.getFullName())) { |
328 | formula = attrdesc.getName(); |
||
329 | 44351 | jjdelcerro | showStoreName = false;
|
330 | } else {
|
||
331 | 44376 | jjdelcerro | formula = builder.function( |
332 | DataManager.FUNCTION_FOREING_VALUE, |
||
333 | builder.constant(StringUtils.join(list,"."))
|
||
334 | ).toString(); |
||
335 | 44351 | jjdelcerro | showStoreName = true;
|
336 | 44262 | jjdelcerro | } |
337 | 44351 | jjdelcerro | FeatureAttribute attribute = new FeatureAttribute(theStore, attrdesc, formula, Search.OrderedAttribute.TYPE_REGURAL, showStoreName);
|
338 | 44262 | jjdelcerro | model.addElement(attribute); |
339 | 44263 | jjdelcerro | IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
340 | this.ddnFields.getIcons().add(iconTheme.get(attrdesc.getDataType().getIconName()));
|
||
341 | 44262 | jjdelcerro | this.setAttribute(model.getSize() - 1); |
342 | } |
||
343 | |||
344 | 44259 | jjdelcerro | public void clear() { |
345 | this.ddnRelationalOperators.setSelectedIndex(0); |
||
346 | 44262 | jjdelcerro | if (this.ddnLogicalOperators != null) { |
347 | 44259 | jjdelcerro | this.ddnLogicalOperators.setSelectedIndex(0); |
348 | } |
||
349 | this.cboValue.setSelectedIndex(-1); |
||
350 | } |
||
351 | 44262 | jjdelcerro | |
352 | 44259 | jjdelcerro | private void doUpdateValuesList() { |
353 | final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
||
354 | 44262 | jjdelcerro | if (attribute == null) { |
355 | 44259 | jjdelcerro | return;
|
356 | 44262 | jjdelcerro | } |
357 | |||
358 | 44259 | jjdelcerro | final List<Object> values = new ArrayList<>(); |
359 | final int limit = 60; |
||
360 | final long timeLimit = System.currentTimeMillis() + limit * 1000; |
||
361 | 44262 | jjdelcerro | final DefaultComboBoxModel model = new DefaultComboBoxModel(); |
362 | this.setEnabled(false); |
||
363 | 44374 | jjdelcerro | final FeatureStore theStore = attribute.getFeatureStore();
|
364 | final FeatureQuery query = theStore.createFeatureQuery();
|
||
365 | query.addAttributeName(attribute.getDescriptor().getName()); |
||
366 | query.setGroup(true);
|
||
367 | query.setLimit(1000);
|
||
368 | 44262 | jjdelcerro | Thread th = new Thread(new Runnable() { |
369 | @Override
|
||
370 | public void run() { |
||
371 | try {
|
||
372 | 44374 | jjdelcerro | FeatureSet set = theStore.getFeatureSet(query); |
373 | 44262 | jjdelcerro | set.accept(new Visitor() {
|
374 | @Override
|
||
375 | public void visit(Object o) throws VisitCanceledException, BaseException { |
||
376 | Object value = ((Feature) o).get(attribute.getDescriptor().getName());
|
||
377 | if (!values.contains(value)) {
|
||
378 | values.add(value); |
||
379 | } |
||
380 | if (System.currentTimeMillis() > timeLimit) { |
||
381 | throw new VisitCanceledException(); |
||
382 | } |
||
383 | if (values.size() > 1000) { |
||
384 | throw new VisitCanceledException(); |
||
385 | } |
||
386 | } |
||
387 | }); |
||
388 | } catch (VisitCanceledException ex) {
|
||
389 | |||
390 | } catch (Exception ex) { |
||
391 | LOGGER.warn("Can't update list of values of '"+attribute.getLabel()+"'.", ex); |
||
392 | } |
||
393 | List<LabeledValue> elements = new ArrayList<>(); |
||
394 | if (!values.isEmpty()) {
|
||
395 | LabeledValue[] availableValues = attribute.getDescriptor().getAvailableValues();
|
||
396 | Map<String, String> availableValuesMap = new HashMap<>(); |
||
397 | if (availableValues != null) { |
||
398 | for (LabeledValue availableValue : availableValues) {
|
||
399 | availableValuesMap.put( |
||
400 | Objects.toString(availableValue.getValue()), |
||
401 | availableValue.getLabel() |
||
402 | ); |
||
403 | } |
||
404 | 44259 | jjdelcerro | } |
405 | 44262 | jjdelcerro | elements.add(new LabeledValueImpl("", null)); |
406 | for (Object value : values) { |
||
407 | String key = Objects.toString(value);
|
||
408 | String label = availableValuesMap.getOrDefault(key, key);
|
||
409 | elements.add(new LabeledValueImpl(label, value));
|
||
410 | 44259 | jjdelcerro | } |
411 | 44262 | jjdelcerro | elements.sort(null);
|
412 | |||
413 | 44259 | jjdelcerro | } |
414 | 44262 | jjdelcerro | for (LabeledValue element : elements) {
|
415 | model.addElement(element); |
||
416 | 44259 | jjdelcerro | } |
417 | 44262 | jjdelcerro | SwingUtilities.invokeLater(new Runnable() { |
418 | @Override
|
||
419 | public void run() { |
||
420 | cboValue.setModel(model); |
||
421 | 44292 | jjdelcerro | if( valueAssigned!=null ) { |
422 | cboValue.setSelectedItem(valueAssigned); |
||
423 | valueAssigned = null;
|
||
424 | } |
||
425 | 44262 | jjdelcerro | setEnabled(true);
|
426 | } |
||
427 | }); |
||
428 | 44259 | jjdelcerro | } |
429 | 44262 | jjdelcerro | }); |
430 | th.start(); |
||
431 | } |
||
432 | |||
433 | public void setEnabled(boolean enabled) { |
||
434 | this.ddnFields.setEnabled(enabled);
|
||
435 | if( this.ddnLogicalOperators!=null ) { |
||
436 | this.ddnLogicalOperators.setEnabled(enabled);
|
||
437 | 44259 | jjdelcerro | } |
438 | 44262 | jjdelcerro | this.ddnRelationalOperators.setEnabled(enabled);
|
439 | this.lblExtraFields.setEnabled(enabled);
|
||
440 | 44259 | jjdelcerro | } |
441 | 44262 | jjdelcerro | |
442 | 44259 | jjdelcerro | public String getRelationalOperator() { |
443 | LabeledValue<String> op = (LabeledValue) this.ddnRelationalOperators.getSelectedItem(); |
||
444 | 44262 | jjdelcerro | if (op == null) { |
445 | 44259 | jjdelcerro | return null; |
446 | } |
||
447 | return op.getValue();
|
||
448 | } |
||
449 | 44262 | jjdelcerro | |
450 | 44292 | jjdelcerro | public int setRelationalOperator(String name) { |
451 | int n = 0; |
||
452 | for (LabeledValue relationalOperator : relationalOperators) {
|
||
453 | if( StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
||
454 | break;
|
||
455 | } |
||
456 | n++; |
||
457 | } |
||
458 | if( this.relationalOperators.length<=n ) { |
||
459 | return -1; |
||
460 | } |
||
461 | this.ddnRelationalOperators.setSelectedIndex(n);
|
||
462 | return n;
|
||
463 | } |
||
464 | |||
465 | 44259 | jjdelcerro | public String getLogicalOperator() { |
466 | 44262 | jjdelcerro | if (this.ddnLogicalOperators == null) { |
467 | 44259 | jjdelcerro | return null; |
468 | } |
||
469 | LabeledValue<String> rel = (LabeledValue) this.ddnLogicalOperators.getSelectedItem(); |
||
470 | 44262 | jjdelcerro | if (rel == null) { |
471 | 44259 | jjdelcerro | return null; |
472 | } |
||
473 | return rel.getValue();
|
||
474 | } |
||
475 | 44262 | jjdelcerro | |
476 | 44259 | jjdelcerro | public Object getValue() { |
477 | final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
||
478 | 44262 | jjdelcerro | if (attribute == null) { |
479 | 44259 | jjdelcerro | return null; |
480 | } |
||
481 | Object v = this.cboValue.getSelectedItem(); |
||
482 | 44262 | jjdelcerro | if (v == null) { |
483 | 44259 | jjdelcerro | return null; |
484 | } |
||
485 | 44262 | jjdelcerro | if (v instanceof LabeledValue) { |
486 | v = ((LabeledValue) v).getValue(); |
||
487 | if (v == null) { |
||
488 | 44259 | jjdelcerro | return null; |
489 | } |
||
490 | } |
||
491 | 44262 | jjdelcerro | if (v instanceof CharSequence) { |
492 | if (StringUtils.isBlank((CharSequence) v)) { |
||
493 | 44259 | jjdelcerro | return null; |
494 | } |
||
495 | } |
||
496 | 44669 | jjdelcerro | Coercion coercion = attribute.getDescriptor().getDataType().getCoercion(); |
497 | 44259 | jjdelcerro | try {
|
498 | return coercion.coerce(v);
|
||
499 | } catch (CoercionException ex) {
|
||
500 | return null; |
||
501 | } |
||
502 | } |
||
503 | 44292 | jjdelcerro | |
504 | public void setValue(Object value) { |
||
505 | this.cboValue.setSelectedItem(value);
|
||
506 | this.valueAssigned = value;
|
||
507 | } |
||
508 | |||
509 | 44259 | jjdelcerro | public boolean isAttributeAnExpression() { |
510 | final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
||
511 | 44262 | jjdelcerro | if (attribute == null) { |
512 | 44259 | jjdelcerro | return false; |
513 | } |
||
514 | return attribute.isExpression();
|
||
515 | } |
||
516 | 44262 | jjdelcerro | |
517 | 44259 | jjdelcerro | public String getAttribute() { |
518 | final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem(); |
||
519 | 44262 | jjdelcerro | if (attribute == null) { |
520 | 44259 | jjdelcerro | return null; |
521 | } |
||
522 | 44263 | jjdelcerro | if( this.getValue()!=null ) { |
523 | attribute.getDescriptor().recentUsed(); |
||
524 | } |
||
525 | 44259 | jjdelcerro | return attribute.getValue();
|
526 | } |
||
527 | 44262 | jjdelcerro | |
528 | 44292 | jjdelcerro | public int setAttribute(String name) { |
529 | 44259 | jjdelcerro | ComboBoxModel<FeatureAttribute> model = this.ddnFields.getModel(); |
530 | for (int i = 0; i < model.getSize(); i++) { |
||
531 | FeatureAttribute x = model.getElementAt(i); |
||
532 | 44278 | jjdelcerro | if (StringUtils.equalsIgnoreCase(name, x.getValue())) {
|
533 | 44262 | jjdelcerro | this.setAttribute(i);
|
534 | 44292 | jjdelcerro | return i;
|
535 | 44259 | jjdelcerro | } |
536 | } |
||
537 | 44262 | jjdelcerro | this.setAttribute(-1); |
538 | 44292 | jjdelcerro | return -1; |
539 | 44259 | jjdelcerro | } |
540 | 44262 | jjdelcerro | |
541 | 44259 | jjdelcerro | public void setAttribute(int index) { |
542 | try {
|
||
543 | this.ddnFields.setSelectedIndex(index);
|
||
544 | 44262 | jjdelcerro | } catch (Exception ex) { |
545 | 44259 | jjdelcerro | this.ddnFields.setSelectedIndex(-1); |
546 | } |
||
547 | 44262 | jjdelcerro | doUpdateValuesList(); |
548 | 44259 | jjdelcerro | } |
549 | 44262 | jjdelcerro | |
550 | 44351 | jjdelcerro | } |