Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / DEMO / ComandosListener.java @ 8745

History | View | Annotate | Download (15.5 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.Graphics2D;
57
import java.awt.HeadlessException;
58
import java.awt.Image;
59
import java.awt.event.ActionEvent;
60
import java.awt.event.ActionListener;
61
import java.awt.image.ImageObserver;
62
import java.io.File;
63
import java.io.FileNotFoundException;
64
import java.io.IOException;
65

    
66
import javax.swing.JDialog;
67

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

    
78
import com.hardcode.driverManager.Driver;
79
import com.hardcode.driverManager.DriverLoadException;
80
import com.iver.cit.gvsig.fmap.DriverException;
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

    
102
import com.iver.cit.gvsig.gui.DataBaseOpenDialog;
103
import com.iver.cit.gvsig.gui.FOpenDialog;
104
import com.iver.cit.gvsig.gui.FPanelLocConfig;
105
import com.iver.cit.gvsig.gui.FileOpenDialog;
106
import com.iver.cit.gvsig.gui.Table;
107
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
108
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
109
import com.iver.cit.gvsig.project.ProjectTable;
110
import com.vividsolutions.jts.geom.LineString;
111
import com.vividsolutions.jts.geom.MultiLineString;
112
import com.vividsolutions.jts.geom.MultiPoint;
113
import com.vividsolutions.jts.geom.Point;
114

    
115

    
116

    
117
//import java.awt.Frame;
118

    
119
/**
120
 * @author Administrador
121
 *
122
 * To change the template for this generated type comment go to
123
 * Window>Preferences>Java>Code Generation>Code and Comments
124
 */
125
public class ComandosListener implements ActionListener {
126
        private MapControl m_MapControl;
127
    private MapContext m_Mapa;
128
    private SingleView theView;
129
    
130
    private static final boolean WKB_ENABLED = true;
131
    
132
    class MyImageObserver implements ImageObserver {
133

    
134
            private MapControl mapCtrl;
135
            public MyImageObserver(MapControl mapCtrl)
136
            {
137
                    this.mapCtrl = mapCtrl;
138
            }
139
                public boolean imageUpdate(Image img,
140
                int infoflags,
141
                int x,
142
                int y,
143
                int width,
144
                int height) {
145
                        if ((infoflags | ImageObserver.ALLBITS) != 0)
146
                        {
147
                                // TODO: A?ADIR ALGO AS? AL MAPCONTROL
148
                                // if (mapCtrl.getStatus() == MapControl.ACTUALIZADO)
149
                                {
150
                                        // System.out.println("Image " + infoflags);
151
                                        mapCtrl.drawGraphics();
152
                                }
153
                        }
154
                        return true;
155
                }
156
            
157
    }
158
    
159
    private MyImageObserver theImgObserver;
160
    
161
    PruebasGT2 pruebasGT2 = new PruebasGT2();
162

    
163
    public ComandosListener(MapControl mapa, SingleView Owner) {
164
        m_Mapa = mapa.getMapContext();
165
        theView = Owner;
166
        m_MapControl = mapa;
167
    }
168
    
169
    /**
170
     * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
171
     * a default style.
172
     *
173
     * @param url The url of the shapefile to load.
174
     * @param name DOCUMENT ME!
175
     *
176
     * @throws IOException is a I/O error occured.
177
     * @throws DataSourceException if an error occured while reading the data source.
178
     * @throws FileNotFoundException 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().getType();
188

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

    
199
        final MapLayer layer = new DefaultMapLayer(features, style);
200
        layer.setTitle(layerName);
201
        
202
        FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
203
        try {
204
                        m_Mapa.getLayers().addLayer(lyrGT2);
205
                } catch (CancelationException e) {
206
                        // TODO Auto-generated catch block
207
                        e.printStackTrace();
208
                }
209
        
210
    }
211
    
212
    
213
    /* (non-Javadoc)
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 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 (DriverException 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() == "SYMBOL_MANAGER") {            
235
            if (e.getActionCommand() == "SYMBOL_MANAGER") {            
236
                JDialog dialog = new JDialog();
237
                /* JSVGCanvas canvas = new JSVGCanvas();
238
                try {
239
                    canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
240
                } catch (MalformedURLException e1) {
241
                    // TODO Auto-generated catch block
242
                    e1.printStackTrace();
243
                } */
244
                SymbolSelector canvas = new SymbolSelector();
245

    
246
                dialog.getContentPane().add( canvas );
247

    
248
                dialog.setSize( 600, 300 );
249
                dialog.show( true );
250
                
251
            }
252
            
253
        }
254

    
255
        if (e.getActionCommand().indexOf("GT2") != -1 && pruebasGT2 != null) {
256
                pruebasGT2.setMapContext(m_Mapa);
257
                pruebasGT2.actionPerformed(e);
258
        }
259
        
260
                if (e.getActionCommand() == "OPEN_LOCATOR") {
261
                        //Set up the dialog that the button brings up.
262
                        JDialog dialog = new JDialog();
263
                        FPanelLocConfig m_panelLoc = new FPanelLocConfig(theView.getMapOverview());
264
                        m_panelLoc.setPreferredSize(m_panelLoc.getSize());
265
                        dialog.getContentPane().add(m_panelLoc);
266
                        dialog.setModal(true);                        
267
                        dialog.pack();
268
                        dialog.show();
269
                } 
270
        
271
                if (e.getActionCommand() == "PRUEBA") {
272
                        int i;
273
                        double valor;
274
                        FThemeManagerWindow dlg = new FThemeManagerWindow();
275
                        
276
                        dlg.show(); 
277
                }
278

    
279
        if (e.getActionCommand() == "ZOOM_MAS") {
280
            m_MapControl.setTool("zoomIn"); // Por defecto
281
        }
282

    
283
        if (e.getActionCommand() == "ZOOM_MENOS") {
284
                        m_MapControl.setTool("zoomout");
285
        }
286

    
287
        if (e.getActionCommand() == "PAN") {
288
                        m_MapControl.setTool("pan");
289
        }
290

    
291
        if (e.getActionCommand() == "INFO") {
292
                        m_MapControl.setTool("info");
293
        }
294
        if (e.getActionCommand() == "SELRECT") {
295
                        m_MapControl.setTool("rectSelection");
296
        }
297
        
298
        if (e.getActionCommand() == "MEDICION") {
299
                        m_MapControl.setTool("medicion");
300
        }
301
        if (e.getActionCommand() == "MEDIRAREA") {
302
                        m_MapControl.setTool("area");
303
            
304
            // Prueba de graficos
305
                        theImgObserver = new MyImageObserver(m_MapControl);
306
                        
307
            GraphicLayer lyr =  m_MapControl.getMapContext().getGraphicsLayer();
308
            lyr.clearAllGraphics();
309
            lyr.clearSymbolsGraphics();
310
            FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
311
            File fIcon = new File("d:/sonic.gif");
312
            // File fIcon = new File("d:/7bart.gif");
313
            theSymbol.setSizeInPixels(true);
314
            
315
            theSymbol.setIconURI(fIcon.toURI());
316
            theSymbol.setSize(theSymbol.getIcon().getHeight(null));
317
            
318
            theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN);
319
            theSymbol.setImgObserver(theImgObserver);
320

    
321
            int idSymbol = lyr.addSymbol(theSymbol);
322
            IGeometry geom = ShapeFactory.createPoint2D(100,100);
323
            FGraphic theGraphic = new FGraphic(geom, idSymbol);
324
            lyr.addGraphic(theGraphic);
325
            FGraphicLabel theLabel = new FGraphicLabel(geom, idSymbol, "Hola colega");
326
            // lyr.addGraphic(theLabel);
327
            m_MapControl.drawGraphics();
328
        }
329
        
330
        if ((e.getActionCommand() == "ADD_LAYER") || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
331
                JDialog dlg = new JDialog();
332
            dlg.setModal(true);
333
            FileOpenDialog fileDlg = new FileOpenDialog(new Class[]{VectorialFileDriver.class, RasterDriver.class});
334
            DataBaseOpenDialog dbop = new DataBaseOpenDialog();
335
            // dbop.setClasses(new Class[]{DBDriver.class});
336
            
337
            FOpenDialog fopen = new FOpenDialog();
338
            fopen.addTab("Fichero", fileDlg);
339
            fopen.addTab("Base de datos", dbop);
340
            // fileDlg.setPreferredSize(fopen.getSize());
341
            dlg.setSize(fopen.getSize());
342
            dlg.getContentPane().add(fopen);
343
            dlg.pack();
344
            dlg.show();
345
            
346
  
347
            // TODO: Hacer lo de la capa WMS tambi?n.
348
                    if (fileDlg.getFiles() == null) return;
349
                    FLayer lyr = null;
350
                    File[] files = fileDlg.getFiles();
351
            String[] driverNames = fileDlg.getDriverNames();
352
            Driver[] drivers = new Driver[driverNames.length];
353
            for (int i = 0; i < drivers.length; i++) {
354
                try {
355
                    drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
356
                } catch (DriverLoadException ex) {
357
                    System.err.println("No se pudo cargar el driver " + ex.getMessage());
358
                }
359
            }
360
            
361
                        m_MapControl.getMapContext()
362
                           .beginAtomicEvent();
363

    
364
                    for  (int iFile=0; iFile < files.length; iFile++)
365
                    {
366
                            File fich = files[iFile];
367
                            String layerName = fich.getName();
368
                            String layerPath = fich.getAbsolutePath();
369
                            
370
                            try {
371
                    if (drivers[iFile] instanceof VectorialFileDriver){
372
                        lyr = LayerFactory.createLayer(layerName,
373
                                (VectorialFileDriver) drivers[iFile], fich, proj);
374
                    }else if (drivers[iFile] instanceof RasterDriver){
375
/*                          lyr = LayerFactory.createLayer(layerName,
376
                                driverNames[iFile], fich, proj);
377
*/                  }
378

    
379
                                    lyr.setVisible(true);
380
                                    lyr.setName(layerName);
381
                                    if (lyr != null )
382
                                    {
383
                                            try {
384
                                                    theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
385
                                            } catch (CancelationException ex) {
386
                                                    // TODO Auto-generated catch block
387
                                                    ex.printStackTrace();
388
                                            }
389
                                            // TODO: Poner una variable y dibujar solo cuando
390
                                            // todas las capas hayan sido cargadas.
391
                                            // theView.getMapControl().drawMap();                                           
392
                                            // theView.getTOC().refresh();
393
                                    }                    
394
                                    
395
                            } catch (DriverException ex) {
396
                                    ex.printStackTrace();
397
                                }
398
                            
399
                    } // for
400
                        m_MapControl.getMapContext()
401
                           .endAtomicEvent();
402
                        theView.getTOC().refresh();
403

    
404
            return;
405

    
406
        }
407

    
408
        /**
409

410
        String pathFich = fc.getDirectory() + fc.getFile();
411

412

413
        System.out.println(pathFich);
414
        FLyrVect lyr = new FLyrVect(fc.getFile(),pathFich);
415

416
        m_Mapa.AddLayer(lyr);
417
        m_Owner.jLeyenda.Refresh();
418

419
        }
420

421
        **/
422
        if (e.getActionCommand() == "EXPORT") {
423
            // Export jExport = new Export(m_MapControl);
424
        }
425

    
426
        if (e.getActionCommand() == "VIEW_TABLE") {
427
                    FLayer[] actives = m_Mapa.getLayers().getActives();
428

    
429
                     for (int i = 0; i < actives.length; i++) {
430
                    if (actives[i] instanceof AlphanumericData){
431
                            AlphanumericData co = (AlphanumericData)actives[i];
432
                            
433
                                        SelectableDataSource dataSource;
434
                                        try {
435
                                                dataSource = co.getRecordset();
436
                                                EditableAdapter ea = new EditableAdapter();
437
                                                ea.setOriginalDataSource(dataSource);
438
                                                ProjectTable projectTable =  ProjectTable.createTable(actives[i].getName(),
439
                                                                        ea);
440
                                                        projectTable.setAssociatedTable(co);
441
                                                
442
                                                Table t = new Table();
443
                                                t.setModel(projectTable);
444
                                    JDialog myDialog = new JDialog(theView, "Tabla de Atributos (" 
445
                                                      + t.getModel().getAssociatedTable().getRecordset().getRowCount() + " registros)" );
446
                                    myDialog.setContentPane(t);
447
        
448
                                    // myViewer.addAttributeTableViewerListener(m_Owner);
449
                                    myDialog.pack();
450
                                    myDialog.show(); 
451
                                        } catch (DriverException e1) {
452
                                                // TODO Auto-generated catch block
453
                                                e1.printStackTrace();
454
                                        } catch (HeadlessException ex) {
455
                                                // TODO Auto-generated catch block
456
                                                ex.printStackTrace();
457
                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
458
                        // TODO Auto-generated catch block
459
                        e1.printStackTrace();
460
                    }
461

    
462
                            }
463
                            
464
                    }
465
                
466
        }
467
    }
468
}