Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / ComandosListener.java @ 10626

History | View | Annotate | Download (15.1 KB)

1
/*
2
 * Created on 17-jun-2003
3
 *
4
 * Copyright (c) 2003
5
 * Francisco Jos?Pearrubia Mart?ez
6
 * IVER Tecnolog?s de la Informacin S.A.
7
 * Salamanca 50
8
 * 46005 Valencia (        SPAIN )
9
 * +34 963163400
10
 * mailto:fran@iver.es
11
 * http://www.iver.es
12
 */
13
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
14
 *
15
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
30
 *
31
 * For more information, contact:
32
 *
33
 *  Generalitat Valenciana
34
 *   Conselleria d'Infraestructures i Transport
35
 *   Av. Blasco Ib??ez, 50
36
 *   46010 VALENCIA
37
 *   SPAIN
38
 *
39
 *      +34 963862235
40
 *   gvsig@gva.es
41
 *      www.gvsig.gva.es
42
 *
43
 *    or
44
 *
45
 *   IVER T.I. S.A
46
 *   Salamanca 50
47
 *   46005 Valencia
48
 *   Spain
49
 *
50
 *   +34 963163400
51
 *   dac@iver.es
52
 */
53
package com.iver.cit.gvsig.DEMO;
54

    
55
import java.awt.Color;
56
import java.awt.HeadlessException;
57
import java.awt.Image;
58
import java.awt.event.ActionEvent;
59
import java.awt.event.ActionListener;
60
import java.awt.image.ImageObserver;
61
import java.io.File;
62
import java.io.FileNotFoundException;
63
import java.io.IOException;
64

    
65
import javax.swing.JDialog;
66
import javax.swing.JOptionPane;
67

    
68
import org.cresques.cts.IProjection;
69
import org.geotools.data.DataSourceException;
70
import org.geotools.data.DataStore;
71
import org.geotools.data.FeatureSource;
72
import org.geotools.map.DefaultMapLayer;
73
import org.geotools.map.MapLayer;
74
import org.geotools.styling.Style;
75
import org.geotools.styling.StyleBuilder;
76

    
77
import com.hardcode.driverManager.Driver;
78
import com.hardcode.driverManager.DriverLoadException;
79
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
80
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
81
import com.iver.cit.gvsig.fmap.MapContext;
82
import com.iver.cit.gvsig.fmap.MapControl;
83
import com.iver.cit.gvsig.fmap.ViewPort;
84
import com.iver.cit.gvsig.fmap.core.IGeometry;
85
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
86
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
87
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
88
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
89
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
90
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
91
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
92
import com.iver.cit.gvsig.fmap.layers.CancelationException;
93
import com.iver.cit.gvsig.fmap.layers.FLayer;
94
import com.iver.cit.gvsig.fmap.layers.FLyrGT2_old;
95
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
96
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
97
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
98
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
99
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
100
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
101
import com.iver.cit.gvsig.project.ProjectFactory;
102
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
103
import com.iver.cit.gvsig.project.documents.gui.FileOpenDialog;
104
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
105
import com.iver.cit.gvsig.project.documents.table.gui.DataBaseOpenDialog;
106
import com.iver.cit.gvsig.project.documents.table.gui.Table;
107
import com.iver.cit.gvsig.project.documents.view.gui.FPanelLocConfig;
108
import com.iver.cit.gvsig.project.documents.view.legend.gui.FThemeManagerWindow;
109
import com.vividsolutions.jts.geom.LineString;
110
import com.vividsolutions.jts.geom.MultiLineString;
111
import com.vividsolutions.jts.geom.MultiPoint;
112
import com.vividsolutions.jts.geom.Point;
113

    
114
// import java.awt.Frame;
115

    
116
/**
117
 * @author Administrador
118
 *
119
 * To change the template for this generated type comment go to
120
 * Window>Preferences>Java>Code Generation>Code and Comments
121
 */
122
public class ComandosListener implements ActionListener {
123
        private MapControl m_MapControl;
124

    
125
        private MapContext m_Mapa;
126

    
127
        private SingleView theView;
128

    
129
        private static final boolean WKB_ENABLED = true;
130

    
131
        class MyImageObserver implements ImageObserver {
132

    
133
                private MapControl mapCtrl;
134

    
135
                public MyImageObserver(MapControl mapCtrl) {
136
                        this.mapCtrl = mapCtrl;
137
                }
138

    
139
                public boolean imageUpdate(Image img, int infoflags, int x, int y,
140
                                int width, int height) {
141
                        if ((infoflags | ImageObserver.ALLBITS) != 0) {
142
                                // TODO: A?ADIR ALGO AS? AL MAPCONTROL
143
                                // if (mapCtrl.getStatus() == MapControl.ACTUALIZADO)
144
                                {
145
                                        // System.out.println("Image " + infoflags);
146
                                        mapCtrl.drawGraphics();
147
                                }
148
                        }
149
                        return true;
150
                }
151

    
152
        }
153

    
154
        private MyImageObserver theImgObserver;
155

    
156
        PruebasGT2 pruebasGT2 = new PruebasGT2();
157

    
158
        public ComandosListener(MapControl mapa, SingleView Owner) {
159
                m_Mapa = mapa.getMapContext();
160
                theView = Owner;
161
                m_MapControl = mapa;
162
        }
163

    
164
        /**
165
         * Load the data from the specified dataStore and construct a
166
         * {@linkPlain Context context} with a default style.
167
         *
168
         * @param url
169
         *            The url of the shapefile to load.
170
         * @param name
171
         *            DOCUMENT ME!
172
         *
173
         * @throws IOException
174
         *             is a I/O error occured.
175
         * @throws DataSourceException
176
         *             if an error occured while reading the data source.
177
         * @throws FileNotFoundException
178
         *             DOCUMENT ME!
179
         */
180
        protected void loadLayer(DataStore store, String layerName)
181
                        throws IOException, DataSourceException {
182
                final FeatureSource features = store.getFeatureSource(layerName);
183

    
184
                // Create the style
185
                final StyleBuilder builder = new StyleBuilder();
186
                final Style style;
187
                Class geometryClass = features.getSchema().getDefaultGeometry()
188
                                .getType();
189

    
190
                if (LineString.class.isAssignableFrom(geometryClass)
191
                                || MultiLineString.class.isAssignableFrom(geometryClass)) {
192
                        style = builder.createStyle(builder.createLineSymbolizer());
193
                } else if (Point.class.isAssignableFrom(geometryClass)
194
                                || MultiPoint.class.isAssignableFrom(geometryClass)) {
195
                        style = builder.createStyle(builder.createPointSymbolizer());
196
                } else {
197
                        style = builder.createStyle(builder.createPolygonSymbolizer(
198
                                        Color.ORANGE, Color.BLACK, 1));
199
                }
200

    
201
                final MapLayer layer = new DefaultMapLayer(features, style);
202
                layer.setTitle(layerName);
203

    
204
                FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
205
                try {
206
                        m_Mapa.getLayers().addLayer(lyrGT2);
207
                } catch (CancelationException e) {
208
                        // TODO Auto-generated catch block
209
                        e.printStackTrace();
210
                }
211

    
212
        }
213

    
214
        /*
215
         * (non-Javadoc)
216
         *
217
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
218
         */
219
        public void actionPerformed(ActionEvent e) {
220
                // TODO LWS Esto debe venir de fuera (proyeccion del fichero, o de la
221
                // vista)
222
                IProjection proj = CRSFactory.getCRS("EPSG:23030");
223
                ViewPort vp = m_Mapa.getViewPort();
224

    
225
                if (e.getActionCommand() == "ZOOM_TODO") {
226
                        try {
227
                                vp.setExtent(m_Mapa.getFullExtent());
228
                                m_MapControl.drawMap(false);
229
                        } catch (ReadDriverException e1) {
230
                                // TODO Auto-generated catch block
231
                                e1.printStackTrace();
232
                        }
233
                        m_MapControl.drawMap(false);
234
                }
235
                if (e.getActionCommand() == "ZOOM_PREVIO") {
236
                        vp.setPreviousExtent();
237
                }
238
                if (e.getActionCommand() == "NETWORK_GENERATEREDFILE") {
239
//                        FLyrVect lyr = (FLyrVect) m_Mapa.getLayers().getActives()[0];
240
//                        NetworkWriter netBuilder = new NetworkWriter();
241
//
242
//                        String redFilePath = lyr.getName().replaceFirst("\\Q.shp\\E", "");
243
//                        File redFile = new File("c:/" + redFilePath + ".red");
244
//                        String fieldType = "tipored"; String fieldDist = "length"; String fieldSense = "sen";
245
//                        try {
246
//                                netBuilder.setLayer(lyr, fieldType, fieldDist, fieldSense);
247
//                                DbfWriter nodeWriter = new DbfWriter();
248
//                                nodeWriter.setFile(new File("c:/nodes.dbf"));
249
//
250
//                                DbfWriter edgeWriter = new DbfWriter();
251
//                                edgeWriter.setFile(new File("c:/edges.dbf"));
252
//
253
//                                netBuilder.setEdgeWriter(edgeWriter);
254
//                                netBuilder.setNodeWriter(nodeWriter);
255
//
256
//                                netBuilder.writeNetwork();
257
//                                // netBuilder.createRedFile(lyr, redFile);
258
//                        } catch (DriverException e1) {
259
//                                // TODO Auto-generated catch block
260
//                                e1.printStackTrace();
261
//                        } catch (EditionException e1) {
262
//                                // TODO Auto-generated catch block
263
//                                e1.printStackTrace();
264
//                        }
265
                        JOptionPane.showMessageDialog(null, "Hecho. ");
266
                }
267

    
268
                if (e.getActionCommand() == "SYMBOL_MANAGER") {
269
                        if (e.getActionCommand() == "SYMBOL_MANAGER") {
270
                                JDialog dialog = new JDialog();
271
                                /*
272
                                 * JSVGCanvas canvas = new JSVGCanvas(); try { canvas.setURI(
273
                                 * new File("D:/java/eclipse30/eclipse/workspace/FMap
274
                                 * 03/docs/fill1.svg").toURL().toString() ); } catch
275
                                 * (MalformedURLException e1) { // TODO Auto-generated catch
276
                                 * block e1.printStackTrace(); }
277
                                 *b/
278
                                SymbolSelector canvas = new SymbolSelector(null, FShape.POINT, new File("../com.iver.cit.gvsig.graph/symbols/"));
279

280
                                dialog.getContentPane().add(canvas);
281

282
                                dialog.setSize(600, 300);
283
                                dialog.show(true);
284
                                ISymbol symbol = canvas.getSymbol();
285
                                FLyrVect lyr = (FLyrVect) m_Mapa.getLayers().getActives()[0];
286
                                if (lyr!=null) {
287

288
                                }*/
289
                        }
290

    
291
                }
292

    
293
                if (e.getActionCommand().indexOf("GT2") != -1 && pruebasGT2 != null) {
294
                        pruebasGT2.setMapContext(m_Mapa);
295
                        pruebasGT2.actionPerformed(e);
296
                }
297

    
298
                if (e.getActionCommand() == "OPEN_LOCATOR") {
299
                        // Set up the dialog that the button brings up.
300
                        JDialog dialog = new JDialog();
301
                        FPanelLocConfig m_panelLoc = new FPanelLocConfig(theView
302
                                        .getMapOverview());
303
                        m_panelLoc.setPreferredSize(m_panelLoc.getSize());
304
                        dialog.getContentPane().add(m_panelLoc);
305
                        dialog.setModal(true);
306
                        dialog.pack();
307
                        dialog.show();
308
                }
309

    
310
                if (e.getActionCommand() == "PRUEBA") {
311
                        int i;
312
                        double valor;
313
                        FThemeManagerWindow dlg = new FThemeManagerWindow();
314

    
315
                        dlg.show();
316
                }
317

    
318
                if (e.getActionCommand() == "ZOOM_MAS") {
319
                        m_MapControl.setTool("zoomIn"); // Por defecto
320
                }
321

    
322
                if (e.getActionCommand() == "ZOOM_MENOS") {
323
                        m_MapControl.setTool("zoomout");
324
                }
325

    
326
                if (e.getActionCommand() == "PAN") {
327
                        m_MapControl.setTool("pan");
328
                }
329

    
330
                if (e.getActionCommand() == "INFO") {
331
                        m_MapControl.setTool("info");
332
                }
333
                if (e.getActionCommand() == "SELRECT") {
334
                        m_MapControl.setTool("rectSelection");
335
                }
336

    
337
                if (e.getActionCommand() == "MEDICION") {
338
                        m_MapControl.setTool("medicion");
339
                }
340
                if (e.getActionCommand() == "MEDIRAREA") {
341
                        m_MapControl.setTool("area");
342

    
343
                        // Prueba de graficos
344
                        theImgObserver = new MyImageObserver(m_MapControl);
345

    
346
                        GraphicLayer lyr = m_MapControl.getMapContext().getGraphicsLayer();
347
                        lyr.clearAllGraphics();
348
                        lyr.clearSymbolsGraphics();
349

    
350
                        FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
351
                        File fIcon = new File("d:/sonic.gif");
352
                        // File fIcon = new File("d:/7bart.gif");
353
                        theSymbol.setSizeInPixels(true);
354

    
355
                        theSymbol.setIconURI(fIcon.toURI());
356
                        theSymbol.setSize(theSymbol.getIcon().getHeight(null));
357

    
358
                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN);
359
                        theSymbol.setImgObserver(theImgObserver);
360

    
361
                        int idSymbol = lyr.addSymbol(theSymbol);
362
                        IGeometry geom = ShapeFactory.createPoint2D(500000, 4499980);
363
                        FGraphic theGraphic = new FGraphic(geom, idSymbol);
364
                        lyr.addGraphic(theGraphic);
365
                        FGraphicLabel theLabel = new FGraphicLabel(geom, idSymbol,
366
                                        "Hola colega");
367
                        // lyr.addGraphic(theLabel);
368
                        m_MapControl.drawGraphics();
369
                }
370

    
371
                if ((e.getActionCommand() == "ADD_LAYER")
372
                                || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
373
                        JDialog dlg = new JDialog();
374
                        dlg.setModal(true);
375
                        FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
376
                                        VectorialFileDriver.class, RasterDriver.class });
377
                        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
378
                        // dbop.setClasses(new Class[]{DBDriver.class});
379

    
380
                        FOpenDialog fopen = new FOpenDialog();
381
                        fopen.addTab("Fichero", fileDlg);
382
                        fopen.addTab("Base de datos", dbop);
383
                        // fileDlg.setPreferredSize(fopen.getSize());
384
                        dlg.setSize(fopen.getSize());
385
                        dlg.getContentPane().add(fopen);
386
                        dlg.pack();
387
                        dlg.show();
388

    
389
                        // TODO: Hacer lo de la capa WMS tambi?n.
390
                        if (fileDlg.getFiles() == null)
391
                                return;
392
                        FLayer lyr = null;
393
                        File[] files = fileDlg.getFiles();
394
                        String[] driverNames = fileDlg.getDriverNames();
395
                        Driver[] drivers = new Driver[driverNames.length];
396
                        for (int i = 0; i < drivers.length; i++) {
397
                                try {
398
                                        drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
399
                                } catch (DriverLoadException ex) {
400
                                        System.err.println("No se pudo cargar el driver "
401
                                                        + ex.getMessage());
402
                                }
403
                        }
404

    
405
                        m_MapControl.getMapContext().beginAtomicEvent();
406

    
407
                        for (int iFile = 0; iFile < files.length; iFile++) {
408
                                File fich = files[iFile];
409
                                String layerName = fich.getName();
410
                                String layerPath = fich.getAbsolutePath();
411

    
412
                                if (drivers[iFile] instanceof VectorialFileDriver) {
413
                                        lyr = LayerFactory.createLayer(layerName,
414
                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
415
                                } else if (drivers[iFile] instanceof RasterDriver) {
416
                                        /*
417
                                         * lyr = LayerFactory.createLayer(layerName,
418
                                         * driverNames[iFile], fich, proj);
419
                                         */}
420

    
421
                                lyr.setVisible(true);
422
                                lyr.setName(layerName);
423
                                if (lyr != null) {
424
                                        try {
425
                                                theView.getMapControl().getMapContext().getLayers()
426
                                                                .addLayer(lyr);
427
                                        } catch (CancelationException ex) {
428
                                                // TODO Auto-generated catch block
429
                                                ex.printStackTrace();
430
                                        }
431
                                        // TODO: Poner una variable y dibujar solo cuando
432
                                        // todas las capas hayan sido cargadas.
433
                                        // theView.getMapControl().drawMap();
434
                                        // theView.getTOC().refresh();
435

    
436
                                }
437

    
438
                        } // for
439
                        m_MapControl.getMapContext().endAtomicEvent();
440
                        theView.getTOC().refresh();
441

    
442
                        return;
443

    
444
                }
445

    
446
                /**
447
                 *
448
                 * String pathFich = fc.getDirectory() + fc.getFile();
449
                 *
450
                 *
451
                 * System.out.println(pathFich); FLyrVect lyr = new
452
                 * FLyrVect(fc.getFile(),pathFich);
453
                 *
454
                 * m_Mapa.AddLayer(lyr); m_Owner.jLeyenda.Refresh();
455
                 *  }
456
                 *
457
                 */
458
                if (e.getActionCommand() == "EXPORT") {
459
                        // Export jExport = new Export(m_MapControl);
460
                }
461

    
462
                if (e.getActionCommand() == "VIEW_TABLE") {
463
                        FLayer[] actives = m_Mapa.getLayers().getActives();
464

    
465
                        for (int i = 0; i < actives.length; i++) {
466
                                if (actives[i] instanceof AlphanumericData) {
467
                                        AlphanumericData co = (AlphanumericData) actives[i];
468

    
469
                                        SelectableDataSource dataSource;
470
                                        try {
471
                                                dataSource = co.getRecordset();
472
                                                EditableAdapter ea = new EditableAdapter();
473
                                                ea.setOriginalDataSource(dataSource);
474
                                                ProjectTable projectTable = ProjectFactory.createTable(
475
                                                                actives[i].getName(), ea);
476
                                                projectTable.setAssociatedTable(co);
477

    
478
                                                Table t = new Table();
479
                                                t.setModel(projectTable);
480
                                                JDialog myDialog = new JDialog(theView,
481
                                                                "Tabla de Atributos ("
482
                                                                                + t.getModel().getAssociatedTable()
483
                                                                                                .getRecordset().getRowCount()
484
                                                                                + " registros)");
485
                                                myDialog.setContentPane(t);
486

    
487
                                                // myViewer.addAttributeTableViewerListener(m_Owner);
488
                                                myDialog.pack();
489
                                                myDialog.show();
490
                                        } catch (ReadDriverException e1) {
491
                                                e1.printStackTrace();
492
                                        } catch (HeadlessException ex) {
493
                                                ex.printStackTrace();
494
                                        }
495

    
496
                                }
497

    
498
                        }
499

    
500
                }
501
        }
502
}