Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 4458

History | View | Annotate | Download (7.52 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.gui.cad;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.Point2D;
48
import java.awt.image.BufferedImage;
49
import java.io.IOException;
50
import java.util.ArrayList;
51

    
52
import com.hardcode.driverManager.DriverLoadException;
53
import com.hardcode.gdbms.engine.data.driver.DriverException;
54
import com.hardcode.gdbms.engine.values.Value;
55
import com.hardcode.gdbms.engine.values.ValueFactory;
56
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.CADExtension;
58
import com.iver.cit.gvsig.fmap.ViewPort;
59
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
62
import com.iver.cit.gvsig.fmap.core.Handler;
63
import com.iver.cit.gvsig.fmap.core.IFeature;
64
import com.iver.cit.gvsig.fmap.core.IGeometry;
65
import com.iver.cit.gvsig.fmap.core.IRow;
66
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
67
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
68
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
70
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
71
import com.iver.cit.gvsig.fmap.layers.FBitSet;
72

    
73
/**
74
 * DOCUMENT ME!
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public abstract class DefaultCADTool implements CADTool {
79
        private CADToolAdapter cadToolAdapter;
80

    
81
        private String question;
82

    
83
        private String[] currentdescriptions;
84

    
85
        /**
86
         * DOCUMENT ME!
87
         */
88
        public void draw(IGeometry geometry) {
89
                if (geometry != null) {
90
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
91
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
92
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
93
                        geometry.draw(gImag, vp, CADTool.drawingSymbol);
94
                }
95
        }
96

    
97
        /**
98
         * DOCUMENT ME!
99
         *
100
         * @param cta
101
         *            DOCUMENT ME!
102
         */
103
        public void setCadToolAdapter(CADToolAdapter cta) {
104
                cadToolAdapter = cta;
105
        }
106

    
107
        /**
108
         * DOCUMENT ME!
109
         *
110
         * @return DOCUMENT ME!
111
         */
112
        public CADToolAdapter getCadToolAdapter() {
113
                return cadToolAdapter;
114
        }
115

    
116
        /**
117
         * DOCUMENT ME!
118
         *
119
         * @param g
120
         *            DOCUMENT ME!
121
         * @param firstPoint
122
         *            DOCUMENT ME!
123
         * @param endPoint
124
         *            DOCUMENT ME!
125
         */
126
        public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint) {
127
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
128
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
129
                elShape.lineTo(endPoint.getX(), endPoint.getY());
130
                ShapeFactory.createPolyline2D(elShape).draw(g,
131
                                getCadToolAdapter().getMapControl().getViewPort(),
132
                                CADTool.drawingSymbol);
133
        }
134

    
135
        /**
136
         * DOCUMENT ME!
137
         *
138
         * @param geometry
139
         *            DOCUMENT ME!
140
         */
141
        public void addGeometry(IGeometry geometry) {
142
                VectorialEditableAdapter vea = getCadToolAdapter()
143
                                .getVectorialAdapter();
144
                try {
145
                        if (vea.getShapeType() == FShape.POLYGON) {
146
                                GeneralPathX gp = new GeneralPathX();
147
                                gp.append(geometry.getGeneralPathXIterator(), true);
148
                                geometry = ShapeFactory.createPolygon2D(gp);
149
                        }
150
                        int numAttr = getCadToolAdapter().getVectorialAdapter()
151
                                        .getRecordset().getFieldCount();
152
                        Value[] values = new Value[numAttr];
153
                        for (int i = 0; i < numAttr; i++) {
154
                                values[i] = ValueFactory.createNullValue();
155
                        }
156
                        DefaultFeature df = new DefaultFeature(geometry, values);
157
                        vea.addRow(df, getName());
158
                } catch (DriverIOException e) {
159
                        // TODO Auto-generated catch block
160
                        e.printStackTrace();
161
                } catch (IOException e) {
162
                        // TODO Auto-generated catch block
163
                        e.printStackTrace();
164
                } catch (DriverException e) {
165
                        // TODO Auto-generated catch block
166
                        e.printStackTrace();
167
                } catch (DriverLoadException e) {
168
                        // TODO Auto-generated catch block
169
                        e.printStackTrace();
170
                }
171

    
172
                draw(geometry.cloneGeometry());
173
        }
174

    
175
        /**
176
         * DOCUMENT ME!
177
         *
178
         * @param geometry
179
         *            DOCUMENT ME!
180
         */
181
        public void modifyFeature(int index, IFeature row) {
182
                try {
183
                        getCadToolAdapter().getVectorialAdapter().modifyRow(index, row,
184
                                        getName());
185
                } catch (IOException e1) {
186
                        // TODO Auto-generated catch block
187
                        e1.printStackTrace();
188
                } catch (DriverIOException e1) {
189
                        // TODO Auto-generated catch block
190
                        e1.printStackTrace();
191
                }
192
                draw(row.getGeometry().cloneGeometry());
193
        }
194

    
195
        /**
196
         * DOCUMENT ME!
197
         *
198
         * @param geometry
199
         *            DOCUMENT ME!
200
         * @param values
201
         *            DOCUMENT ME!
202
         */
203
        public void addGeometry(IGeometry geometry, Value[] values) {
204
        }
205

    
206
        /**
207
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
208
         *
209
         * @return Cadena para mostrar por consola.
210
         */
211
        public String getQuestion() {
212
                return question;
213
        }
214

    
215
        /**
216
         * Actualiza la cadena que corresponde al estado actual.
217
         *
218
         * @param s
219
         *            Cadena que aparecer? en consola.
220
         */
221
        public void setQuestion(String s) {
222
                question = s;
223
        }
224

    
225
        /**
226
         * DOCUMENT ME!
227
         */
228
        public void refresh() {
229
                getCadToolAdapter().getMapControl().drawMap(false);
230
        }
231

    
232
        /*public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
233
                        throws DriverIOException {
234
                for (int i = sel.nextSetBit(0); i >= 0; i = sel.nextSetBit(i + 1)) {
235
                        IGeometry ig = getCadToolAdapter().getVectorialAdapter()
236
                                        .getShape(i).cloneGeometry();
237
                        if (ig == null)
238
                                continue;
239
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
240
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
241
                }
242
        }
243
*/
244
        public void drawHandlers(Graphics g, ArrayList selectedRows,
245
                        AffineTransform at) {
246
                for (int i = 0; i < selectedRows.size(); i++) {
247
                        IRowEdited edRow = (IRowEdited) selectedRows.get(i);
248
                        IFeature feat = (IFeature) edRow.getLinkedRow();
249
                        // IFeature feat = (IFeature) selectedRows.get(i);
250
                        IGeometry ig = feat.getGeometry().cloneGeometry();
251
                        if (ig == null)
252
                                continue;
253
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
254
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
255
                }
256
        }
257

    
258
        public void setDescription(String[] currentdescriptions) {
259
                this.currentdescriptions = currentdescriptions;
260
        }
261

    
262
        public String[] getDescriptions() {
263
                return currentdescriptions;
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         *
269
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
270
         */
271
        public void end() {
272
                CADExtension.setCADTool("selection");
273
                PluginServices.getMainFrame().setSelectedTool("SELCAD");
274
        }
275

    
276
        public void init() {
277
                CADTool.drawingSymbol.setOutlined(true);
278
                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
279

    
280
        }
281

    
282
}