Statistics
| Revision:

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

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

    
85
/**
86
 * DOCUMENT ME!
87
 *
88
 * @author Vicente Caballero Navarro
89
 */
90
public abstract class DefaultCADTool implements CADTool {
91
        public static FSymbol drawingSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
92
                        new Color(255, 0,0, 100)); // Le ponemos una transparencia
93
        public static FSymbol modifySymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
94
                        new Color(100, 100, 100, 100));
95
        public static FSymbol selectSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
96
                        Color.ORANGE);
97

    
98
        private static Logger logger = Logger.getLogger(DefaultCADTool.class
99
                        .getName());
100

    
101
        private CADToolAdapter cadToolAdapter;
102

    
103
        private String question;
104

    
105
        private String[] currentdescriptions;
106

    
107
        private String tool = "selection";
108

    
109
        private DefaultCADTool previousTool;
110
        /**
111
         * DOCUMENT ME!
112
         */
113
        public void draw(IGeometry geometry) {
114
                if (geometry != null) {
115
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
116
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
117
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
118
                        geometry.draw(gImag, vp, DefaultCADTool.drawingSymbol);
119
                }
120
        }
121

    
122
        /**
123
         * DOCUMENT ME!
124
         *
125
         * @param cta
126
         *            DOCUMENT ME!
127
         */
128
        public void setCadToolAdapter(CADToolAdapter cta) {
129
                cadToolAdapter = cta;
130
        }
131

    
132
        /**
133
         * DOCUMENT ME!
134
         *
135
         * @return DOCUMENT ME!
136
         */
137
        public CADToolAdapter getCadToolAdapter() {
138
                return cadToolAdapter;
139
        }
140

    
141
        public VectorialLayerEdited getVLE() {
142
                return (VectorialLayerEdited) CADExtension.getEditionManager()
143
                                .getActiveLayerEdited();
144
        }
145

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

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

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

    
193

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

    
213
                } catch (DriverIOException e) {
214
                        e.printStackTrace();
215
                } catch (DriverException e) {
216
                        e.printStackTrace();
217
                } catch (DriverLoadException e) {
218
                        e.printStackTrace();
219
                }
220

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

    
224
        /**
225
         * DOCUMENT ME!
226
         *
227
         * @param geometry
228
         *            DOCUMENT ME!
229
         */
230
        public void modifyFeature(int index, IFeature row) {
231
                try {
232
                        getVLE().getVEA().modifyRow(index, row, getName(),
233
                                        EditionEvent.GRAPHIC);
234
                } catch (IOException e1) {
235
                        e1.printStackTrace();
236
                } catch (DriverIOException e1) {
237
                        e1.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 (DriverIOException e) {
259
                        e.printStackTrace();
260
                } catch (IOException e) {
261
                        e.printStackTrace();
262
                } catch (DriverLoadException e) {
263
                        e.printStackTrace();
264
                }
265
                return vea.getInversedIndex(index);
266
        }
267

    
268
        /**
269
         * Devuelve la cadena que corresponde al estado en el que nos encontramos.
270
         *
271
         * @return Cadena para mostrar por consola.
272
         */
273
        public String getQuestion() {
274
                return question;
275
        }
276

    
277
        /**
278
         * Actualiza la cadena que corresponde al estado actual.
279
         *
280
         * @param s
281
         *            Cadena que aparecer? en consola.
282
         */
283
        public void setQuestion(String s) {
284
                question = s;
285
                // ConsoleToken.addQuestion(s);
286
        }
287

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

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

    
322
        public void setDescription(String[] currentdescriptions) {
323
                this.currentdescriptions = currentdescriptions;
324
        }
325

    
326
        public String[] getDescriptions() {
327
                return currentdescriptions;
328
        }
329

    
330
        /*
331
         * (non-Javadoc)
332
         *
333
         * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
334
         */
335
        public void end() {
336
                CADExtension.setCADTool("_selection", true);
337
                PluginServices.getMainFrame().setSelectedTool("_selection");
338
                CADTool cadtool=CADExtension.getCADTool();
339
                cadtool.setPreviosTool(this);
340
        }
341

    
342
        public void init() {
343
                DefaultCADTool.drawingSymbol.setOutlined(true);
344
                DefaultCADTool.drawingSymbol.setOutlineColor(Color.GREEN);
345

    
346
        }
347

    
348
        protected ArrayList getSelectedRows() {
349
                VectorialLayerEdited vle = getVLE();
350
                ArrayList selectedRow = vle.getSelectedRow();
351
                return selectedRow;
352
        }
353

    
354
        protected ArrayList getSelectedHandlers() {
355
                VectorialLayerEdited vle = getVLE();
356
                ArrayList selectedHandlers = vle.getSelectedHandler();
357
                return selectedHandlers;
358
        }
359

    
360
        public void clearSelection() {
361
                VectorialLayerEdited vle = getVLE();
362
                ArrayList selectedRow = vle.getSelectedRow();
363
                ArrayList selectedHandlers = vle.getSelectedHandler();
364
                selectedRow.clear();
365
                selectedHandlers.clear();
366
                VectorialEditableAdapter vea = vle.getVEA();
367
                FBitSet selection = vea.getSelection();
368
                selection.clear();
369
                //vea.setSelectionImage(null);
370
                //vea.setHandlersImage(null);
371

    
372
        }
373

    
374
        public String getNextTool() {
375
                return tool;
376
        }
377

    
378
        public void setNextTool(String tool) {
379
                this.tool = tool;
380
        }
381

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

    
410
        public boolean isApplicable(int shapeType) {
411
                return true;
412
        }
413

    
414
        public abstract String toString();
415

    
416
        public void throwValueException(String s, double d) {
417
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
418
                vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
419
        }
420

    
421
        public void throwOptionException(String s, String o) {
422
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
423
                vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
424
        }
425

    
426
        public void throwPointException(String s, double x, double y) {
427
                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
428
                vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y,
429
                                JConsole.ERROR);
430
        }
431

    
432
        public void setPreviosTool(DefaultCADTool tool) {
433
                previousTool=tool;
434
        }
435
        public void restorePreviousTool() {
436
                CADExtension.setCADTool(previousTool.toString(), true);
437
                PluginServices.getMainFrame().setSelectedTool(previousTool.toString());
438
        }
439

    
440
}