Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Open.java @ 12763

History | View | Annotate | Download (19.4 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
import java.io.IOException;
52
import java.util.ArrayList;
53

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

    
63
import org.cresques.cts.ICoordTrans;
64
import org.cresques.cts.IProjection;
65

    
66
import com.hardcode.driverManager.Driver;
67
import com.hardcode.driverManager.DriverLoadException;
68
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
69
import com.iver.andami.PluginServices;
70
import com.iver.andami.messages.NotificationManager;
71
import com.iver.cit.gvsig.fmap.DriverException;
72
import com.iver.cit.gvsig.fmap.DriverIOExceptionType;
73
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
74
import com.iver.cit.gvsig.fmap.MapControl;
75
import com.iver.cit.gvsig.fmap.ViewPort;
76
import com.iver.cit.gvsig.fmap.core.FShape;
77
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
78
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
79
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
80
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
81
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
82
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
83
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
84
import com.iver.cit.gvsig.fmap.layers.FLayer;
85
import com.iver.cit.gvsig.fmap.layers.FLayers;
86
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
87
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
88
import com.iver.cit.gvsig.fmap.layers.LayerListener;
89
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
90
import com.iver.cit.gvsig.gui.WizardPanel;
91
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
92
import com.iver.cit.gvsig.project.documents.view.gui.View;
93
import com.iver.utiles.swing.JComboBox;
94

    
95

    
96
/**
97
 * Dialog to open an annotation layer.
98
 *
99
 * @author Vicente Caballero Navarro
100
 */
101
public class Annotation_Open extends WizardPanel {
102
    private static String lastPath = null;
103
    private JPanel pGeneral = null;
104
    private JButton bSelectFile = null;
105
    private String fName;
106
    private JTextField tFile;
107
    private CRSSelectPanel pProyection = null;
108
    private JPanel pFileSelection;
109
    private JPanel pControls;
110
    private JPanel pInPixels;
111
    private JComboBox cmbInPixels;
112

    
113
    public Annotation_Open() {
114
        super();
115
        initialize();
116
    }
117

    
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    private void initialize() {
122
        this.setPreferredSize(new java.awt.Dimension(750, 320));
123
        this.setSize(new java.awt.Dimension(510, 311));
124
        this.setLocation(new java.awt.Point(0, 0));
125
        this.add(getPGeneral(), null);
126
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
127
                public void actionPerformed(java.awt.event.ActionEvent evt) {
128
                    acceptButtonActionPerformed(evt);
129
                }
130
            });
131
    }
132

    
133
    /**
134
     * Evento de pulsado del bot?n de seleccionar fichero
135
     *
136
     * @param e
137
     */
138
    private void acceptButtonActionPerformed(ActionEvent e) {
139
        // Selector de Fichero que se quiere georeferenciar
140
        if (e.getSource().equals(this.getBSelectFile())) {
141
            JFileChooser chooser = new JFileChooser(lastPath);
142
            chooser.setDialogTitle(PluginServices.getText(this,
143
                    "seleccionar_fichero"));
144

    
145
            chooser.addChoosableFileFilter(new FileFilter() {
146
                    public boolean accept(File f) {
147
                        if (f.isDirectory()) {
148
                            return true;
149
                        }
150

    
151
                        if (isPointShapeType(f)) {
152
                            return f.getAbsolutePath().toLowerCase().endsWith(".shp");
153
                        }
154

    
155
                        return false;
156
                    }
157

    
158
                    public String getDescription() {
159
                        return PluginServices.getText(this, "Point Shape Files");
160
                    }
161
                });
162
            chooser.addChoosableFileFilter(new FileFilter() {
163
                    public boolean accept(File f) {
164
                        if (f.isDirectory()) {
165
                            return true;
166
                        }
167

    
168
                        String[] files = f.getParentFile().list();
169

    
170
                        for (int i = 0; i < files.length; i++) {
171
                            if (!files[i].equals(f.getName()) &&
172
                                    files[i].replaceAll("gva", "shp")
173
                                                .toLowerCase().equals(f.getName()
174
                                                                           .toLowerCase())) {
175
                                return (isPointShapeType(f));
176
                            }
177
                        }
178

    
179
                        return false;
180
                    }
181

    
182
                    public String getDescription() {
183
                        return PluginServices.getText(this, "Annotations");
184
                    }
185
                });
186

    
187
            int returnVal = chooser.showOpenDialog(this);
188

    
189
            if (returnVal == JFileChooser.APPROVE_OPTION) {
190
                this.fName = chooser.getSelectedFile().toString();
191

    
192
                // FileFilter filter = chooser.getFileFilter();
193
                this.getTFile().setText(fName);
194
                lastPath = chooser.getCurrentDirectory().getAbsolutePath();
195

    
196
                if (PluginServices.getMainFrame() == null) {
197
                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
198
                } else {
199
                    callStateChanged(true);
200
                }
201
            }
202
        }
203
    }
204

    
205
    /**
206
     * This method initializes jTextField
207
     *
208
     * @return javax.swing.JTextField
209
     */
210
    private JTextField getTFile() {
211
        if (tFile == null) {
212
            tFile = new JTextField();
213
            tFile.setPreferredSize(new java.awt.Dimension(350, 25));
214
        }
215

    
216
        return tFile;
217
    }
218

    
219
    /**
220
     * This method initializes jPanel
221
     *
222
     * @return javax.swing.JPanel
223
     */
224
    private JPanel getPGeneral() {
225
        if (pGeneral == null) {
226
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
227
            gridBagConstraints1.insets = new java.awt.Insets(5, 0, 0, 0);
228
            gridBagConstraints1.gridy = 3;
229
            gridBagConstraints1.gridx = 0;
230

    
231
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
232
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
233
            gridBagConstraints.gridy = 0;
234
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
235
            gridBagConstraints.gridx = 0;
236

    
237
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
238
            gridBagConstraints2.insets = new java.awt.Insets(0, 0, 2, 0);
239
            gridBagConstraints2.gridy = 1;
240
            gridBagConstraints2.gridx = 0;
241

    
242
            pGeneral = new JPanel();
243
            pGeneral.setLayout(new GridBagLayout());
244
            pGeneral.setPreferredSize(new java.awt.Dimension(750, 350));
245
            gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
246
            gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
247
            pGeneral.add(getPFileSelection(), gridBagConstraints);
248
            pGeneral.add(getPControls(), gridBagConstraints1);
249
            pGeneral.add(getPInPixels(), gridBagConstraints2);
250
        }
251

    
252
        return pGeneral;
253
    }
254

    
255
    /**
256
     * DOCUMENT ME!
257
     *
258
     * @return DOCUMENT ME!
259
     */
260
    private JPanel getPControls() {
261
        if (pControls == null) {
262
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
263
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
264
            gridBagConstraints2.gridx = 0;
265
            gridBagConstraints2.gridy = 1;
266
            pControls = new JPanel();
267
            pControls.setLayout(new GridBagLayout());
268
            pControls.setPreferredSize(new java.awt.Dimension(475, 200));
269
            pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(
270
                    null, "",
271
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
272
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
273
            gridBagConstraints11.gridx = 0;
274
            gridBagConstraints11.gridy = 0;
275

    
276
            pControls.add(getPProyection(), gridBagConstraints2);
277
        }
278

    
279
        return pControls;
280
    }
281

    
282
    /**
283
     * This method initializes jPanel
284
     *
285
     * @return javax.swing.JPanel
286
     */
287
    private JPanel getPFileSelection() {
288
        if (pFileSelection == null) {
289
            FlowLayout flowLayout = new FlowLayout();
290
            flowLayout.setVgap(10);
291
            pFileSelection = new JPanel();
292
            pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(
293
                    null, PluginServices.getText(this, "cargar"),
294
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
295
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
296
            pFileSelection.setLayout(flowLayout);
297
            pFileSelection.setPreferredSize(new java.awt.Dimension(475, 70));
298
            pFileSelection.add(getTFile(), null);
299
            pFileSelection.add(getBSelectFile(), null);
300
        }
301

    
302
        return pFileSelection;
303
    }
304

    
305
    /**
306
     * This method initializes jButton
307
     *
308
     * @return javax.swing.JButton
309
     */
310
    private JButton getBSelectFile() {
311
        if (bSelectFile == null) {
312
            bSelectFile = new JButton();
313
            bSelectFile.setText(PluginServices.getText(this, "cargar"));
314
        }
315

    
316
        return bSelectFile;
317
    }
318

    
319
    /**
320
     * DOCUMENT ME!
321
     */
322
    public void initWizard() {
323
    }
324

    
325
    /**
326
     * DOCUMENT ME!
327
     */
328
    public void execute() {
329
        // Creamos la capa y la cargamos
330
            boolean ok = false;
331
            try {
332
            View theView = null;
333

    
334
            try {
335
                theView = (View) PluginServices.getMDIManager().getActiveWindow();
336
            } catch (ClassCastException exc) {
337
                return;
338
            }
339

    
340
            File fich = new File(fName);
341
            ok = loadFileAnnotationLayer(theView.getMapControl(), fich);
342
        } catch (DriverLoadException exc) {
343
            NotificationManager.addError("No se pudo acceder a los drivers", exc);
344
        }
345
        if (!ok)
346
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"incorrect_annotation_format"));
347
    }
348

    
349
    /**
350
     * DOCUMENT ME!
351
     *
352
     * @return DOCUMENT ME!
353
     */
354
    public FLayer getLayer() {
355
        return null;
356
    }
357

    
358
    /**
359
     * This method initializes jPanel
360
     *
361
     * @return javax.swing.JPanel
362
     */
363
    private CRSSelectPanel getPProyection() {
364
        if (pProyection == null) {
365
            pProyection = CRSSelectPanel.getPanel(FOpenDialog.getLastProjection());
366
            pProyection.addActionListener(new java.awt.event.ActionListener() {
367
                    public void actionPerformed(java.awt.event.ActionEvent e) {
368
                        if (pProyection.isOkPressed()) {
369
                            FOpenDialog.setLastProjection(pProyection.getCurProj());
370
                        }
371
                    }
372
                });
373
        }
374

    
375
        return pProyection;
376
    }
377

    
378
    /**
379
     * This method initializes jPanel
380
     *
381
     * @return javax.swing.JPanel
382
     */
383
    private JPanel getPInPixels() {
384
        if (pInPixels == null) {
385
            pInPixels = new JPanel(); // CRSSelectPanel.getPanel(FOpenDialog.getLastProjection());
386

    
387
            JLabel lbl = new JLabel(PluginServices.getText(this,
388
                        "units_of_annotations"));
389
            pInPixels.add(lbl);
390
            pInPixels.add(getCmbInPixels());
391
        }
392

    
393
        return pInPixels;
394
    }
395

    
396
    /**
397
     * DOCUMENT ME!
398
     *
399
     * @return DOCUMENT ME!
400
     */
401
    private JComboBox getCmbInPixels() {
402
        if (cmbInPixels == null) {
403
            cmbInPixels = new JComboBox();
404
            cmbInPixels.addItem(PluginServices.getText(this, "pixels"));
405
            cmbInPixels.addItem(PluginServices.getText(this, "meters"));
406

    
407
        }
408

    
409
        return cmbInPixels;
410
    }
411

    
412
    /*
413
     * (non-Javadoc)
414
     *
415
     * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
416
     */
417
    public String getTabName() {
418
        return PluginServices.getText(this, "annotation");
419
    }
420

    
421
    /**
422
     * DOCUMENT ME!
423
     *
424
     * @param file DOCUMENT ME!
425
     *
426
     * @return DOCUMENT ME!
427
     */
428
    private boolean isPointShapeType(File file) {
429
        if (!file.getAbsolutePath().toLowerCase().endsWith(".shp")) {
430
            return false;
431
        }
432

    
433
        VectorialFileAdapter adapter = new VectorialFileAdapter(file);
434
        String driverName = "gvSIG shp driver";
435

    
436
        try {
437
            Driver driver = LayerFactory.getDM().getDriver(driverName);
438
            adapter.setDriver((VectorialDriver) driver);
439
        } catch (DriverLoadException e) {
440
            return false;
441
        }
442

    
443
        FLyrVect capa = new FLyrVect();
444
        capa.setSource(adapter);
445

    
446
        int type;
447

    
448
        try {
449
            ((VectorialFileDriver) capa.getSource().getDriver()).open(file);
450
            ((VectorialFileDriver) capa.getSource().getDriver()).initialize();
451
            type = capa.getSource().getShapeType();
452
        } catch (DriverIOException e) {
453
            return false;
454
        } catch (IOException e) {
455
            return false;
456
        }
457

    
458
        return (type == FShape.POINT);
459
    }
460

    
461
    /**
462
     * Adds to mapcontrol all the file based layers selected by user in
463
     * fileOpenDialog instance.
464
     *
465
     * @param mapControl MapControl where we want to add the selected layers
466
     * @param file FileOpenDialog where user selected file based layers
467
     *
468
     * @return boolean flag to report sucess of the operation
469
     */
470
    private boolean loadFileAnnotationLayer(MapControl mapControl, File file) {
471
        if (file == null) {
472
            return false;
473
        }
474

    
475
        FLayer lyr = null;
476
        IProjection proj = FOpenDialog.getLastProjection();
477
        String driverName = "gvSIG shp driver";
478
        Driver driver = null; // Driver[driverNames.length];
479

    
480
        try {
481
            driver = LayerFactory.getDM().getDriver(driverName);
482
        } catch (DriverLoadException e) {
483
                PluginServices.getLogger().error(e.getMessage(),e);
484
                return false;
485
        }
486

    
487
        // A?adir capas al mapControl se trata como una transaccion
488
        mapControl.getMapContext().beginAtomicEvent();
489

    
490
        String layerName = file.getName();
491
        lyr = LayerFactory.createLayer(layerName, (VectorialFileDriver) driver,
492
                file, proj);
493

    
494
        Annotation_Layer al = new Annotation_Layer();
495
        LayerListener[] layerListeners = lyr.getLayerListeners();
496

    
497
        for (int i = 0; i < layerListeners.length; i++) {
498
            al.addLayerListener(layerListeners[i]);
499
        }
500

    
501
        al.setSource(((FLyrVect) lyr).getSource());
502
        al.setProjection(lyr.getProjection());
503
        al.setName(layerName);
504

    
505
        try {
506
            Annotation_Mapping.addAnnotationMapping(al);
507
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
508
                PluginServices.getLogger().error(e.getMessage(),e);
509
                return false;
510
        } catch (FieldNotFoundException e) {
511
                PluginServices.getLogger().error(e.getMessage(),e);
512
                return false;
513
                } catch (DriverException e) {
514
                        PluginServices.getLogger().error(e.getMessage(),e);
515
                return false;
516
                } catch (Exception e) {
517
                        PluginServices.getLogger().error(e.getMessage(),e);
518
                return false;
519
                }
520

    
521
        ((FSymbol) al.getLegend().getDefaultSymbol()).setFontSizeInPixels(getCmbInPixels()
522
                                                                              .getSelectedItem()
523
                                                                              .equals(PluginServices.getText(
524
                    this, "pixels")));
525

    
526
        if (lyr != null) {
527
            al.setVisible(true);
528
            checkProjection(al, mapControl.getViewPort());
529
            mapControl.getMapContext().getLayers().addLayer(al);
530
        } // if
531

    
532
        mapControl.getMapContext().endAtomicEvent();
533

    
534
        return true;
535
    }
536

    
537
    // TODO Este m?todo est? igual en AddLayer pero es privado y
538
    // no est?tico y he tenido que replicar c?digo
539
    // Comentar a Joaqu?n para la 1.0.3
540
    public static void checkProjection(FLayer lyr, ViewPort viewPort) {
541
        if (lyr instanceof FLayers) {
542
            FLayers layers = (FLayers) lyr;
543

    
544
            for (int i = 0; i < layers.getLayersCount(); i++) {
545
                checkProjection(layers.getLayer(i), viewPort);
546
            }
547
        }
548

    
549
        if (lyr instanceof FLyrVect) {
550
            FLyrVect lyrVect = (FLyrVect) lyr;
551
            IProjection proj = lyr.getProjection();
552

    
553
            // Comprobar que la projecci?n es la misma que la vista
554
            if (proj == null) {
555
                // SUPONEMOS que la capa est? en la proyecci?n que
556
                // estamos pidiendo (que ya es mucho suponer, ya).
557
                lyrVect.setProjection(viewPort.getProjection());
558

    
559
                return;
560
            }
561

    
562
            if (proj != viewPort.getProjection()) {
563
                int option = JOptionPane.YES_OPTION;
564

    
565
                if (!CRSFactory.doesRigurousTransformations()) {
566
                    option = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
567
                            PluginServices.getText(Annotation_Layer.class,
568
                                "reproyectar_aviso") + "\n" +
569
                            PluginServices.getText(Annotation_Layer.class,
570
                                "Capa") + ": " + lyrVect.getName(),
571
                            PluginServices.getText(Annotation_Layer.class,
572
                                "reproyectar_pregunta"),
573
                            JOptionPane.YES_NO_OPTION);
574
                }
575

    
576
                if (option != JOptionPane.OK_OPTION) {
577
                    return;
578
                }
579

    
580
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
581
                lyrVect.setCoordTrans(ct);
582
                System.err.println("coordTrans = " + proj.getAbrev() + " " +
583
                    viewPort.getProjection().getAbrev());
584
            }
585
        }
586
    }
587
}