Statistics
| Revision:

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

History | View | Annotate | Download (13.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
import java.net.MalformedURLException;
63

    
64
import javax.swing.JDialog;
65

    
66
// import org.apache.batik.swing.JSVGCanvas;
67
//import com.iver.cit.gvsig.fmap.rendering.styling.panels.SymbolSelector;
68

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

    
79
import com.hardcode.driverManager.Driver;
80
import com.hardcode.driverManager.DriverLoadException;
81
import com.iver.andami.messages.NotificationManager;
82
import com.iver.cit.gvsig.fmap.DriverException;
83
import com.iver.cit.gvsig.fmap.FMap;
84
import com.iver.cit.gvsig.fmap.MapControl;
85
import com.iver.cit.gvsig.fmap.ViewPort;
86
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
87
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
88
import com.iver.cit.gvsig.fmap.layers.CancelationException;
89
import com.iver.cit.gvsig.fmap.layers.FLayer;
90
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
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

    
95
import com.iver.cit.gvsig.gui.DataBaseOpenDialog;
96
import com.iver.cit.gvsig.gui.FOpenDialog;
97
import com.iver.cit.gvsig.gui.FPanelLocConfig;
98
import com.iver.cit.gvsig.gui.FileOpenDialog;
99
import com.iver.cit.gvsig.gui.Table;
100
import com.iver.cit.gvsig.gui.legendmanager.FLegendManagerWindow;
101
import com.iver.cit.gvsig.project.ProjectTable;
102
import com.vividsolutions.jts.geom.LineString;
103
import com.vividsolutions.jts.geom.MultiLineString;
104
import com.vividsolutions.jts.geom.MultiPoint;
105
import com.vividsolutions.jts.geom.Point;
106

    
107

    
108

    
109
//import java.awt.Frame;
110

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

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

    
147
        // Create the style
148
        final StyleBuilder builder = new StyleBuilder();
149
        final Style style;
150
        Class geometryClass = features.getSchema().getDefaultGeometry().getType();
151

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

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

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

208
            dialog.getContentPane().add( canvas );
209

210
            dialog.setSize( 600, 300 );
211
            dialog.show( true ); */
212
            
213
        }
214

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

    
239
        if (e.getActionCommand() == "ZOOM_MAS") {
240
            m_MapControl.setTool("zoomIn"); // Por defecto
241
        }
242

    
243
        if (e.getActionCommand() == "ZOOM_MENOS") {
244
                        m_MapControl.setTool("zoomout");
245
        }
246

    
247
        if (e.getActionCommand() == "PAN") {
248
                        m_MapControl.setTool("pan");
249
        }
250

    
251
        if (e.getActionCommand() == "INFO") {
252
                        m_MapControl.setTool("info");
253
        }
254
        if (e.getActionCommand() == "SELRECT") {
255
                        m_MapControl.setTool("rectSelection");
256
        }
257
        
258
        if (e.getActionCommand() == "MEDICION") {
259
                        m_MapControl.setTool("medicion");
260
        }
261
        if (e.getActionCommand() == "MEDIRAREA") {
262
                        m_MapControl.setTool("area");
263
        }
264
        
265
        if ((e.getActionCommand() == "ADD_LAYER") || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
266
                JDialog dlg = new JDialog();
267
            dlg.setModal(true);
268
            FileOpenDialog fileDlg = new FileOpenDialog(new Class[]{VectorialFileDriver.class, RasterDriver.class});
269
            DataBaseOpenDialog dbop = new DataBaseOpenDialog();
270
            // dbop.setClasses(new Class[]{DBDriver.class});
271
            
272
            FOpenDialog fopen = new FOpenDialog();
273
            fopen.addTab("Fichero", fileDlg);
274
            fopen.addTab("Base de datos", dbop);
275
            // fileDlg.setPreferredSize(fopen.getSize());
276
            dlg.setSize(fopen.getSize());
277
            dlg.getContentPane().add(fopen);
278
            dlg.pack();
279
            dlg.show();
280
            
281
  
282
            // TODO: Hacer lo de la capa WMS tambi?n.
283
                    if (fileDlg.getFiles() == null) return;
284
                    FLayer lyr = null;
285
                    File[] files = fileDlg.getFiles();
286
            String[] driverNames = fileDlg.getDriverNames();
287
            Driver[] drivers = new Driver[driverNames.length];
288
            for (int i = 0; i < drivers.length; i++) {
289
                try {
290
                    drivers[i] = LayerFactory.getDM().getDriver(driverNames[i]);
291
                } catch (DriverLoadException ex) {
292
                    System.err.println("No se pudo cargar el driver " + ex.getMessage());
293
                }
294
            }
295
            
296
                        m_MapControl.getMapContext()
297
                           .beginAtomicEvent();
298

    
299
                    for  (int iFile=0; iFile < files.length; iFile++)
300
                    {
301
                            File fich = files[iFile];
302
                            String layerName = fich.getName();
303
                            String layerPath = fich.getAbsolutePath();
304
                            
305
                            try {
306
                    if (drivers[iFile] instanceof VectorialFileDriver){
307
                        lyr = LayerFactory.createLayer(layerName,
308
                                (VectorialFileDriver) drivers[iFile], fich, proj);
309
                    }else if (drivers[iFile] instanceof RasterDriver){
310
/*                          lyr = LayerFactory.createLayer(layerName,
311
                                driverNames[iFile], fich, proj);
312
*/                  }
313

    
314
                                    lyr.setVisible(true);
315
                                    lyr.setName(layerName);
316
                                    if (lyr != null )
317
                                    {
318
                                            try {
319
                                                    theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
320
                                            } catch (CancelationException ex) {
321
                                                    // TODO Auto-generated catch block
322
                                                    ex.printStackTrace();
323
                                            }
324
                                            // TODO: Poner una variable y dibujar solo cuando
325
                                            // todas las capas hayan sido cargadas.
326
                                            // theView.getMapControl().drawMap();                                           
327
                                            // theView.getTOC().refresh();
328
                                    }                    
329
                                    
330
                            } catch (DriverException ex) {
331
                                    ex.printStackTrace();
332
                                }
333
                            
334
                    } // for
335
                        m_MapControl.getMapContext()
336
                           .endAtomicEvent();
337
                        theView.getTOC().refresh();
338

    
339
            return;
340

    
341
        }
342

    
343
        /**
344

345
        String pathFich = fc.getDirectory() + fc.getFile();
346

347

348
        System.out.println(pathFich);
349
        FLyrVect lyr = new FLyrVect(fc.getFile(),pathFich);
350

351
        m_Mapa.AddLayer(lyr);
352
        m_Owner.jLeyenda.Refresh();
353

354
        }
355

356
        **/
357
        if (e.getActionCommand() == "EXPORT") {
358
            // Export jExport = new Export(m_MapControl);
359
        }
360

    
361
        if (e.getActionCommand() == "VIEW_TABLE") {
362
                    FLayer[] actives = m_Mapa.getLayers().getActives();
363

    
364
                     for (int i = 0; i < actives.length; i++) {
365
                    if (actives[i] instanceof AlphanumericData){
366
                            AlphanumericData co = (AlphanumericData)actives[i];
367
                            
368
                                        SelectableDataSource dataSource;
369
                                        try {
370
                                                dataSource = co.getRecordset();
371
                                                ProjectTable projectTable =  ProjectTable.createTable(actives[i].getName(),
372
                                                                        dataSource);
373
                                                        projectTable.setAssociatedTable(co);
374
                                                
375
                                                Table t = new Table();
376
                                                t.setModel(projectTable);
377
                                    JDialog myDialog = new JDialog(theView, "Tabla de Atributos (" 
378
                                                      + t.getModel().getAssociatedTable().getRecordset().getRowCount() + " registros)" );
379
                                    myDialog.setContentPane(t);
380
        
381
                                    // myViewer.addAttributeTableViewerListener(m_Owner);
382
                                    myDialog.pack();
383
                                    myDialog.show(); 
384
                                        } catch (DriverException e1) {
385
                                                // TODO Auto-generated catch block
386
                                                e1.printStackTrace();
387
                                        } catch (HeadlessException ex) {
388
                                                // TODO Auto-generated catch block
389
                                                ex.printStackTrace();
390
                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
391
                        // TODO Auto-generated catch block
392
                        e1.printStackTrace();
393
                    }
394

    
395
                            }
396
                            
397
                    }
398
                
399
        }
400
    }
401
}