Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 27149

History | View | Annotate | Download (12.2 KB)

1 3883 caballero
/* 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.tools;
42
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45 4637 caballero
import java.awt.Image;
46 4313 fjp
import java.awt.event.InputEvent;
47 4637 caballero
import java.awt.geom.AffineTransform;
48 3883 caballero
import java.awt.geom.Point2D;
49
import java.awt.geom.Point2D.Double;
50 4637 caballero
import java.awt.image.ImagingOpException;
51 4522 caballero
import java.util.ArrayList;
52 3883 caballero
53 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
54 3883 caballero
import com.iver.andami.PluginServices;
55 12739 caballero
import com.iver.andami.messages.NotificationManager;
56 3883 caballero
import com.iver.cit.gvsig.CADExtension;
57 10626 caballero
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
58
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
59
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
60 4637 caballero
import com.iver.cit.gvsig.fmap.ViewPort;
61 3883 caballero
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63 4522 caballero
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
64 5184 caballero
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
65 4637 caballero
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
66 3904 fjp
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
67 3883 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
68
import com.iver.cit.gvsig.gui.cad.CADTool;
69
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
70 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
71 3883 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
72
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
73 4637 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
74 3883 caballero
75
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class ScaleCADTool extends DefaultCADTool {
82
    private ScaleCADToolContext _fsm;
83
    private Point2D firstPoint;
84
    private Point2D lastPoint;
85
        private Point2D scalePoint;
86
        private Double orr;
87
        private Double frr;
88
        private Double ore;
89
        private Double fre;
90
91
    /**
92
     * Crea un nuevo PolylineCADTool.
93
     */
94
    public ScaleCADTool() {
95
    }
96
97
    /**
98
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
99
     * carga previa a la utilizaci?n de la herramienta.
100
     */
101
    public void init() {
102
        _fsm = new ScaleCADToolContext(this);
103
    }
104
105
    /* (non-Javadoc)
106
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
107
     */
108 4313 fjp
    public void transition(double x, double y, InputEvent event) {
109 4324 caballero
        _fsm.addPoint(x, y, event);
110 3883 caballero
    }
111
112
    /* (non-Javadoc)
113
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
114
     */
115
    public void transition(double d) {
116
        _fsm.addValue(d);
117
    }
118
119
    /* (non-Javadoc)
120
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
121
     */
122 5735 caballero
    public void transition(String s) throws CommandException {
123 4892 caballero
            if (!super.changeCommand(s)){
124
                    _fsm.addOption(s);
125
            }
126 3883 caballero
    }
127
128
    /**
129
     * DOCUMENT ME!
130
     */
131
    public void selection() {
132 4584 caballero
        ArrayList rowSelected=getSelectedRows();
133
        if (rowSelected.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
134 5071 caballero
            CADExtension.setCADTool("_selection",false);
135 4584 caballero
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
136 5071 caballero
                "_scale");
137 3883 caballero
        }
138
    }
139
140
    /**
141
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
142
     * editableFeatureSource que ya estar? creado.
143
     *
144
     * @param x par?metro x del punto que se pase en esta transici?n.
145
     * @param y par?metro y del punto que se pase en esta transici?n.
146
     */
147 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
148 3883 caballero
        ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
149
        String status = actualState.getName();
150
151 3978 caballero
        if (status.equals("Scale.PointMain")) {
152 3883 caballero
                                firstPoint = new Point2D.Double(x, y);
153
                            scalePoint = firstPoint;
154 3978 caballero
                } else if (status.equals("Scale.ScaleFactorOrReference")) {
155 3883 caballero
                        PluginServices.getMDIManager().setWaitCursor();
156
                        lastPoint = new Point2D.Double(x, y);
157
158 4522 caballero
                        //double w;
159
                        //double h;
160
                        //w = lastPoint.getX() - firstPoint.getX();
161
                        //h = lastPoint.getY() - firstPoint.getY();
162 3883 caballero
163
                        try {
164
                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
165
                                scale(firstPoint.distance(lastPoint)/(size/40));
166 10626 caballero
                        } catch (ValidateRowException e) {
167 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
168 10626 caballero
                        } catch (ExpansionFileWriteException e) {
169 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
170 10626 caballero
                        } catch (ReadDriverException e) {
171 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
172 3883 caballero
                        }
173 13869 jaume
174 3883 caballero
                        PluginServices.getMDIManager().restoreCursor();
175 3978 caballero
                } else if (status.equals("Scale.PointOriginOrScaleFactor")) {
176 3883 caballero
                        orr = new Point2D.Double(x, y);
177 3978 caballero
                } else if (status.equals("Scale.EndPointReference")) {
178 3883 caballero
                        frr = new Point2D.Double(x, y);
179 3978 caballero
                } else if (status.equals("Scale.OriginPointScale")) {
180 3883 caballero
                        ore = new Point2D.Double(x, y);
181
                        firstPoint = ore;
182 3978 caballero
                } else if (status.equals("Scale.EndPointScale")) {
183 3883 caballero
                        fre = new Point2D.Double(x, y);
184
185
                        double distrr = orr.distance(frr);
186
                        double distre = ore.distance(fre);
187
                        double escalado = distre / distrr;
188
189
                        try {
190
                                scale(escalado);
191 10626 caballero
                        } catch (ValidateRowException e) {
192 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
193 10626 caballero
                        } catch (ExpansionFileWriteException e) {
194 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
195 10626 caballero
                        } catch (ReadDriverException e) {
196 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
197 13869 jaume
                        }
198 3883 caballero
                }
199
200
    }
201
202
    /**
203
     * M?todo para dibujar la lo necesario para el estado en el que nos
204
     * encontremos.
205
     *
206
     * @param g Graphics sobre el que dibujar.
207
     * @param x par?metro x del punto que se pase para dibujar.
208
     * @param y par?metro x del punto que se pase para dibujar.
209
     */
210
    public void drawOperation(Graphics g, double x, double y) {
211 9121 caballero
                ScaleCADToolState actualState = _fsm.getState();
212 4522 caballero
                String status = actualState.getName();
213 4584 caballero
                ArrayList selectedRow = getSelectedRows();
214 4522 caballero
                Point2D currentPoint = new Point2D.Double(x, y);
215 3883 caballero
216 4522 caballero
                if (status.equals("Scale.ScaleFactorOrReference")) {
217 4637 caballero
                        VectorialLayerEdited vle = getVLE();
218 6881 cesar
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
219 4637 caballero
                        Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
220
                                        .getY());
221
                        double size = getCadToolAdapter().getMapControl().getViewPort()
222
                                        .toMapDistance(
223
                                                        getCadToolAdapter().getMapControl().getWidth());
224
                        double scale = firstPoint.distance(currentPoint) / (size / 40);
225 11437 caballero
                        drawLine((Graphics2D) g, firstPoint, currentPoint,DefaultCADTool.axisReferencesSymbol);
226 4637 caballero
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
227
                                for (int i = 0; i < selectedRow.size(); i++) {
228
                                        DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
229
                                                        .get(i)).getLinkedRow();
230
                                        IGeometry geometry = fea.getGeometry().cloneGeometry();
231 3883 caballero
232 4637 caballero
                                        UtilFunctions.scaleGeom(geometry, firstPoint, scale, scale);
233
                                        geometry.draw((Graphics2D) g, getCadToolAdapter()
234
                                                        .getMapControl().getViewPort(),
235 11437 caballero
                                                        DefaultCADTool.axisReferencesSymbol);
236 4637 caballero
237
                                }
238
239
                        } else {
240
                                AffineTransform at = new AffineTransform();
241
                                at.setToTranslation(point.getX(), point.getY());
242
                                at.scale(scale, scale);
243
                                at.translate(-point.getX(), -point.getY());
244
                                Image imgSel = vle.getSelectionImage();
245
                                try {
246
                                        ((Graphics2D) g).drawImage(imgSel, at, null);
247
                                } catch (ImagingOpException e) {
248
                                }
249 3883 caballero
                        }
250 4637 caballero
                        PluginServices.getMainFrame().getStatusBar().setMessage("5",
251
                                        "Factor = " + scale);
252 3978 caballero
                } else if (status.equals("Scale.EndPointScale")) {
253 4637 caballero
                        VectorialLayerEdited vle = getVLE();
254 6881 cesar
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
255 4637 caballero
                        Point2D point = vp.fromMapPoint(scalePoint.getX(), scalePoint
256
                                        .getY());
257 3883 caballero
258 4637 caballero
                        double distrr = orr.distance(frr);
259
                        double distre = ore.distance(currentPoint);
260
                        double escalado = distre / distrr;
261
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
262
                                for (int i = 0; i < selectedRow.size(); i++) {
263
                                                DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
264
                                                                .get(i)).getLinkedRow();
265
                                                IGeometry geometry = fea.getGeometry().cloneGeometry();
266
                                                UtilFunctions.scaleGeom(geometry, scalePoint, escalado,
267
                                                                escalado);
268
                                                geometry.draw((Graphics2D) g, getCadToolAdapter()
269
                                                                .getMapControl().getViewPort(),
270 11437 caballero
                                                                DefaultCADTool.axisReferencesSymbol);
271 3883 caballero
272 4637 caballero
                                        }
273
                                } else {
274
                                        AffineTransform at = new AffineTransform();
275
                                        at.setToTranslation(point.getX(), point.getY());
276
                                        at.scale(escalado, escalado);
277
                                        at.translate(-point.getX(), -point.getY());
278
                                        Image imgSel = vle.getSelectionImage();
279
                                        try {
280
                                                ((Graphics2D) g).drawImage(imgSel, at, null);
281
                                        } catch (ImagingOpException e) {
282
                                        }
283
                                }
284 11437 caballero
                                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.axisReferencesSymbol);
285 3883 caballero
                        }
286 4522 caballero
        }
287 3883 caballero
288
    /**
289 4522 caballero
         * Add a diferent option.
290
         *
291
         * @param s
292
         *            Diferent option.
293
         */
294 3883 caballero
    public void addOption(String s) {
295
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
296 13869 jaume
            String status = actualState.getName();
297
            if (status.equals("Scale.ScaleFactorOrReference")) {
298
                    /*        try {
299 3883 caballero
                                scale(2);
300
                        } catch (DriverIOException e) {
301
                                // TODO Auto-generated catch block
302
                                e.printStackTrace();
303
                        } catch (IOException e) {
304
                                // TODO Auto-generated catch block
305
                                e.printStackTrace();
306
                        }
307 13869 jaume
                     */
308
            }
309
    }
310 3883 caballero
311
    /* (non-Javadoc)
312
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
313
     */
314
    public void addValue(double d) {
315
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
316 13869 jaume
            String status = actualState.getName();
317
            if (status.equals("Scale.ScaleFactorOrReference")) {
318
                    try {
319
                            scale(d);
320
                    } catch (ValidateRowException e) {
321
                            NotificationManager.addError(e.getMessage(),e);
322
                    } catch (ExpansionFileWriteException e) {
323
                            NotificationManager.addError(e.getMessage(),e);
324
                    } catch (ReadDriverException e) {
325
                            NotificationManager.addError(e.getMessage(),e);
326
                    }
327 3883 caballero
            }
328
    }
329 10626 caballero
    private void scale(double scaleFactor) throws ValidateRowException, ExpansionFileWriteException, ReadDriverException, ExpansionFileReadException{
330 5985 caballero
            VectorialLayerEdited vle=getVLE();
331
            VectorialEditableAdapter vea=vle.getVEA();
332
            vea.startComplexRow();
333
            ArrayList selectedRow=getSelectedRows();
334
            ArrayList selectedRowAux=new ArrayList();
335
            for (int i = 0; i < selectedRow.size(); i++) {
336
                    IRowEdited edRow = (IRowEdited) selectedRow.get(i);
337
                    DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
338
                        UtilFunctions.scaleGeom(fea.getGeometry(), scalePoint, scaleFactor, scaleFactor);
339
                    vea.modifyRow(edRow.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
340
                    selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
341 3883 caballero
            }
342 6071 caballero
            vea.endComplexRow(getName());
343 8938 caballero
            vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
344 5985 caballero
            //clearSelection();
345
            //selectedRow.addAll(selectedRowAux);
346
    }
347 4118 caballero
348 9121 caballero
    public String getName() {
349 4584 caballero
                return PluginServices.getText(this,"scale_");
350 4118 caballero
        }
351 4892 caballero
352
        public String toString() {
353
                return "_scale";
354
        }
355 5170 caballero
356 3883 caballero
}