Statistics
| Revision:

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

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

    
111
// import java.awt.Frame;
112

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

    
122
        private MapContext m_Mapa;
123

    
124
        private SingleView theView;
125

    
126
        private static final boolean WKB_ENABLED = true;
127

    
128
        class MyImageObserver implements ImageObserver {
129

    
130
                private MapControl mapCtrl;
131

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

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

    
149
        }
150

    
151
        private MyImageObserver theImgObserver;
152

    
153
        PruebasGT2 pruebasGT2 = new PruebasGT2();
154

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

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

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

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

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

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

    
209
        }
210

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

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

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

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

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

285
                                }*/
286
                        }
287

    
288
                }
289

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

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

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

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

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

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

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

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

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

    
339
                        File fIcon = new File("d:/sonic.gif");
340
                        ISymbol theSymbol = null;
341
                        try {
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
                        FileOpenDialog fileDlg = new FileOpenDialog(new Class[] {
365
                                        VectorialFileDriver.class}); //, RasterDriver.class });
366
                        DataBaseOpenDialog dbop = new DataBaseOpenDialog();
367
                        // dbop.setClasses(new Class[]{DBDriver.class});
368

    
369
                        FOpenDialog fopen = new FOpenDialog();
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
}