Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 5878

History | View | Annotate | Download (11.1 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.ShapeFactory;
66
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
67
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
68
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
69
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
70
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
71
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
72
import com.iver.cit.gvsig.fmap.layers.FBitSet;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.gui.View;
75
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
76
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
77
import com.iver.utiles.console.JConsole;
78

    
79
/**
80
 * DOCUMENT ME!
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public abstract class DefaultCADTool implements CADTool {
85
        private CADToolAdapter cadToolAdapter;
86

    
87
        private String question;
88

    
89
        private String[] currentdescriptions;
90

    
91
        private String tool = "selection";
92

    
93
        /**
94
         * DOCUMENT ME!
95
         */
96
        public void draw(IGeometry geometry) {
97
                if (geometry != null) {
98
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
99
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
100
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
101
                        geometry.draw(gImag, vp, CADTool.drawingSymbol);
102
                }
103
        }
104

    
105
        /**
106
         * DOCUMENT ME!
107
         *
108
         * @param cta
109
         *            DOCUMENT ME!
110
         */
111
        public void setCadToolAdapter(CADToolAdapter cta) {
112
                cadToolAdapter = cta;
113
        }
114

    
115
        /**
116
         * DOCUMENT ME!
117
         *
118
         * @return DOCUMENT ME!
119
         */
120
        public CADToolAdapter getCadToolAdapter() {
121
                return cadToolAdapter;
122
        }
123
        public VectorialLayerEdited getVLE(){
124
                return (VectorialLayerEdited) CADExtension.getEditionManager().getActiveLayerEdited();
125
        }
126

    
127
        /**
128
         * DOCUMENT ME!
129
         *
130
         * @param g
131
         *            DOCUMENT ME!
132
         * @param firstPoint
133
         *            DOCUMENT ME!
134
         * @param endPoint
135
         *            DOCUMENT ME!
136
         */
137
        public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint) {
138
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
139
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
140
                elShape.lineTo(endPoint.getX(), endPoint.getY());
141
                ShapeFactory.createPolyline2D(elShape).draw(g,
142
                                getCadToolAdapter().getMapControl().getViewPort(),
143
                                CADTool.drawingSymbol);
144
        }
145

    
146
        /**
147
         * DOCUMENT ME!
148
         *
149
         * @param geometry
150
         *            DOCUMENT ME!
151
         */
152
        public void addGeometry(IGeometry geometry) {
153
                VectorialEditableAdapter vea = getVLE().getVEA();
154
                try {
155
                        int numAttr = vea.getRecordset().getFieldCount();
156
                        Value[] values = new Value[numAttr];
157
                        for (int i = 0; i < numAttr; i++) {
158
                                values[i] = ValueFactory.createNullValue();
159
                        }
160
                        int num=vea.getRowCount();
161
                        DefaultFeature df = new DefaultFeature(geometry, values,String.valueOf(num));
162
                        int index = vea.addRow(df, getName(),EditionEvent.GRAPHIC);
163

    
164

    
165
                        VectorialLayerEdited vle = getVLE();
166
                        ArrayList selectedHandler = vle.getSelectedHandler();
167
                        ArrayList selectedRow = vle.getSelectedRow();
168
                        selectedHandler.clear();
169
                        selectedRow.clear();
170

    
171
                        ViewPort vp=vle.getLayer().getFMap().getViewPort();
172
                        BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
173
                        Graphics2D gs = selectionImage.createGraphics();
174
                        selectedRow.add(new DefaultRowEdited(df, IRowEdited.STATUS_ADDED, index));
175
                        IGeometry geom=df.getGeometry();
176
                        geom.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol);
177
                        vle.drawHandlers(geom.cloneGeometry(),gs,vp);
178
                        vea.setSelectionImage(selectionImage);
179
                } catch (DriverIOException e) {
180
                        e.printStackTrace();
181
                } catch (IOException e) {
182
                        e.printStackTrace();
183
                } catch (DriverException e) {
184
                        e.printStackTrace();
185
                } catch (DriverLoadException e) {
186
                        e.printStackTrace();
187
                }
188

    
189
                draw(geometry.cloneGeometry());
190
        }
191

    
192
        /**
193
         * DOCUMENT ME!
194
         *
195
         * @param geometry
196
         *            DOCUMENT ME!
197
         */
198
        public void modifyFeature(int index, IFeature row) {
199
                try {
200
                        getVLE().getVEA().modifyRow(index, row,
201
                                        getName(),EditionEvent.GRAPHIC);
202
                } catch (IOException e1) {
203
                        e1.printStackTrace();
204
                } catch (DriverIOException e1) {
205
                        e1.printStackTrace();
206
                }
207
                draw(row.getGeometry().cloneGeometry());
208
        }
209

    
210
        /**
211
         * DOCUMENT ME!
212
         *
213
         * @param geometry
214
         *            DOCUMENT ME!
215
         * @param values
216
         *            DOCUMENT ME!
217
         */
218
        public int addGeometry(IGeometry geometry, Value[] values) {
219
                int index =0;
220
                VectorialEditableAdapter vea = getVLE().getVEA();
221
                try {
222
                        int num=vea.getRowCount();
223
                        DefaultFeature df = new DefaultFeature(geometry, values,String.valueOf(num));
224
                        index = vea.addRow(df, getName(),EditionEvent.GRAPHIC);
225
                } catch (DriverIOException e) {
226
                        e.printStackTrace();
227
                } catch (IOException e) {
228
                        e.printStackTrace();
229
                } catch (DriverLoadException e) {
230
                        e.printStackTrace();
231
                }
232
                return index;
233
        }
234

    
235
        /**
236
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
237
         *
238
         * @return Cadena para mostrar por consola.
239
         */
240
        public String getQuestion() {
241
                return question;
242
        }
243

    
244
        /**
245
         * Actualiza la cadena que corresponde al estado actual.
246
         *
247
         * @param s
248
         *            Cadena que aparecer? en consola.
249
         */
250
        public void setQuestion(String s) {
251
                question = s;
252
                //ConsoleToken.addQuestion(s);
253
        }
254

    
255
        /**
256
         * DOCUMENT ME!
257
         */
258
        public void refresh() {
259
                getCadToolAdapter().getMapControl().drawMap(false);
260
        }
261

    
262
        /*public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
263
                        throws DriverIOException {
264
                for (int i = sel.nextSetBit(0); i >= 0; i = sel.nextSetBit(i + 1)) {
265
                        IGeometry ig = getCadToolAdapter().getVectorialAdapter()
266
                                        .getShape(i).cloneGeometry();
267
                        if (ig == null)
268
                                continue;
269
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
270
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
271
                }
272
        }
273
*/
274
        public void drawHandlers(Graphics g, ArrayList selectedRows,
275
                        AffineTransform at) {
276
                for (int i = 0; i < selectedRows.size(); i++) {
277
                        IRowEdited edRow = (IRowEdited) selectedRows.get(i);
278
                        IFeature feat = (IFeature) edRow.getLinkedRow();
279
                        // IFeature feat = (IFeature) selectedRows.get(i);
280
                        IGeometry ig = feat.getGeometry().cloneGeometry();
281
                        if (ig == null)
282
                                continue;
283
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
284
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
285
                }
286
        }
287

    
288
        public void setDescription(String[] currentdescriptions) {
289
                this.currentdescriptions = currentdescriptions;
290
        }
291

    
292
        public String[] getDescriptions() {
293
                return currentdescriptions;
294
        }
295

    
296
        /*
297
         * (non-Javadoc)
298
         *
299
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
300
         */
301
        public void end() {
302
                CADExtension.setCADTool("_selection",true);
303
                PluginServices.getMainFrame().setSelectedTool("_selection");
304
        }
305

    
306
        public void init() {
307
                CADTool.drawingSymbol.setOutlined(true);
308
                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
309

    
310
        }
311
        protected ArrayList getSelectedRows(){
312
                VectorialLayerEdited vle = getVLE();
313
            ArrayList selectedRow = vle.getSelectedRow();
314
            return selectedRow;
315
        }
316
        protected ArrayList getSelectedHandlers(){
317
                VectorialLayerEdited vle = getVLE();
318
            ArrayList selectedHandlers = vle.getSelectedHandler();
319
            return selectedHandlers;
320
        }
321
        public void clearSelection(){
322
                VectorialLayerEdited vle = getVLE();
323
                ArrayList selectedRow = vle.getSelectedRow();
324
            ArrayList selectedHandlers = vle.getSelectedHandler();
325
            selectedRow.clear();
326
            selectedHandlers.clear();
327
            VectorialEditableAdapter vea=vle.getVEA();
328
            FBitSet selection = vea.getSelection();
329
            selection.clear();
330
            vea.setSelectionImage(null);
331
            vea.setHandlersImage(null);
332

    
333
        }
334
        public String getNextTool() {
335
                return tool;
336
        }
337

    
338
        public void setNextTool(String tool) {
339
                this.tool = tool;
340
        }
341
        public boolean changeCommand(String name)throws CommandException{
342
                CADTool[] cadtools=CADExtension.getCADTools();
343
                for (int i=0;i<cadtools.length;i++){
344
                        CADTool ct=cadtools[i];
345
                        if (name.equalsIgnoreCase(ct.getName())|| name.equalsIgnoreCase(ct.toString())){
346
                                int type=FShape.POINT;
347
                                try {
348
                                        type=((FLyrVect)getVLE().getLayer()).getShapeType();
349
                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
350
                                        e.printStackTrace();
351
                                }
352
                                if (ct.isApplicable(type)) {
353
                                        getCadToolAdapter().setCadTool(ct);
354
                                        ct.init();
355
                                        View vista = (View) PluginServices.getMDIManager()
356
                                                        .getActiveView();
357
                                        vista.getConsolePanel().addText("\n" + ct.getName(),
358
                                                        JConsole.COMMAND);
359
                                        return true;
360
                                }else{
361
                                        throw new CommandException(name);
362
                                }
363
                        }
364
                }
365
                return false;
366
        }
367
        public boolean isApplicable(int shapeType) {
368
                return true;
369
        }
370
        public abstract String toString();
371

    
372
        public void throwValueException(String s,double d) {
373
                View vista = (View) PluginServices.getMDIManager()
374
                        .getActiveView();
375
                vista.getConsolePanel().addText(s+ " : " + d, JConsole.ERROR);
376
        }
377
        public void throwOptionException(String s,String o) {
378
                View vista = (View) PluginServices.getMDIManager()
379
                        .getActiveView();
380
                vista.getConsolePanel().addText(s+ " : " + o, JConsole.ERROR);
381
        }
382
        public void throwPointException(String s,double x,double y) {
383
                View vista = (View) PluginServices.getMDIManager()
384
                        .getActiveView();
385
                vista.getConsolePanel().addText(s+ " : " + " X = "+x+ ", Y = "+y, JConsole.ERROR);
386
        }
387

    
388
}