Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / NewLayerExtension.java @ 1729

History | View | Annotate | Download (5.61 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.dxf.write.DxfWriter;
52
import com.iver.cit.gvsig.fmap.edition.EditionException;
53
import com.iver.cit.gvsig.fmap.layers.FLayer;
54
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
55
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditableVectorialLayer;
56
import com.iver.cit.gvsig.gui.FOpenDialog;
57
import com.iver.cit.gvsig.gui.View;
58
import com.iver.cit.gvsig.project.ProjectView;
59

    
60
import com.iver.utiles.GenericFileFilter;
61

    
62
import org.cresques.cts.ICoordTrans;
63
import org.cresques.cts.IProjection;
64
import org.cresques.cts.gt2.CoordSys;
65
import org.cresques.cts.gt2.CoordTrans;
66

    
67
import java.awt.Component;
68

    
69
import java.io.File;
70

    
71
import javax.swing.JFileChooser;
72
import javax.swing.JOptionPane;
73

    
74

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

    
87
        /**
88
         * @see com.iver.andami.plugins.Extension#execute(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().getMapControl();
95

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

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

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

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

    
123
                        DxfWriter dxfwriter = new DxfWriter();
124

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

    
132
                if (file == null) {
133
                        return;
134
                }
135

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

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

    
145
                        if (lyr != null) {
146
                                lyr.setVisible(true);
147
                                lyr.setActive(true);
148

    
149
                                // Comprobar que la projecci?n es la misma que la vista
150
                                if (proj != vista.getProjection()) {
151
                                        int option = JOptionPane.showConfirmDialog(null,
152
                                                        "La proyecci?n de la capa no es igual que la de la vista",
153
                                                        "?Desea reproyectar?", JOptionPane.YES_NO_OPTION);
154

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

    
166
                                vista.getMapControl().getMapContext().getLayers().addLayer(lyr);
167

    
168
                                if (mapa.getFullExtent() == null) {
169
                                        vista.getMapControl().getMapContext().getViewPort()
170
                                                 .setExtent(lyr.getFullExtent());
171
                                }
172
                        }
173

    
174
                        EditableVectorialLayer capa = (EditableVectorialLayer) lyr; // mapa.getLayers().getLayer(file.getName());
175
                        PluginServices.getMDIManager().setWaitCursor();
176

    
177
                        try {
178
                                capa.startEdition();
179
                        } catch (EditionException e) {
180
                                e.printStackTrace();
181
                        }
182

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

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

    
197
        /**
198
         * @see com.iver.andami.plugins.Extension#isVisible()
199
         */
200
        public boolean isVisible() {
201
                return true;
202
        }
203
}