Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / AutoCompletePolygon.java @ 37138

History | View | Annotate | Download (5.78 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.editing.gui.cad.tools;
23

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

    
38
public class AutoCompletePolygon extends PolylineCADTool {
39

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

    
73
    private Geometry autoComplete(Geometry digitizedGeom)
74
        throws CreateGeometryException {
75
        com.vividsolutions.jts.geom.Geometry jtsGeom = null;
76
        FeatureSet selected = null;
77
        DisposableIterator iterator = null;
78
        try {
79
            jtsGeom =
80
                (com.vividsolutions.jts.geom.Geometry) digitizedGeom
81
                    .invokeOperation(ToJTS.CODE, null);
82
            FLyrVect lyrVect = (FLyrVect) getVLE().getLayer();
83
            // Se supone que debe ser r?pido, ya que est? indexado
84

    
85
            selected =
86
                lyrVect.queryByGeometry(digitizedGeom, lyrVect
87
                    .getFeatureStore().getDefaultFeatureType());
88
            iterator = selected.iterator();
89
            while (iterator.hasNext()) {
90
                Feature feature = (Feature) iterator.next();
91
                Geometry aux = feature.getDefaultGeometry();
92
                com.vividsolutions.jts.geom.Geometry jtsAux =
93
                    (com.vividsolutions.jts.geom.Geometry) aux.invokeOperation(
94
                        ToJTS.CODE, null);
95
                jtsGeom = jtsGeom.difference(jtsAux);
96
            }
97
        } catch (ReadException e) {
98
            NotificationManager.showMessageError(
99
                PluginServices.getText(this,
100
                    "Error_in_Autocomplete_Polygon_Tool_")
101
                    + " "
102
                    + e.getLocalizedMessage(), e);
103
        } catch (com.vividsolutions.jts.geom.TopologyException e) {
104
            NotificationManager.showMessageError(
105
                PluginServices.getText(this,
106
                    "Error_in_Autocomplete_Polygon_Tool_")
107
                    + " "
108
                    + e.getLocalizedMessage(), e);
109
        } catch (Exception e) {
110
            NotificationManager.showMessageError(
111
                PluginServices.getText(this,
112
                    "Error_in_Autocomplete_Polygon_Tool_")
113
                    + " "
114
                    + e.getLocalizedMessage(), e);
115
        } finally {
116
            if (iterator != null) {
117
                iterator.dispose();
118
            }
119
            if (selected != null) {
120
                selected.dispose();
121
            }
122
        }
123

    
124
        return Converter.jtsToGeometry(jtsGeom);
125
    }
126

    
127
    @Override
128
    public Feature insertGeometry(Geometry geometry) {
129
        Geometry newGeom;
130
        try {
131
            newGeom = autoComplete(geometry);
132
            return super.insertGeometry(newGeom);
133
        } catch (CreateGeometryException e) {
134
            NotificationManager.addError(e);
135
        }
136
        return null;
137
    }
138

    
139
    public boolean isApplicable(int shapeType) {
140
        switch (shapeType) {
141
        case Geometry.TYPES.SURFACE:
142
            return true;
143
        }
144
        return false;
145
    }
146
}