Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ThemeControls.java @ 5525

History | View | Annotate | Download (8.76 KB)

1 312 fernando
/*
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 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig;
48
49 5005 jorpiell
import java.awt.Component;
50
import java.awt.geom.Rectangle2D;
51
import java.io.File;
52
import java.util.BitSet;
53
54
import javax.swing.JFileChooser;
55
56
import org.apache.log4j.Logger;
57
58 596 fernando
import com.iver.andami.PluginServices;
59 1294 fernando
import com.iver.andami.messages.NotificationManager;
60 596 fernando
import com.iver.andami.plugins.Extension;
61 1270 vcaballero
import com.iver.cit.gvsig.fmap.DriverException;
62 312 fernando
import com.iver.cit.gvsig.fmap.FMap;
63 1223 fernando
import com.iver.cit.gvsig.fmap.MapControl;
64 1270 vcaballero
import com.iver.cit.gvsig.fmap.core.IGeometry;
65 3186 jmorell
import com.iver.cit.gvsig.fmap.drivers.dxf.write.DxfGisWriter;
66 1169 vcaballero
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
67 312 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
68 2421 caballero
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69 1270 vcaballero
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
70 1169 vcaballero
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
71 1270 vcaballero
import com.iver.cit.gvsig.fmap.operations.strategies.SelectedShapeVisitor;
72
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
73 312 fernando
import com.iver.cit.gvsig.gui.View;
74 1221 fernando
import com.iver.cit.gvsig.project.ProjectView;
75 1169 vcaballero
import com.iver.utiles.GenericFileFilter;
76 312 fernando
77
78
/**
79 1219 vcaballero
 * Extensi?n de operaciones sobre el tema.
80 312 fernando
 *
81
 * @author Vicente Caballero Navarro
82
 */
83 5005 jorpiell
public class ThemeControls extends Extension {
84 1219 vcaballero
        private static Logger logger = Logger.getLogger(ThemeControls.class.getName());
85 312 fernando
86 1219 vcaballero
        /**
87 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
88 1219 vcaballero
         */
89
        public void execute(String s) {
90
                View vista = (View) PluginServices.getMDIManager().getActiveView();
91
                ProjectView model = vista.getModel();
92 1169 vcaballero
                FMap mapa = model.getMapContext();
93 1223 fernando
                MapControl mapCtrl = vista.getMapControl();
94 1169 vcaballero
                logger.debug("Comand : " + s);
95 1219 vcaballero
96 2538 jmorell
        if (s.compareTo("SHAPE_SELECTED") == 0) {
97 1169 vcaballero
                        createShape(mapa);
98 1668 jmorell
                } else if (s.compareTo("DXF_SELECTED") == 0) {
99
                        createDxf(mapa);
100 1219 vcaballero
                } else if (s.compareTo("DEL_SELECTION") == 0) {
101
                        boolean refresh = false;
102
103
                        for (int i = 0; i < mapa.getLayers().getLayersCount(); i++) {
104 5282 jmvivo
                                /* JMV: Esto deberia de ser asi (Las capas son 'Selectable' o no)
105
                                 * pero de momento lo cambiamos para que use una capa vectorial
106
                                 *
107

108
                                if (map.getLayers().getLayer(i) instanceof Selectable) {
109 1219 vcaballero
                                        if (mapa.getLayers().getLayer(i).isActive()) {
110 2555 jmorell
                                                Selectable selectable = (Selectable) mapa.getLayers().getLayer(i);
111 1219 vcaballero

112 2555 jmorell
                                                if (selectable.getSelection().cardinality() != 0) {
113 1219 vcaballero
                                                        refresh = true;
114
                                                }
115 2555 jmorell
                        selectable.clearSelection();
116 1219 vcaballero
                                        }
117 1169 vcaballero
                                }
118 5282 jmvivo

119
                                */
120
                                if (mapa.getLayers().getLayer(i) instanceof FLyrVect) {
121
                                        if (mapa.getLayers().getLayer(i).isActive()) {
122
123
                                                try {
124
                                                        SelectableDataSource dataSource;
125
126
                                                        dataSource = ((FLyrVect)mapa.getLayers().getLayer(i)).getRecordset();
127
                                                        if (dataSource.getSelection().cardinality() != 0) {
128
                                                                refresh = true;
129
                                                        }
130
                                dataSource.clearSelection();
131
                                                } catch (DriverException e) {
132
                                                        // TODO Auto-generated catch block
133
                                                        e.printStackTrace();
134
                                                }
135
136
                                        }
137
                                }
138 1169 vcaballero
                        }
139 1219 vcaballero
140
                        if (refresh) {
141
                                mapCtrl.drawMap(false);
142 1169 vcaballero
                        }
143 1219 vcaballero
                } else if (s.compareTo("ZOOM_SELECT") == 0) {
144 1169 vcaballero
                        Rectangle2D selectedExtent = mapa.getSelectionBounds();
145
146
                        if (selectedExtent != null) {
147
                                mapa.getViewPort().setExtent(selectedExtent);
148
                        }
149
                }
150 1219 vcaballero
        }
151
152
        /**
153
         * Crea un nuevo shape.
154
         *
155
         * @param map FMap de donde coger las capas a copiar.
156
         */
157
        private void createShape(FMap map) {
158 1169 vcaballero
                if (map.getSelectionBounds() != null) {
159
                        JFileChooser jfc = new JFileChooser();
160 1219 vcaballero
                        jfc.addChoosableFileFilter(new GenericFileFilter("shp",
161 5503 cesar
                                        PluginServices.getText(this, "Shapefile")));
162 1219 vcaballero
163
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
164
                                File file=jfc.getSelectedFile();
165
                                if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
166
                                        file=new File(file.getPath()+".shp");
167
                                }
168 1270 vcaballero
                                //SHP.SHPFileFromSelected(map, file);
169
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
170
                                try {
171
                                        map.getLayers().process(ssv);
172
                                        } catch (DriverException e1) {
173
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
174
                                                                e1);
175
                                        } catch (VisitException e) {
176
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
177
                                                                e);
178
                                        }
179
                                IGeometry[] fgs=ssv.getSelectedGeometries();
180
                                SelectableDataSource sds=ssv.getSelectableDataSource();
181
                                BitSet bitset=ssv.getBitSet();
182 1294 fernando
                                try {
183
                                        sds.start();
184
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
185
                                        sds.stop();
186 1830 fernando
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e2) {
187 1294 fernando
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
188
                                }
189 1169 vcaballero
                        }
190 1219 vcaballero
                } // else {
191
192 1169 vcaballero
                //}
193 1219 vcaballero
        }
194 1668 jmorell
195
        /**
196
         * Crea un DXF partiendo de los objetos seleccionados. Desarrollado en el
197
         * piloto de CAD. Lo de aqu? no sirve.
198
         * @param map
199
         */
200
        private void createDxf(FMap map) {
201
                if (map.getSelectionBounds() != null) {
202
                        JFileChooser jfc = new JFileChooser();
203
                        jfc.addChoosableFileFilter(new GenericFileFilter("dxf",
204 5503 cesar
                                        PluginServices.getText(this, "Dxffiles")));
205 1219 vcaballero
206 1668 jmorell
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
207
                                File file=jfc.getSelectedFile();
208
                                if (!(file.getPath().endsWith(".dxf") || file.getPath().endsWith(".DXF"))){
209
                                        file=new File(file.getPath()+".dxf");
210
                                }
211
                                //SHP.SHPFileFromSelected(map, file);
212
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
213
                                try {
214
                                        map.getLayers().process(ssv);
215
                                        } catch (DriverException e1) {
216
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
217
                                                                e1);
218
                                        } catch (VisitException e) {
219
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
220
                                                                e);
221
                                        }
222 3186 jmorell
                                IGeometry[] fgs=ssv.getSelectedGeometries();
223
                                DxfGisWriter dxfGisWriter = new DxfGisWriter();
224 1668 jmorell
                                try {
225 3436 jmorell
                                        // map.getLayers() devuelve solo una capa porque la
226
                    // herramienta est? inhabilitada cuando hay varias capas
227
                    // seleccionadas.
228
                    FLayer layer = map.getLayers().getActives()[0];
229 3442 jmorell
                    //dxfGisWriter.write(fgs, layer, file);
230 1668 jmorell
                                } catch (Exception e2) {
231
                                        // TODO Auto-generated catch block
232
                                        e2.printStackTrace();
233 3186 jmorell
                                }
234 1668 jmorell
                                /*SelectableDataSource sds=ssv.getSelectableDataSource();
235
                                BitSet bitset=ssv.getBitSet();
236
                                try {
237
                                        sds.start();
238
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
239
                                        sds.stop();
240
                                } catch (com.hardcode.gdbms.engine.data.DriverException e2) {
241
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
242
                                }*/
243
                        }
244
                }
245
        }
246
247 1219 vcaballero
        /**
248 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
249 1219 vcaballero
         */
250
        public boolean isVisible() {
251
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
252
                                                                                                                         .getActiveView();
253
254
                if (f == null) {
255
                        return false;
256 1169 vcaballero
                }
257 312 fernando
258 1219 vcaballero
                if (f.getClass() == View.class) {
259
                        FMap mapa = ((View) f).getModel().getMapContext();
260 312 fernando
261 2421 caballero
                        //View v = (View) f;
262 312 fernando
263 1219 vcaballero
                        return mapa.getLayers().getLayersCount() > 0;
264
                } else {
265
                        return false;
266
                }
267
        }
268 312 fernando
269 1219 vcaballero
        /**
270 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
271 1219 vcaballero
         */
272
        public boolean isEnabled() {
273
                View f = (View) PluginServices.getMDIManager().getActiveView();
274 312 fernando
275 1219 vcaballero
                if (f == null) {
276
                        return false;
277
                }
278 312 fernando
279 1219 vcaballero
                FLayer[] selected = f.getModel().getMapContext().getLayers().getActives();
280 2538 jmorell
                if (selected.length == 1 && selected[0] instanceof FLyrVect){
281 2421 caballero
                        return true;
282
                }
283
                return false;
284 1219 vcaballero
        }
285 596 fernando
286
        /**
287 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
288 596 fernando
         */
289 5005 jorpiell
        public void initialize() {
290 596 fernando
        }
291 312 fernando
}