Statistics
| Revision:

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

History | View | Annotate | Download (11.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.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.gdbms.engine.data.DBDriver;
76
import com.iver.cit.gvsig.fmap.DriverException;
77
import com.iver.cit.gvsig.fmap.FMap;
78
import com.iver.cit.gvsig.fmap.MapControl;
79
import com.iver.cit.gvsig.fmap.ViewPort;
80
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
81
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
82
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
83
import com.iver.cit.gvsig.fmap.layers.CancelationException;
84
import com.iver.cit.gvsig.fmap.layers.FLayer;
85
import com.iver.cit.gvsig.fmap.layers.FLyrGT2;
86
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
87
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
88
import com.iver.cit.gvsig.fmap.layers.XMLException;
89
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
90
import com.iver.cit.gvsig.gui.DataBaseOpenDialog;
91
import com.iver.cit.gvsig.gui.FOpenDialog;
92
import com.iver.cit.gvsig.gui.FPanelLocConfig;
93
import com.iver.cit.gvsig.gui.FileOpenDialog;
94
import com.iver.cit.gvsig.gui.Table;
95
import com.iver.cit.gvsig.gui.legendmanager.FLegendManagerWindow;
96
import com.iver.cit.gvsig.project.ProjectTable;
97
import com.vividsolutions.jts.geom.LineString;
98
import com.vividsolutions.jts.geom.MultiLineString;
99
import com.vividsolutions.jts.geom.MultiPoint;
100
import com.vividsolutions.jts.geom.Point;
101

    
102

    
103

    
104
//import java.awt.Frame;
105

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

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

    
142
        // Create the style
143
        final StyleBuilder builder = new StyleBuilder();
144
        final Style style;
145
        Class geometryClass = features.getSchema().getDefaultGeometry().getType();
146

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

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

    
179
        if (e.getActionCommand() == "ZOOM_TODO") {
180
            try {
181
                                vp.setExtent(m_Mapa.getFullExtent());
182
                                m_MapControl.drawMap(false);
183
                        } catch (DriverException e1) {
184
                                // TODO Auto-generated catch block
185
                                e1.printStackTrace();
186
                        }
187
            m_MapControl.drawMap(false);
188
        }
189
        if (e.getActionCommand() == "ZOOM_PREVIO") {            
190
            vp.setPreviousExtent();
191
        }
192
        if (e.getActionCommand().indexOf("GT2") != -1 && pruebasGT2 != null) {
193
                pruebasGT2.setMapContext(m_Mapa);
194
                pruebasGT2.actionPerformed(e);
195
        }
196
        
197
                if (e.getActionCommand() == "OPEN_LOCATOR") {
198
                        //Set up the dialog that the button brings up.
199
                        JDialog dialog = new JDialog();
200
                        FPanelLocConfig m_panelLoc = new FPanelLocConfig(theView.getMapOverview());
201
                        m_panelLoc.setPreferredSize(m_panelLoc.getSize());
202
                        dialog.getContentPane().add(m_panelLoc);
203
                        dialog.setModal(true);                        
204
                        dialog.pack();
205
                        dialog.show();
206
                } 
207
        
208
                if (e.getActionCommand() == "PRUEBA") {
209
                        int i;
210
                        double valor;
211
                        FLegendManagerWindow dlg = new FLegendManagerWindow();
212
                        
213
                        dlg.show(); 
214
                }
215

    
216
        if (e.getActionCommand() == "ZOOM_MAS") {
217
                m_MapControl.setTool("zoomrect");
218
        }
219

    
220
        if (e.getActionCommand() == "ZOOM_MENOS") {
221
                        m_MapControl.setTool("zoomout");
222
        }
223

    
224
        if (e.getActionCommand() == "PAN") {
225
                        m_MapControl.setTool("pan");
226
        }
227

    
228
        if (e.getActionCommand() == "INFO") {
229
                        m_MapControl.setTool("info");
230
        }
231
        if (e.getActionCommand() == "SELRECT") {
232
                        m_MapControl.setTool("rectSelection");
233
        }
234
        
235
        if (e.getActionCommand() == "MEDICION") {
236
                        m_MapControl.setTool("medicion");
237
        }
238
        if (e.getActionCommand() == "MEDIRAREA") {
239
                        m_MapControl.setTool("area");
240
        }
241
        
242
        if ((e.getActionCommand() == "ADD_LAYER") || (e.getActionCommand() == "ADD_MEMORY_LAYER")) {
243
                JDialog dlg = new JDialog();
244
            dlg.setModal(true);
245
            FileOpenDialog fileDlg = new FileOpenDialog(new Class[]{VectorialFileDriver.class, RasterDriver.class});
246
            DataBaseOpenDialog dbop = new DataBaseOpenDialog();
247
            dbop.setClasses(new Class[]{DBDriver.class});
248
            
249
            FOpenDialog fopen = new FOpenDialog();
250
            fopen.addTab("Fichero", fileDlg);
251
            fopen.addTab("Base de datos", dbop);
252
            // fileDlg.setPreferredSize(fopen.getSize());
253
            dlg.setSize(fopen.getSize());
254
            dlg.getContentPane().add(fopen);
255
            dlg.pack();
256
            dlg.show();
257
            
258
  
259
            // TODO: Hacer lo de la capa WMS tambi?n.
260
                    if (fileDlg.getFiles() == null) return;
261
                    FLayer lyr = null;
262
                    File[] files = fileDlg.getFiles();
263
                    String[] driverNames = fileDlg.getDriverNames();
264
                        m_MapControl.getMapContext()
265
                           .beginAtomicEvent();
266

    
267
                    for  (int iFile=0; iFile < files.length; iFile++)
268
                    {
269
                            File fich = files[iFile];
270
                            String layerName = fich.getName();
271
                            String layerPath = fich.getAbsolutePath();
272
                            
273
                            try {
274
                                    lyr = LayerFactory.createLayer(layerName, driverNames[iFile], fich, proj);
275

    
276
                                    lyr.setVisible(true);
277
                                    lyr.setName(layerName);
278
                                    if (lyr != null )
279
                                    {
280
                                            try {
281
                                                    theView.getMapControl().getMapContext().getLayers().addLayer(lyr);
282
                                            } catch (CancelationException ex) {
283
                                                    // TODO Auto-generated catch block
284
                                                    ex.printStackTrace();
285
                                            }
286
                                            // TODO: Poner una variable y dibujar solo cuando
287
                                            // todas las capas hayan sido cargadas.
288
                                            // theView.getMapControl().drawMap();                                           
289
                                            // theView.getTOC().refresh();
290
                                    }                    
291
                                    
292
                            } catch (DriverException ex) {
293
                                    ex.printStackTrace();
294
                                }
295
                            
296
                    } // for
297
                        m_MapControl.getMapContext()
298
                           .beginAtomicEvent();
299

    
300
            return;
301

    
302
        }
303

    
304
        /**
305

306
        String pathFich = fc.getDirectory() + fc.getFile();
307

308

309
        System.out.println(pathFich);
310
        FLyrVect lyr = new FLyrVect(fc.getFile(),pathFich);
311

312
        m_Mapa.AddLayer(lyr);
313
        m_Owner.jLeyenda.Refresh();
314

315
        }
316

317
        **/
318
        if (e.getActionCommand() == "EXPORT") {
319
            // Export jExport = new Export(m_MapControl);
320
        }
321

    
322
        if (e.getActionCommand() == "VIEW_TABLE") {
323
                    FLayer[] actives = m_Mapa.getLayers().getActives();
324

    
325
                     for (int i = 0; i < actives.length; i++) {
326
                    if (actives[i] instanceof AlphanumericData){
327
                            AlphanumericData co = (AlphanumericData)actives[i];
328
                            
329
                                        SelectableDataSource dataSource;
330
                                        try {
331
                                                dataSource = co.getRecordset();
332
                                                ProjectTable projectTable =  ProjectTable.createTable(actives[i].getName(),
333
                                                                        dataSource);
334
                                                        projectTable.setAssociatedTable(co);
335
                                                
336
                                                Table t = new Table();
337
                                                t.setModel(projectTable);
338
                                    JDialog myDialog = new JDialog(theView, "Tabla de Atributos (" 
339
                                                      + t.getModel().getAssociatedTable().getRecordset().getRowCount() + " registros)" );
340
                                    myDialog.setContentPane(t);
341
        
342
                                    // myViewer.addAttributeTableViewerListener(m_Owner);
343
                                    myDialog.pack();
344
                                    myDialog.show(); 
345
                                        } catch (DriverException e1) {
346
                                                // TODO Auto-generated catch block
347
                                                e1.printStackTrace();
348
                                        } catch (HeadlessException ex) {
349
                                                // TODO Auto-generated catch block
350
                                                ex.printStackTrace();
351
                                        } catch (com.hardcode.gdbms.engine.data.DriverException ex) {
352
                                                // TODO Auto-generated catch block
353
                                                ex.printStackTrace();
354
                                        }
355

    
356
                            }
357
                            
358
                    }
359
                
360
        }
361
    }
362
}