Statistics
| Revision:

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

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

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

    
92
        private CADToolAdapter cadToolAdapter;
93

    
94
        private String question;
95

    
96
        private String[] currentdescriptions;
97

    
98
        private String tool = "selection";
99

    
100
        private DefaultCADTool previousTool;
101

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

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

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

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

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

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

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

    
185

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

    
205
                } catch (DriverIOException e) {
206
                        e.printStackTrace();
207
                } catch (DriverException e) {
208
                        e.printStackTrace();
209
                } catch (DriverLoadException e) {
210
                        e.printStackTrace();
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 (IOException e1) {
227
                        e1.printStackTrace();
228
                } catch (DriverIOException e1) {
229
                        e1.printStackTrace();
230
                }
231
                draw(row.getGeometry().cloneGeometry());
232
        }
233

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

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

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

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

    
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
}