Statistics
| Revision:

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

History | View | Annotate | Download (8.15 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 596 fernando
import com.iver.andami.PluginServices;
50 1294 fernando
import com.iver.andami.messages.NotificationManager;
51 596 fernando
import com.iver.andami.plugins.Extension;
52 1219 vcaballero
53 1270 vcaballero
import com.iver.cit.gvsig.fmap.DriverException;
54 312 fernando
import com.iver.cit.gvsig.fmap.FMap;
55 1223 fernando
import com.iver.cit.gvsig.fmap.MapControl;
56 1270 vcaballero
import com.iver.cit.gvsig.fmap.core.IGeometry;
57 3186 jmorell
import com.iver.cit.gvsig.fmap.drivers.dxf.write.DxfGisWriter;
58 1169 vcaballero
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
59 312 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
60 2421 caballero
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
61 1270 vcaballero
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
62 3436 jmorell
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
63 1169 vcaballero
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
64 1270 vcaballero
import com.iver.cit.gvsig.fmap.operations.strategies.SelectedShapeVisitor;
65
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
66 312 fernando
import com.iver.cit.gvsig.gui.View;
67 1221 fernando
import com.iver.cit.gvsig.project.ProjectView;
68 1219 vcaballero
69 1169 vcaballero
import com.iver.utiles.GenericFileFilter;
70 312 fernando
71 1219 vcaballero
import org.apache.log4j.Logger;
72 312 fernando
73 1219 vcaballero
import java.awt.Component;
74
import java.awt.geom.Rectangle2D;
75
import java.io.File;
76
77
import java.util.BitSet;
78
79
import javax.swing.JFileChooser;
80
81
82 312 fernando
/**
83 1219 vcaballero
 * Extensi?n de operaciones sobre el tema.
84 312 fernando
 *
85
 * @author Vicente Caballero Navarro
86
 */
87 596 fernando
public class ThemeControls implements Extension {
88 1219 vcaballero
        private static Logger logger = Logger.getLogger(ThemeControls.class.getName());
89 312 fernando
90 1219 vcaballero
        /**
91
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
92
         */
93
        public void execute(String s) {
94
                View vista = (View) PluginServices.getMDIManager().getActiveView();
95
                ProjectView model = vista.getModel();
96 1169 vcaballero
                FMap mapa = model.getMapContext();
97 1223 fernando
                MapControl mapCtrl = vista.getMapControl();
98 1169 vcaballero
                logger.debug("Comand : " + s);
99 1219 vcaballero
100 2538 jmorell
        if (s.compareTo("SHAPE_SELECTED") == 0) {
101 1169 vcaballero
                        createShape(mapa);
102 1668 jmorell
                } else if (s.compareTo("DXF_SELECTED") == 0) {
103
                        createDxf(mapa);
104 1219 vcaballero
                } else if (s.compareTo("DEL_SELECTION") == 0) {
105
                        boolean refresh = false;
106
107
                        for (int i = 0; i < mapa.getLayers().getLayersCount(); i++) {
108
                                if (mapa.getLayers().getLayer(i) instanceof Selectable) {
109
                                        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
                        }
119 1219 vcaballero
120
                        if (refresh) {
121
                                mapCtrl.drawMap(false);
122 1169 vcaballero
                        }
123 1219 vcaballero
                } else if (s.compareTo("ZOOM_SELECT") == 0) {
124 1169 vcaballero
                        Rectangle2D selectedExtent = mapa.getSelectionBounds();
125
126
                        if (selectedExtent != null) {
127
                                mapa.getViewPort().setExtent(selectedExtent);
128
                        }
129
                }
130 1219 vcaballero
        }
131
132
        /**
133
         * Crea un nuevo shape.
134
         *
135
         * @param map FMap de donde coger las capas a copiar.
136
         */
137
        private void createShape(FMap map) {
138 1169 vcaballero
                if (map.getSelectionBounds() != null) {
139
                        JFileChooser jfc = new JFileChooser();
140 1219 vcaballero
                        jfc.addChoosableFileFilter(new GenericFileFilter("shp",
141
                                        PluginServices.getText(this, "ShapeFile")));
142
143
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
144
                                File file=jfc.getSelectedFile();
145
                                if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
146
                                        file=new File(file.getPath()+".shp");
147
                                }
148 1270 vcaballero
                                //SHP.SHPFileFromSelected(map, file);
149
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
150
                                try {
151
                                        map.getLayers().process(ssv);
152
                                        } catch (DriverException e1) {
153
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
154
                                                                e1);
155
                                        } catch (VisitException e) {
156
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
157
                                                                e);
158
                                        }
159
                                IGeometry[] fgs=ssv.getSelectedGeometries();
160
                                SelectableDataSource sds=ssv.getSelectableDataSource();
161
                                BitSet bitset=ssv.getBitSet();
162 1294 fernando
                                try {
163
                                        sds.start();
164
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
165
                                        sds.stop();
166 1830 fernando
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e2) {
167 1294 fernando
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
168
                                }
169 1169 vcaballero
                        }
170 1219 vcaballero
                } // else {
171
172 1169 vcaballero
                //}
173 1219 vcaballero
        }
174 1668 jmorell
175
        /**
176
         * Crea un DXF partiendo de los objetos seleccionados. Desarrollado en el
177
         * piloto de CAD. Lo de aqu? no sirve.
178
         * @param map
179
         */
180
        private void createDxf(FMap map) {
181
                if (map.getSelectionBounds() != null) {
182
                        JFileChooser jfc = new JFileChooser();
183
                        jfc.addChoosableFileFilter(new GenericFileFilter("dxf",
184
                                        PluginServices.getText(this, "DxfFile")));
185 1219 vcaballero
186 1668 jmorell
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
187
                                File file=jfc.getSelectedFile();
188
                                if (!(file.getPath().endsWith(".dxf") || file.getPath().endsWith(".DXF"))){
189
                                        file=new File(file.getPath()+".dxf");
190
                                }
191
                                //SHP.SHPFileFromSelected(map, file);
192
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
193
                                try {
194
                                        map.getLayers().process(ssv);
195
                                        } catch (DriverException e1) {
196
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
197
                                                                e1);
198
                                        } catch (VisitException e) {
199
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
200
                                                                e);
201
                                        }
202 3186 jmorell
                                IGeometry[] fgs=ssv.getSelectedGeometries();
203
                                DxfGisWriter dxfGisWriter = new DxfGisWriter();
204 1668 jmorell
                                try {
205 3436 jmorell
                                        // map.getLayers() devuelve solo una capa porque la
206
                    // herramienta est? inhabilitada cuando hay varias capas
207
                    // seleccionadas.
208
                    FLayer layer = map.getLayers().getActives()[0];
209 3442 jmorell
                    //dxfGisWriter.write(fgs, layer, file);
210 1668 jmorell
                                } catch (Exception e2) {
211
                                        // TODO Auto-generated catch block
212
                                        e2.printStackTrace();
213 3186 jmorell
                                }
214 1668 jmorell
                                /*SelectableDataSource sds=ssv.getSelectableDataSource();
215
                                BitSet bitset=ssv.getBitSet();
216
                                try {
217
                                        sds.start();
218
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
219
                                        sds.stop();
220
                                } catch (com.hardcode.gdbms.engine.data.DriverException e2) {
221
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
222
                                }*/
223
                        }
224
                }
225
        }
226
227 1219 vcaballero
        /**
228
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
229
         */
230
        public boolean isVisible() {
231
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
232
                                                                                                                         .getActiveView();
233
234
                if (f == null) {
235
                        return false;
236 1169 vcaballero
                }
237 312 fernando
238 1219 vcaballero
                if (f.getClass() == View.class) {
239
                        FMap mapa = ((View) f).getModel().getMapContext();
240 312 fernando
241 2421 caballero
                        //View v = (View) f;
242 312 fernando
243 1219 vcaballero
                        return mapa.getLayers().getLayersCount() > 0;
244
                } else {
245
                        return false;
246
                }
247
        }
248 312 fernando
249 1219 vcaballero
        /**
250
         * @see com.iver.andami.plugins.Extension#isEnabled()
251
         */
252
        public boolean isEnabled() {
253
                View f = (View) PluginServices.getMDIManager().getActiveView();
254 312 fernando
255 1219 vcaballero
                if (f == null) {
256
                        return false;
257
                }
258 312 fernando
259 1219 vcaballero
                FLayer[] selected = f.getModel().getMapContext().getLayers().getActives();
260 2538 jmorell
                if (selected.length == 1 && selected[0] instanceof FLyrVect){
261 2421 caballero
                        return true;
262
                }
263
                return false;
264 1219 vcaballero
        }
265 596 fernando
266
        /**
267
         * @see com.iver.andami.plugins.Extension#inicializar()
268
         */
269
        public void inicializar() {
270
        }
271 312 fernando
}