Statistics
| Revision:

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

History | View | Annotate | Download (13.7 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.SymbologyFactory;
71
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
72
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
73
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
74
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
75
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
76
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
77
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
78
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
79
import com.iver.cit.gvsig.fmap.layers.FBitSet;
80
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
81
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
82
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
83
import com.iver.cit.gvsig.project.documents.view.gui.View;
84
import com.iver.utiles.console.JConsole;
85

    
86
/**
87
 * DOCUMENT ME!
88
 *
89
 * @author Vicente Caballero Navarro
90
 */
91
public abstract class DefaultCADTool implements CADTool {
92
        public static ISymbol selectionSymbol = SymbologyFactory.
93
        createDefaultSymbolByShapeType(FShape.MULTI, new Color(255, 0,0, 100)); // Le ponemos una transparencia
94
        public static ISymbol axisReferencesSymbol = SymbologyFactory.
95
        createDefaultSymbolByShapeType(FShape.MULTI, new Color(100, 100, 100, 100));
96
        public static ISymbol geometrySelectSymbol = SymbologyFactory.
97
        createDefaultSymbolByShapeType(FShape.MULTI, Color.RED);
98
        public static ISymbol handlerSymbol = SymbologyFactory.
99
        createDefaultSymbolByShapeType(FShape.MULTI, Color.ORANGE);
100
        private static Logger logger = Logger.getLogger(DefaultCADTool.class
101
                        .getName());
102

    
103

    
104
        private CADToolAdapter cadToolAdapter;
105

    
106
        private String question;
107

    
108
        private String[] currentdescriptions;
109

    
110
        private String tool = "selection";
111

    
112
        private DefaultCADTool previousTool;
113

    
114
        /**
115
         * DOCUMENT ME!
116
         */
117
        public void draw(IGeometry geometry) {
118
                if (geometry != null) {
119
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
120
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
121
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
122
                        geometry.draw(gImag, vp, DefaultCADTool.selectionSymbol);
123
                }
124
        }
125

    
126
        /**
127
         * DOCUMENT ME!
128
         *
129
         * @param cta
130
         *            DOCUMENT ME!
131
         */
132
        public void setCadToolAdapter(CADToolAdapter cta) {
133
                cadToolAdapter = cta;
134
        }
135

    
136
        /**
137
         * DOCUMENT ME!
138
         *
139
         * @return DOCUMENT ME!
140
         */
141
        public CADToolAdapter getCadToolAdapter() {
142
                return cadToolAdapter;
143
        }
144

    
145
        public VectorialLayerEdited getVLE() {
146
                return (VectorialLayerEdited) CADExtension.getEditionManager()
147
                                .getActiveLayerEdited();
148
        }
149

    
150
        /**
151
         * DOCUMENT ME!
152
         *
153
         * @param g
154
         *            DOCUMENT ME!
155
         * @param firstPoint
156
         *            DOCUMENT ME!
157
         * @param endPoint
158
         *            DOCUMENT ME!
159
         */
160
        public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint, ISymbol symbol) {
161
                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
162
                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
163
                elShape.lineTo(endPoint.getX(), endPoint.getY());
164
                ShapeFactory.createPolyline2D(elShape).draw(g,
165
                                getCadToolAdapter().getMapControl().getViewPort(),
166
                                        symbol);
167
        }
168

    
169
        /**
170
         * DOCUMENT ME!
171
         *
172
         * @param geometry
173
         *            DOCUMENT ME!
174
         */
175
        public void addGeometry(IGeometry geometry) {
176
                VectorialEditableAdapter vea = getVLE().getVEA();
177
                try {
178
                        // Deber?amos comprobar que lo que escribimos es correcto:
179
                        // Lo hacemos en el VectorialAdapter, justo antes de
180
                        // a?adir, borrar o modificar una feature
181

    
182
                        int numAttr = vea.getRecordset().getFieldCount();
183
                        Value[] values = new Value[numAttr];
184
                        for (int i = 0; i < numAttr; i++) {
185
                                values[i] = ValueFactory.createNullValue();
186
                        }
187
                        int num;
188
                                num = vea.getRowCount();
189
                                DefaultFeature df = new DefaultFeature(geometry, values, String
190
                                                .valueOf(num));
191
                                int index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
192
                                VectorialLayerEdited vle = getVLE();
193
                                clearSelection();
194
                                //ArrayList selectedRow = vle.getSelectedRow();
195

    
196

    
197
                                ViewPort vp = vle.getLayer().getMapContext().getViewPort();
198
                                BufferedImage selectionImage = new BufferedImage(vp
199
                                                .getImageWidth(), vp.getImageHeight(),
200
                                                BufferedImage.TYPE_INT_ARGB);
201
                                Graphics2D gs = selectionImage.createGraphics();
202
                                int inversedIndex=vea.getInversedIndex(index);
203
                                vle.addSelectionCache(new DefaultRowEdited(df,
204
                                                IRowEdited.STATUS_ADDED, inversedIndex ));
205
                                vea.getSelection().set(inversedIndex);
206
                                IGeometry geom = df.getGeometry();
207
                                        geom.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
208
                                vle.drawHandlers(geom.cloneGeometry(), gs, vp);
209
                                vea.setSelectionImage(selectionImage);
210

    
211

    
212
                } catch (ReadDriverException e) {
213
                        e.printStackTrace();
214
                        return;
215
                } catch (ValidateRowException e) {
216
                        e.printStackTrace();
217
                        return;
218
                } catch (ExpansionFileWriteException e) {
219
                        e.printStackTrace();
220
                        return;
221
                }
222

    
223
                draw(geometry.cloneGeometry());
224
        }
225

    
226
        /**
227
         * DOCUMENT ME!
228
         *
229
         * @param geometry
230
         *            DOCUMENT ME!
231
         */
232
        public void modifyFeature(int index, IFeature row) {
233
                try {
234
                        getVLE().getVEA().modifyRow(index, row, getName(),
235
                                        EditionEvent.GRAPHIC);
236
                } catch (ValidateRowException e) {
237
                        // TODO Auto-generated catch block
238
                        e.printStackTrace();
239
                } catch (ExpansionFileWriteException e) {
240
                        // TODO Auto-generated catch block
241
                        e.printStackTrace();
242
                } catch (ReadDriverException e) {
243
                        // TODO Auto-generated catch block
244
                        e.printStackTrace();
245
                } catch (ExpansionFileReadException e) {
246
                        // TODO Auto-generated catch block
247
                        e.printStackTrace();
248
                }
249
                draw(row.getGeometry().cloneGeometry());
250
        }
251

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

    
281
        /**
282
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
283
         *
284
         * @return Cadena para mostrar por consola.
285
         */
286
        public String getQuestion() {
287
                return question;
288
        }
289

    
290
        /**
291
         * Actualiza la cadena que corresponde al estado actual.
292
         *
293
         * @param s
294
         *            Cadena que aparecer? en consola.
295
         */
296
        public void setQuestion(String s) {
297
                question = s;
298
                // ConsoleToken.addQuestion(s);
299
        }
300

    
301
        /**
302
         * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
303
         * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
304
         * edici?n y las que est?n por encima de ella en el TOC.
305
         */
306
        public void refresh() {
307
                // getCadToolAdapter().getMapControl().drawMap(false);
308
                getVLE().getLayer().setDirty(true);
309

    
310
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
311
        }
312

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

    
336
        public void setDescription(String[] currentdescriptions) {
337
                this.currentdescriptions = currentdescriptions;
338
        }
339

    
340
        public String[] getDescriptions() {
341
                return currentdescriptions;
342
        }
343

    
344
        /*
345
         * (non-Javadoc)
346
         *
347
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
348
         */
349
        public void end() {
350
                CADExtension.setCADTool("_selection", true);
351
                PluginServices.getMainFrame().setSelectedTool("_selection");
352
                CADTool cadtool=CADExtension.getCADTool();
353
                cadtool.setPreviosTool(this);
354
        }
355

    
356
        public void init() {
357
// jaume, should not be necessary
358
//                CADTool.drawingSymbol.setOutlined(true);
359
//                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
360

    
361
        }
362

    
363
        protected ArrayList getSelectedRows() {
364
                VectorialLayerEdited vle = getVLE();
365
                ArrayList selectedRow = vle.getSelectedRow();
366
                return selectedRow;
367
        }
368

    
369
        protected ArrayList getSelectedHandlers() {
370
                VectorialLayerEdited vle = getVLE();
371
                ArrayList selectedHandlers = vle.getSelectedHandler();
372
                return selectedHandlers;
373
        }
374

    
375
        public void clearSelection() throws ReadDriverException {
376
                VectorialLayerEdited vle = getVLE();
377
                ArrayList selectedRow = vle.getSelectedRow();
378
                ArrayList selectedHandlers = vle.getSelectedHandler();
379
                selectedRow.clear();
380
                selectedHandlers.clear();
381
                VectorialEditableAdapter vea = vle.getVEA();
382
                FBitSet selection = vea.getSelection();
383
                selection.clear();
384
//                vea.setSelectionImage(null);
385
//                vea.setHandlersImage(null);
386

    
387
        }
388

    
389
        public String getNextTool() {
390
                return tool;
391
        }
392

    
393
        public void setNextTool(String tool) {
394
                this.tool = tool;
395
        }
396

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

    
427
        public boolean isApplicable(int shapeType) {
428
                return true;
429
        }
430

    
431
        public abstract String toString();
432

    
433
        public void throwValueException(String s, double d) {
434
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
435
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
436
        }
437

    
438
        public void throwOptionException(String s, String o) {
439
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
440
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
441
        }
442

    
443
        public void throwPointException(String s, double x, double y) {
444
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
445
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
446
                                JConsole.ERROR);
447
        }
448

    
449
        public void setPreviosTool(DefaultCADTool tool) {
450
                previousTool=tool;
451
        }
452
        public void restorePreviousTool() {
453
                CADExtension.setCADTool(previousTool.toString(), true);
454
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
455
        }
456

    
457
}