~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Handler/Polygon.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
 
 
6
/**
 
7
 * @requires OpenLayers/Handler/Path.js
 
8
 * @requires OpenLayers/Geometry/Polygon.js
 
9
 */
 
10
 
 
11
/**
 
12
 * Class: OpenLayers.Handler.Polygon
 
13
 * Handler to draw a polygon on the map.  Polygon is displayed on mouse down,
 
14
 * moves on mouse move, and is finished on mouse up.
 
15
 *
 
16
 * Inherits from:
 
17
 *  - <OpenLayers.Handler.Path>
 
18
 *  - <OpenLayers.Handler>
 
19
 */
 
20
OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
 
21
    
 
22
    /**
 
23
     * Parameter: polygon
 
24
     * {<OpenLayers.Feature.Vector>}
 
25
     */
 
26
    polygon: null,
 
27
 
 
28
    /**
 
29
     * Constructor: OpenLayers.Handler.Polygon
 
30
     * Create a Polygon Handler.
 
31
     *
 
32
     * Parameters:
 
33
     * control - {<OpenLayers.Control>} 
 
34
     * callbacks - {Object} An object with a 'done' property whos value is
 
35
     *                          a function to be called when the path drawing is
 
36
     *                          finished. The callback should expect to recieve a
 
37
     *                          single argument, the polygon geometry.
 
38
     *                          If the callbacks object contains a 'point'
 
39
     *                          property, this function will be sent each point
 
40
     *                          as they are added.  If the callbacks object contains
 
41
     *                          a 'cancel' property, this function will be called when
 
42
     *                          the handler is deactivated while drawing.  The cancel
 
43
     *                          should expect to receive a geometry.
 
44
     * options - {Object} 
 
45
     */
 
46
    initialize: function(control, callbacks, options) {
 
47
        OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments);
 
48
    },
 
49
    
 
50
    /**
 
51
     * Method: createFeature
 
52
     * Add temporary geometries
 
53
     */
 
54
    createFeature: function() {
 
55
        this.polygon = new OpenLayers.Feature.Vector(
 
56
                                        new OpenLayers.Geometry.Polygon());
 
57
        this.line = new OpenLayers.Feature.Vector(
 
58
                                        new OpenLayers.Geometry.LinearRing());
 
59
        this.polygon.geometry.addComponent(this.line.geometry);
 
60
        this.point = new OpenLayers.Feature.Vector(
 
61
                                        new OpenLayers.Geometry.Point());
 
62
        this.layer.addFeatures([this.polygon, this.point], {silent: true});
 
63
    },
 
64
 
 
65
    /**
 
66
     * Method: destroyFeature
 
67
     * Destroy temporary geometries
 
68
     */
 
69
    destroyFeature: function() {
 
70
        OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
 
71
        this.polygon = null;
 
72
    },
 
73
 
 
74
    /**
 
75
     * Method: modifyFeature
 
76
     * Modify the existing geometry given the new point
 
77
     * 
 
78
     */
 
79
    modifyFeature: function() {
 
80
        var index = this.line.geometry.components.length - 2;
 
81
        this.line.geometry.components[index].x = this.point.geometry.x;
 
82
        this.line.geometry.components[index].y = this.point.geometry.y;
 
83
        this.line.geometry.components[index].clearBounds();
 
84
    },
 
85
 
 
86
    /**
 
87
     * Method: drawFeature
 
88
     * Render geometries on the temporary layer.
 
89
     */
 
90
    drawFeature: function() {
 
91
        this.layer.drawFeature(this.polygon, this.style);
 
92
        this.layer.drawFeature(this.point, this.style);
 
93
    },
 
94
    
 
95
    /**
 
96
     * Method: getGeometry
 
97
     * Return the sketch geometry.  If <multi> is true, this will return
 
98
     *     a multi-part geometry.
 
99
     *
 
100
     * Returns:
 
101
     * {<OpenLayers.Geometry.Polygon>}
 
102
     */
 
103
    getGeometry: function() {
 
104
        var geometry = this.polygon.geometry;
 
105
        if(this.multi) {
 
106
            geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
 
107
        }
 
108
        return geometry;
 
109
    },
 
110
 
 
111
    /**
 
112
     * Method: dblclick
 
113
     * Handle double-clicks.  Finish the geometry and send it back
 
114
     * to the control.
 
115
     * 
 
116
     * Parameters:
 
117
     * evt - {Event} 
 
118
     */
 
119
    dblclick: function(evt) {
 
120
        if(!this.freehandMode(evt)) {
 
121
            // remove the penultimate point
 
122
            var index = this.line.geometry.components.length - 2;
 
123
            this.line.geometry.removeComponent(this.line.geometry.components[index]);
 
124
            if(this.persist) {
 
125
                this.destroyPoint();
 
126
            }
 
127
            this.finalize();
 
128
        }
 
129
        return false;
 
130
    },
 
131
 
 
132
    CLASS_NAME: "OpenLayers.Handler.Polygon"
 
133
});