Statistics
| Revision:

root / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Open.java @ 11536

History | View | Annotate | Download (18.7 KB)

1
package com.iver.cit.gvsig.project.documents.gui;
2

    
3
import java.awt.Component;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.io.File;
10
import java.io.IOException;
11
import java.util.ArrayList;
12

    
13
import javax.swing.JButton;
14
import javax.swing.JDialog;
15
import javax.swing.JFileChooser;
16
import javax.swing.JLabel;
17
import javax.swing.JOptionPane;
18
import javax.swing.JPanel;
19
import javax.swing.JTextField;
20
import javax.swing.filechooser.FileFilter;
21

    
22
import org.cresques.cts.ICoordTrans;
23
import org.cresques.cts.IProjection;
24

    
25
import com.hardcode.driverManager.Driver;
26
import com.hardcode.driverManager.DriverLoadException;
27
import com.iver.andami.PluginServices;
28
import com.iver.andami.messages.NotificationManager;
29
import com.iver.cit.gvsig.fmap.DriverException;
30
import com.iver.cit.gvsig.fmap.DriverIOExceptionType;
31
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType;
32
import com.iver.cit.gvsig.fmap.MapControl;
33
import com.iver.cit.gvsig.fmap.ViewPort;
34
import com.iver.cit.gvsig.fmap.core.FShape;
35
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
36
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
37
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
38
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
39
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
40
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
41
import com.iver.cit.gvsig.fmap.layers.FLayer;
42
import com.iver.cit.gvsig.fmap.layers.FLayers;
43
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
44
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
45
import com.iver.cit.gvsig.fmap.layers.LayerListener;
46
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
47
import com.iver.cit.gvsig.gui.WizardPanel;
48
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
49
import com.iver.cit.gvsig.project.documents.view.gui.View;
50
import com.iver.utiles.swing.JComboBox;
51

    
52

    
53
/**
54
 * Dialog to open an annotation layer.
55
 *
56
 * @author Vicente Caballero Navarro
57
 */
58
public class Annotation_Open extends WizardPanel {
59
    private static String lastPath = null;
60
    private JPanel pGeneral = null;
61
    private JButton bSelectFile = null;
62
    private String fName;
63
    private JTextField tFile;
64
    private CRSSelectPanel pProyection = null;
65
    private JPanel pFileSelection;
66
    private JPanel pControls;
67
    private JPanel pInPixels;
68
    private JComboBox cmbInPixels;
69
    public Annotation_Open() {
70
        super();
71
        initialize();
72
    }
73

    
74
    private void initialize() {
75
        this.setPreferredSize(new java.awt.Dimension(750, 320));
76
        this.setSize(new java.awt.Dimension(510, 311));
77
        this.setLocation(new java.awt.Point(0, 0));
78
        this.add(getPGeneral(), null);
79
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
80
                public void actionPerformed(java.awt.event.ActionEvent evt) {
81
                    acceptButtonActionPerformed(evt);
82
                }
83
            });
84
    }
85

    
86
    /**
87
     * Evento de pulsado del bot?n de seleccionar fichero
88
     *
89
     * @param e
90
     */
91
    private void acceptButtonActionPerformed(ActionEvent e) {
92
        //Selector de Fichero que se quiere georeferenciar
93
        if (e.getSource().equals(this.getBSelectFile())) {
94
            JFileChooser chooser = new JFileChooser(lastPath);
95
            chooser.setDialogTitle(PluginServices.getText(this,
96
                    "seleccionar_fichero"));
97

    
98
            //                        FileFilter f = new DriverFileFilter("Annotation gvSIG Driver");
99
            chooser.addChoosableFileFilter(new FileFilter() {
100
                    public boolean accept(File f) {
101
                        if (f.isDirectory()) {
102
                            return true;
103
                        }
104

    
105
                        if (isPointShapeType(f)) {
106
                            return f.getAbsolutePath().toLowerCase().endsWith(".shp");
107
                        }
108

    
109
                        return false;
110
                    }
111

    
112
                    public String getDescription() {
113
                        return PluginServices.getText(this, "Point Shape Files");
114
                    }
115
                });
116
            chooser.addChoosableFileFilter(new FileFilter() {
117
                    public boolean accept(File f) {
118
                        if (f.isDirectory()) {
119
                            return true;
120
                        }
121

    
122
                        String[] files = f.getParentFile().list();
123

    
124
                        for (int i = 0; i < files.length; i++) {
125
                            if (!files[i].equals(f.getName()) &&
126
                                    files[i].replaceAll("gva", "shp")
127
                                                .toLowerCase().equals(f.getName()
128
                                                                           .toLowerCase())) {
129
                                return (isPointShapeType(f));
130
                            }
131
                        }
132

    
133
                        return false;
134
                    }
135

    
136
                    public String getDescription() {
137
                        return PluginServices.getText(this, "Annotations");
138
                    }
139
                });
140

    
141
            int returnVal = chooser.showOpenDialog(this);
142

    
143
            if (returnVal == JFileChooser.APPROVE_OPTION) {
144
                this.fName = chooser.getSelectedFile().toString();
145

    
146
                //FileFilter filter = chooser.getFileFilter();
147
                this.getTFile().setText(fName);
148
                lastPath = chooser.getCurrentDirectory().getAbsolutePath();
149

    
150
                if (PluginServices.getMainFrame() == null) {
151
                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
152
                } else {
153
                        callStateChanged(true);
154
//                    //Creamos la capa y la cargamos
155
//                    try {
156
//                        View theView = null;
157
//
158
//                        try {
159
//                            theView = (View) PluginServices.getMDIManager()
160
//                                                           .getActiveWindow();
161
//                        } catch (ClassCastException exc) {
162
//                            return;
163
//                        }
164
//
165
//                        File fich = new File(fName);
166
//                        boolean ok = loadFileAnnotationLayer(theView.getMapControl(),
167
//                                fich);
168
//                        callStateChanged(ok);
169
//                    } catch (DriverLoadException exc) {
170
//                        NotificationManager.addError("No se pudo acceder a los drivers",
171
//                            exc);
172
//                    }
173
                }
174
            }
175
        }
176
    }
177

    
178
    /**
179
     * This method initializes jTextField
180
     *
181
     * @return javax.swing.JTextField
182
     */
183
    private JTextField getTFile() {
184
        if (tFile == null) {
185
            tFile = new JTextField();
186
            tFile.setPreferredSize(new java.awt.Dimension(350, 25));
187
        }
188

    
189
        return tFile;
190
    }
191

    
192
    /**
193
     * This method initializes jPanel
194
     *
195
     * @return javax.swing.JPanel
196
     */
197
    private JPanel getPGeneral() {
198
        if (pGeneral == null) {
199
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
200
            gridBagConstraints1.insets = new java.awt.Insets(5, 0, 0, 0);
201
            gridBagConstraints1.gridy = 3;
202
            gridBagConstraints1.gridx = 0;
203

    
204
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
205
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
206
            gridBagConstraints.gridy = 0;
207
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
208
            gridBagConstraints.gridx = 0;
209

    
210
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
211
            gridBagConstraints2.insets = new java.awt.Insets(0, 0, 2, 0);
212
            gridBagConstraints2.gridy = 1;
213
            gridBagConstraints2.gridx = 0;
214

    
215
            pGeneral = new JPanel();
216
            pGeneral.setLayout(new GridBagLayout());
217
            pGeneral.setPreferredSize(new java.awt.Dimension(750, 350));
218
            gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
219
            gridBagConstraints2.anchor = java.awt.GridBagConstraints.NORTH;
220
            pGeneral.add(getPFileSelection(), gridBagConstraints);
221
            pGeneral.add(getPControls(), gridBagConstraints1);
222
            pGeneral.add(getPInPixels(),gridBagConstraints2);
223
        }
224

    
225
        return pGeneral;
226
    }
227

    
228
   private JPanel getPControls() {
229
        if (pControls == null) {
230
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
231
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
232
            gridBagConstraints2.gridx = 0;
233
            gridBagConstraints2.gridy = 1;
234
            pControls = new JPanel();
235
            pControls.setLayout(new GridBagLayout());
236
            pControls.setPreferredSize(new java.awt.Dimension(475, 200));
237
            pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(
238
                    null, "",
239
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
240
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
241
            gridBagConstraints11.gridx = 0;
242
            gridBagConstraints11.gridy = 0;
243

    
244
            //                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
245
            pControls.add(getPProyection(), gridBagConstraints2);
246
        }
247

    
248
        return pControls;
249
    }
250

    
251
    /**
252
     * This method initializes jPanel
253
     *
254
     * @return javax.swing.JPanel
255
     */
256
    private JPanel getPFileSelection() {
257
        if (pFileSelection == null) {
258
            FlowLayout flowLayout = new FlowLayout();
259
            flowLayout.setVgap(10);
260
            pFileSelection = new JPanel();
261
            pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(
262
                    null, PluginServices.getText(this, "cargar"),
263
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
264
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
265
            pFileSelection.setLayout(flowLayout);
266
            pFileSelection.setPreferredSize(new java.awt.Dimension(475, 70));
267
            pFileSelection.add(getTFile(), null);
268
            pFileSelection.add(getBSelectFile(), null);
269
        }
270

    
271
        return pFileSelection;
272
    }
273

    
274
    /**
275
     * This method initializes jButton
276
     *
277
     * @return javax.swing.JButton
278
     */
279
    private JButton getBSelectFile() {
280
        if (bSelectFile == null) {
281
            bSelectFile = new JButton();
282
            bSelectFile.setText(PluginServices.getText(this, "cargar"));
283
        }
284

    
285
        return bSelectFile;
286
    }
287

    
288
    public void initWizard() {
289
        // TODO Auto-generated method stub
290
    }
291

    
292
    public void execute() {
293
             //Creamos la capa y la cargamos
294
        try {
295
            View theView = null;
296

    
297
            try {
298
                theView = (View) PluginServices.getMDIManager()
299
                                               .getActiveWindow();
300
            } catch (ClassCastException exc) {
301
                return;
302
            }
303

    
304
            File fich = new File(fName);
305
            boolean ok = loadFileAnnotationLayer(theView.getMapControl(),
306
                    fich);
307

    
308
        } catch (DriverLoadException exc) {
309
            NotificationManager.addError("No se pudo acceder a los drivers",
310
                exc);
311
        }
312
    }
313

    
314
    public FLayer getLayer() {
315
        return null;
316
    }
317

    
318
    /**
319
     * This method initializes jPanel
320
     *
321
     * @return javax.swing.JPanel
322
     */
323
    private CRSSelectPanel getPProyection() {
324
        if (pProyection == null) {
325
            pProyection = CRSSelectPanel.getPanel(FOpenDialog.getLastProjection());
326
            pProyection.addActionListener(new java.awt.event.ActionListener() {
327
                    public void actionPerformed(java.awt.event.ActionEvent e) {
328
                        if (pProyection.isOkPressed()) {
329
                            FOpenDialog.setLastProjection(pProyection.getCurProj());
330
                        }
331
                    }
332
                });
333
        }
334

    
335
        return pProyection;
336
    }
337
    /**
338
     * This method initializes jPanel
339
     *
340
     * @return javax.swing.JPanel
341
     */
342
    private JPanel getPInPixels() {
343
        if (pInPixels == null) {
344
            pInPixels = new JPanel();//CRSSelectPanel.getPanel(FOpenDialog.getLastProjection());
345
            JLabel lbl=new JLabel(PluginServices.getText(this,"units_of_annotations"));
346
            pInPixels.add(lbl);
347
            pInPixels.add(getCmbInPixels());
348
        }
349

    
350
        return pInPixels;
351
    }
352
    private JComboBox getCmbInPixels() {
353
            if (cmbInPixels==null){
354
            cmbInPixels=new JComboBox();
355
        cmbInPixels.addItem(PluginServices.getText(this,"pixels"));
356
        cmbInPixels.addItem(PluginServices.getText(this,"meters"));
357
//        cmbInPixels.addActionListener(new ActionListener(){
358
//                        public void actionPerformed(ActionEvent e) {
359
//                                if (e.getSource().equals(PluginServices.getText(this,"pixels"))){
360
//
361
//                                }
362
//
363
//                        }
364
//
365
//        });
366
            }
367
        return cmbInPixels;
368
        }
369

    
370
        /* (non-Javadoc)
371
     * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
372
     */
373
    public String getTabName() {
374
        return PluginServices.getText(this, "annotation");
375
    }
376

    
377
    private boolean isPointShapeType(File file) {
378
        if (!file.getAbsolutePath().toLowerCase().endsWith(".shp")) {
379
            return false;
380
        }
381

    
382
        VectorialFileAdapter adapter = new VectorialFileAdapter(file);
383
        String driverName = "gvSIG shp driver";
384

    
385
        try {
386
            Driver driver = LayerFactory.getDM().getDriver(driverName);
387
            adapter.setDriver((VectorialDriver) driver);
388
        } catch (DriverLoadException e) {
389
            return false;
390
        }
391

    
392
        FLyrVect capa = new FLyrVect();
393
        capa.setSource(adapter);
394

    
395
        int type;
396

    
397
        try {
398
            ((VectorialFileDriver) capa.getSource().getDriver()).open(file);
399
            ((VectorialFileDriver) capa.getSource().getDriver()).initialize();
400
            type = capa.getSource().getShapeType();
401
        } catch (DriverIOException e) {
402
            return false;
403
        } catch (IOException e) {
404
            return false;
405
        }
406

    
407
        return (type == FShape.POINT);
408
    }
409

    
410
    /**
411
     * Adds to mapcontrol all the file based layers selected by user in
412
     * fileOpenDialog  instance.
413
     *
414
     * @param mapControl MapControl where we want to add the selected layers
415
     * @param file FileOpenDialog where user selected file based layers
416
     *
417
     * @return boolean flag to report sucess of the operation
418
     */
419
    private boolean loadFileAnnotationLayer(MapControl mapControl, File file) {
420
        if (file == null) {
421
            return false;
422
        }
423

    
424
        FLayer lyr = null;
425
        IProjection proj = FOpenDialog.getLastProjection();
426
        String driverName = "gvSIG shp driver";
427
        Driver driver = null; // Driver[driverNames.length];
428
        ArrayList errors = new ArrayList();
429

    
430
        try {
431
            driver = LayerFactory.getDM().getDriver(driverName);
432
        } catch (DriverLoadException e) {
433
            DriverNotLoadedExceptionType type = new DriverNotLoadedExceptionType();
434
            type.setDriverName(driverName);
435

    
436
            DriverException exception = new DriverException(e, type);
437
            errors.add(exception);
438
        }
439

    
440
        // A?adir capas al mapControl se trata como una transaccion
441
        mapControl.getMapContext().beginAtomicEvent();
442

    
443
        String layerName = file.getName();
444
        lyr = LayerFactory.createLayer(layerName, (VectorialFileDriver) driver,
445
                file, proj);
446

    
447
        Annotation_Layer al = new Annotation_Layer();
448
        LayerListener[] layerListeners = lyr.getLayerListeners();
449

    
450
        for (int i = 0; i < layerListeners.length; i++) {
451
            al.addLayerListener(layerListeners[i]);
452
        }
453

    
454
        al.setSource(((FLyrVect) lyr).getSource());
455
        al.setProjection(lyr.getProjection());
456
        al.setName(layerName);
457

    
458
        try {
459
            Annotation_Mapping.addAnnotationMapping(al);
460
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
461
            DriverIOExceptionType type = new DriverIOExceptionType();
462
            type.setFile(file);
463

    
464
            DriverException newException = new DriverException(e, type);
465
            lyr.addError(newException);
466
            lyr.setAvailable(false);
467
            errors.add(newException);
468
        }
469
        al.setInPixels(getCmbInPixels().getSelectedItem().equals(PluginServices.getText(this,"pixels")));
470

    
471
        if (lyr != null) {
472
            lyr.setVisible(true);
473
            checkProjection(lyr, mapControl.getViewPort());
474
            mapControl.getMapContext().getLayers().addLayer(al);
475
        } // if
476

    
477
        mapControl.getMapContext().endAtomicEvent();
478

    
479
        return true;
480
    }
481

    
482
    //TODO Este m?todo est? igual en AddLayer pero es privado y
483
    //no est?tico y he tenido que replicar c?digo
484
    //Comentar a Joaqu?n para la 1.0.3
485
    private static void checkProjection(FLayer lyr, ViewPort viewPort) {
486
        if (lyr instanceof FLayers) {
487
            FLayers layers = (FLayers) lyr;
488

    
489
            for (int i = 0; i < layers.getLayersCount(); i++) {
490
                checkProjection(layers.getLayer(i), viewPort);
491
            }
492
        }
493

    
494
        if (lyr instanceof FLyrVect) {
495
            FLyrVect lyrVect = (FLyrVect) lyr;
496
            IProjection proj = lyr.getProjection();
497

    
498
            // Comprobar que la projecci?n es la misma que la vista
499
            if (proj == null) {
500
                // SUPONEMOS que la capa est? en la proyecci?n que
501
                // estamos pidiendo (que ya es mucho suponer, ya).
502
                lyrVect.setProjection(viewPort.getProjection());
503

    
504
                return;
505
            }
506

    
507
            if (proj != viewPort.getProjection()) {
508
                int option = JOptionPane.YES_OPTION;
509

    
510
                if (!CRSFactory.doesRigurousTransformations()) {
511
                    option = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
512
                            PluginServices.getText(Annotation_Layer.class,
513
                                "reproyectar_aviso") + "\n" +
514
                            PluginServices.getText(Annotation_Layer.class,
515
                                "Capa") + ": " + lyrVect.getName(),
516
                            PluginServices.getText(Annotation_Layer.class,
517
                                "reproyectar_pregunta"),
518
                            JOptionPane.YES_NO_OPTION);
519
                }
520

    
521
                if (option != JOptionPane.OK_OPTION) {
522
                    return;
523
                }
524

    
525
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
526
                lyrVect.setCoordTrans(ct);
527
                System.err.println("coordTrans = " + proj.getAbrev() + " " +
528
                    viewPort.getProjection().getAbrev());
529
            }
530
        }
531
    }
532
}