Statistics
| Revision:

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

History | View | Annotate | Download (12.4 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.cad.exception.CommandException;
78
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
79
import com.iver.cit.gvsig.project.documents.view.gui.View;
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
        private DefaultCADTool previousTool;
100

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

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

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

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

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

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

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

    
184

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

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

    
212
                draw(geometry.cloneGeometry());
213
        }
214

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

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

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

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

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

    
288
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
289
        }
290

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

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

    
318
        public String[] getDescriptions() {
319
                return currentdescriptions;
320
        }
321

    
322
        /*
323
         * (non-Javadoc)
324
         *
325
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
326
         */
327
        public void end() {
328
                CADExtension.setCADTool("_selection", true);
329
                PluginServices.getMainFrame().setSelectedTool("_selection");
330
                CADTool cadtool=CADExtension.getCADTool();
331
                cadtool.setPreviosTool(this);
332
        }
333

    
334
        public void init() {
335
                CADTool.drawingSymbol.setOutlined(true);
336
                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
337

    
338
        }
339

    
340
        protected ArrayList getSelectedRows() {
341
                VectorialLayerEdited vle = getVLE();
342
                ArrayList selectedRow = vle.getSelectedRow();
343
                return selectedRow;
344
        }
345

    
346
        protected ArrayList getSelectedHandlers() {
347
                VectorialLayerEdited vle = getVLE();
348
                ArrayList selectedHandlers = vle.getSelectedHandler();
349
                return selectedHandlers;
350
        }
351

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

    
364
        }
365

    
366
        public String getNextTool() {
367
                return tool;
368
        }
369

    
370
        public void setNextTool(String tool) {
371
                this.tool = tool;
372
        }
373

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

    
405
        public boolean isApplicable(int shapeType) {
406
                return true;
407
        }
408

    
409
        public abstract String toString();
410

    
411
        public void throwValueException(String s, double d) {
412
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
413
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
414
        }
415

    
416
        public void throwOptionException(String s, String o) {
417
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
418
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
419
        }
420

    
421
        public void throwPointException(String s, double x, double y) {
422
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
423
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
424
                                JConsole.ERROR);
425
        }
426

    
427
        public void setPreviosTool(DefaultCADTool tool) {
428
                previousTool=tool;
429
        }
430
        public void restorePreviousTool() {
431
                CADExtension.setCADTool(previousTool.toString(), true);
432
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
433
        }
434

    
435
}