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

History | View | Annotate | Download (17.4 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.text.Collator;
30
import java.util.ArrayList;
31
import java.util.Comparator;
32
import java.util.Date;
33
import java.util.List;
34
import java.util.TreeSet;
35

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

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

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

    
72

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

    
83
        private static final long serialVersionUID = 1L;
84

    
85
        private static final Logger logger =
86
        LoggerFactory.getLogger(DataSelectionPanel.class);
87

    
88
    private JComboBox layerComboBox = null;
89
        private JLabel jLabel = null;
90
        private JLabel jLabel1 = null;
91
        private JComboBox fieldValueComboBox = null;
92
        private JLabel jLabel2 = null;
93
        private JComboBox fieldNameComboBox = null;
94
    private WindowInfo viewInfo = null;
95
        private JButton goButton = null;
96
        private JButton closeButton = null;
97
        private FLayer layerToZoom = null;
98
        private int fieldToZoomIndex = 0;
99
        private Object itemToZoom = null;
100
        private MapContext mapCtxt = null;
101
        private JPanel jPanelButtons = null;
102

    
103

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

    
110

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

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

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

    
132
        c.gridx = 0;
133
        c.gridy = 0;
134
        centerPanelGrid.add(jLabel, c);
135

    
136
        c.gridx = 0;
137
        c.gridy = 1;
138
        centerPanelGrid.add(jLabel1, c);
139

    
140
        c.gridx = 0;
141
        c.gridy = 2;
142
        centerPanelGrid.add(jLabel2, c);
143

    
144
        c.anchor = GridBagConstraints.CENTER;
145
        c.weightx = 1;
146

    
147
        c.gridx = 1;
148
        c.gridy = 0;
149
        centerPanelGrid.add(getLayersComboBox(), c);
150

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

    
155
        c.gridx = 1;
156
        c.gridy = 2;
157
        centerPanelGrid.add(getValuesComboBox(), c);
158

    
159
        this.add(centerPanelGrid, BorderLayout.CENTER);
160

    
161

    
162
        // Panel Sur
163
        jPanelButtons = new JPanel();
164
        FlowLayout flowLayor = new FlowLayout(FlowLayout.RIGHT);
165
        jPanelButtons.setLayout(flowLayor);
166
        jPanelButtons.add(getGoButton(), null);
167
        jPanelButtons.add(getCloseButton(), null);
168
        this.add(jPanelButtons, BorderLayout.SOUTH);
169
        }
170

    
171

    
172
        private FeatureAttributeDescriptor[] getFields(FLyrVect vect) {
173

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

    
185
        }
186

    
187

    
188
        private Object[] getNewValues(FLyrVect vlayer, String fname) {
189

    
190
                FeatureStore featureStore;
191
                if (vlayer == null || fname == null) {
192
                    return new Object[0];
193
                }
194

    
195
                FeatureSet set = null;
196

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

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

    
242

    
243
    public WindowInfo getWindowInfo() {
244
        if (viewInfo == null) {
245
            viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
246
            viewInfo.setTitle(PluginServices.getText(this,"Localizador_por_atributo"));
247
            viewInfo.setHeight(this.getHeight());
248
            viewInfo.setWidth(this.getWidth());
249
        }
250
        return viewInfo;
251
    }
252
        /**
253
         * This method initializes jComboBox
254
         *
255
         * @return javax.swing.JComboBox
256
         */
257
        private JComboBox getLayersComboBox() {
258
                if (layerComboBox == null) {
259
                        layerComboBox = new JComboBox();
260

    
261
                        List<FLyrVect> vs = DataLocatorExtension.getVectorLayers(mapCtxt.getLayers());
262
                        LayersComboItem[] items = new LayersComboItem[vs.size()];
263
                        for (int i=0; i<vs.size(); i++) {
264
                            items[i] = new LayersComboItem(vs.get(i));
265
                        }
266
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(items);
267
            layerComboBox.setModel(defaultModel);
268
                        layerComboBox.addActionListener(this);
269
                        if (items.length > 0) {
270
                            layerComboBox.setSelectedIndex(0);
271
                        }
272
                }
273
                return layerComboBox;
274
        }
275

    
276
        /**
277
         * This method initializes jComboBox1
278
         *
279
         * @return javax.swing.JComboBox
280
         */
281
        private JComboBox getFieldsComboBox() {
282
                if (fieldNameComboBox == null) {
283
                    fieldNameComboBox = new JComboBox();
284
                    fieldNameComboBox.addActionListener(this);
285
                    fieldNameComboBox.setEnabled(false);
286
                }
287
                return fieldNameComboBox;
288
        }
289

    
290

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

    
305

    
306

    
307

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

    
337

    
338

    
339
        public Object getWindowProfile() {
340
                return WindowInfo.DIALOG_PROFILE;
341
        }
342

    
343
        class ValueEvaluator implements Evaluator {
344

    
345
                private String fieldName = null;
346
                private Object value = null;
347
                private EvaluatorFieldsInfo info = null;
348
                private boolean nonnull = true;
349
                private DataType dataType = null;
350

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

    
363
                /**
364
                 * This value
365
                 * @param name
366
                 * @param val
367
                 */
368
                public ValueEvaluator(String name, Object val, DataType dtype) {
369

    
370
                    fieldName = name;
371
                        info = new EvaluatorFieldsInfo();
372
                        info.addFieldValue(name);
373
                        value = val;
374
                        nonnull = false;
375
                        dataType = dtype;
376
                }
377

    
378
                public Object evaluate(EvaluatorData data)
379
                                throws EvaluatorException {
380

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

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

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

    
417
                public EvaluatorFieldsInfo getFieldsInfo() {
418
                        return this.info;
419
                }
420

    
421
                public String getName() {
422
                        return this.getClass().getName();
423
                }
424

    
425
    }
426

    
427
    public void actionPerformed(ActionEvent e) {
428

    
429
        Object src = e.getSource();
430
        if (src == this.getLayersComboBox()) {
431

    
432
            LayersComboItem litem =
433
                (LayersComboItem) getLayersComboBox().getSelectedItem();
434
            if (litem != null) {
435
                FeatureAttributeDescriptor[] atts = getFields(litem.getLayer());
436
                if (atts.length > 0) {
437
                    getFieldsComboBox().setEnabled(true);
438
                    getFieldsComboBox().removeAllItems();
439

    
440
                    for (int i=0; i<atts.length; i++) {
441
                        if (atts[i].getType() != DataTypes.GEOMETRY) {
442
                            getFieldsComboBox().addItem(new FieldsComboItem(atts[i]));
443
                        }
444
                    }
445
                    getFieldsComboBox().setSelectedIndex(0);
446
                    /*
447
                     * OK
448
                     */
449
                    return;
450
                }
451
            }
452
            /*
453
             * Not OK
454
             */
455
            getFieldsComboBox().setEnabled(false);
456
            getValuesComboBox().setEnabled(false);
457
            return;
458
        }
459

    
460
        // ==========================================================
461
        // ==========================================================
462

    
463
        if (src == this.getFieldsComboBox()) {
464

    
465
            LayersComboItem litem =
466
                (LayersComboItem) getLayersComboBox().getSelectedItem();
467
            FieldsComboItem fatt =
468
                (FieldsComboItem) getFieldsComboBox().getSelectedItem();
469
            if (litem != null && fatt != null) {
470

    
471
                Object[] vals = this.getNewValues(litem.getLayer(), fatt.toString());
472
                if (vals.length > 0) {
473

    
474
                    this.getValuesComboBox().setEnabled(true);
475
                    this.getValuesComboBox().removeAllItems();
476
                    DefaultComboBoxModel model = new DefaultComboBoxModel(vals);
477
                    getValuesComboBox().setModel(model);
478
                    getValuesComboBox().setSelectedIndex(0);
479
                    this.getGoButton().setEnabled(true);
480
                    /*
481
                     * OK
482
                     */
483
                    return;
484
                }
485
            }
486
            /*
487
             * Not OK
488
             */
489
            this.getValuesComboBox().setEnabled(false);
490
            this.getGoButton().setEnabled(false);
491
            return;
492
        }
493

    
494
        // ==========================================================
495
        // ==========================================================
496

    
497
        if (src == this.getValuesComboBox()) {
498

    
499
            return;
500
        }
501

    
502
        // ==========================================================
503
        // ==========================================================
504

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

    
510
        // ==========================================================
511
        // ==========================================================
512

    
513
        if (src == this.getGoButton()) {
514

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

    
542
                if (env_data != null) {
543
                    if (lyr.getCoordTrans() != null) {
544
                        env_data = env_data.convert(lyr.getCoordTrans());
545
                    }
546
                    this.mapCtxt.getViewPort().setEnvelope(env_data);
547
                }
548
            // =======================================
549

    
550
            } catch (Exception exc) {
551
                logger.error("While zooming to value", exc);
552
            }
553
            return;
554
        }
555

    
556
    }
557

    
558

    
559
}