Statistics
| Revision:

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

History | View | Annotate | Download (7.62 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 6878 cesar
import com.iver.cit.gvsig.fmap.MapContext;
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 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
74 7304 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
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 6880 cesar
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
91 7392 sbayarri
                IProjectView model = vista.getModel();
92 6878 cesar
                MapContext mapa = model.getMapContext();
93 1223 fernando
                MapControl mapCtrl = vista.getMapControl();
94 1169 vcaballero
                logger.debug("Comand : " + s);
95 1219 vcaballero
96 6599 caballero
        if (s.equals("SHAPE_SELECTED")) {
97 1169 vcaballero
                        createShape(mapa);
98 6599 caballero
                } else if (s.equals("DXF_SELECTED")) {
99 1668 jmorell
                        createDxf(mapa);
100 6599 caballero
                } else if (s.equals("ZOOM_SELECT")) {
101 1169 vcaballero
                        Rectangle2D selectedExtent = mapa.getSelectionBounds();
102
103
                        if (selectedExtent != null) {
104
                                mapa.getViewPort().setExtent(selectedExtent);
105
                        }
106
                }
107 1219 vcaballero
        }
108
109
        /**
110
         * Crea un nuevo shape.
111
         *
112
         * @param map FMap de donde coger las capas a copiar.
113
         */
114 6878 cesar
        private void createShape(MapContext map) {
115 1169 vcaballero
                if (map.getSelectionBounds() != null) {
116
                        JFileChooser jfc = new JFileChooser();
117 1219 vcaballero
                        jfc.addChoosableFileFilter(new GenericFileFilter("shp",
118 5503 cesar
                                        PluginServices.getText(this, "Shapefile")));
119 1219 vcaballero
120
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
121
                                File file=jfc.getSelectedFile();
122
                                if (!(file.getPath().endsWith(".shp") || file.getPath().endsWith(".SHP"))){
123
                                        file=new File(file.getPath()+".shp");
124
                                }
125 1270 vcaballero
                                //SHP.SHPFileFromSelected(map, file);
126
                                SelectedShapeVisitor ssv=new SelectedShapeVisitor();
127
                                try {
128
                                        map.getLayers().process(ssv);
129
                                        } catch (DriverException e1) {
130
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
131
                                                                e1);
132
                                        } catch (VisitException e) {
133
                                                throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
134
                                                                e);
135
                                        }
136
                                IGeometry[] fgs=ssv.getSelectedGeometries();
137
                                SelectableDataSource sds=ssv.getSelectableDataSource();
138
                                BitSet bitset=ssv.getBitSet();
139 1294 fernando
                                try {
140
                                        sds.start();
141
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
142
                                        sds.stop();
143 1830 fernando
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e2) {
144 1294 fernando
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
145
                                }
146 1169 vcaballero
                        }
147 1219 vcaballero
                } // else {
148
149 1169 vcaballero
                //}
150 1219 vcaballero
        }
151 6599 caballero
152 1668 jmorell
        /**
153
         * Crea un DXF partiendo de los objetos seleccionados. Desarrollado en el
154
         * piloto de CAD. Lo de aqu? no sirve.
155
         * @param map
156
         */
157 6878 cesar
        private void createDxf(MapContext map) {
158 1668 jmorell
                if (map.getSelectionBounds() != null) {
159
                        JFileChooser jfc = new JFileChooser();
160
                        jfc.addChoosableFileFilter(new GenericFileFilter("dxf",
161 5503 cesar
                                        PluginServices.getText(this, "Dxffiles")));
162 1219 vcaballero
163 1668 jmorell
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
164
                                File file=jfc.getSelectedFile();
165
                                if (!(file.getPath().endsWith(".dxf") || file.getPath().endsWith(".DXF"))){
166
                                        file=new File(file.getPath()+".dxf");
167
                                }
168
                                //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 3186 jmorell
                                IGeometry[] fgs=ssv.getSelectedGeometries();
180
                                DxfGisWriter dxfGisWriter = new DxfGisWriter();
181 1668 jmorell
                                try {
182 3436 jmorell
                                        // map.getLayers() devuelve solo una capa porque la
183
                    // herramienta est? inhabilitada cuando hay varias capas
184
                    // seleccionadas.
185
                    FLayer layer = map.getLayers().getActives()[0];
186 3442 jmorell
                    //dxfGisWriter.write(fgs, layer, file);
187 1668 jmorell
                                } catch (Exception e2) {
188
                                        // TODO Auto-generated catch block
189
                                        e2.printStackTrace();
190 3186 jmorell
                                }
191 1668 jmorell
                                /*SelectableDataSource sds=ssv.getSelectableDataSource();
192
                                BitSet bitset=ssv.getBitSet();
193
                                try {
194
                                        sds.start();
195
                                        SHP.SHPFileFromGeometries(fgs,bitset,sds,file);
196
                                        sds.stop();
197
                                } catch (com.hardcode.gdbms.engine.data.DriverException e2) {
198
                                        NotificationManager.addError("No se pudo escribir la capa", e2);
199
                                }*/
200
                        }
201
                }
202
        }
203
204 1219 vcaballero
        /**
205 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
206 1219 vcaballero
         */
207
        public boolean isVisible() {
208 6877 cesar
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
209 6880 cesar
                                                                                                                         .getActiveWindow();
210 1219 vcaballero
211
                if (f == null) {
212
                        return false;
213 1169 vcaballero
                }
214 312 fernando
215 5900 jorpiell
                if (f instanceof View) {
216 6878 cesar
                        MapContext mapa = ((View) f).getModel().getMapContext();
217 312 fernando
218 2421 caballero
                        //View v = (View) f;
219 312 fernando
220 1219 vcaballero
                        return mapa.getLayers().getLayersCount() > 0;
221
                } else {
222
                        return false;
223
                }
224
        }
225 312 fernando
226 1219 vcaballero
        /**
227 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
228 1219 vcaballero
         */
229
        public boolean isEnabled() {
230 6880 cesar
                View f = (View) PluginServices.getMDIManager().getActiveWindow();
231 312 fernando
232 1219 vcaballero
                if (f == null) {
233
                        return false;
234
                }
235 312 fernando
236 1219 vcaballero
                FLayer[] selected = f.getModel().getMapContext().getLayers().getActives();
237 6778 jmvivo
                if (selected.length == 1 && selected[0] instanceof FLyrVect && selected[0].isAvailable()){
238 2421 caballero
                        return true;
239
                }
240
                return false;
241 1219 vcaballero
        }
242 596 fernando
243
        /**
244 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
245 596 fernando
         */
246 5005 jorpiell
        public void initialize() {
247 596 fernando
        }
248 312 fernando
}