Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.datalocator.app / org.gvsig.datalocator.app.mainplugin / src / main / java / org / gvsig / datalocator / gui / DataSelectionPanel.java @ 42251

History | View | Annotate | Download (17.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.datalocator.gui;
25

    
26
import java.awt.*;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.util.ArrayList;
30
import java.util.Comparator;
31
import java.util.Date;
32
import java.util.List;
33
import java.util.TreeSet;
34

    
35
import javax.swing.DefaultComboBoxModel;
36
import javax.swing.JButton;
37
import javax.swing.JComboBox;
38
import javax.swing.JLabel;
39
import javax.swing.JPanel;
40
import javax.swing.border.EmptyBorder;
41

    
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
import org.gvsig.andami.PluginServices;
46
import org.gvsig.andami.ui.mdiManager.IWindow;
47
import org.gvsig.andami.ui.mdiManager.WindowInfo;
48
import org.gvsig.app.ApplicationLocator;
49
import org.gvsig.datalocator.DataLocatorExtension;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.dal.feature.FeatureQuery;
54
import org.gvsig.fmap.dal.feature.FeatureSelection;
55
import org.gvsig.fmap.dal.feature.FeatureSet;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.dal.feature.FeatureType;
58
import org.gvsig.fmap.geom.DataTypes;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.fmap.mapcontext.MapContext;
61
import org.gvsig.fmap.mapcontext.layers.FLayer;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63
import org.gvsig.fmap.mapcontext.rendering.strategies.SelectedZoomVisitor;
64
import org.gvsig.tools.dataTypes.DataType;
65
import org.gvsig.tools.dispose.DisposableIterator;
66
import org.gvsig.tools.evaluator.Evaluator;
67
import org.gvsig.tools.evaluator.EvaluatorData;
68
import org.gvsig.tools.evaluator.EvaluatorException;
69
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
70

    
71

    
72
/**
73
 * 
74
 * This panel lets the user choose a vector layer from a view and a field and a value
75
 * and then it zooms to the extent of the features which match the value.
76
 * 
77
 * @author jldominguez
78
 */
79
public class DataSelectionPanel extends JPanel
80
implements IWindow, ActionListener {
81

    
82
        private static final long serialVersionUID = 1L;
83
        
84
        private static final Logger logger =
85
        LoggerFactory.getLogger(DataSelectionPanel.class);
86
            
87
    private JComboBox layerComboBox = null;
88
        private JLabel jLabel = null; 
89
        private JLabel jLabel1 = null; 
90
        private JComboBox fieldValueComboBox = null;
91
        private JLabel jLabel2 = null;  
92
        private JComboBox fieldNameComboBox = null;
93
    private WindowInfo viewInfo = null;
94
        private JButton goButton = null;
95
        private JButton closeButton = null;
96
        private FLayer layerToZoom = null;
97
        private int fieldToZoomIndex = 0;
98
        private Object itemToZoom = null;
99
        private MapContext mapCtxt = null;
100
        private JPanel jPanelButtons = null;
101

    
102

    
103
        public DataSelectionPanel(MapContext mapContext) {
104
                super();
105
                this.mapCtxt = mapContext;
106
        initialize();
107
        }
108
        
109

    
110
        private void initialize() {
111
        jLabel2 = new JLabel();
112
        jLabel1 = new JLabel();
113
        jLabel = new JLabel();
114
        this.setLayout(new BorderLayout());
115
        this.setBorder(new EmptyBorder(new Insets(0,5,5,5)));
116
        this.setSize(275, 90);        
117
        jLabel.setText(PluginServices.getText(this,"Capa") + ":");
118
        jLabel1.setText(PluginServices.getText(this,"Campo") + ":");
119
        jLabel2.setText(PluginServices.getText(this,"Valor") + ":");
120

    
121
        // Panel Central
122
        GridBagLayout gbl = new GridBagLayout();
123
        JPanel centerPanelGrid = new JPanel(gbl);
124
        GridBagConstraints c = new GridBagConstraints();
125

    
126
        c.fill = GridBagConstraints.BOTH;
127
        c.anchor = GridBagConstraints.LINE_START;
128
        c.insets = new Insets(3,3,3,3);
129
        c.weightx = 0;
130

    
131
        c.gridx = 0;
132
        c.gridy = 0;
133
        centerPanelGrid.add(jLabel, c);
134
        
135
        c.gridx = 0;
136
        c.gridy = 1;
137
        centerPanelGrid.add(jLabel1, c);
138
        
139
        c.gridx = 0;
140
        c.gridy = 2;
141
        centerPanelGrid.add(jLabel2, c);
142

    
143
        c.anchor = GridBagConstraints.CENTER;
144
        c.weightx = 1;
145
        
146
        c.gridx = 1;
147
        c.gridy = 0;
148
        centerPanelGrid.add(getLayersComboBox(), c);
149

    
150
        c.gridx = 1;
151
        c.gridy = 1;
152
        centerPanelGrid.add(getFieldsComboBox(), c);
153

    
154
        c.gridx = 1;
155
        c.gridy = 2;
156
        centerPanelGrid.add(getValuesComboBox(), c);
157
        
158
        this.add(centerPanelGrid, BorderLayout.CENTER);
159
        
160
        
161
        // Panel Sur
162
        jPanelButtons = new JPanel();
163
        FlowLayout flowLayor = new FlowLayout(FlowLayout.RIGHT);
164
        jPanelButtons.setLayout(flowLayor);
165
        jPanelButtons.add(getGoButton(), null);
166
        jPanelButtons.add(getCloseButton(), null);
167
        this.add(jPanelButtons, BorderLayout.SOUTH);
168
        }
169
        
170

    
171
        private FeatureAttributeDescriptor[] getFields(FLyrVect vect) {
172
            
173
                FeatureStore featureStore;
174
                try {
175
            featureStore = vect.getFeatureStore();
176
            FeatureType fty = featureStore.getDefaultFeatureType();
177
            FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
178
            return atts;
179
        } catch (DataException e) {
180
            logger.error("While getting field names.", e);
181
            return new FeatureAttributeDescriptor[0];
182
                }
183
                
184
        }
185

    
186

    
187
        private Object[] getNewValues(FLyrVect vlayer, String fname) {
188
            
189
                FeatureStore featureStore;
190
                if (vlayer == null || fname == null) {
191
                    return new Object[0];
192
                }
193
                
194
                FeatureSet set = null;
195

    
196
                try {
197
                        featureStore = vlayer.getFeatureStore();
198
                        FeatureQuery query = featureStore.createFeatureQuery();
199
                        Evaluator myEvaluator = new ValueEvaluator(fname);
200
                        query.setFilter(myEvaluator);
201
                        query.setAttributeNames(new String[] { fname });
202
                        set = featureStore.getFeatureSet(query);
203
                        /*
204
                         * Removing duplicated and sorting
205
                         */
206
                        TreeSet<Object> treeSet =
207
                                        new TreeSet<Object>(new Comparator<Object>() {
208
                                                public int compare(Object o1, Object o2) {
209
                                                        if (o1 instanceof Number && o2 instanceof Number) {
210
                                                                if (((Number) o1).doubleValue() < ((Number) o2).doubleValue())
211
                                                                        return -1;
212
                                                                if (((Number) o1).doubleValue() > ((Number) o2).doubleValue())
213
                                                                        return 1;
214
                                                        } else if (o1 instanceof String
215
                                                                        && o2 instanceof String) {
216
                                                                return ((String) o1).compareTo((String) o2);
217
                                                        } else if (o1 instanceof Date && o2 instanceof Date) {
218
                                                                return ((Date) o1).compareTo((Date) o2);
219
                                                        }
220
                                                        return 0;
221
                                                }
222
                                        });
223
                        DisposableIterator diter = set.fastIterator();
224
                        Feature feat = null;
225
                        while (diter.hasNext()) {
226
                            feat = (Feature) diter.next();
227
                            treeSet.add(feat.get(fname));
228
                            if (treeSet.size() > 200) {
229
                                /*
230
                                 * Not more than 200 elements
231
                                 */
232
                                break;
233
                            }
234
                        }
235
                        diter.dispose();
236
                        return treeSet.toArray();
237

    
238
                } catch (DataException e) {
239
                    logger.error("While getting sample values.", e);
240
                    return new Object[0];
241
                }
242
        }
243

    
244

    
245
    public WindowInfo getWindowInfo() {
246
        if (viewInfo == null) {
247
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
248
            viewInfo.setTitle(PluginServices.getText(this,"Localizador_por_atributo"));
249
            viewInfo.setHeight(this.getHeight());
250
            viewInfo.setWidth(this.getWidth());
251
        }
252
        return viewInfo;
253
    }
254
        /**
255
         * This method initializes jComboBox
256
         *
257
         * @return javax.swing.JComboBox
258
         */
259
        private JComboBox getLayersComboBox() {
260
                if (layerComboBox == null) {
261
                        layerComboBox = new JComboBox();
262
                        
263
                        List<FLyrVect> vs = DataLocatorExtension.getVectorLayers(mapCtxt.getLayers());
264
                        LayersComboItem[] items = new LayersComboItem[vs.size()];
265
                        for (int i=0; i<vs.size(); i++) {
266
                            items[i] = new LayersComboItem(vs.get(i));
267
                        }
268
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(items);
269
            layerComboBox.setModel(defaultModel);
270
                        layerComboBox.addActionListener(this);
271
                        if (items.length > 0) {
272
                            layerComboBox.setSelectedIndex(0);
273
                        }
274
                }
275
                return layerComboBox;
276
        }
277
        
278
        /**
279
         * This method initializes jComboBox1
280
         *
281
         * @return javax.swing.JComboBox
282
         */
283
        private JComboBox getFieldsComboBox() {
284
                if (fieldNameComboBox == null) {
285
                    fieldNameComboBox = new JComboBox();
286
                    fieldNameComboBox.addActionListener(this);
287
                    fieldNameComboBox.setEnabled(false);
288
                }
289
                return fieldNameComboBox;
290
        }
291

    
292

    
293
        /**
294
         * This method initializes jComboBox2
295
         *
296
         * @return javax.swing.JComboBox
297
         */
298
        private JComboBox getValuesComboBox() {
299
                if (fieldValueComboBox == null) {
300
                    fieldValueComboBox = new JComboBox();
301
                    fieldValueComboBox.addActionListener(this);
302
                    fieldValueComboBox.setEnabled(false);
303
                }
304
                return fieldValueComboBox;
305
        }
306
        
307
        
308

    
309

    
310
        /**
311
         * This method initializes jButton
312
         *
313
         * @return javax.swing.JButton
314
         */
315
        private JButton getGoButton() {
316
                if (goButton == null) {
317
                    goButton = new JButton();
318
                    goButton.setPreferredSize(new Dimension(80, 23));
319
                    goButton.setText(PluginServices.getText(this,"_Go"));
320
                    goButton.addActionListener(this);
321
                }
322
                return goButton;
323
        }
324
        /**
325
         * This method initializes jButton1
326
         *
327
         * @return javax.swing.JButton
328
         */
329
        private JButton getCloseButton() {
330
                if (closeButton == null) {
331
                    closeButton = new JButton();
332
                    closeButton.setPreferredSize(new Dimension(80, 23));
333
                    closeButton.setText(PluginServices.getText(this,"_Close"));
334
                    closeButton.addActionListener(this);
335
                }
336
                return closeButton;
337
        }
338

    
339

    
340

    
341
        public Object getWindowProfile() {
342
                return WindowInfo.DIALOG_PROFILE;
343
        }
344
        
345
        class ValueEvaluator implements Evaluator {
346
            
347
                private String fieldName = null;
348
                private Object value = null;
349
                private EvaluatorFieldsInfo info = null;
350
                private boolean nonnull = true;
351
                private DataType dataType = null;
352

    
353
            /**
354
             * Non null
355
             * 
356
             * @param name
357
             */
358
                public ValueEvaluator(String name) {
359
                    fieldName = name;
360
                    info = new EvaluatorFieldsInfo();
361
                    info.addFieldValue(name);
362
                    nonnull = true;
363
                }
364

    
365
                /**
366
                 * This value
367
                 * @param name
368
                 * @param val
369
                 */
370
                public ValueEvaluator(String name, Object val, DataType dtype) {
371
                    
372
                    fieldName = name;
373
                        info = new EvaluatorFieldsInfo();
374
                        info.addFieldValue(name);
375
                        value = val;
376
                        nonnull = false;
377
                        dataType = dtype;
378
                }
379

    
380
                public Object evaluate(EvaluatorData data)
381
                                throws EvaluatorException {
382
                    
383
                        Object obj = data.getDataValue(fieldName);
384
                        if (nonnull) {
385
                            /*
386
                             * Accepts non null
387
                             */
388
                    if (obj == null) {
389
                        return Boolean.FALSE;
390
                    } else {
391
                        return Boolean.TRUE;
392
                    }
393
                        } else {
394
                            return (value == null && obj == null)
395
                                ||
396
                                (value != null && obj != null && value.equals(obj));
397
                        }
398
                }
399

    
400
                public String getSQL() {
401
                    String resp = null;
402
                    if (nonnull) {
403
                        resp = fieldName + " is not null";
404
                    } else {
405
                        if (value == null) {
406
                            resp = fieldName + " is null";
407
                        } else {
408
                            String quote = dataType.isNumeric() ? "" : "'"; 
409
                            resp = fieldName + " = "  + quote + value.toString() + quote;
410
                        }
411
                    }
412
                    return resp;
413
                }
414

    
415
                public String getDescription() {
416
                        return "Evaluates if a field is not null";
417
                }
418

    
419
                public EvaluatorFieldsInfo getFieldsInfo() {
420
                        return this.info;
421
                }
422

    
423
                public String getName() {
424
                        return this.getClass().getName();
425
                }
426

    
427
    }
428

    
429
    public void actionPerformed(ActionEvent e) {
430
        
431
        Object src = e.getSource();
432
        if (src == this.getLayersComboBox()) {
433
            
434
            LayersComboItem litem =
435
                (LayersComboItem) getLayersComboBox().getSelectedItem();
436
            if (litem != null) {
437
                FeatureAttributeDescriptor[] atts = getFields(litem.getLayer());
438
                if (atts.length > 0) {
439
                    getFieldsComboBox().setEnabled(true);
440
                    getFieldsComboBox().removeAllItems();
441
                    
442
                    for (int i=0; i<atts.length; i++) {
443
                        if (atts[i].getType() != DataTypes.GEOMETRY) {
444
                            getFieldsComboBox().addItem(new FieldsComboItem(atts[i]));
445
                        }
446
                    }
447
                    getFieldsComboBox().setSelectedIndex(0);
448
                    /*
449
                     * OK
450
                     */
451
                    return;
452
                }
453
            }
454
            /*
455
             * Not OK
456
             */
457
            getFieldsComboBox().setEnabled(false);
458
            getValuesComboBox().setEnabled(false);
459
            return;
460
        }
461
        
462
        // ==========================================================
463
        // ==========================================================
464
        
465
        if (src == this.getFieldsComboBox()) {
466
            
467
            LayersComboItem litem =
468
                (LayersComboItem) getLayersComboBox().getSelectedItem();
469
            FieldsComboItem fatt =
470
                (FieldsComboItem) getFieldsComboBox().getSelectedItem();
471
            if (litem != null && fatt != null) {
472
                
473
                Object[] vals = this.getNewValues(litem.getLayer(), fatt.toString());
474
                if (vals.length > 0) {
475
                    
476
                    this.getValuesComboBox().setEnabled(true);
477
                    this.getValuesComboBox().removeAllItems();
478
                    DefaultComboBoxModel model = new DefaultComboBoxModel(vals);
479
                    getValuesComboBox().setModel(model);
480
                    getValuesComboBox().setSelectedIndex(0);
481
                    this.getGoButton().setEnabled(true);
482
                    /*
483
                     * OK
484
                     */
485
                    return;
486
                }
487
            }
488
            /*
489
             * Not OK
490
             */
491
            this.getValuesComboBox().setEnabled(false);
492
            this.getGoButton().setEnabled(false);
493
            return;
494
        }
495

    
496
        // ==========================================================
497
        // ==========================================================
498

    
499
        if (src == this.getValuesComboBox()) {
500
            
501
            return;
502
        }
503

    
504
        // ==========================================================
505
        // ==========================================================
506

    
507
        if (src == this.getCloseButton()) {
508
            ApplicationLocator.getManager().getUIManager().closeWindow(this);
509
            return;
510
        }
511

    
512
        // ==========================================================
513
        // ==========================================================
514

    
515
        if (src == this.getGoButton()) {
516

    
517
            LayersComboItem litem =
518
                (LayersComboItem) getLayersComboBox().getSelectedItem();
519
            FieldsComboItem fatt = (FieldsComboItem)
520
                getFieldsComboBox().getSelectedItem();
521
            Object value = this.getValuesComboBox().getSelectedItem();
522
            if (litem == null || fatt == null || value == null) {
523
                // Problem with combo boxes
524
                return;
525
            }
526
            FLyrVect lyr = litem.getLayer();
527
            FeatureStore featureStore;
528
            FeatureSet featureSet = null;
529
            DisposableIterator diter = null;
530
            try {
531
                featureStore = lyr.getFeatureStore();
532
                FeatureQuery query = featureStore.createFeatureQuery();
533
                Evaluator myEvaluator =
534
                    new ValueEvaluator(fatt.toString(), value, fatt.getType());
535
                query.setFilter(myEvaluator);
536
                String geoname =
537
                    featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
538
                query.setAttributeNames(new String[] { fatt.toString(), geoname });
539
                featureSet = featureStore.getFeatureSet(query);
540
                SelectedZoomVisitor visitor = new SelectedZoomVisitor();
541
                featureSet.accept(visitor);
542
                Envelope env_data = visitor.getSelectBound();
543
                
544
                if (env_data != null) {
545
                    if (lyr.getCoordTrans() != null) {
546
                        env_data = env_data.convert(lyr.getCoordTrans());
547
                    }
548
                    this.mapCtxt.getViewPort().setEnvelope(env_data);            
549
                }
550
            // =======================================
551

    
552
            } catch (Exception exc) {
553
                logger.error("While zooming to value", exc);
554
            }
555
            return;
556
        }
557
        
558
    }
559

    
560

    
561
}