Statistics
| Revision:

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

History | View | Annotate | Download (13.6 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.andami.messages.NotificationManager;
59
import com.iver.cit.gvsig.CADExtension;
60
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
61
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
62
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
63
import com.iver.cit.gvsig.fmap.ViewPort;
64
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
67
import com.iver.cit.gvsig.fmap.core.Handler;
68
import com.iver.cit.gvsig.fmap.core.IFeature;
69
import com.iver.cit.gvsig.fmap.core.IGeometry;
70
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
71
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
72
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
73
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
74
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
75
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
76
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
77
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
78
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
79
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
80
import com.iver.cit.gvsig.fmap.layers.FBitSet;
81
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
82
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
83
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
84
import com.iver.cit.gvsig.project.documents.view.gui.View;
85
import com.iver.utiles.console.JConsole;
86

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

    
104

    
105
        private CADToolAdapter cadToolAdapter;
106

    
107
        private String question;
108

    
109
        private String[] currentdescriptions;
110

    
111
        private String tool = "selection";
112

    
113
        private DefaultCADTool previousTool;
114

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

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

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

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

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

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

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

    
195

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

    
210

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

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

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

    
251
        /**
252
         * DOCUMENT ME!
253
         *
254
         * @param geometry
255
         *            DOCUMENT ME!
256
         * @param values
257
         *            DOCUMENT ME!
258
         */
259
        public int addGeometry(IGeometry geometry, Value[] values) {
260
                int index = 0;
261
                VectorialEditableAdapter vea = getVLE().getVEA();
262
                try {
263
                        String newFID = vea.getNewFID();
264
                        DefaultFeature df = new DefaultFeature(geometry, values, newFID);
265
                        index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
266
                } catch (ValidateRowException e) {
267
                        NotificationManager.addError(e);
268
                } catch (ReadDriverException e) {
269
                        NotificationManager.addError(e);
270
                } catch (ExpansionFileWriteException e) {
271
                        NotificationManager.addError(e);
272
                }
273
                return vea.getInversedIndex(index);
274
        }
275

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

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

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

    
305
                getCadToolAdapter().getMapControl().rePaintDirtyLayers();
306
        }
307

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

    
331
        public void setDescription(String[] currentdescriptions) {
332
                this.currentdescriptions = currentdescriptions;
333
        }
334

    
335
        public String[] getDescriptions() {
336
                return currentdescriptions;
337
        }
338

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

    
351
        public void init() {
352
// jaume, should not be necessary
353
//                CADTool.drawingSymbol.setOutlined(true);
354
//                CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
355

    
356
        }
357

    
358
        protected ArrayList getSelectedRows() {
359
                VectorialLayerEdited vle = getVLE();
360
                ArrayList selectedRow = vle.getSelectedRow();
361
                return selectedRow;
362
        }
363

    
364
        protected ArrayList getSelectedHandlers() {
365
                VectorialLayerEdited vle = getVLE();
366
                ArrayList selectedHandlers = vle.getSelectedHandler();
367
                return selectedHandlers;
368
        }
369

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

    
382
        }
383

    
384
        public String getNextTool() {
385
                return tool;
386
        }
387

    
388
        public void setNextTool(String tool) {
389
                this.tool = tool;
390
        }
391

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

    
422
        public boolean isApplicable(int shapeType) {
423
                return true;
424
        }
425

    
426
        public abstract String toString();
427

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

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

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

    
444
        public void setPreviosTool(DefaultCADTool tool) {
445
                previousTool=tool;
446
        }
447
        public void restorePreviousTool() {
448
                CADExtension.setCADTool(previousTool.toString(), true);
449
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
450
        }
451

    
452
}