Statistics
| Revision:

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

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 org.cresques.cts.IProjection;
68
import org.cresques.cts.ProjectionPool;
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.iver.andami.messages.NotificationManager;
80
import com.iver.cit.gvsig.fmap.DriverException;
81
import com.iver.cit.gvsig.fmap.FMap;
82
import com.iver.cit.gvsig.fmap.MapControl;
83
import com.iver.cit.gvsig.fmap.ViewPort;
84
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
85
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
86
import com.iver.cit.gvsig.fmap.layers.CancelationException;
87
import com.iver.cit.gvsig.fmap.layers.FLayer;
88
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
89
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
90
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
91
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
92
import com.iver.cit.gvsig.fmap.rendering.styling.panels.SymbolSelector;
93
import com.iver.cit.gvsig.gui.DataBaseOpenDialog;
94
import com.iver.cit.gvsig.gui.FOpenDialog;
95
import com.iver.cit.gvsig.gui.FPanelLocConfig;
96
import com.iver.cit.gvsig.gui.FileOpenDialog;
97
import com.iver.cit.gvsig.gui.Table;
98
import com.iver.cit.gvsig.gui.legendmanager.FLegendManagerWindow;
99
import com.iver.cit.gvsig.project.ProjectTable;
100
import com.vividsolutions.jts.geom.LineString;
101
import com.vividsolutions.jts.geom.MultiLineString;
102
import com.vividsolutions.jts.geom.MultiPoint;
103
import com.vividsolutions.jts.geom.Point;
104

    
105

    
106

    
107
//import java.awt.Frame;
108

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

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

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

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

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

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

    
206
            dialog.getContentPane().add( canvas );
207

    
208
            dialog.setSize( 600, 300 );
209
            dialog.show( true );
210
            
211
        }
212

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

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

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

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

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

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

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

    
337
            return;
338

    
339
        }
340

    
341
        /**
342

343
        String pathFich = fc.getDirectory() + fc.getFile();
344

345

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

349
        m_Mapa.AddLayer(lyr);
350
        m_Owner.jLeyenda.Refresh();
351

352
        }
353

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

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

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

    
393
                            }
394
                            
395
                    }
396
                
397
        }
398
    }
399
}