Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geocoding.extension / src / org / gvsig / geocoding / gui / results / ResultsPanel.java @ 32526

History | View | Annotate | Download (11 KB)

1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5

    
6
/*
7
 * SettingsPanel.java
8
 *
9
 * Created on 16-mar-2009, 12:41:50
10
 */
11

    
12
package org.gvsig.geocoding.gui.results;
13

    
14
import java.awt.event.MouseEvent;
15
import java.awt.event.MouseListener;
16
import java.io.File;
17
import java.util.Iterator;
18

    
19
import javax.swing.ImageIcon;
20
import javax.swing.JButton;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23
import javax.swing.JTable;
24

    
25
import org.gvsig.fmap.dal.DALLocator;
26
import org.gvsig.fmap.dal.DataManager;
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.EditableFeature;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureQuery;
31
import org.gvsig.fmap.dal.feature.FeatureSet;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
34
import org.gvsig.fmap.dal.feature.FeatureType;
35
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.geocoding.extension.GeocodingController;
38
import org.gvsig.geocoding.gui.TableResultsModel;
39
import org.gvsig.geocoding.utils.GeocodingExtTags;
40
import org.gvsig.tools.evaluator.Evaluator;
41
import org.gvsig.tools.observer.Observable;
42
import org.gvsig.tools.observer.Observer;
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

    
50
/**
51
 * 
52
 * @author vsanjaime
53
 */
54
public class ResultsPanel extends JPanel implements IWindow, Observer,
55
                MouseListener {
56

    
57
        private static final long serialVersionUID = 1L;
58
        private Logger log = LoggerFactory.getLogger(ResultsPanel.class);
59

    
60
        private GeocodingController control = null;
61
        private FeatureStore allstore = null;
62
        private FeatureStore shpstore = null;
63
        private JButton jButUpdate;
64
        private JButton jButCancel;
65
        private JPanel jPanButtons;
66
        private JPanel jPanResults;
67
        private JScrollPane jScrollResults;
68
        private JTable jTableResults;
69

    
70
        /**
71
         * Constructor
72
         */
73
        public ResultsPanel(GeocodingController control) {
74
                this.control = control;
75
                initComponents();
76

    
77
                setImages();
78
                setMesages();
79
        }
80

    
81
        /**
82
         * Initialize panel components
83
         */
84
        private void initComponents() {
85
                java.awt.GridBagConstraints gridBagConstraints;
86

    
87
                jPanResults = new JPanel();
88
                jScrollResults = new JScrollPane();
89
                jTableResults = new JTable();
90
                jPanButtons = new JPanel();
91
                jButUpdate = new JButton();
92
                jButCancel = new JButton();
93

    
94
                setLayout(new java.awt.GridBagLayout());
95

    
96
                jPanResults.setLayout(new java.awt.GridBagLayout());
97

    
98
                jTableResults.setModel(new TableResultsModel(control));
99
                jTableResults.addMouseListener(this);
100

    
101
                jScrollResults.setViewportView(jTableResults);
102

    
103
                gridBagConstraints = new java.awt.GridBagConstraints();
104
                gridBagConstraints.gridx = 0;
105
                gridBagConstraints.gridy = 0;
106
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
107
                gridBagConstraints.weightx = 1.0;
108
                gridBagConstraints.weighty = 1.0;
109
                jPanResults.add(jScrollResults, gridBagConstraints);
110

    
111
                gridBagConstraints = new java.awt.GridBagConstraints();
112
                gridBagConstraints.gridx = 0;
113
                gridBagConstraints.gridy = 0;
114
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
115
                gridBagConstraints.weightx = 1.0;
116
                gridBagConstraints.weighty = 1.0;
117
                gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
118
                add(jPanResults, gridBagConstraints);
119

    
120
                jPanButtons.setLayout(new java.awt.GridBagLayout());
121

    
122
                jButUpdate.setText("Update");
123
                jButUpdate.setEnabled(false);
124
                jButUpdate.addActionListener(new java.awt.event.ActionListener() {
125
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
126
                                evUpdate(evt);
127
                        }
128
                });
129
                gridBagConstraints = new java.awt.GridBagConstraints();
130
                gridBagConstraints.gridx = 0;
131
                gridBagConstraints.gridy = 0;
132
                gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
133
                jPanButtons.add(jButUpdate, gridBagConstraints);
134

    
135
                jButCancel.setText("cancel");
136
                jButCancel.addActionListener(new java.awt.event.ActionListener() {
137
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
138
                                evCancel(evt);
139
                        }
140
                });
141
                jPanButtons.add(jButCancel, new java.awt.GridBagConstraints());
142

    
143
                gridBagConstraints = new java.awt.GridBagConstraints();
144
                gridBagConstraints.gridx = 0;
145
                gridBagConstraints.gridy = 1;
146
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
147
                gridBagConstraints.weightx = 1.0;
148
                gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
149
                add(jPanButtons, gridBagConstraints);
150
        }
151

    
152
        /**
153
         * 
154
         * @param evt
155
         */
156
        private void evUpdate(java.awt.event.ActionEvent evt) {
157
                // updateStoreSelectedResults();
158
                updateStoreAllResults();
159

    
160
        }
161

    
162
        /**
163
         * 
164
         * @param evt
165
         */
166
        private void evCancel(java.awt.event.ActionEvent evt) {
167
                IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
168
                for (int i = 0; i < iws.length; i++) {
169
                        if (iws[i] instanceof ResultsPanel) {
170
                                PluginServices.getMDIManager().closeWindow(iws[i]);
171
                        }
172
                }
173
        }
174

    
175
        /**
176
         * 
177
         * @param nres
178
         */
179
        public void loadResults(int nres) {
180
                DataManager manager = DALLocator.getDataManager();
181
                try {
182
                        FeatureQuery query = allstore.createFeatureQuery();
183
                        String exp = GeocodingExtTags.gID_RESUL + " like " + nres;
184
                        Evaluator eval = manager.createExpresion(exp);
185
                        query.setFilter(eval);
186

    
187
                        FeatureSet set = allstore.getFeatureSet(query);
188
                        FeatureType type = allstore.getDefaultFeatureType();
189
                        TableResultsModel model = new TableResultsModel(type, set);
190
                        jTableResults.setModel(model);
191
                        jTableResults.validate();
192

    
193
                } catch (DataException e) {
194
                        log.error("ERROR loading selected results in table", e);
195
                }
196
        }
197

    
198
        /**
199
         * 
200
         */
201
        private void setImages() {
202
                PluginServices ps = PluginServices.getPluginServices(this);
203
                if (ps != null) {
204
                        String baseDir = ps.getClassLoader().getBaseDir();
205
                        jButUpdate.setIcon(new ImageIcon(baseDir + File.separator
206
                                        + "images" + File.separator + "icons16" + File.separator
207
                                        + "open.png"));
208
                        jButCancel.setIcon(new ImageIcon(baseDir + File.separator
209
                                        + "images" + File.separator + "icons16" + File.separator
210
                                        + "out.png"));
211
                }
212
        }
213

    
214
        /**
215
         * 
216
         */
217
        private void setMesages() {
218
                PluginServices ps = PluginServices.getPluginServices(this);
219
                if (ps != null) {
220
                        this.jButUpdate.setToolTipText(ps.getText("butloadpattern"));
221
                        this.jButCancel.setText(ps.getText("exit"));
222
                }
223
        }
224

    
225
        /**
226
         * get window info
227
         */
228
        public WindowInfo getWindowInfo() {
229
                WindowInfo info = new WindowInfo(WindowInfo.RESIZABLE);
230
                info.setMinimumSize(this.getMinimumSize());
231
                info.setWidth(500);
232
                info.setHeight(200);
233
                info.setTitle(PluginServices.getText(this, "geocoding_all_results"));
234
                return info;
235
        }
236

    
237
        public Object getWindowProfile() {
238

    
239
                return null;
240
        }
241

    
242
        public void update(Observable observable, Object notification) {
243
                // observable.equals(getFeatureStore()
244
                if (notification instanceof FeatureStoreNotification) {
245
                        FeatureStoreNotification fsNotification = (FeatureStoreNotification) notification;
246
                        String type = fsNotification.getType();
247

    
248
                        // If there are new, updated or deleted features
249
                        // reload the table data
250
                        if (FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {
251
                                FeatureSet sel = null;
252
                                //FIXME
253
                                //control.clearGeocodingPoint();
254
                                jButUpdate.setEnabled(false);
255
                                try {
256
                                        sel = (FeatureSet) fsNotification.getSource()
257
                                                        .getSelection();
258

    
259
                                        if (sel.getSize() > 0) {
260
                                                Iterator<Feature> it = sel.iterator(0);
261
                                                Feature feat = it.next();
262
                                                reloadResults(feat);
263
                                        } else {
264
                                                jTableResults.setModel(new TableResultsModel(control));
265
                                        }
266
                                } catch (DataException e) {
267
                                        log.error("ERROR updateing the posible results table", e);
268
                                }
269

    
270
                        }
271

    
272
                }
273
        }
274

    
275
        private void reloadResults(Feature feature) {
276
                int nres = feature.getInt(GeocodingExtTags.gID_RESUL);
277
                loadResults(nres);
278
        }
279

    
280
        public void setAllStore(FeatureStore allstore) {
281
                this.allstore = allstore;
282
        }
283

    
284
        public void mouseClicked(MouseEvent e) {
285
                // nothing to do
286

    
287
        }
288

    
289
        public void mouseEntered(MouseEvent e) {
290
                // nothing to do
291

    
292
        }
293

    
294
        public void mouseExited(MouseEvent e) {
295
                // nothing to do
296

    
297
        }
298

    
299
        public void mousePressed(MouseEvent e) {
300
                // nothing to do
301

    
302
        }
303

    
304
        public void mouseReleased(MouseEvent e) {
305
                int row = jTableResults.getSelectedRow();
306
                TableResultsModel model = (TableResultsModel) jTableResults.getModel();
307
                if (row != -1) {
308
                        // active update button
309
                        int isselect = (Integer) model.getValueAt(row, 2);
310
                        if (isselect == 1) {
311
                                jButUpdate.setEnabled(false);
312
                        } else {
313
                                jButUpdate.setEnabled(true);
314
                        }
315

    
316
                        // draw and zoom to geom
317

    
318
                        Point pto = model.getGeometry(row, 4, 5);
319
                        try {
320
                                control.showSingleResultsPositionsOnView(pto);
321
                                control.zoomToPoint(pto.getX(), pto.getY());
322
                        } catch (Exception e1) {
323
                                log.error("Doing zoom to point", e1);
324
                        }
325
                }
326

    
327
        }
328

    
329
        /**
330
         * 
331
         * @param shpstore
332
         */
333
        public void setSelectedStore(DefaultFeatureStore shpstore) {
334
                this.shpstore = shpstore;
335
        }
336

    
337
        /**
338
         * 
339
         */
340
        private void updateStoreSelectedResults() {
341
                int rowsel = jTableResults.getSelectedRow();
342
                if (rowsel >= 0) {
343
                        TableResultsModel model = (TableResultsModel) jTableResults
344
                                        .getModel();
345
                        double score = (Double) model.getValueAt(rowsel, 3);
346
                        double x = (Double) model.getValueAt(rowsel, 4);
347
                        double y = (Double) model.getValueAt(rowsel, 5);
348

    
349
                        try {
350
                                shpstore.edit(FeatureStore.MODE_FULLEDIT);
351
                                FeatureSet selfeats = (FeatureSet) shpstore.getSelection();
352
                                if (selfeats.getSize() > 0) {
353
                                        Iterator<Feature> it = selfeats.iterator(0);
354
                                        EditableFeature shpfeat = (EditableFeature) it.next()
355
                                                        .getEditable();
356
                                        shpfeat.set(GeocodingExtTags.gSCORE, score);
357
                                        shpfeat.set(GeocodingExtTags.gX, x);
358
                                        shpfeat.set(GeocodingExtTags.gY, y);
359
                                        selfeats.update(shpfeat);
360
                                        selfeats.dispose();
361
                                }
362
                                shpstore.finishEditing();
363
                                shpstore.dispose();
364

    
365
                        } catch (DataException e) {
366
                                log.error("", e);
367
                        }
368
                }
369
        }
370

    
371
        /**
372
         * 
373
         */
374
        private void updateStoreAllResults() {
375
                int rowsel = jTableResults.getSelectedRow();
376
                if (rowsel >= 0) {
377
                        TableResultsModel model = (TableResultsModel) jTableResults
378
                                        .getModel();
379
                        String idres = (String) model.getValueAt(rowsel, 0);
380

    
381
                        try {
382
                                allstore.edit(FeatureStore.MODE_FULLEDIT);
383
                                FeatureSet feats = (FeatureSet) allstore.getDataSet();
384
                                if (feats.getSize() > 0) {
385
                                        Iterator<Feature> it = feats.iterator(0);
386
                                        while (it.hasNext()) {
387
                                                EditableFeature efeat = it.next().getEditable();
388
                                                String strId = (String) efeat.get(GeocodingExtTags.gID);
389
                                                String strSel = (String) efeat
390
                                                                .get(GeocodingExtTags.gSELECT);
391
                                                // selected row change value to one
392
                                                if (strId.compareTo(idres) == 0) {
393
                                                        efeat.set(GeocodingExtTags.gSELECT, 1);
394
                                                        feats.update(efeat);
395
                                                }
396
                                                // no selected row with selected value zero, nothing to
397
                                                // do
398
                                                else if (strSel.compareTo("0") == 0) {
399

    
400
                                                }
401
                                                // no selected row with selected value one, change to
402
                                                // zero
403
                                                else {
404
                                                        efeat.set(GeocodingExtTags.gSELECT, 0);
405
                                                        feats.update(efeat);
406
                                                }
407
                                        }
408
                                }
409
                                feats.dispose();
410
                                allstore.dispose();
411
                                allstore.finishEditing();
412

    
413
                        } catch (DataException e) {
414
                                log.error("", e);
415
                        }
416
                }
417
        }
418

    
419
}