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 @ 2444

History | View | Annotate | Download (8.93 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.AcceptValueListener;
61
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

    
65
public class DefaultEditingBehavior extends Behavior {
66

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

    
70
    private DefaultEditingContext editingContext;
71

    
72
    private Point adjustedPoint;
73

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

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

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

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

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

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

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

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

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

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

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

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

    
147
                try {
148
                    //FIXME: Estar?a bien hacer esto
149
                    editingContext.setValue(currentParam, point);
150
//                    editingContext.getActiveService().setValue(point);
151
                } catch (VectorEditingException ex) {
152
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
153
                    editingContext.showConsoleMessage(i18nManager
154
                        .getTranslation("invalid_option"));
155
                }
156

    
157
//                editingContext.nextParameter();
158

    
159
            } else if (SwingUtilities.isRightMouseButton(e)) {
160

    
161
                EditingSwingManager swingManager =
162
                    EditingSwingLocator.getSwingManager();
163

    
164
                EditingContextMenu contextMenu =
165
                    swingManager.getContextualMenu(getMapControl(),
166
                        new AcceptValueListener() {
167

    
168
                            public void acceptValue(Object value) {
169
                                editingContext.textEntered((String) value);
170

    
171
                            }
172
                        }, currentParam, editingContext);
173

    
174
                contextMenu.show(getMapControl(), e.getX(), e.getY());
175
            }
176
        }
177
    }
178

    
179
    @Override
180
    public void mouseEntered(MouseEvent e) throws BehaviorException {
181
    }
182

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

    
189
    @Override
190
    public void mousePressed(MouseEvent e) throws BehaviorException {
191
    }
192

    
193
    @Override
194
    public void mouseReleased(MouseEvent e) throws BehaviorException {
195
    }
196

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

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

    
207
        DrawingStatus helperGeo = null;
208

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

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