Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / DefaultEditingBehavior.java @ 2867

History | View | Annotate | Download (8.91 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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 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
 * 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

    
25
package org.gvsig.vectorediting.swing.impl;
26

    
27
import java.awt.Color;
28
import java.awt.Graphics;
29
import java.awt.Image;
30
import java.awt.event.MouseEvent;
31
import java.awt.geom.Point2D;
32
import java.awt.image.BufferedImage;
33
import java.util.Iterator;
34
import java.util.Set;
35
import javax.swing.SwingUtilities;
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.aggregate.MultiPoint;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.mapcontext.ViewPort;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
43
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
44
import org.gvsig.fmap.mapcontrol.MapControlLocator;
45
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
46
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
47
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.exception.BaseException;
50
import org.gvsig.tools.i18n.I18nManager;
51
import org.gvsig.vectorediting.lib.api.DrawingStatus;
52
import org.gvsig.vectorediting.lib.api.DrawingStatus.Status;
53
import org.gvsig.vectorediting.lib.api.EditingService;
54
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
55
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
56
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException;
57
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
58
import org.gvsig.vectorediting.swing.api.EditingSwingLocator;
59
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
60
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64
public class DefaultEditingBehavior extends Behavior {
65

    
66
    private static final Logger logger = LoggerFactory
67
        .getLogger(DefaultEditingBehavior.class);
68

    
69
    private DefaultEditingContext editingContext;
70

    
71
    private Point adjustedPoint;
72

    
73
    private static final Image imageCursor = new BufferedImage(32, 32,
74
        BufferedImage.TYPE_INT_ARGB);
75
    static {
76
        Graphics g = imageCursor.getGraphics();
77
        int size1 = 15;
78
        int x = 16;
79
        int y = 16;
80
        g.setColor(Color.MAGENTA);
81
        g.drawLine((x - size1), (y), (x + size1), (y));
82
        g.drawLine((x), (y - size1), (x), (y + size1));
83
        g.drawRect((x - 6), (y - 6), 12, 12);
84
        g.drawRect((x - 3), (y - 3), 6, 6);
85
    }
86

    
87
    public DefaultEditingBehavior(DefaultEditingContext editingContext)
88
        throws CreateEditingBehaviorException {
89

    
90
        if (editingContext.getMapControl() != null) {
91
            this.editingContext = editingContext;
92
            setMapControl(editingContext.getMapControl());
93
        }
94
    }
95

    
96
    @Override
97
    public ToolListener getListener() {
98
        return new ToolListener() {
99

    
100
            @Override
101
            public boolean cancelDrawing() {
102
                return false;
103
            }
104

    
105
            @Override
106
            public Image getImageCursor() {
107
                return imageCursor;
108
            }
109
        };
110
    }
111

    
112
    @Override
113
    public void mouseClicked(MouseEvent e) {
114
        ViewPort vp = editingContext.getMapControl().getViewPort();
115
        EditingServiceParameter currentParam = editingContext.getCurrentParam();
116
        EditingService activeService = editingContext.getActiveService();
117

    
118
        if ((activeService != null) && (currentParam != null)) {
119

    
120
            if (SwingUtilities.isLeftMouseButton(e)) {
121

    
122
                Set<TYPE> typesOfParam = currentParam.getTypes();
123

    
124
                if (typesOfParam.contains(TYPE.LIST_POSITIONS)) {
125
                    if (e.getClickCount() == 2) {
126
                        editingContext.finishService();
127
                        return;
128
                    }
129
                }
130

    
131
                Point point = null;
132
                Point2D point2D = getMapControl().getMapAdjustedPoint();
133
                if (point2D == null) {
134
                    point2D = getMapControl().getAdjustedPoint();
135
                    point = vp.convertToMapPoint(point2D);
136
                } else {
137
                    try {
138
                        point =
139
                            GeometryLocator.getGeometryManager().createPoint(
140
                                point2D.getX(), point2D.getY(),
141
                                Geometry.SUBTYPES.GEOM2D);
142
                    } catch (BaseException ex) {
143
                        logger.warn("Can't create point geometry from "
144
                            + point2D.toString());
145
                    }
146
                }
147

    
148
                try {
149
                    //FIXME: Estar?a bien hacer esto
150
                    editingContext.setValue(currentParam, point);
151
                    editingContext.nextParameter();
152

    
153
//                    editingContext.getActiveService().setValue(point);
154
                } catch (VectorEditingException ex) {
155
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
156
                    editingContext.showConsoleMessage(i18nManager
157
                        .getTranslation("invalid_option"));
158
                }
159

    
160
//                editingContext.nextParameter();
161

    
162
            } else if (SwingUtilities.isRightMouseButton(e)) {
163

    
164
                EditingSwingManager swingManager =
165
                    EditingSwingLocator.getSwingManager();
166

    
167
                    EditingContextMenu contextMenu =
168
                        swingManager.getContextualMenu(getMapControl(), (Object value) -> {
169
                            editingContext.textEntered((String) value);
170
                    }, currentParam, editingContext);
171

    
172
                    contextMenu.show(getMapControl(), e.getX(), e.getY());
173
            }
174
        }
175
    }
176

    
177
    @Override
178
    public void mouseEntered(MouseEvent e) throws BehaviorException {
179
    }
180

    
181
    @Override
182
    public void mouseMoved(MouseEvent e) throws BehaviorException {
183
        ViewPort vp = editingContext.getMapControl().getViewPort();
184
        adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY());
185
    }
186

    
187
    @Override
188
    public void mousePressed(MouseEvent e) throws BehaviorException {
189
    }
190

    
191
    @Override
192
    public void mouseReleased(MouseEvent e) throws BehaviorException {
193
    }
194

    
195
    @Override
196
    @SuppressWarnings("rawtypes")
197
    public void paintComponent(MapControlDrawer mapControlDrawer) {
198
        super.paintComponent(mapControlDrawer);
199

    
200
        if ((editingContext.getActiveService() == null)
201
            || (adjustedPoint == null)) {
202
            return;
203
        }
204

    
205
        DrawingStatus helperGeo = null;
206

    
207
        try {
208
            helperGeo =
209
                editingContext.getActiveService().getDrawingStatus(
210
                    adjustedPoint);
211
        } catch (VectorEditingException e) {
212
            logger.info("An error ocurred when draw service geometries", e);
213
        }
214

    
215
        if (helperGeo != null) {
216
            for (Iterator iterator = helperGeo.getStatus().iterator(); iterator
217
                .hasNext();) {
218
                Status status = (Status) iterator.next();
219
                ISymbol symbol = status.getSymbol();
220
                if (symbol == null) {
221
                    symbol =
222
                        MapControlLocator.getMapControlManager()
223
                            .getAxisReferenceSymbol();
224
                }
225
                if (symbol instanceof ITextSymbol) {
226
                    ((ITextSymbol) symbol).setText(status.getText());
227
                }
228
                Geometry geom = status.getGeometry();
229
                if(geom != null){
230
                    if(geom.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT)){
231
                        MultiPoint multipoint = (MultiPoint)geom;
232
                        for (Geometry geometry : multipoint) {
233
                            editingContext.getMapControl().getMapControlDrawer()
234
                                .draw(geometry, symbol);
235
                        }
236
                    } else {
237
                        editingContext.getMapControl().getMapControlDrawer()
238
                            .draw(geom, symbol);
239
                    }
240
                }
241
            }
242
        }
243
    }
244
}