Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ThemeControls.java @ 1668

History | View | Annotate | Download (7.83 KB)

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

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.messages.NotificationManager;
51
import com.iver.andami.plugins.Extension;
52

    
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.FMap;
55
import com.iver.cit.gvsig.fmap.MapControl;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
58
import com.iver.cit.gvsig.fmap.layers.FLayer;
59
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
61
import com.iver.cit.gvsig.fmap.operations.strategies.SelectedShapeVisitor;
62
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
63
import com.iver.cit.gvsig.gui.View;
64
import com.iver.cit.gvsig.project.ProjectView;
65

    
66
import com.iver.utiles.GenericFileFilter;
67

    
68
import org.apache.log4j.Logger;
69

    
70
import java.awt.Component;
71
import java.awt.geom.Rectangle2D;
72
import java.io.File;
73

    
74
import java.util.BitSet;
75

    
76
import javax.swing.JFileChooser;
77

    
78

    
79
/**
80
 * Extensi?n de operaciones sobre el tema.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class ThemeControls implements Extension {
85
        private static Logger logger = Logger.getLogger(ThemeControls.class.getName());
86

    
87
        /**
88
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
89
         */
90
        public void execute(String s) {
91
                View vista = (View) PluginServices.getMDIManager().getActiveView();
92
                ProjectView model = vista.getModel();
93
                FMap mapa = model.getMapContext();
94
                MapControl mapCtrl = vista.getMapControl();
95
                logger.debug("Comand : " + s);
96

    
97
                if (s.compareTo("PROPERTIES") == 0) {
98
                        vista.openThemeProperties();
99
                // 050207, jmorell: A?adido para mostrar el men? de propiedades seg?n el pliego.
100
                } else if (s.compareTo("PLIEGO_PROPERTIES") == 0) {
101
                        vista.openPliegoThemeProperties();
102
                } else if (s.compareTo("SHAPE_SELECTED") == 0) {
103
                        createShape(mapa);
104
                } else if (s.compareTo("DXF_SELECTED") == 0) {
105
                        createDxf(mapa);
106
                } else if (s.compareTo("DEL_SELECTION") == 0) {
107
                        boolean refresh = false;
108

    
109
                        for (int i = 0; i < mapa.getLayers().getLayersCount(); i++) {
110
                                if (mapa.getLayers().getLayer(i) instanceof Selectable) {
111
                                        if (mapa.getLayers().getLayer(i).isActive()) {
112
                                                BitSet bitset = ((Selectable) mapa.getLayers().getLayer(i)).getSelection();
113

    
114
                                                if (bitset.cardinality() != 0) {
115
                                                        refresh = true;
116
                                                }
117

    
118
                                                bitset.clear();
119
                                        }
120
                                }
121
                        }
122

    
123
                        if (refresh) {
124
                                mapCtrl.drawMap(false);
125
                        }
126
                } else if (s.compareTo("ZOOM_SELECT") == 0) {
127
                        Rectangle2D selectedExtent = mapa.getSelectionBounds();
128

    
129
                        if (selectedExtent != null) {
130
                                mapa.getViewPort().setExtent(selectedExtent);
131
                        }
132
                }
133
        }
134

    
135
        /**
136
         * Crea un nuevo shape.
137
         *
138
         * @param map FMap de donde coger las capas a copiar.
139
         */
140
        private void createShape(FMap map) {
141
                if (map.getSelectionBounds() != null) {
142
                        JFileChooser jfc = new JFileChooser();
143
                        jfc.addChoosableFileFilter(new GenericFileFilter("shp",
144
                                        PluginServices.getText(this, "ShapeFile")));
145

    
146
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
147
                                File file=jfc.getSelectedFile();
148
                                if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
149
                                        file=new File(file.getPath()+".shp");
150
                                }
151
                                //SHP.SHPFileFromSelected(map, file);
152
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
153
                                try {
154
                                        map.getLayers().process(ssv);
155
                                        } catch (DriverException e1) {
156
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
157
                                                                e1);
158
                                        } catch (VisitException e) {
159
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
160
                                                                e);
161
                                        }
162
                                IGeometry[] fgs=ssv.getSelectedGeometries();
163
                                SelectableDataSource sds=ssv.getSelectableDataSource();
164
                                BitSet bitset=ssv.getBitSet();
165
                                try {
166
                                        sds.start();
167
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
168
                                        sds.stop();
169
                                } catch (com.hardcode.gdbms.engine.data.DriverException e2) {
170
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
171
                                }
172
                        }
173
                } // else {
174

    
175
                //}
176
        }
177
        
178
        /**
179
         * Crea un DXF partiendo de los objetos seleccionados. Desarrollado en el
180
         * piloto de CAD. Lo de aqu? no sirve.
181
         * @param map
182
         */
183
        private void createDxf(FMap map) {
184
                if (map.getSelectionBounds() != null) {
185
                        JFileChooser jfc = new JFileChooser();
186
                        jfc.addChoosableFileFilter(new GenericFileFilter("dxf",
187
                                        PluginServices.getText(this, "DxfFile")));
188

    
189
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
190
                                File file=jfc.getSelectedFile();
191
                                if (!(file.getPath().endsWith(".dxf") || file.getPath().endsWith(".DXF"))){
192
                                        file=new File(file.getPath()+".dxf");
193
                                }
194
                                //SHP.SHPFileFromSelected(map, file);
195
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
196
                                try {
197
                                        map.getLayers().process(ssv);
198
                                        } catch (DriverException e1) {
199
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
200
                                                                e1);
201
                                        } catch (VisitException e) {
202
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
203
                                                                e);
204
                                        }
205
                                IGeometry[] fgs=ssv.getSelectedGeometries();
206
                                /*DxfWriter dxfWriter = new DxfWriter();
207
                                try {
208
                                        dxfWriter.write(fgs, file);
209
                                } catch (Exception e2) {
210
                                        // TODO Auto-generated catch block
211
                                        e2.printStackTrace();
212
                                }*/
213
                                /*SelectableDataSource sds=ssv.getSelectableDataSource();
214
                                BitSet bitset=ssv.getBitSet();
215
                                try {
216
                                        sds.start();
217
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
218
                                        sds.stop();
219
                                } catch (com.hardcode.gdbms.engine.data.DriverException e2) {
220
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
221
                                }*/
222
                        }
223
                }
224
        }
225

    
226
        /**
227
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
228
         */
229
        public boolean isVisible() {
230
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
231
                                                                                                                         .getActiveView();
232

    
233
                if (f == null) {
234
                        return false;
235
                }
236

    
237
                if (f.getClass() == View.class) {
238
                        FMap mapa = ((View) f).getModel().getMapContext();
239

    
240
                        View v = (View) f;
241

    
242
                        return mapa.getLayers().getLayersCount() > 0;
243
                } else {
244
                        return false;
245
                }
246
        }
247

    
248
        /**
249
         * @see com.iver.andami.plugins.Extension#isEnabled()
250
         */
251
        public boolean isEnabled() {
252
                View f = (View) PluginServices.getMDIManager().getActiveView();
253

    
254
                if (f == null) {
255
                        return false;
256
                }
257

    
258
                FLayer[] selected = f.getModel().getMapContext().getLayers().getActives();
259

    
260
                return selected.length > 0;
261
        }
262

    
263
        /**
264
         * @see com.iver.andami.plugins.Extension#inicializar()
265
         */
266
        public void inicializar() {
267
        }
268
}