Statistics
| Revision:

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 @ 44644

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