Statistics
| Revision:

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

History | View | Annotate | Download (14.7 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.addlayer.AddLayerDialog;
81
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
82
import com.iver.cit.gvsig.fmap.MapContext;
83
import com.iver.cit.gvsig.fmap.MapControl;
84
import com.iver.cit.gvsig.fmap.ViewPort;
85
import com.iver.cit.gvsig.fmap.core.IGeometry;
86
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
87
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
88
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
89
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
90
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
91
import com.iver.cit.gvsig.fmap.layers.CancelationException;
92
import com.iver.cit.gvsig.fmap.layers.FLayer;
93
import com.iver.cit.gvsig.fmap.layers.FLyrGT2_old;
94
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
95
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
96
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
97
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
98
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
99
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
100
import com.iver.cit.gvsig.project.ProjectFactory;
101
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
102
import com.iver.cit.gvsig.project.documents.table.gui.DataBaseOpenDialog;
103
import com.iver.cit.gvsig.project.documents.table.gui.Table;
104
import com.iver.cit.gvsig.project.documents.view.gui.FPanelLocConfig;
105
import com.vividsolutions.jts.geom.LineString;
106
import com.vividsolutions.jts.geom.MultiLineString;
107
import com.vividsolutions.jts.geom.MultiPoint;
108
import com.vividsolutions.jts.geom.Point;
109

    
110
// import java.awt.Frame;
111

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

    
121
        private MapContext m_Mapa;
122

    
123
        private SingleView theView;
124

    
125
        private static final boolean WKB_ENABLED = true;
126

    
127
        class MyImageObserver implements ImageObserver {
128

    
129
                private MapControl mapCtrl;
130

    
131
                public MyImageObserver(MapControl mapCtrl) {
132
                        this.mapCtrl = mapCtrl;
133
                }
134

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

    
148
        }
149

    
150
        private MyImageObserver theImgObserver;
151

    
152
        PruebasGT2 pruebasGT2 = new PruebasGT2();
153

    
154
        public ComandosListener(MapControl mapa, SingleView Owner) {
155
                m_Mapa = mapa.getMapContext();
156
                theView = Owner;
157
                m_MapControl = mapa;
158
        }
159

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

    
180
                // Create the style
181
                final StyleBuilder builder = new StyleBuilder();
182
                final Style style;
183
                Class geometryClass = features.getSchema().getDefaultGeometry()
184
                                .getType();
185

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

    
197
                final MapLayer layer = new DefaultMapLayer(features, style);
198
                layer.setTitle(layerName);
199

    
200
                FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
201
                try {
202
                        m_Mapa.getLayers().addLayer(lyrGT2);
203
                } catch (CancelationException e) {
204
                        // TODO Auto-generated catch block
205
                        e.printStackTrace();
206
                }
207

    
208
        }
209

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

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

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

276
                                dialog.getContentPane().add(canvas);
277

278
                                dialog.setSize(600, 300);
279
                                dialog.show(true);
280
                                ISymbol symbol = canvas.getSymbol();
281
                                FLyrVect lyr = (FLyrVect) m_Mapa.getLayers().getActives()[0];
282
                                if (lyr!=null) {
283

284
                                }*/
285
                        }
286

    
287
                }
288

    
289
                if (e.getActionCommand().indexOf("GT2") != -1 && pruebasGT2 != null) {
290
                        pruebasGT2.setMapContext(m_Mapa);
291
                        pruebasGT2.actionPerformed(e);
292
                }
293

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

    
306
                if (e.getActionCommand() == "ZOOM_MAS") {
307
                        m_MapControl.setTool("zoomIn"); // Por defecto
308
                }
309

    
310
                if (e.getActionCommand() == "ZOOM_MENOS") {
311
                        m_MapControl.setTool("zoomout");
312
                }
313

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

    
318
                if (e.getActionCommand() == "INFO") {
319
                        m_MapControl.setTool("info");
320
                }
321
                if (e.getActionCommand() == "SELRECT") {
322
                        m_MapControl.setTool("rectSelection");
323
                }
324

    
325
                if (e.getActionCommand() == "MEDICION") {
326
                        m_MapControl.setTool("medicion");
327
                }
328
                if (e.getActionCommand() == "MEDIRAREA") {
329
                        m_MapControl.setTool("area");
330

    
331
                        // Prueba de graficos
332
                        theImgObserver = new MyImageObserver(m_MapControl);
333

    
334
                        GraphicLayer lyr = m_MapControl.getMapContext().getGraphicsLayer();
335
                        lyr.clearAllGraphics();
336
                        lyr.clearSymbolsGraphics();
337

    
338
                        File fIcon = new File("d:/sonic.gif");
339
                        ISymbol theSymbol = null;
340
//                        try {
341
                        // picture marker symbol now belongs to extSymbology project
342
//                                theSymbol = new PictureMarkerSymbol(fIcon, null);
343
//                        } catch (IOException e1) {
344
//                                e1.printStackTrace();
345
//                        }
346

    
347
                        // File fIcon = new File("d:/7bart.gif");
348

    
349

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

    
360
                if ((e.getActionCommand() == "ADD_LAYER")
361
                                || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
362
                        JDialog dlg = new JDialog();
363
                        dlg.setModal(true);
364
                        
365
                        FileOpenWizard fileDlg = null; //new FileOpenWizard(new IFileOpen[] {VectorialFileDriver.class}); //, RasterDriver.class });
366
                        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
367
                        // dbop.setClasses(new Class[]{DBDriver.class});
368

    
369
                        AddLayerDialog fopen = new AddLayerDialog();
370
                        fopen.addTab("Fichero", fileDlg);
371
                        fopen.addTab("Base de datos", dbop);
372
                        // fileDlg.setPreferredSize(fopen.getSize());
373
                        dlg.setSize(fopen.getSize());
374
                        dlg.getContentPane().add(fopen);
375
                        dlg.pack();
376
                        dlg.show();
377

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

    
394
                        m_MapControl.getMapContext().beginAtomicEvent();
395

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

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

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

    
426
                                }
427

    
428
                        } // for
429
                        m_MapControl.getMapContext().endAtomicEvent();
430
                        theView.getTOC().refresh();
431

    
432
                        return;
433

    
434
                }
435

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

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

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

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

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

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

    
486
                                }
487

    
488
                        }
489

    
490
                }
491
        }
492
}