Revision 44262 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

View differences:

SearchFieldController.java
1 1
package org.gvsig.fmap.dal.swing.impl.searchpanel;
2 2

  
3 3
import java.awt.Cursor;
4
import java.awt.Dimension;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
4 7
import java.awt.event.ItemEvent;
5 8
import java.awt.event.ItemListener;
6 9
import java.awt.event.MouseAdapter;
......
14 17
import javax.swing.DefaultComboBoxModel;
15 18
import javax.swing.JComboBox;
16 19
import javax.swing.JLabel;
20
import javax.swing.JScrollPane;
21
import javax.swing.JTree;
22
import javax.swing.SwingUtilities;
23
import javax.swing.tree.TreePath;
17 24
import org.apache.commons.lang3.ObjectUtils;
18 25
import org.apache.commons.lang3.StringUtils;
19 26
import org.gvsig.expressionevaluator.ExpressionBuilder;
27
import org.gvsig.expressionevaluator.ExpressionUtils;
28
import org.gvsig.fmap.dal.DataManager;
29
import org.gvsig.fmap.dal.complements.Search;
30
import org.gvsig.fmap.dal.exception.DataException;
20 31
import org.gvsig.fmap.dal.feature.Feature;
21 32
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
22 33
import org.gvsig.fmap.dal.feature.FeatureSet;
23 34
import org.gvsig.fmap.dal.feature.FeatureStore;
24 35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.swing.impl.searchpanel.AdvancedAttributeSelectionTreeModel.Node;
37
import org.gvsig.tools.ToolsLocator;
25 38
import org.gvsig.tools.dataTypes.CoercionException;
26 39
import org.gvsig.tools.dataTypes.DataTypesManager;
27 40
import org.gvsig.tools.exception.BaseException;
28 41
import org.gvsig.tools.swing.api.DropDown;
29 42
import org.gvsig.tools.swing.api.ToolsSwingLocator;
30 43
import org.gvsig.tools.swing.api.ToolsSwingManager;
44
import org.gvsig.tools.swing.api.windowmanager.Dialog;
45
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
46
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
31 47
import org.gvsig.tools.util.LabeledValue;
32 48
import org.gvsig.tools.util.LabeledValueImpl;
33 49
import org.gvsig.tools.visitor.VisitCanceledException;
34 50
import org.gvsig.tools.visitor.Visitor;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
35 53

  
36 54
/**
37 55
 *
......
40 58
@SuppressWarnings("UseSpecificCatch")
41 59
public class SearchFieldController {
42 60

  
61
    private static final Logger LOGGER = LoggerFactory.getLogger(SearchFieldController.class);
62
    
43 63
    private static class FeatureAttribute extends LabeledValueImpl<String> {
44 64

  
45 65
        FeatureAttributeDescriptor attrdesc;
66
        private final FeatureStore store;
46 67

  
47
        public FeatureAttribute(FeatureAttributeDescriptor attrdesc) {
48
            this(attrdesc, null, null);
68
        public FeatureAttribute(FeatureStore store, FeatureAttributeDescriptor attrdesc) {
69
            this(store, attrdesc, null, null);
49 70
        }
50
        
51
        public FeatureAttribute(FeatureAttributeDescriptor attrdesc, String label, String value) {
71

  
72
        public FeatureAttribute(
73
                FeatureStore store,
74
                FeatureAttributeDescriptor attrdesc, 
75
                String label, 
76
                String value
77
            ) {
52 78
            super(
53
                    ObjectUtils.defaultIfNull(label, attrdesc.getLabel()), 
79
                    ObjectUtils.defaultIfNull(label, attrdesc.getLabel()),
54 80
                    ObjectUtils.defaultIfNull(value, attrdesc.getName())
55 81
            );
82
            this.store = store;
56 83
            this.attrdesc = attrdesc;
57 84
        }
58
        
85

  
59 86
        public FeatureAttributeDescriptor getDescriptor() {
60 87
            return this.attrdesc;
61 88
        }
89

  
90
        public FeatureStore getFeatureStore() {
91
            return this.store;
92
        }
62 93
        
63 94
        public boolean isExpression() {
64 95
            FeatureType type = this.attrdesc.getFeatureType();
65
            if( type == null ) {
96
            if (type == null) {
66 97
                return false;
67 98
            }
68 99
            Object x = type.get(this.getValue());
69
            return x==null;
100
            return x == null;
70 101
        }
71 102

  
72 103
    }
......
82 113
    private DropDown ddnLogicalOperators;
83 114
    private DropDown ddnRelationalOperators;
84 115

  
85

  
86

  
87 116
    private final LabeledValue[] relationalOperators = {
88 117
        new LabeledValueImpl("Equals to", ExpressionBuilder.OPERATOR_EQ),
89 118
        new LabeledValueImpl("Like to", ExpressionBuilder.OPERATOR_ILIKE),
......
122 151

  
123 152
            this.ddnFields = toolsSwingManager.createDropDown(lblFields);
124 153
            this.ddnRelationalOperators = toolsSwingManager.createDropDown(lblRelationalOperators);
125
            if( lblLogicalOperators!=null ) {
154
            if (lblLogicalOperators != null) {
126 155
                this.ddnLogicalOperators = toolsSwingManager.createDropDown(lblLogicalOperators);
127 156
            }
128 157

  
......
132 161
            }
133 162
            this.ddnRelationalOperators.setModel(modelRelationalOperators);
134 163

  
135
            if( this.ddnLogicalOperators!=null ) {
164
            if (this.ddnLogicalOperators != null) {
136 165
                DefaultComboBoxModel modelLogicalOperators = new DefaultComboBoxModel();
137 166
                for (LabeledValue op : logicalOperators) {
138 167
                    modelLogicalOperators.addElement(op);
139 168
                }
140 169
                this.ddnLogicalOperators.setModel(modelLogicalOperators);
141 170
            }
142
            
143
            List<FeatureAttributeDescriptor> orderedAttributes = SearchUtils.getOrderedAttributes(
144
                    this.store, 
145
                    SearchUtils.BASIC_TYPES_FILTER,
146
                    SearchUtils.STR_INT_LONG_LABEL_ORDER,
171
            FeatureType featureType = store.getDefaultFeatureType();
172
            Search search = (Search) ToolsLocator.getComplementsManager().get(
173
                    Search.COMPLEMENT_MANE, featureType
174
            );
175
            List<FeatureAttributeDescriptor> orderedAttributes = search.getOrderedAttributes(
176
                    Search.BASIC_TYPES_FILTER,
177
                    Search.STR_INT_LONG_LABEL_ORDER,
147 178
                    20
148 179
            );
149 180
            DefaultComboBoxModel model = new DefaultComboBoxModel();
150 181
            for (FeatureAttributeDescriptor attrdesc : orderedAttributes) {
151
                model.addElement(new FeatureAttribute(attrdesc));
182
                model.addElement(new FeatureAttribute(this.store, attrdesc));
152 183
            }
153 184

  
154 185
            this.ddnFields.setModel(model);
......
161 192

  
162 193
                }
163 194
            });
164
            
195

  
165 196
            this.lblExtraFields.addMouseListener(new MouseAdapter() {
166 197
                @Override
167 198
                public void mouseClicked(MouseEvent e) {
......
174 205
        }
175 206
    }
176 207

  
208
    private FeatureType getFeatureType() {
209
        try {
210
            return this.store.getDefaultFeatureType();
211
        } catch (DataException ex) {
212
            return null;
213
        }
214
    }
215

  
177 216
    private void doSelectExtraField() {
178
        
217
        FeatureType featureType = this.getFeatureType();
218
        Search search = (Search) ToolsLocator.getComplementsManager().get(
219
                Search.COMPLEMENT_MANE, featureType
220
        );
221
        List<FeatureAttributeDescriptor> orderedAttributes = search.getOrderedAttributes(
222
                Search.BASIC_TYPES_FILTER,
223
                Search.LABEL_ORDER,
224
                -1
225
        );
226
        AdvancedAttributeSelectionTreeModel treeModel = new AdvancedAttributeSelectionTreeModel(
227
                this.store,
228
                orderedAttributes
229
        );
230
        final JTree tree = new JTree();
231
        tree.setCellRenderer(new AdvancedAttributeSelectionTreeCellRenderer());
232
        tree.setModel(treeModel);
233
        JScrollPane scrollpane = new JScrollPane(tree);
234
        scrollpane.setPreferredSize(new Dimension(400, 300));
235
        scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
236
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
237

  
238
        WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
239
        final Dialog dialog = winManager.createDialog(
240
                scrollpane,
241
                "Select attribute",
242
                null,
243
                WindowManager_v2.BUTTONS_OK_CANCEL
244
        );
245
        dialog.addActionListener(new ActionListener() {
246
            @Override
247
            public void actionPerformed(ActionEvent e) {
248
                if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) {
249
                    TreePath path = tree.getSelectionPath();
250
                    doAddAndSelect(path.getPath());
251
                }
252
            }
253
        });
254
        dialog.show(WindowManager.MODE.DIALOG);
255

  
179 256
    }
180
    
257

  
258
    private void doAddAndSelect(Object[] nodes) {
259
        ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
260
        ExpressionBuilder.Function list = builder.list();
261
        for (int i = 1; i < nodes.length; i++) {
262
            Node node = (Node) nodes[i];
263
            FeatureAttributeDescriptor attrdesc = node.getValue();
264
            list.parameter(builder.constant(attrdesc.getName()));
265
        }
266
        Node node = (Node) nodes[nodes.length - 1];
267
        FeatureStore theStore = node.getFeatureStore();
268
        FeatureAttributeDescriptor attrdesc = node.getValue();
269
        String storeFullName = theStore.getFullName();
270
        DefaultComboBoxModel<FeatureAttribute> model = (DefaultComboBoxModel) this.ddnFields.getModel();
271
        for (int i = 0; i < model.getSize(); i++) {
272
            FeatureAttribute attr = model.getElementAt(i);
273
            FeatureAttributeDescriptor attrdescN = attr.getDescriptor();
274
            if (StringUtils.equalsIgnoreCase(storeFullName, attrdescN.getStore().getFullName())
275
                    && StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdescN.getName())) {
276
                this.setAttribute(i);
277
                return;
278
            }
279
        }
280
        String formula = builder.function(DataManager.FUNCTION_FOREING_VALUE, list).toString();
281
        String label = attrdesc.getLabel() + " [" + theStore.getName() + "]";
282
        if (StringUtils.equalsIgnoreCase(storeFullName, this.store.getFullName())) {
283
            label = attrdesc.getLabel();
284
            formula = attrdesc.getName();
285
        }
286
        FeatureAttribute attribute = new FeatureAttribute(theStore, attrdesc, label, formula);
287
        model.addElement(attribute);
288
        this.setAttribute(model.getSize() - 1);
289
    }
290

  
181 291
    public void clear() {
182 292
        this.ddnRelationalOperators.setSelectedIndex(0);
183
        if( this.ddnLogicalOperators!=null ) {
293
        if (this.ddnLogicalOperators != null) {
184 294
            this.ddnLogicalOperators.setSelectedIndex(0);
185 295
        }
186 296
        this.cboValue.setSelectedIndex(-1);
187 297
    }
188
    
298

  
189 299
    private void doUpdateValuesList() {
190 300
        final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem();
191
        if( attribute==null ) {
301
        if (attribute == null) {
192 302
            return;
193
        }        
303
        }
304

  
194 305
        final List<Object> values = new ArrayList<>();
195 306
        final int limit = 60;
196 307
        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);
308
        final DefaultComboBoxModel model = new DefaultComboBoxModel();
309
        this.setEnabled(false);
310
        Thread th = new Thread(new Runnable() {
311
            @Override
312
            public void run() {
313
                try {
314
                    FeatureSet set = attribute.getFeatureStore().getFeatureSet();
315
                    set.accept(new Visitor() {
316
                        @Override
317
                        public void visit(Object o) throws VisitCanceledException, BaseException {
318
                            Object value = ((Feature) o).get(attribute.getDescriptor().getName());
319
                            if (!values.contains(value)) {
320
                                values.add(value);
321
                            }
322
                            if (System.currentTimeMillis() > timeLimit) {
323
                                throw new VisitCanceledException();
324
                            }
325
                            if (values.size() > 1000) {
326
                                throw new VisitCanceledException();
327
                            }
328
                        }
329
                    });
330
                } catch (VisitCanceledException ex) {
331

  
332
                } catch (Exception ex) {
333
                    LOGGER.warn("Can't update list of values of '"+attribute.getLabel()+"'.", ex);
334
                }
335
                List<LabeledValue> elements = new ArrayList<>();
336
                if (!values.isEmpty()) {
337
                    LabeledValue[] availableValues = attribute.getDescriptor().getAvailableValues();
338
                    Map<String, String> availableValuesMap = new HashMap<>();
339
                    if (availableValues != null) {
340
                        for (LabeledValue availableValue : availableValues) {
341
                            availableValuesMap.put(
342
                                    Objects.toString(availableValue.getValue()),
343
                                    availableValue.getLabel()
344
                            );
345
                        }
205 346
                    }
206
                    if (System.currentTimeMillis() > timeLimit) {
207
                        throw new VisitCanceledException();
347
                    elements.add(new LabeledValueImpl("", null));
348
                    for (Object value : values) {
349
                        String key = Objects.toString(value);
350
                        String label = availableValuesMap.getOrDefault(key, key);
351
                        elements.add(new LabeledValueImpl(label, value));
208 352
                    }
209
                    if (values.size()>1000 ) {
210
                        throw new VisitCanceledException();
211
                    }
353
                    elements.sort(null);
354
                    
212 355
                }
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
                    );
356
                for (LabeledValue element : elements) {
357
                    model.addElement(element);
229 358
                }
359
                SwingUtilities.invokeLater(new Runnable() {
360
                    @Override
361
                    public void run() {
362
                        cboValue.setModel(model);
363
                        setEnabled(true);
364
                    }
365
                });
230 366
            }
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
            }
367
        });
368
        th.start();
369
    }
370

  
371
    public void setEnabled(boolean enabled) {
372
        this.ddnFields.setEnabled(enabled);
373
        if( this.ddnLogicalOperators!=null ) {
374
            this.ddnLogicalOperators.setEnabled(enabled);
237 375
        }
238
        DefaultComboBoxModel model = new DefaultComboBoxModel();
239
        for (LabeledValue element : elements) {
240
            model.addElement(element);
241
        }
242
        this.cboValue.setModel(model);
376
        this.ddnRelationalOperators.setEnabled(enabled);
377
        this.lblExtraFields.setEnabled(enabled);
243 378
    }
244

  
379
    
245 380
    public String getRelationalOperator() {
246 381
        LabeledValue<String> op = (LabeledValue) this.ddnRelationalOperators.getSelectedItem();
247
        if( op == null ) {
382
        if (op == null) {
248 383
            return null;
249 384
        }
250 385
        return op.getValue();
251 386
    }
252
    
387

  
253 388
    public String getLogicalOperator() {
254
        if( this.ddnLogicalOperators==null ) {
389
        if (this.ddnLogicalOperators == null) {
255 390
            return null;
256 391
        }
257 392
        LabeledValue<String> rel = (LabeledValue) this.ddnLogicalOperators.getSelectedItem();
258
        if( rel == null ) {
393
        if (rel == null) {
259 394
            return null;
260 395
        }
261 396
        return rel.getValue();
262 397
    }
263
    
398

  
264 399
    public Object getValue() {
265 400
        final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem();
266
        if( attribute==null ) {
401
        if (attribute == null) {
267 402
            return null;
268 403
        }
269 404
        Object v = this.cboValue.getSelectedItem();
270
        if( v == null ) {
405
        if (v == null) {
271 406
            return null;
272 407
        }
273
        if( v instanceof LabeledValue ) {
274
            v = ((LabeledValue)v).getValue();
275
            if( v == null ) {
408
        if (v instanceof LabeledValue) {
409
            v = ((LabeledValue) v).getValue();
410
            if (v == null) {
276 411
                return null;
277 412
            }
278 413
        }
279
        if( v instanceof CharSequence ) {
280
            if( StringUtils.isBlank((CharSequence) v)) {
414
        if (v instanceof CharSequence) {
415
            if (StringUtils.isBlank((CharSequence) v)) {
281 416
                return null;
282 417
            }
283 418
        }
......
288 423
            return null;
289 424
        }
290 425
    }
291
    
426

  
292 427
    public boolean isAttributeAnExpression() {
293 428
        final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem();
294
        if( attribute==null ) {
429
        if (attribute == null) {
295 430
            return false;
296 431
        }
297 432
        return attribute.isExpression();
298 433
    }
299
    
434

  
300 435
    public String getAttribute() {
301 436
        final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem();
302
        if( attribute==null ) {
437
        if (attribute == null) {
303 438
            return null;
304 439
        }
305
        SearchUtils.getRecentUseds().add(attribute.getDescriptor());
440
        attribute.getDescriptor().recentUsed();
306 441
        return attribute.getValue();
307 442
    }
308
    
443

  
309 444
    public void setAttribute(String name) {
310 445
        ComboBoxModel<FeatureAttribute> model = this.ddnFields.getModel();
311 446
        for (int i = 0; i < model.getSize(); i++) {
312 447
            FeatureAttribute x = model.getElementAt(i);
313
            if( StringUtils.equalsIgnoreCase(name, x.getLabel())) {
314
                this.ddnFields.setSelectedIndex(i);
448
            if (StringUtils.equalsIgnoreCase(name, x.getLabel())) {
449
                this.setAttribute(i);
315 450
                return;
316 451
            }
317 452
        }
318
        this.ddnFields.setSelectedIndex(-1);
453
        this.setAttribute(-1);
319 454
    }
320
    
455

  
321 456
    public void setAttribute(int index) {
322 457
        try {
323 458
            this.ddnFields.setSelectedIndex(index);
324
        } catch(Exception ex) {
459
        } catch (Exception ex) {
325 460
            this.ddnFields.setSelectedIndex(-1);
326 461
        }
462
        doUpdateValuesList();
327 463
    }
328
    
329
}
464

  
465
};

Also available in: Unified diff