Statistics
| Revision:

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

History | View | Annotate | Download (13 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.util.ArrayList;
50

    
51
import org.apache.log4j.Logger;
52

    
53
import com.hardcode.driverManager.DriverLoadException;
54
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
55
import com.hardcode.gdbms.engine.values.Value;
56
import com.hardcode.gdbms.engine.values.ValueFactory;
57
import com.iver.andami.PluginServices;
58
import com.iver.cit.gvsig.CADExtension;
59
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
60
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
61
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
62
import com.iver.cit.gvsig.fmap.ViewPort;
63
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
66
import com.iver.cit.gvsig.fmap.core.Handler;
67
import com.iver.cit.gvsig.fmap.core.IFeature;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
70
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
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
                                num = vea.getRowCount();
176
                                DefaultFeature df = new DefaultFeature(geometry, values, String
177
                                                .valueOf(num));
178
                                int index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
179
                                VectorialLayerEdited vle = getVLE();
180
                                clearSelection();
181
                                //ArrayList selectedRow = vle.getSelectedRow();
182

    
183

    
184
                                ViewPort vp = vle.getLayer().getMapContext().getViewPort();
185
                                BufferedImage selectionImage = new BufferedImage(vp
186
                                                .getImageWidth(), vp.getImageHeight(),
187
                                                BufferedImage.TYPE_INT_ARGB);
188
                                Graphics2D gs = selectionImage.createGraphics();
189
                                int inversedIndex=vea.getInversedIndex(index);
190
                                vle.addSelectionCache(new DefaultRowEdited(df,
191
                                                IRowEdited.STATUS_ADDED, inversedIndex ));
192
                                vea.getSelection().set(inversedIndex);
193
                                IGeometry geom = df.getGeometry();
194
                                geom.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol);
195
                                vle.drawHandlers(geom.cloneGeometry(), gs, vp);
196
                                vea.setSelectionImage(selectionImage);
197

    
198

    
199
                } catch (DriverLoadException e) {
200
                        e.printStackTrace();
201
                        return;
202
                } catch (ReadDriverException e) {
203
                        e.printStackTrace();
204
                        return;
205
                } catch (ValidateRowException e) {
206
                        e.printStackTrace();
207
                        return;
208
                } catch (ExpansionFileWriteException e) {
209
                        e.printStackTrace();
210
                        return;
211
                }
212

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

    
216
        /**
217
         * DOCUMENT ME!
218
         *
219
         * @param geometry
220
         *            DOCUMENT ME!
221
         */
222
        public void modifyFeature(int index, IFeature row) {
223
                try {
224
                        getVLE().getVEA().modifyRow(index, row, getName(),
225
                                        EditionEvent.GRAPHIC);
226
                } catch (ValidateRowException e) {
227
                        // TODO Auto-generated catch block
228
                        e.printStackTrace();
229
                } catch (ExpansionFileWriteException e) {
230
                        // TODO Auto-generated catch block
231
                        e.printStackTrace();
232
                } catch (ReadDriverException e) {
233
                        // TODO Auto-generated catch block
234
                        e.printStackTrace();
235
                } catch (ExpansionFileReadException e) {
236
                        // TODO Auto-generated catch block
237
                        e.printStackTrace();
238
                }
239
                draw(row.getGeometry().cloneGeometry());
240
        }
241

    
242
        /**
243
         * DOCUMENT ME!
244
         *
245
         * @param geometry
246
         *            DOCUMENT ME!
247
         * @param values
248
         *            DOCUMENT ME!
249
         */
250
        public int addGeometry(IGeometry geometry, Value[] values) {
251
                int index = 0;
252
                VectorialEditableAdapter vea = getVLE().getVEA();
253
                try {
254
                        int num = vea.getRowCount();
255
                        DefaultFeature df = new DefaultFeature(geometry, values, String
256
                                        .valueOf(num));
257
                        index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
258
                } catch (DriverLoadException e) {
259
                        e.printStackTrace();
260
                } catch (ValidateRowException e) {
261
                        // TODO Auto-generated catch block
262
                        e.printStackTrace();
263
                } catch (ReadDriverException e) {
264
                        // TODO Auto-generated catch block
265
                        e.printStackTrace();
266
                } catch (ExpansionFileWriteException e) {
267
                        // TODO Auto-generated catch block
268
                        e.printStackTrace();
269
                }
270
                return vea.getInversedIndex(index);
271
        }
272

    
273
        /**
274
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
275
         *
276
         * @return Cadena para mostrar por consola.
277
         */
278
        public String getQuestion() {
279
                return question;
280
        }
281

    
282
        /**
283
         * Actualiza la cadena que corresponde al estado actual.
284
         *
285
         * @param s
286
         *            Cadena que aparecer? en consola.
287
         */
288
        public void setQuestion(String s) {
289
                question = s;
290
                // ConsoleToken.addQuestion(s);
291
        }
292

    
293
        /**
294
         * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
295
         * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
296
         * edici?n y las que est?n por encima de ella en el TOC.
297
         */
298
        public void refresh() {
299
                // getCadToolAdapter().getMapControl().drawMap(false);
300
                getVLE().getLayer().setDirty(true);
301

    
302
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
303
        }
304

    
305
        /*
306
         * public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
307
         * throws DriverIOException { for (int i = sel.nextSetBit(0); i >= 0; i =
308
         * sel.nextSetBit(i + 1)) { IGeometry ig =
309
         * getCadToolAdapter().getVectorialAdapter() .getShape(i).cloneGeometry();
310
         * if (ig == null) continue; Handler[] handlers =
311
         * ig.getHandlers(IGeometry.SELECTHANDLER);
312
         * FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers); } }
313
         */
314
        public void drawHandlers(Graphics g, ArrayList selectedRows,
315
                        AffineTransform at) {
316
                for (int i = 0; i < selectedRows.size(); i++) {
317
                        IRowEdited edRow = (IRowEdited) selectedRows.get(i);
318
                        IFeature feat = (IFeature) edRow.getLinkedRow();
319
                        // IFeature feat = (IFeature) selectedRows.get(i);
320
                        IGeometry ig = feat.getGeometry().cloneGeometry();
321
                        if (ig == null)
322
                                continue;
323
                        Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER);
324
                        FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
325
                }
326
        }
327

    
328
        public void setDescription(String[] currentdescriptions) {
329
                this.currentdescriptions = currentdescriptions;
330
        }
331

    
332
        public String[] getDescriptions() {
333
                return currentdescriptions;
334
        }
335

    
336
        /*
337
         * (non-Javadoc)
338
         *
339
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
340
         */
341
        public void end() {
342
                CADExtension.setCADTool("_selection", true);
343
                PluginServices.getMainFrame().setSelectedTool("_selection");
344
                CADTool cadtool=CADExtension.getCADTool();
345
                cadtool.setPreviosTool(this);
346
        }
347

    
348
        public void init() {
349
                CADTool.drawingSymbol.setOutlined(true);
350
                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
351

    
352
        }
353

    
354
        protected ArrayList getSelectedRows() {
355
                VectorialLayerEdited vle = getVLE();
356
                ArrayList selectedRow = vle.getSelectedRow();
357
                return selectedRow;
358
        }
359

    
360
        protected ArrayList getSelectedHandlers() {
361
                VectorialLayerEdited vle = getVLE();
362
                ArrayList selectedHandlers = vle.getSelectedHandler();
363
                return selectedHandlers;
364
        }
365

    
366
        public void clearSelection() throws ReadDriverException {
367
                VectorialLayerEdited vle = getVLE();
368
                ArrayList selectedRow = vle.getSelectedRow();
369
                ArrayList selectedHandlers = vle.getSelectedHandler();
370
                selectedRow.clear();
371
                selectedHandlers.clear();
372
                VectorialEditableAdapter vea = vle.getVEA();
373
                FBitSet selection = vea.getSelection();
374
                selection.clear();
375
//                vea.setSelectionImage(null);
376
//                vea.setHandlersImage(null);
377

    
378
        }
379

    
380
        public String getNextTool() {
381
                return tool;
382
        }
383

    
384
        public void setNextTool(String tool) {
385
                this.tool = tool;
386
        }
387

    
388
        public boolean changeCommand(String name) throws CommandException {
389
                CADTool[] cadtools = CADExtension.getCADTools();
390
                for (int i = 0; i < cadtools.length; i++) {
391
                        CADTool ct = cadtools[i];
392
                        if (name.equalsIgnoreCase(ct.getName())
393
                                        || name.equalsIgnoreCase(ct.toString())) {
394
                                int type = FShape.POINT;
395
                                try {
396
                                        type = ((FLyrVect) getVLE().getLayer()).getShapeType();
397
                                } catch (ReadDriverException e) {
398
                                        e.printStackTrace();
399
                                }
400
                                if (ct.isApplicable(type)) {
401
                                        getCadToolAdapter().setCadTool(ct);
402
                                        ct.init();
403
                                        View vista = (View) PluginServices.getMDIManager()
404
                                                        .getActiveWindow();
405
                                        vista.getConsolePanel().addText("\n" + ct.getName(),
406
                                                        JConsole.COMMAND);
407
                                        String question=ct.getQuestion();
408
                                        vista.getConsolePanel().addText(
409
                                                        "\n" + "#" + question + " > ", JConsole.MESSAGE);
410
                                        return true;
411
                                }
412
                                throw new CommandException(name);
413
                        }
414
                }
415
                return false;
416
        }
417

    
418
        public boolean isApplicable(int shapeType) {
419
                return true;
420
        }
421

    
422
        public abstract String toString();
423

    
424
        public void throwValueException(String s, double d) {
425
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
426
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
427
        }
428

    
429
        public void throwOptionException(String s, String o) {
430
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
431
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
432
        }
433

    
434
        public void throwPointException(String s, double x, double y) {
435
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
436
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
437
                                JConsole.ERROR);
438
        }
439

    
440
        public void setPreviosTool(DefaultCADTool tool) {
441
                previousTool=tool;
442
        }
443
        public void restorePreviousTool() {
444
                CADExtension.setCADTool(previousTool.toString(), true);
445
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
446
        }
447

    
448
}