Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / AutoCompletePolygon.java @ 40557

History | View | Annotate | Download (4.54 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import org.gvsig.andami.PluginServices;
27
import org.gvsig.andami.messages.NotificationManager;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.exception.CreateGeometryException;
32
import org.gvsig.fmap.geom.primitive.GeneralPathX;
33
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
34
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
35
import org.gvsig.tools.dispose.DisposableIterator;
36

    
37
public class AutoCompletePolygon extends PolylineCADTool {
38

    
39
    @Override
40
    /**
41
     * M?todo para dibujar la lo necesario para el estado en el que nos
42
     * encontremos.
43
     *
44
     * @param g Graphics sobre el que dibujar.
45
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
46
     * @param x par?metro x del punto que se pase para dibujar.
47
     * @param y par?metro x del punto que se pase para dibujar.
48
     */
49
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
50
        Geometry geom = getGeometry();
51
        if (geom.getHandlers(Geometry.SELECTHANDLER).length == 0
52
            && firstPoint != null) {
53
            GeneralPathX gpx = new GeneralPathX();
54
            gpx.moveTo(firstPoint.getX(), firstPoint.getY());
55
            gpx.lineTo(x, y);
56
            renderer.draw(createCurve(gpx),
57
                mapControlManager.getGeometrySelectionSymbol());
58
        } else
59
            if (geom.getHandlers(Geometry.SELECTHANDLER).length > 1) {
60
                GeneralPathX gpxGeom = new GeneralPathX();
61
                gpxGeom.moveTo(x, y);
62
                gpxGeom
63
                    .append(
64
                        geom.getPathIterator(null, geomManager.getFlatness()),
65
                        true);
66
                gpxGeom.closePath();
67
                renderer.draw(createCurve(gpxGeom),
68
                    mapControlManager.getGeometrySelectionSymbol());
69
            }
70
    }
71

    
72
    private Geometry autoComplete(Geometry digitizedGeom)
73
        throws CreateGeometryException {
74
        FeatureSet selected = null;
75
        DisposableIterator iterator = null;
76
        try {
77
            FLyrVect lyrVect = (FLyrVect) getVLE().getLayer();
78
            // Se supone que debe ser r?pido, ya que est? indexado
79

    
80
            selected =
81
                lyrVect.queryByGeometry(digitizedGeom, lyrVect
82
                    .getFeatureStore().getDefaultFeatureType());
83
            iterator = selected.fastIterator();
84
            while (iterator.hasNext()) {
85
                Feature feature = (Feature) iterator.next();
86
                Geometry aux = feature.getDefaultGeometry();
87
                digitizedGeom = digitizedGeom.difference(aux);
88
            }
89
        } catch (Exception e) {
90
            NotificationManager.showMessageError(
91
                PluginServices.getText(this,
92
                    "Error_in_Autocomplete_Polygon_Tool_")
93
                    + " "
94
                    + e.getLocalizedMessage(), e);
95
        } finally {
96
            if (iterator != null) {
97
                iterator.dispose();
98
            }
99
            if (selected != null) {
100
                selected.dispose();
101
            }
102
        }
103

    
104
        return digitizedGeom;
105
    }
106

    
107
    @Override
108
    public Feature insertGeometry(Geometry geometry) {
109
        Geometry newGeom;
110
        try {
111
            newGeom = autoComplete(geometry);
112
            return super.insertGeometry(newGeom);
113
        } catch (CreateGeometryException e) {
114
            NotificationManager.addError(e);
115
        }
116
        return null;
117
    }
118
}