Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / NewLayerExtension.java @ 3513

History | View | Annotate | Download (6.06 KB)

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

    
43
import com.iver.andami.PluginServices;
44
import com.iver.andami.messages.NotificationManager;
45
import com.iver.andami.plugins.Extension;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.FMap;
49
import com.iver.cit.gvsig.fmap.MapControl;
50
import com.iver.cit.gvsig.fmap.core.IGeometry;
51
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
52
import com.iver.cit.gvsig.fmap.drivers.dxf.write.DxfWriter;
53
import com.iver.cit.gvsig.fmap.edition.EditionException;
54
import com.iver.cit.gvsig.fmap.layers.FLayer;
55
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
56
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
57
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditableVectorialLayer;
58
import com.iver.cit.gvsig.gui.FOpenDialog;
59
import com.iver.cit.gvsig.gui.View;
60
import com.iver.cit.gvsig.project.ProjectView;
61

    
62
import com.iver.utiles.GenericFileFilter;
63

    
64
import org.cresques.cts.ICoordTrans;
65
import org.cresques.cts.IProjection;
66
import org.cresques.cts.gt2.CoordSys;
67
import org.cresques.cts.gt2.CoordTrans;
68

    
69
import java.awt.Component;
70

    
71
import java.io.File;
72

    
73
import javax.swing.JFileChooser;
74
import javax.swing.JOptionPane;
75

    
76

    
77
/**
78
 * Extensi?n encargada de crear una nueva capa dxf para editarla.
79
 *
80
 * @author Vicente Caballero Navarro
81
 */
82
public class NewLayerExtension implements Extension {
83
        /**
84
         * @see com.iver.andami.plugins.Extension#inicializar()
85
         */
86
        public void inicializar() {
87
        }
88

    
89
        /**
90
         * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
91
         */
92
        public void execute(String s) {
93
                View vista = (View) PluginServices.getMDIManager().getActiveView();
94
                ProjectView model = vista.getModel();
95
                FMap mapa = model.getMapContext();
96
                MapControl mapCtrl = vista.getMapControl().getMapControl();
97

    
98
                if (s.compareTo("NUEVA_CAPA_EDICION") == 0) {
99
                        addNewLayer(vista);
100
                }
101
        }
102

    
103
        /**
104
         * A?ade una nueva capa a partir de un filechooser perparada para editar
105
         * directamente sobre ella.
106
         *
107
         * @param vista Vista en la que nos encontramos.
108
         */
109
        private void addNewLayer(View vista) {
110
                ProjectView model = vista.getModel();
111
                FMap mapa = model.getMapContext();
112
                File file = null;
113
                JFileChooser jfc = new JFileChooser();
114
                jfc.addChoosableFileFilter(new GenericFileFilter("dxf",
115
                                PluginServices.getText(this, "DxfFiles")));
116

    
117
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
118
                        file = jfc.getSelectedFile();
119

    
120
                        if (!(file.getPath().endsWith(".dxf") ||
121
                                        file.getPath().endsWith(".DXF"))) {
122
                                file = new File(file.getPath() + ".dxf");
123
                        }
124

    
125
                        DxfWriter dxfwriter = new DxfWriter();
126

    
127
                        try {
128
                                dxfwriter.write(new IGeometry[0], file);
129
                        } catch (Exception e3) {
130
                                e3.printStackTrace();
131
                        }
132
                }
133

    
134
                if (file == null) {
135
                        return;
136
                }
137

    
138
                IProjection proj = FOpenDialog.getLastProjection();
139
                FLayer lyr = null;
140
                String layerName = file.getName();
141

    
142
                //        String layerPath = file.getAbsolutePath();
143
                try {
144
                        lyr = LayerFactory.createLayer(layerName,
145
                                        "gvSIG DXF CAD Model Driver", file, proj);
146

    
147
                        if (lyr != null) {
148
                                lyr.setVisible(true);
149
                                lyr.setActive(true);
150
                                ((FLyrVect)lyr).setFile(file);
151
                                
152
                                // Comprobar que la projecci?n es la misma que la vista
153
                                if (proj != vista.getProjection()) {
154
                                        int option = JOptionPane.showConfirmDialog(null,
155
                                                        "La proyecci?n de la capa no es igual que la de la vista",
156
                                                        "?Desea reproyectar?", JOptionPane.YES_NO_OPTION);
157

    
158
                                        if (option == JOptionPane.NO_OPTION) {
159
                                                //continue;
160
                                        } else {
161
                                                ICoordTrans ct = new CoordTrans((CoordSys) proj,
162
                                                                (CoordSys) vista.getProjection());
163
                                                lyr.setCoordTrans(ct);
164
                                                System.err.println("coordTrans = " + proj.getAbrev() +
165
                                                        " " + vista.getProjection().getAbrev());
166
                                        }
167
                                }
168

    
169
                                vista.getMapControl().getMapContext().getLayers().addLayer(lyr);
170

    
171
                                if (mapa.getFullExtent() == null) {
172
                                        vista.getMapControl().getMapContext().getViewPort()
173
                                                 .setExtent(lyr.getFullExtent());
174
                                }
175
                        }
176

    
177
                        EditableVectorialLayer capa = (EditableVectorialLayer) lyr; // mapa.getLayers().getLayer(file.getName());
178
                        PluginServices.getMDIManager().setWaitCursor();
179

    
180
                        try {
181
                                capa.startEdition();
182
                        } catch (EditionException e) {
183
                                e.printStackTrace();
184
                        }
185

    
186
                        PluginServices.getMDIManager().restoreCursor();
187
                        vista.getMapControl().getMapControl().drawMap(false);
188
                } catch (DriverException e) {
189
                        NotificationManager.addError("Error al crear la capa", e);
190
                }
191
        }
192

    
193
        /**
194
         * @see com.iver.andami.plugins.Extension#isEnabled()
195
         */
196
        public boolean isEnabled() {
197
                return true;
198
        }
199

    
200
        /**
201
         * @see com.iver.andami.plugins.Extension#isVisible()
202
         */
203
        public boolean isVisible() {
204
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
205
                 .getActiveView();
206

    
207
                if (f == null) {
208
                        return false;
209
                }
210

    
211
                if (f.getClass() == View.class) {
212
                        View vista = (View) f;
213
                        ProjectView model = vista.getModel();
214
                        FMap mapa = model.getMapContext();
215

    
216
                        return true;
217
                } else {
218
                        return false;
219
                }
220
        }
221
}