Statistics
| Revision:

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

History | View | Annotate | Download (12.9 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.ISymbol;
69
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
70
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
71
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
72
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
73
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
74
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
75
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
76
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
77
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
78
import com.iver.cit.gvsig.fmap.layers.FBitSet;
79
import com.iver.cit.gvsig.fmap.layers.FLayer;
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 FSymbol selectionSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
93
                        new Color(255, 0,0, 100)); // Le ponemos una transparencia
94
        public static FSymbol axisReferencesSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
95
                        new Color(100, 100, 100, 100));
96
        public static FSymbol geometrySelectSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
97
                        Color.RED);
98
        public static FSymbol handlerSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,
99
                        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
         * DOCUMENT ME!
115
         */
116
        public void draw(IGeometry geometry) {
117
                if (geometry != null) {
118
                        BufferedImage img = getCadToolAdapter().getMapControl().getImage();
119
                        Graphics2D gImag = (Graphics2D) img.getGraphics();
120
                        ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
121
                        geometry.draw(gImag, vp, DefaultCADTool.selectionSymbol);
122
                }
123
        }
124

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

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

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

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

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

    
181
                        int numAttr = vea.getRecordset().getFieldCount();
182
                        Value[] values = new Value[numAttr];
183
                        for (int i = 0; i < numAttr; i++) {
184
                                values[i] = ValueFactory.createNullValue();
185
                        }
186
                        String newFID;
187
                        try {
188
                                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
                                selectedRow.add(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
                        } catch (IOException e) {
210
                                logger.debug(e);
211
                                NotificationManager.addError(e);
212
                                return;
213
                        }
214

    
215
                } catch (DriverIOException e) {
216
                        e.printStackTrace();
217
                } catch (DriverException e) {
218
                        e.printStackTrace();
219
                } catch (DriverLoadException e) {
220
                        e.printStackTrace();
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 (IOException e1) {
237
                        e1.printStackTrace();
238
                } catch (DriverIOException e1) {
239
                        e1.printStackTrace();
240
                }
241
                draw(row.getGeometry().cloneGeometry());
242
        }
243

    
244
        /**
245
         * DOCUMENT ME!
246
         *
247
         * @param geometry
248
         *            DOCUMENT ME!
249
         * @param values
250
         *            DOCUMENT ME!
251
         */
252
        public int addGeometry(IGeometry geometry, Value[] values) {
253
                int index = 0;
254
                VectorialEditableAdapter vea = getVLE().getVEA();
255
                try {
256
                        String newFID = vea.getNewFID();
257
                        DefaultFeature df = new DefaultFeature(geometry, values, newFID);
258
                        index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
259
                } catch (DriverIOException e) {
260
                        NotificationManager.addError(e);
261
                } catch (IOException e) {
262
                        NotificationManager.addError(e);
263
                } catch (DriverLoadException e) {
264
                        NotificationManager.addError(e);
265
                }
266
                return vea.getInversedIndex(index);
267
        }
268

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

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

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

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

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

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

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

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

    
347
        }
348

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

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

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

    
373
        }
374

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

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

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

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

    
415
        public abstract String toString();
416

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

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

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

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

    
441
}