Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / DEMO / ComandosListener.java @ 7570

History | View | Annotate | Download (15.6 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 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.panels.ProjChooserPanel;
108
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
109
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
110
import com.iver.cit.gvsig.project.ProjectTable;
111
import com.vividsolutions.jts.geom.LineString;
112
import com.vividsolutions.jts.geom.MultiLineString;
113
import com.vividsolutions.jts.geom.MultiPoint;
114
import com.vividsolutions.jts.geom.Point;
115

    
116

    
117

    
118
//import java.awt.Frame;
119

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

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

    
164
    public ComandosListener(MapControl mapa, SingleView Owner) {
165
        m_Mapa = mapa.getMapContext();
166
        theView = Owner;
167
        m_MapControl = mapa;
168
    }
169
    
170
    /**
171
     * Load the data from the specified dataStore and construct a {@linkPlain Context context} with
172
     * a default style.
173
     *
174
     * @param url The url of the shapefile to load.
175
     * @param name DOCUMENT ME!
176
     *
177
     * @throws IOException is a I/O error occured.
178
     * @throws DataSourceException if an error occured while reading the data source.
179
     * @throws FileNotFoundException DOCUMENT ME!
180
     */
181
    protected void loadLayer(DataStore store, String layerName)
182
        throws IOException, DataSourceException {
183
        final FeatureSource features = store.getFeatureSource(layerName);
184

    
185
        // Create the style
186
        final StyleBuilder builder = new StyleBuilder();
187
        final Style style;
188
        Class geometryClass = features.getSchema().getDefaultGeometry().getType();
189

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

    
200
        final MapLayer layer = new DefaultMapLayer(features, style);
201
        layer.setTitle(layerName);
202
        
203
        FLyrGT2_old lyrGT2 = new FLyrGT2_old(layer);
204
        try {
205
                        m_Mapa.getLayers().addLayer(lyrGT2);
206
                } catch (CancelationException e) {
207
                        // TODO Auto-generated catch block
208
                        e.printStackTrace();
209
                }
210
        
211
    }
212
    
213
    
214
    /* (non-Javadoc)
215
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
216
     */
217
    public void actionPerformed(ActionEvent e) {
218
                // TODO LWS Esto debe venir de fuera (proyeccion del fichero, o de la vista)
219
            IProjection proj = CRSFactory.getCRS(ProjChooserPanel.getCurProj().getAbrev());
220
                ViewPort vp = m_Mapa.getViewPort();
221

    
222
        if (e.getActionCommand() == "ZOOM_TODO") {
223
            try {
224
                                vp.setExtent(m_Mapa.getFullExtent());
225
                                m_MapControl.drawMap(false);
226
                        } catch (DriverException e1) {
227
                                // TODO Auto-generated catch block
228
                                e1.printStackTrace();
229
                        }
230
            m_MapControl.drawMap(false);
231
        }
232
        if (e.getActionCommand() == "ZOOM_PREVIO") {            
233
            vp.setPreviousExtent();
234
        }
235
        if (e.getActionCommand() == "SYMBOL_MANAGER") {            
236
            if (e.getActionCommand() == "SYMBOL_MANAGER") {            
237
                JDialog dialog = new JDialog();
238
                /* JSVGCanvas canvas = new JSVGCanvas();
239
                try {
240
                    canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
241
                } catch (MalformedURLException e1) {
242
                    // TODO Auto-generated catch block
243
                    e1.printStackTrace();
244
                } */
245
                SymbolSelector canvas = new SymbolSelector();
246

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

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

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

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

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

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

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

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

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

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

    
405
            return;
406

    
407
        }
408

    
409
        /**
410

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

413

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

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

420
        }
421

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

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

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

    
463
                            }
464
                            
465
                    }
466
                
467
        }
468
    }
469
}