Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / ComandosListener.java @ 10679

History | View | Annotate | Download (15 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.ThemeManagerWindow;
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() == "ZOOM_MAS") {
311
                        m_MapControl.setTool("zoomIn"); // Por defecto
312
                }
313

    
314
                if (e.getActionCommand() == "ZOOM_MENOS") {
315
                        m_MapControl.setTool("zoomout");
316
                }
317

    
318
                if (e.getActionCommand() == "PAN") {
319
                        m_MapControl.setTool("pan");
320
                }
321

    
322
                if (e.getActionCommand() == "INFO") {
323
                        m_MapControl.setTool("info");
324
                }
325
                if (e.getActionCommand() == "SELRECT") {
326
                        m_MapControl.setTool("rectSelection");
327
                }
328

    
329
                if (e.getActionCommand() == "MEDICION") {
330
                        m_MapControl.setTool("medicion");
331
                }
332
                if (e.getActionCommand() == "MEDIRAREA") {
333
                        m_MapControl.setTool("area");
334

    
335
                        // Prueba de graficos
336
                        theImgObserver = new MyImageObserver(m_MapControl);
337

    
338
                        GraphicLayer lyr = m_MapControl.getMapContext().getGraphicsLayer();
339
                        lyr.clearAllGraphics();
340
                        lyr.clearSymbolsGraphics();
341

    
342
                        FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
343
                        File fIcon = new File("d:/sonic.gif");
344
                        // File fIcon = new File("d:/7bart.gif");
345
                        theSymbol.setSizeInPixels(true);
346

    
347
                        theSymbol.setIconURI(fIcon.toURI());
348
                        theSymbol.setSize(theSymbol.getIcon().getHeight(null));
349

    
350
                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN);
351
                        theSymbol.setImgObserver(theImgObserver);
352

    
353
                        int idSymbol = lyr.addSymbol(theSymbol);
354
                        IGeometry geom = ShapeFactory.createPoint2D(500000, 4499980);
355
                        FGraphic theGraphic = new FGraphic(geom, idSymbol);
356
                        lyr.addGraphic(theGraphic);
357
                        FGraphicLabel theLabel = new FGraphicLabel(geom, idSymbol,
358
                                        "Hola colega");
359
                        // lyr.addGraphic(theLabel);
360
                        m_MapControl.drawGraphics();
361
                }
362

    
363
                if ((e.getActionCommand() == "ADD_LAYER")
364
                                || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
365
                        JDialog dlg = new JDialog();
366
                        dlg.setModal(true);
367
                        FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
368
                                        VectorialFileDriver.class, RasterDriver.class });
369
                        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
370
                        // dbop.setClasses(new Class[]{DBDriver.class});
371

    
372
                        FOpenDialog fopen = new FOpenDialog();
373
                        fopen.addTab("Fichero", fileDlg);
374
                        fopen.addTab("Base de datos", dbop);
375
                        // fileDlg.setPreferredSize(fopen.getSize());
376
                        dlg.setSize(fopen.getSize());
377
                        dlg.getContentPane().add(fopen);
378
                        dlg.pack();
379
                        dlg.show();
380

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

    
397
                        m_MapControl.getMapContext().beginAtomicEvent();
398

    
399
                        for (int iFile = 0; iFile < files.length; iFile++) {
400
                                File fich = files[iFile];
401
                                String layerName = fich.getName();
402
                                String layerPath = fich.getAbsolutePath();
403

    
404
                                if (drivers[iFile] instanceof VectorialFileDriver) {
405
                                        lyr = LayerFactory.createLayer(layerName,
406
                                                        (VectorialFileDriver) drivers[iFile], fich, proj);
407
                                } else if (drivers[iFile] instanceof RasterDriver) {
408
                                        /*
409
                                         * lyr = LayerFactory.createLayer(layerName,
410
                                         * driverNames[iFile], fich, proj);
411
                                         */}
412

    
413
                                lyr.setVisible(true);
414
                                lyr.setName(layerName);
415
                                if (lyr != null) {
416
                                        try {
417
                                                theView.getMapControl().getMapContext().getLayers()
418
                                                                .addLayer(lyr);
419
                                        } catch (CancelationException ex) {
420
                                                // TODO Auto-generated catch block
421
                                                ex.printStackTrace();
422
                                        }
423
                                        // TODO: Poner una variable y dibujar solo cuando
424
                                        // todas las capas hayan sido cargadas.
425
                                        // theView.getMapControl().drawMap();
426
                                        // theView.getTOC().refresh();
427

    
428
                                }
429

    
430
                        } // for
431
                        m_MapControl.getMapContext().endAtomicEvent();
432
                        theView.getTOC().refresh();
433

    
434
                        return;
435

    
436
                }
437

    
438
                /**
439
                 *
440
                 * String pathFich = fc.getDirectory() + fc.getFile();
441
                 *
442
                 *
443
                 * System.out.println(pathFich); FLyrVect lyr = new
444
                 * FLyrVect(fc.getFile(),pathFich);
445
                 *
446
                 * m_Mapa.AddLayer(lyr); m_Owner.jLeyenda.Refresh();
447
                 *  }
448
                 *
449
                 */
450
                if (e.getActionCommand() == "EXPORT") {
451
                        // Export jExport = new Export(m_MapControl);
452
                }
453

    
454
                if (e.getActionCommand() == "VIEW_TABLE") {
455
                        FLayer[] actives = m_Mapa.getLayers().getActives();
456

    
457
                        for (int i = 0; i < actives.length; i++) {
458
                                if (actives[i] instanceof AlphanumericData) {
459
                                        AlphanumericData co = (AlphanumericData) actives[i];
460

    
461
                                        SelectableDataSource dataSource;
462
                                        try {
463
                                                dataSource = co.getRecordset();
464
                                                EditableAdapter ea = new EditableAdapter();
465
                                                ea.setOriginalDataSource(dataSource);
466
                                                ProjectTable projectTable = ProjectFactory.createTable(
467
                                                                actives[i].getName(), ea);
468
                                                projectTable.setAssociatedTable(co);
469

    
470
                                                Table t = new Table();
471
                                                t.setModel(projectTable);
472
                                                JDialog myDialog = new JDialog(theView,
473
                                                                "Tabla de Atributos ("
474
                                                                                + t.getModel().getAssociatedTable()
475
                                                                                                .getRecordset().getRowCount()
476
                                                                                + " registros)");
477
                                                myDialog.setContentPane(t);
478

    
479
                                                // myViewer.addAttributeTableViewerListener(m_Owner);
480
                                                myDialog.pack();
481
                                                myDialog.show();
482
                                        } catch (ReadDriverException e1) {
483
                                                e1.printStackTrace();
484
                                        } catch (HeadlessException ex) {
485
                                                ex.printStackTrace();
486
                                        }
487

    
488
                                }
489

    
490
                        }
491

    
492
                }
493
        }
494
}