Statistics
| Revision:

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

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
                        int num;
187
                        try {
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
                                selectedRow.add(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
                        } catch (IOException e) {
211
                                logger.debug(e);
212
                                NotificationManager.addError(e);
213
                                return;
214
                        }
215

    
216
                } catch (DriverIOException e) {
217
                        e.printStackTrace();
218
                } catch (DriverException e) {
219
                        e.printStackTrace();
220
                } catch (DriverLoadException e) {
221
                        e.printStackTrace();
222
                }
223

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

    
227
        /**
228
         * DOCUMENT ME!
229
         *
230
         * @param geometry
231
         *            DOCUMENT ME!
232
         */
233
        public void modifyFeature(int index, IFeature row) {
234
                try {
235
                        getVLE().getVEA().modifyRow(index, row, getName(),
236
                                        EditionEvent.GRAPHIC);
237
                } catch (IOException e1) {
238
                        e1.printStackTrace();
239
                } catch (DriverIOException e1) {
240
                        e1.printStackTrace();
241
                }
242
                draw(row.getGeometry().cloneGeometry());
243
        }
244

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

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

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

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

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

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

    
329
        public String[] getDescriptions() {
330
                return currentdescriptions;
331
        }
332

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

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

    
349
        }
350

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

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

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

    
375
        }
376

    
377
        public String getNextTool() {
378
                return tool;
379
        }
380

    
381
        public void setNextTool(String tool) {
382
                this.tool = tool;
383
        }
384

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

    
413
        public boolean isApplicable(int shapeType) {
414
                return true;
415
        }
416

    
417
        public abstract String toString();
418

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

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

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

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

    
443
}