Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Open.java @ 28606

History | View | Annotate | Download (15.2 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

    
43
package com.iver.cit.gvsig.project.documents.gui;
44

    
45
import java.awt.Component;
46
import java.awt.FlowLayout;
47
import java.awt.GridBagConstraints;
48
import java.awt.GridBagLayout;
49
import java.awt.event.ActionEvent;
50
import java.io.File;
51

    
52
import javax.swing.JButton;
53
import javax.swing.JDialog;
54
import javax.swing.JFileChooser;
55
import javax.swing.JLabel;
56
import javax.swing.JOptionPane;
57
import javax.swing.JPanel;
58
import javax.swing.JTextField;
59
import javax.swing.filechooser.FileFilter;
60

    
61
import com.hardcode.driverManager.Driver;
62
import com.hardcode.driverManager.DriverLoadException;
63
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
64
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
65
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.iver.andami.PluginServices;
67
import com.iver.cit.gvsig.AddLayer;
68
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
69
import com.iver.cit.gvsig.fmap.MapControl;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
73
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
74
import com.iver.cit.gvsig.fmap.layers.Annotation_LayerFactory;
75
import com.iver.cit.gvsig.fmap.layers.FLayer;
76
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
77
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
78
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
79
import com.iver.cit.gvsig.gui.JComboBoxUnits;
80
import com.iver.cit.gvsig.gui.WizardPanel;
81
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
82
import com.iver.cit.gvsig.project.Project;
83
import com.iver.cit.gvsig.project.documents.view.gui.View;
84

    
85

    
86
/**
87
 * Dialog to open an annotation layer.
88
 *
89
 * @author Vicente Caballero Navarro
90
 */
91
public class Annotation_Open extends WizardPanel {
92
    private static String lastPath = null;
93
    private JPanel pGeneral = null;
94
    private JButton bSelectFile = null;
95
    private String fName;
96
    private JTextField tFile;
97
    private CRSSelectPanel pProyection = null;
98
    private JPanel pFileSelection;
99
    private JPanel pControls;
100
    private JPanel pInPixels;
101
    private JComboBoxUnits cmbUnits;
102

    
103
    public Annotation_Open() {
104
        super();
105
        initialize();
106
    }
107

    
108
    /**
109
     * DOCUMENT ME!
110
     */
111
    private void initialize() {
112
        this.setPreferredSize(new java.awt.Dimension(750, 320));
113
        this.setSize(new java.awt.Dimension(510, 311));
114
        this.setLocation(new java.awt.Point(0, 0));
115
        this.add(getPGeneral(), null);
116
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
117
                public void actionPerformed(java.awt.event.ActionEvent evt) {
118
                    acceptButtonActionPerformed(evt);
119
                }
120
            });
121
    }
122

    
123
    /**
124
     * Evento de pulsado del bot?n de seleccionar fichero
125
     *
126
     * @param e
127
     */
128
    private void acceptButtonActionPerformed(ActionEvent e) {
129
        // Selector de Fichero que se quiere georeferenciar
130
        if (e.getSource().equals(this.getBSelectFile())) {
131
            JFileChooser chooser = new JFileChooser(lastPath);
132
            chooser.setDialogTitle(PluginServices.getText(this,
133
                    "seleccionar_fichero"));
134

    
135
            chooser.addChoosableFileFilter(new FileFilter() {
136
                    public boolean accept(File f) {
137
                        if (f.isDirectory()) {
138
                            return true;
139
                        }
140

    
141
                        if (isPointShapeType(f)) {
142
                            return f.getAbsolutePath().toLowerCase().endsWith(".shp");
143
                        }
144

    
145
                        return false;
146
                    }
147

    
148
                    public String getDescription() {
149
                        return PluginServices.getText(this, "Point Shape Files");
150
                    }
151
                });
152
            chooser.addChoosableFileFilter(new FileFilter() {
153
                    public boolean accept(File f) {
154
                        if (f.isDirectory()) {
155
                            return true;
156
                        }
157

    
158
                        String[] files = f.getParentFile().list();
159

    
160
                        for (int i = 0; i < files.length; i++) {
161
                            if (!files[i].equals(f.getName()) &&
162
                                    files[i].replaceAll("gva", "shp")
163
                                                .toLowerCase().equals(f.getName()
164
                                                                           .toLowerCase())) {
165
                                return (isPointShapeType(f));
166
                            }
167
                        }
168

    
169
                        return false;
170
                    }
171

    
172
                    public String getDescription() {
173
                        return PluginServices.getText(this, "Annotations");
174
                    }
175
                });
176

    
177
            int returnVal = chooser.showOpenDialog(this);
178

    
179
            if (returnVal == JFileChooser.APPROVE_OPTION) {
180
                this.fName = chooser.getSelectedFile().toString();
181

    
182
                // FileFilter filter = chooser.getFileFilter();
183
                this.getTFile().setText(fName);
184
                lastPath = chooser.getCurrentDirectory().getAbsolutePath();
185

    
186
                if (PluginServices.getMainFrame() == null) {
187
                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
188
                } else {
189
                    callStateChanged(true);
190
                }
191
            }
192
        }
193
    }
194

    
195
    /**
196
     * This method initializes jTextField
197
     *
198
     * @return javax.swing.JTextField
199
     */
200
    private JTextField getTFile() {
201
        if (tFile == null) {
202
            tFile = new JTextField();
203
            tFile.setPreferredSize(new java.awt.Dimension(350, 25));
204
        }
205

    
206
        return tFile;
207
    }
208

    
209
    /**
210
     * This method initializes jPanel
211
     *
212
     * @return javax.swing.JPanel
213
     */
214
    private JPanel getPGeneral() {
215
        if (pGeneral == null) {
216
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
217
            gridBagConstraints1.insets = new java.awt.Insets(5, 0, 0, 0);
218
            gridBagConstraints1.gridy = 3;
219
            gridBagConstraints1.gridx = 0;
220

    
221
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
222
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
223
            gridBagConstraints.gridy = 0;
224
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
225
            gridBagConstraints.gridx = 0;
226

    
227
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
228
            gridBagConstraints2.insets = new java.awt.Insets(0, 0, 2, 0);
229
            gridBagConstraints2.gridy = 1;
230
            gridBagConstraints2.gridx = 0;
231

    
232
            pGeneral = new JPanel();
233
            pGeneral.setLayout(new GridBagLayout());
234
            pGeneral.setPreferredSize(new java.awt.Dimension(750, 350));
235
            gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
236
            gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
237
            pGeneral.add(getPFileSelection(), gridBagConstraints);
238
            pGeneral.add(getPControls(), gridBagConstraints1);
239
            pGeneral.add(getPInPixels(), gridBagConstraints2);
240
        }
241

    
242
        return pGeneral;
243
    }
244

    
245
    /**
246
     * DOCUMENT ME!
247
     *
248
     * @return DOCUMENT ME!
249
     */
250
    private JPanel getPControls() {
251
        if (pControls == null) {
252
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
253
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
254
            gridBagConstraints2.gridx = 0;
255
            gridBagConstraints2.gridy = 1;
256
            pControls = new JPanel();
257
            pControls.setLayout(new GridBagLayout());
258
            pControls.setPreferredSize(new java.awt.Dimension(475, 200));
259
            pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(
260
                    null, "",
261
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
262
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
263
            gridBagConstraints11.gridx = 0;
264
            gridBagConstraints11.gridy = 0;
265

    
266
            pControls.add(getPProyection(), gridBagConstraints2);
267
        }
268

    
269
        return pControls;
270
    }
271

    
272
    /**
273
     * This method initializes jPanel
274
     *
275
     * @return javax.swing.JPanel
276
     */
277
    private JPanel getPFileSelection() {
278
        if (pFileSelection == null) {
279
            FlowLayout flowLayout = new FlowLayout();
280
            flowLayout.setVgap(10);
281
            pFileSelection = new JPanel();
282
            pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(
283
                    null, PluginServices.getText(this, "cargar"),
284
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
285
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
286
            pFileSelection.setLayout(flowLayout);
287
            pFileSelection.setPreferredSize(new java.awt.Dimension(475, 70));
288
            pFileSelection.add(getTFile(), null);
289
            pFileSelection.add(getBSelectFile(), null);
290
        }
291

    
292
        return pFileSelection;
293
    }
294

    
295
    /**
296
     * This method initializes jButton
297
     *
298
     * @return javax.swing.JButton
299
     */
300
    private JButton getBSelectFile() {
301
        if (bSelectFile == null) {
302
            bSelectFile = new JButton();
303
            bSelectFile.setText(PluginServices.getText(this, "cargar"));
304
        }
305

    
306
        return bSelectFile;
307
    }
308

    
309
    /**
310
     * DOCUMENT ME!
311
     */
312
    public void initWizard() {
313
    }
314

    
315
    /**
316
     * DOCUMENT ME!
317
     */
318
    public void execute() {
319
                // Creamos la capa y la cargamos
320
                boolean ok = false;
321
                View theView = null;
322

    
323
                try {
324
                        theView = (View) PluginServices.getMDIManager().getActiveWindow();
325
                } catch (ClassCastException exc) {
326
                        return;
327
                }
328

    
329
                File fich = new File(fName);
330
                ok = loadFileAnnotationLayer(theView.getMapControl(), fich);
331
                if (!ok)
332
                        JOptionPane.showMessageDialog((Component) PluginServices
333
                                        .getMainFrame(), PluginServices.getText(this,
334
                                        "incorrect_annotation_format"));
335
        }
336

    
337
    /**
338
         * DOCUMENT ME!
339
         *
340
         * @return DOCUMENT ME!
341
         */
342
    public FLayer getLayer() {
343
        return null;
344
    }
345

    
346
    /**
347
     * This method initializes jPanel
348
     *
349
     * @return javax.swing.JPanel
350
     */
351
    private CRSSelectPanel getPProyection() {
352
        if (pProyection == null) {
353
            pProyection = CRSSelectPanel.getPanel(AddLayerDialog.getLastProjection());
354
            pProyection.addActionListener(new java.awt.event.ActionListener() {
355
                    public void actionPerformed(java.awt.event.ActionEvent e) {
356
                        if (pProyection.isOkPressed()) {
357
                                AddLayerDialog.setLastProjection(pProyection.getCurProj());
358
                        }
359
                    }
360
                });
361
        }
362

    
363
        return pProyection;
364
    }
365

    
366
    /**
367
     * This method initializes jPanel
368
     *
369
     * @return javax.swing.JPanel
370
     */
371
    private JPanel getPInPixels() {
372
        if (pInPixels == null) {
373
            pInPixels = new JPanel(); // CRSSelectPanel.getPanel(FOpenDialog.getLastProjection());
374

    
375
            JLabel lbl = new JLabel(PluginServices.getText(this,
376
                        "units_of_annotations"));
377
            pInPixels.add(lbl);
378
            pInPixels.add(getCmbUnits());
379
        }
380

    
381
        return pInPixels;
382
    }
383

    
384
    /**
385
     * DOCUMENT ME!
386
     *
387
     * @return DOCUMENT ME!
388
     */
389
    public JComboBoxUnits getCmbUnits() {
390
                if (cmbUnits == null) {
391
                        cmbUnits = new JComboBoxUnits();
392
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
393
                        cmbUnits.setName("CMBUNITS");
394
                }
395

    
396
                return cmbUnits;
397
        }
398

    
399
    /*
400
     * (non-Javadoc)
401
     *
402
     * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
403
     */
404
    public String getTabName() {
405
        return PluginServices.getText(this, "annotation");
406
    }
407

    
408
    /**
409
     * DOCUMENT ME!
410
     *
411
     * @param file DOCUMENT ME!
412
     *
413
     * @return DOCUMENT ME!
414
     */
415
    private boolean isPointShapeType(File file) {
416
        if (!file.getAbsolutePath().toLowerCase().endsWith(".shp")) {
417
            return false;
418
        }
419

    
420
        VectorialFileAdapter adapter = new VectorialFileAdapter(file);
421
        String driverName = "gvSIG shp driver";
422

    
423
        try {
424
            Driver driver = LayerFactory.getDM().getDriver(driverName);
425
            adapter.setDriver((VectorialDriver) driver);
426
        } catch (DriverLoadException e) {
427
            return false;
428
        }
429

    
430
        FLyrVect capa = new FLyrVect();
431
        capa.setSource(adapter);
432

    
433
        int type;
434

    
435
        try {
436
            ((VectorialFileDriver) capa.getSource().getDriver()).open(file);
437
            ((VectorialFileDriver) capa.getSource().getDriver()).initialize();
438
            type = capa.getSource().getShapeType();
439
        } catch (OpenDriverException e) {
440
                        return false;
441
                } catch (InitializeDriverException e) {
442
                        return false;
443
                } catch (ReadDriverException e) {
444
                        return false;
445
                }
446

    
447
        return (type == FShape.POINT);
448
    }
449

    
450
    /**
451
     * Adds to mapcontrol all the file based layers selected by user in
452
     * fileOpenDialog instance.
453
     *
454
     * @param mapControl MapControl where we want to add the selected layers
455
     * @param file FileOpenDialog where user selected file based layers
456
     *
457
     * @return boolean flag to report sucess of the operation
458
     */
459
    private boolean loadFileAnnotationLayer(MapControl mapControl, File file) {
460
            try {
461
                    Annotation_Layer al = Annotation_LayerFactory.createLayer(file.getName(), file, AddLayerDialog.getLastProjection(), getCmbUnits().getSelectedUnitIndex());
462

    
463
            // A?adir capas al mapControl se trata como una transaccion
464
            mapControl.getMapContext().beginAtomicEvent();
465

    
466
            if (al != null) {
467
                al.setVisible(true);
468
                AddLayer.checkProjection(al, mapControl.getViewPort());
469
                mapControl.getMapContext().getLayers().addLayer(al);
470
            } // if
471

    
472
            mapControl.getMapContext().endAtomicEvent();
473
            return true;
474
            }
475
            catch (Exception ex) {
476
                    PluginServices.getLogger().error("", ex);
477
                    return false;
478
            }
479
    }
480
}