Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_904 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 10724

History | View | Annotate | Download (12.3 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.FLayer;
77
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
78
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
79
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
80
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
81
import com.iver.cit.gvsig.project.documents.view.gui.View;
82
import com.iver.utiles.console.JConsole;
83

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

    
93
        private CADToolAdapter cadToolAdapter;
94

    
95
        private String question;
96

    
97
        private String[] currentdescriptions;
98

    
99
        private String tool = "selection";
100

    
101
        private DefaultCADTool previousTool;
102

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

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

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

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

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

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

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

    
186

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

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

    
214
                draw(geometry.cloneGeometry());
215
        }
216

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

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

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

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

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

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

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

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

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

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

    
339
        }
340

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

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

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

    
365
        }
366

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

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

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

    
403
        public boolean isApplicable(int shapeType) {
404
                return true;
405
        }
406

    
407
        public abstract String toString();
408

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

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

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

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

    
433
}