Statistics
| Revision:

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

History | View | Annotate | Download (14.1 KB)

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

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

    
63
import javax.swing.JDialog;
64

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

    
75
import com.hardcode.driverManager.Driver;
76
import com.hardcode.driverManager.DriverLoadException;
77
import com.iver.cit.gvsig.fmap.DriverException;
78
import com.iver.cit.gvsig.fmap.FMap;
79
import com.iver.cit.gvsig.fmap.MapControl;
80
import com.iver.cit.gvsig.fmap.ViewPort;
81
import com.iver.cit.gvsig.fmap.core.IGeometry;
82
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
83
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
84
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
85
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
86
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
87
import com.iver.cit.gvsig.fmap.layers.CancelationException;
88
import com.iver.cit.gvsig.fmap.layers.FLayer;
89
import com.iver.cit.gvsig.fmap.layers.FLyrGT2_old;
90
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
91
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
92
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
93
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
94
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
95
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
96

    
97
import com.iver.cit.gvsig.gui.DataBaseOpenDialog;
98
import com.iver.cit.gvsig.gui.FOpenDialog;
99
import com.iver.cit.gvsig.gui.FPanelLocConfig;
100
import com.iver.cit.gvsig.gui.FileOpenDialog;
101
import com.iver.cit.gvsig.gui.Table;
102
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
103
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
104
import com.iver.cit.gvsig.project.ProjectTable;
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

    
111

    
112
//import java.awt.Frame;
113

    
114
/**
115
 * @author Administrador
116
 *
117
 * To change the template for this generated type comment go to
118
 * Window>Preferences>Java>Code Generation>Code and Comments
119
 */
120
public class ComandosListener implements ActionListener {
121
        private MapControl m_MapControl;
122
    private FMap m_Mapa;
123
    private SingleView theView;
124
    
125
    private static final boolean WKB_ENABLED = true;
126
    
127
    PruebasGT2 pruebasGT2 = new PruebasGT2();
128

    
129
    public ComandosListener(MapControl mapa, SingleView Owner) {
130
        m_Mapa = mapa.getMapContext();
131
        theView = Owner;
132
        m_MapControl = mapa;
133
    }
134
    
135
    /**
136
     * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
137
     * a default style.
138
     *
139
     * @param url The url of the shapefile to load.
140
     * @param name DOCUMENT ME!
141
     *
142
     * @throws IOException is a I/O error occured.
143
     * @throws DataSourceException if an error occured while reading the data source.
144
     * @throws FileNotFoundException DOCUMENT ME!
145
     */
146
    protected void loadLayer(DataStore store, String layerName)
147
        throws IOException, DataSourceException {
148
        final FeatureSource features = store.getFeatureSource(layerName);
149

    
150
        // Create the style
151
        final StyleBuilder builder = new StyleBuilder();
152
        final Style style;
153
        Class geometryClass = features.getSchema().getDefaultGeometry().getType();
154

    
155
        if (LineString.class.isAssignableFrom(geometryClass)
156
                || MultiLineString.class.isAssignableFrom(geometryClass)) {
157
            style = builder.createStyle(builder.createLineSymbolizer());
158
        } else if (Point.class.isAssignableFrom(geometryClass)
159
                || MultiPoint.class.isAssignableFrom(geometryClass)) {
160
            style = builder.createStyle(builder.createPointSymbolizer());
161
        } else {
162
            style = builder.createStyle(builder.createPolygonSymbolizer(Color.ORANGE, Color.BLACK, 1));
163
        }
164

    
165
        final MapLayer layer = new DefaultMapLayer(features, style);
166
        layer.setTitle(layerName);
167
        
168
        FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
169
        try {
170
                        m_Mapa.getLayers().addLayer(lyrGT2);
171
                } catch (CancelationException e) {
172
                        // TODO Auto-generated catch block
173
                        e.printStackTrace();
174
                }
175
        
176
    }
177
    
178
    
179
    /* (non-Javadoc)
180
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
181
     */
182
    public void actionPerformed(ActionEvent e) {
183
                // TODO LWS Esto debe venir de fuera (proyeccion del fichero, o de la vista)
184
                IProjection proj = ProjectionPool.get("EPSG:23030");
185
                ViewPort vp = m_Mapa.getViewPort();
186

    
187
        if (e.getActionCommand() == "ZOOM_TODO") {
188
            try {
189
                                vp.setExtent(m_Mapa.getFullExtent());
190
                                m_MapControl.drawMap(false);
191
                        } catch (DriverException e1) {
192
                                // TODO Auto-generated catch block
193
                                e1.printStackTrace();
194
                        }
195
            m_MapControl.drawMap(false);
196
        }
197
        if (e.getActionCommand() == "ZOOM_PREVIO") {            
198
            vp.setPreviousExtent();
199
        }
200
        if (e.getActionCommand() == "SYMBOL_MANAGER") {            
201
            if (e.getActionCommand() == "SYMBOL_MANAGER") {            
202
                JDialog dialog = new JDialog();
203
                /* JSVGCanvas canvas = new JSVGCanvas();
204
                try {
205
                    canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
206
                } catch (MalformedURLException e1) {
207
                    // TODO Auto-generated catch block
208
                    e1.printStackTrace();
209
                } */
210
                SymbolSelector canvas = new SymbolSelector();
211

    
212
                dialog.getContentPane().add( canvas );
213

    
214
                dialog.setSize( 600, 300 );
215
                dialog.show( true );
216
                
217
            }
218
            
219
        }
220

    
221
        if (e.getActionCommand().indexOf("GT2") != -1 && pruebasGT2 != null) {
222
                pruebasGT2.setMapContext(m_Mapa);
223
                pruebasGT2.actionPerformed(e);
224
        }
225
        
226
                if (e.getActionCommand() == "OPEN_LOCATOR") {
227
                        //Set up the dialog that the button brings up.
228
                        JDialog dialog = new JDialog();
229
                        FPanelLocConfig m_panelLoc = new FPanelLocConfig(theView.getMapOverview());
230
                        m_panelLoc.setPreferredSize(m_panelLoc.getSize());
231
                        dialog.getContentPane().add(m_panelLoc);
232
                        dialog.setModal(true);                        
233
                        dialog.pack();
234
                        dialog.show();
235
                } 
236
        
237
                if (e.getActionCommand() == "PRUEBA") {
238
                        int i;
239
                        double valor;
240
                        FThemeManagerWindow dlg = new FThemeManagerWindow();
241
                        
242
                        dlg.show(); 
243
                }
244

    
245
        if (e.getActionCommand() == "ZOOM_MAS") {
246
            m_MapControl.setTool("zoomIn"); // Por defecto
247
        }
248

    
249
        if (e.getActionCommand() == "ZOOM_MENOS") {
250
                        m_MapControl.setTool("zoomout");
251
        }
252

    
253
        if (e.getActionCommand() == "PAN") {
254
                        m_MapControl.setTool("pan");
255
        }
256

    
257
        if (e.getActionCommand() == "INFO") {
258
                        m_MapControl.setTool("info");
259
        }
260
        if (e.getActionCommand() == "SELRECT") {
261
                        m_MapControl.setTool("rectSelection");
262
        }
263
        
264
        if (e.getActionCommand() == "MEDICION") {
265
                        m_MapControl.setTool("medicion");
266
        }
267
        if (e.getActionCommand() == "MEDIRAREA") {
268
                        m_MapControl.setTool("area");
269
            
270
            // Prueba de graficos
271
            GraphicLayer lyr =  m_MapControl.getMapContext().getGraphicsLayer();
272
            FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);            
273
            int idSymbol = lyr.addSymbol(theSymbol);
274
            IGeometry geom = ShapeFactory.createPoint2D(100,100);
275
            FGraphic theGraphic = new FGraphic(geom, idSymbol);
276
            lyr.addGraphic(theGraphic);
277
            FGraphicLabel theLabel = new FGraphicLabel(geom, idSymbol, "Hola colega");
278
            lyr.addGraphic(theLabel);
279
            m_MapControl.drawGraphics();
280
        }
281
        
282
        if ((e.getActionCommand() == "ADD_LAYER") || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
283
                JDialog dlg = new JDialog();
284
            dlg.setModal(true);
285
            FileOpenDialog fileDlg = new FileOpenDialog(new Class[]{VectorialFileDriver.class, RasterDriver.class});
286
            DataBaseOpenDialog dbop = new DataBaseOpenDialog();
287
            // dbop.setClasses(new Class[]{DBDriver.class});
288
            
289
            FOpenDialog fopen = new FOpenDialog();
290
            fopen.addTab("Fichero", fileDlg);
291
            fopen.addTab("Base de datos", dbop);
292
            // fileDlg.setPreferredSize(fopen.getSize());
293
            dlg.setSize(fopen.getSize());
294
            dlg.getContentPane().add(fopen);
295
            dlg.pack();
296
            dlg.show();
297
            
298
  
299
            // TODO: Hacer lo de la capa WMS tambi?n.
300
                    if (fileDlg.getFiles() == null) return;
301
                    FLayer lyr = null;
302
                    File[] files = fileDlg.getFiles();
303
            String[] driverNames = fileDlg.getDriverNames();
304
            Driver[] drivers = new Driver[driverNames.length];
305
            for (int i = 0; i < drivers.length; i++) {
306
                try {
307
                    drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
308
                } catch (DriverLoadException ex) {
309
                    System.err.println("No se pudo cargar el driver " + ex.getMessage());
310
                }
311
            }
312
            
313
                        m_MapControl.getMapContext()
314
                           .beginAtomicEvent();
315

    
316
                    for  (int iFile=0; iFile < files.length; iFile++)
317
                    {
318
                            File fich = files[iFile];
319
                            String layerName = fich.getName();
320
                            String layerPath = fich.getAbsolutePath();
321
                            
322
                            try {
323
                    if (drivers[iFile] instanceof VectorialFileDriver){
324
                        lyr = LayerFactory.createLayer(layerName,
325
                                (VectorialFileDriver) drivers[iFile], fich, proj);
326
                    }else if (drivers[iFile] instanceof RasterDriver){
327
/*                          lyr = LayerFactory.createLayer(layerName,
328
                                driverNames[iFile], fich, proj);
329
*/                  }
330

    
331
                                    lyr.setVisible(true);
332
                                    lyr.setName(layerName);
333
                                    if (lyr != null )
334
                                    {
335
                                            try {
336
                                                    theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
337
                                            } catch (CancelationException ex) {
338
                                                    // TODO Auto-generated catch block
339
                                                    ex.printStackTrace();
340
                                            }
341
                                            // TODO: Poner una variable y dibujar solo cuando
342
                                            // todas las capas hayan sido cargadas.
343
                                            // theView.getMapControl().drawMap();                                           
344
                                            // theView.getTOC().refresh();
345
                                    }                    
346
                                    
347
                            } catch (DriverException ex) {
348
                                    ex.printStackTrace();
349
                                }
350
                            
351
                    } // for
352
                        m_MapControl.getMapContext()
353
                           .endAtomicEvent();
354
                        theView.getTOC().refresh();
355

    
356
            return;
357

    
358
        }
359

    
360
        /**
361

362
        String pathFich = fc.getDirectory() + fc.getFile();
363

364

365
        System.out.println(pathFich);
366
        FLyrVect lyr = new FLyrVect(fc.getFile(),pathFich);
367

368
        m_Mapa.AddLayer(lyr);
369
        m_Owner.jLeyenda.Refresh();
370

371
        }
372

373
        **/
374
        if (e.getActionCommand() == "EXPORT") {
375
            // Export jExport = new Export(m_MapControl);
376
        }
377

    
378
        if (e.getActionCommand() == "VIEW_TABLE") {
379
                    FLayer[] actives = m_Mapa.getLayers().getActives();
380

    
381
                     for (int i = 0; i < actives.length; i++) {
382
                    if (actives[i] instanceof AlphanumericData){
383
                            AlphanumericData co = (AlphanumericData)actives[i];
384
                            
385
                                        SelectableDataSource dataSource;
386
                                        try {
387
                                                dataSource = co.getRecordset();
388
                                                ProjectTable projectTable =  ProjectTable.createTable(actives[i].getName(),
389
                                                                        dataSource);
390
                                                        projectTable.setAssociatedTable(co);
391
                                                
392
                                                Table t = new Table();
393
                                                t.setModel(projectTable);
394
                                    JDialog myDialog = new JDialog(theView, "Tabla de Atributos (" 
395
                                                      + t.getModel().getAssociatedTable().getRecordset().getRowCount() + " registros)" );
396
                                    myDialog.setContentPane(t);
397
        
398
                                    // myViewer.addAttributeTableViewerListener(m_Owner);
399
                                    myDialog.pack();
400
                                    myDialog.show(); 
401
                                        } catch (DriverException e1) {
402
                                                // TODO Auto-generated catch block
403
                                                e1.printStackTrace();
404
                                        } catch (HeadlessException ex) {
405
                                                // TODO Auto-generated catch block
406
                                                ex.printStackTrace();
407
                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
408
                        // TODO Auto-generated catch block
409
                        e1.printStackTrace();
410
                    }
411

    
412
                            }
413
                            
414
                    }
415
                
416
        }
417
    }
418
}