Statistics
| Revision:

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

History | View | Annotate | Download (11.9 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 org.apache.log4j.Logger;
53

    
54
import com.hardcode.driverManager.DriverLoadException;
55
import com.hardcode.gdbms.engine.data.driver.DriverException;
56
import com.hardcode.gdbms.engine.values.Value;
57
import com.hardcode.gdbms.engine.values.ValueFactory;
58
import com.iver.andami.PluginServices;
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.cit.gvsig.CADExtension;
61
import com.iver.cit.gvsig.fmap.ViewPort;
62
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
65
import com.iver.cit.gvsig.fmap.core.Handler;
66
import com.iver.cit.gvsig.fmap.core.IFeature;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
69
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
70
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
71
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
72
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
73
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
74
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
75
import com.iver.cit.gvsig.fmap.layers.FBitSet;
76
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
77
import com.iver.cit.gvsig.gui.View;
78
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
79
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
80
import com.iver.utiles.console.JConsole;
81

    
82
/**
83
 * DOCUMENT ME!
84
 *
85
 * @author Vicente Caballero Navarro
86
 */
87
public abstract class DefaultCADTool implements CADTool {
88
        private static Logger logger = Logger.getLogger(DefaultCADTool.class
89
                        .getName());
90

    
91
        private CADToolAdapter cadToolAdapter;
92

    
93
        private String question;
94

    
95
        private String[] currentdescriptions;
96

    
97
        private String tool = "selection";
98

    
99
        /**
100
         * DOCUMENT ME!
101
         */
102
        public void draw(IGeometry geometry) {
103
                if (geometry != null) {
104
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
105
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
106
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
107
                        geometry.draw(gImag, vp, CADTool.drawingSymbol);
108
                }
109
        }
110

    
111
        /**
112
         * DOCUMENT ME!
113
         *
114
         * @param cta
115
         *            DOCUMENT ME!
116
         */
117
        public void setCadToolAdapter(CADToolAdapter cta) {
118
                cadToolAdapter = cta;
119
        }
120

    
121
        /**
122
         * DOCUMENT ME!
123
         *
124
         * @return DOCUMENT ME!
125
         */
126
        public CADToolAdapter getCadToolAdapter() {
127
                return cadToolAdapter;
128
        }
129

    
130
        public VectorialLayerEdited getVLE() {
131
                return (VectorialLayerEdited) CADExtension.getEditionManager()
132
                                .getActiveLayerEdited();
133
        }
134

    
135
        /**
136
         * DOCUMENT ME!
137
         *
138
         * @param g
139
         *            DOCUMENT ME!
140
         * @param firstPoint
141
         *            DOCUMENT ME!
142
         * @param endPoint
143
         *            DOCUMENT ME!
144
         */
145
        public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint) {
146
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
147
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
148
                elShape.lineTo(endPoint.getX(), endPoint.getY());
149
                ShapeFactory.createPolyline2D(elShape).draw(g,
150
                                getCadToolAdapter().getMapControl().getViewPort(),
151
                                CADTool.drawingSymbol);
152
        }
153

    
154
        /**
155
         * DOCUMENT ME!
156
         *
157
         * @param geometry
158
         *            DOCUMENT ME!
159
         */
160
        public void addGeometry(IGeometry geometry) {
161
                VectorialEditableAdapter vea = getVLE().getVEA();
162
                try {
163
                        // Deber?amos comprobar que lo que escribimos es correcto:
164
                        // Lo hacemos en el VectorialAdapter, justo antes de
165
                        // a?adir, borrar o modificar una feature
166

    
167
                        int numAttr = vea.getRecordset().getFieldCount();
168
                        Value[] values = new Value[numAttr];
169
                        for (int i = 0; i < numAttr; i++) {
170
                                values[i] = ValueFactory.createNullValue();
171
                        }
172
                        int num;
173
                        try {
174
                                num = vea.getRowCount();
175
                                DefaultFeature df = new DefaultFeature(geometry, values, String
176
                                                .valueOf(num));
177
                                int index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
178
                                VectorialLayerEdited vle = getVLE();
179
                                clearSelection();
180
                                ArrayList selectedRow = vle.getSelectedRow();
181

    
182

    
183
                                ViewPort vp = vle.getLayer().getFMap().getViewPort();
184
                                BufferedImage selectionImage = new BufferedImage(vp
185
                                                .getImageWidth(), vp.getImageHeight(),
186
                                                BufferedImage.TYPE_INT_ARGB);
187
                                Graphics2D gs = selectionImage.createGraphics();
188
                                int inversedIndex=vea.getInversedIndex(index);
189
                                selectedRow.add(new DefaultRowEdited(df,
190
                                                IRowEdited.STATUS_ADDED, inversedIndex ));
191
                                vea.getSelection().set(inversedIndex);
192
                                IGeometry geom = df.getGeometry();
193
                                geom.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol);
194
                                vle.drawHandlers(geom.cloneGeometry(), gs, vp);
195
                                vea.setSelectionImage(selectionImage);
196
                        } catch (IOException e) {
197
                                logger.debug(e);
198
                                NotificationManager.addError(e);
199
                                return;
200
                        }
201

    
202
                } catch (DriverIOException e) {
203
                        e.printStackTrace();
204
                } catch (DriverException e) {
205
                        e.printStackTrace();
206
                } catch (DriverLoadException e) {
207
                        e.printStackTrace();
208
                }
209

    
210
                draw(geometry.cloneGeometry());
211
        }
212

    
213
        /**
214
         * DOCUMENT ME!
215
         *
216
         * @param geometry
217
         *            DOCUMENT ME!
218
         */
219
        public void modifyFeature(int index, IFeature row) {
220
                try {
221
                        getVLE().getVEA().modifyRow(index, row, getName(),
222
                                        EditionEvent.GRAPHIC);
223
                } catch (IOException e1) {
224
                        e1.printStackTrace();
225
                } catch (DriverIOException e1) {
226
                        e1.printStackTrace();
227
                }
228
                draw(row.getGeometry().cloneGeometry());
229
        }
230

    
231
        /**
232
         * DOCUMENT ME!
233
         *
234
         * @param geometry
235
         *            DOCUMENT ME!
236
         * @param values
237
         *            DOCUMENT ME!
238
         */
239
        public int addGeometry(IGeometry geometry, Value[] values) {
240
                int index = 0;
241
                VectorialEditableAdapter vea = getVLE().getVEA();
242
                try {
243
                        int num = vea.getRowCount();
244
                        DefaultFeature df = new DefaultFeature(geometry, values, String
245
                                        .valueOf(num));
246
                        index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
247
                } catch (DriverIOException e) {
248
                        e.printStackTrace();
249
                } catch (IOException e) {
250
                        e.printStackTrace();
251
                } catch (DriverLoadException e) {
252
                        e.printStackTrace();
253
                }
254
                return vea.getInversedIndex(index);
255
        }
256

    
257
        /**
258
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
259
         *
260
         * @return Cadena para mostrar por consola.
261
         */
262
        public String getQuestion() {
263
                return question;
264
        }
265

    
266
        /**
267
         * Actualiza la cadena que corresponde al estado actual.
268
         *
269
         * @param s
270
         *            Cadena que aparecer? en consola.
271
         */
272
        public void setQuestion(String s) {
273
                question = s;
274
                // ConsoleToken.addQuestion(s);
275
        }
276

    
277
        /**
278
         * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
279
         * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
280
         * edici?n y las que est?n por encima de ella en el TOC.
281
         */
282
        public void refresh() {
283
                // getCadToolAdapter().getMapControl().drawMap(false);
284
                getVLE().getLayer().setDirty(true);
285

    
286
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
287
        }
288

    
289
        /*
290
         * public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
291
         * throws DriverIOException { for (int i = sel.nextSetBit(0); i >= 0; i =
292
         * sel.nextSetBit(i + 1)) { IGeometry ig =
293
         * getCadToolAdapter().getVectorialAdapter() .getShape(i).cloneGeometry();
294
         * if (ig == null) continue; Handler[] handlers =
295
         * ig.getHandlers(IGeometry.SELECTHANDLER);
296
         * FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers); } }
297
         */
298
        public void drawHandlers(Graphics g, ArrayList selectedRows,
299
                        AffineTransform at) {
300
                for (int i = 0; i < selectedRows.size(); i++) {
301
                        IRowEdited edRow = (IRowEdited) selectedRows.get(i);
302
                        IFeature feat = (IFeature) edRow.getLinkedRow();
303
                        // IFeature feat = (IFeature) selectedRows.get(i);
304
                        IGeometry ig = feat.getGeometry().cloneGeometry();
305
                        if (ig == null)
306
                                continue;
307
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
308
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
309
                }
310
        }
311

    
312
        public void setDescription(String[] currentdescriptions) {
313
                this.currentdescriptions = currentdescriptions;
314
        }
315

    
316
        public String[] getDescriptions() {
317
                return currentdescriptions;
318
        }
319

    
320
        /*
321
         * (non-Javadoc)
322
         *
323
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
324
         */
325
        public void end() {
326
                CADExtension.setCADTool("_selection", true);
327
                PluginServices.getMainFrame().setSelectedTool("_selection");
328
        }
329

    
330
        public void init() {
331
                CADTool.drawingSymbol.setOutlined(true);
332
                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
333

    
334
        }
335

    
336
        protected ArrayList getSelectedRows() {
337
                VectorialLayerEdited vle = getVLE();
338
                ArrayList selectedRow = vle.getSelectedRow();
339
                return selectedRow;
340
        }
341

    
342
        protected ArrayList getSelectedHandlers() {
343
                VectorialLayerEdited vle = getVLE();
344
                ArrayList selectedHandlers = vle.getSelectedHandler();
345
                return selectedHandlers;
346
        }
347

    
348
        public void clearSelection() {
349
                VectorialLayerEdited vle = getVLE();
350
                ArrayList selectedRow = vle.getSelectedRow();
351
                ArrayList selectedHandlers = vle.getSelectedHandler();
352
                selectedRow.clear();
353
                selectedHandlers.clear();
354
                VectorialEditableAdapter vea = vle.getVEA();
355
                FBitSet selection = vea.getSelection();
356
                selection.clear();
357
                vea.setSelectionImage(null);
358
                vea.setHandlersImage(null);
359

    
360
        }
361

    
362
        public String getNextTool() {
363
                return tool;
364
        }
365

    
366
        public void setNextTool(String tool) {
367
                this.tool = tool;
368
        }
369

    
370
        public boolean changeCommand(String name) throws CommandException {
371
                CADTool[] cadtools = CADExtension.getCADTools();
372
                for (int i = 0; i < cadtools.length; i++) {
373
                        CADTool ct = cadtools[i];
374
                        if (name.equalsIgnoreCase(ct.getName())
375
                                        || name.equalsIgnoreCase(ct.toString())) {
376
                                int type = FShape.POINT;
377
                                try {
378
                                        type = ((FLyrVect) getVLE().getLayer()).getShapeType();
379
                                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
380
                                        e.printStackTrace();
381
                                }
382
                                if (ct.isApplicable(type)) {
383
                                        getCadToolAdapter().setCadTool(ct);
384
                                        ct.init();
385
                                        View vista = (View) PluginServices.getMDIManager()
386
                                                        .getActiveView();
387
                                        vista.getConsolePanel().addText("\n" + ct.getName(),
388
                                                        JConsole.COMMAND);
389
                                        return true;
390
                                } else {
391
                                        throw new CommandException(name);
392
                                }
393
                        }
394
                }
395
                return false;
396
        }
397

    
398
        public boolean isApplicable(int shapeType) {
399
                return true;
400
        }
401

    
402
        public abstract String toString();
403

    
404
        public void throwValueException(String s, double d) {
405
                View vista = (View) PluginServices.getMDIManager().getActiveView();
406
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
407
        }
408

    
409
        public void throwOptionException(String s, String o) {
410
                View vista = (View) PluginServices.getMDIManager().getActiveView();
411
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
412
        }
413

    
414
        public void throwPointException(String s, double x, double y) {
415
                View vista = (View) PluginServices.getMDIManager().getActiveView();
416
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
417
                                JConsole.ERROR);
418
        }
419

    
420
}