Statistics
| Revision:

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

History | View | Annotate | Download (4.5 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.feature.Feature;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.exception.CreateGeometryException;
30
import org.gvsig.fmap.geom.primitive.GeneralPathX;
31
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
32
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
33
import org.gvsig.tools.dispose.DisposableIterator;
34

    
35
public class AutoCompletePolygon extends PolylineCADTool {
36

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

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

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

    
102
        return digitizedGeom;
103
    }
104

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