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

History | View | Annotate | Download (8.31 KB)

1 159 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3 52 fdiaz
 *
4 159 llmarques
 * 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 9 llmarques
 */
24 159 llmarques
25 42 llmarques
package org.gvsig.vectorediting.swing.impl;
26 9 llmarques
27 15 llmarques
import java.awt.Color;
28
import java.awt.Graphics;
29
import java.awt.Image;
30 10 llmarques
import java.awt.event.MouseEvent;
31 372 llmarques
import java.awt.geom.Point2D;
32 15 llmarques
import java.awt.image.BufferedImage;
33 48 llmarques
import java.util.Iterator;
34 156 llmarques
import java.util.Set;
35 10 llmarques
36 191 llmarques
import javax.swing.SwingUtilities;
37
38 109 llmarques
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40
41 372 llmarques
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.GeometryLocator;
43 37 llmarques
import org.gvsig.fmap.geom.primitive.Point;
44 23 llmarques
import org.gvsig.fmap.mapcontext.ViewPort;
45 307 fdiaz
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
47 28 llmarques
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.fmap.mapcontrol.MapControlLocator;
49 10 llmarques
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
50 9 llmarques
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
51
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
52 65 fdiaz
import org.gvsig.tools.ToolsLocator;
53 372 llmarques
import org.gvsig.tools.exception.BaseException;
54 65 fdiaz
import org.gvsig.tools.i18n.I18nManager;
55 55 fdiaz
import org.gvsig.vectorediting.lib.api.DrawingStatus;
56 307 fdiaz
import org.gvsig.vectorediting.lib.api.DrawingStatus.Status;
57 15 llmarques
import org.gvsig.vectorediting.lib.api.EditingService;
58
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
59 44 llmarques
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
60 78 fdiaz
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException;
61 61 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
62 191 llmarques
import org.gvsig.vectorediting.swing.api.EditingSwingLocator;
63
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
64
import org.gvsig.vectorediting.swing.api.contextmenu.AcceptValueListener;
65
import org.gvsig.vectorediting.swing.api.contextmenu.EditingContextMenu;
66 9 llmarques
67 226 llmarques
public class DefaultEditingBehavior extends Behavior {
68 15 llmarques
69 109 llmarques
    private static final Logger logger = LoggerFactory
70
        .getLogger(DefaultEditingBehavior.class);
71 53 fdiaz
72 226 llmarques
    private DefaultEditingContext editingContext;
73 35 llmarques
74 109 llmarques
    private Point adjustedPoint;
75 23 llmarques
76 109 llmarques
    private static final Image imageCursor = new BufferedImage(32, 32,
77
        BufferedImage.TYPE_INT_ARGB);
78
    static {
79
        Graphics g = imageCursor.getGraphics();
80
        int size1 = 15;
81
        int x = 16;
82
        int y = 16;
83
        g.setColor(Color.MAGENTA);
84
        g.drawLine((x - size1), (y), (x + size1), (y));
85
        g.drawLine((x), (y - size1), (x), (y + size1));
86
        g.drawRect((x - 6), (y - 6), 12, 12);
87
        g.drawRect((x - 3), (y - 3), 6, 6);
88
    }
89 89 llmarques
90 226 llmarques
    public DefaultEditingBehavior(DefaultEditingContext editingContext)
91 109 llmarques
        throws CreateEditingBehaviorException {
92 26 llmarques
93 109 llmarques
        if (editingContext.getMapControl() != null) {
94
            this.editingContext = editingContext;
95 321 fdiaz
            setMapControl(editingContext.getMapControl());
96 109 llmarques
        }
97 80 llmarques
    }
98 48 llmarques
99 109 llmarques
    @Override
100
    public ToolListener getListener() {
101
        return new ToolListener() {
102 28 llmarques
103 109 llmarques
            public boolean cancelDrawing() {
104
                return false;
105
            }
106
107
            public Image getImageCursor() {
108
                return imageCursor;
109
            }
110
        };
111 80 llmarques
    }
112 26 llmarques
113 227 llmarques
    @Override
114 109 llmarques
    public void mouseClicked(MouseEvent e) {
115
        ViewPort vp = editingContext.getMapControl().getViewPort();
116 226 llmarques
        EditingServiceParameter currentParam = editingContext.getCurrentParam();
117
        EditingService activeService = editingContext.getActiveService();
118 130 llmarques
119 227 llmarques
        if ((activeService != null) && (currentParam != null)) {
120 226 llmarques
121 191 llmarques
            if (SwingUtilities.isLeftMouseButton(e)) {
122
123 156 llmarques
                Set<TYPE> typesOfParam = currentParam.getTypes();
124 130 llmarques
125 365 llmarques
                if (typesOfParam.contains(TYPE.LIST_POSITIONS)) {
126
                    if (e.getClickCount() == 2) {
127
                        editingContext.finishService();
128
                        return;
129 109 llmarques
                    }
130 365 llmarques
                }
131 130 llmarques
132 372 llmarques
                Point point = null;
133
                Point2D point2D = getMapControl().getMapAdjustedPoint();
134
                if (point2D == null) {
135
                    point2D = getMapControl().getAdjustedPoint();
136
                    point = vp.convertToMapPoint(point2D);
137 366 llmarques
                } else {
138 372 llmarques
                    try {
139
                        point =
140
                            GeometryLocator.getGeometryManager().createPoint(
141
                                point2D.getX(), point2D.getY(),
142
                                Geometry.SUBTYPES.GEOM2D);
143
                    } catch (BaseException ex) {
144
                        logger.warn("Can't create point geometry from "
145
                            + point2D.toString());
146
                    }
147 366 llmarques
                }
148 130 llmarques
149 365 llmarques
                try {
150
                    editingContext.getActiveService().setValue(point);
151
                } catch (VectorEditingException ex) {
152
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
153
                    editingContext.showConsoleMessage(i18nManager
154
                        .getTranslation("invalid_option"));
155 109 llmarques
                }
156 130 llmarques
157 365 llmarques
                editingContext.getNextParameter();
158 191 llmarques
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 226 llmarques
                                editingContext.textEntered((String) value);
170 191 llmarques
171
                            }
172
                        }, currentParam);
173
174 227 llmarques
                contextMenu.show(getMapControl(), e.getX(), e.getY());
175 109 llmarques
            }
176
        }
177
    }
178 28 llmarques
179 227 llmarques
    @Override
180 109 llmarques
    public void mouseEntered(MouseEvent e) throws BehaviorException {
181 80 llmarques
    }
182 78 fdiaz
183 227 llmarques
    @Override
184 109 llmarques
    public void mouseMoved(MouseEvent e) throws BehaviorException {
185
        ViewPort vp = editingContext.getMapControl().getViewPort();
186
        adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY());
187 80 llmarques
    }
188 35 llmarques
189 227 llmarques
    @Override
190 109 llmarques
    public void mousePressed(MouseEvent e) throws BehaviorException {
191 80 llmarques
    }
192 28 llmarques
193 227 llmarques
    @Override
194 109 llmarques
    public void mouseReleased(MouseEvent e) throws BehaviorException {
195 80 llmarques
    }
196 78 fdiaz
197 227 llmarques
    @Override
198 156 llmarques
    @SuppressWarnings("rawtypes")
199 109 llmarques
    public void paintComponent(MapControlDrawer mapControlDrawer) {
200
        super.paintComponent(mapControlDrawer);
201 140 llmarques
202 227 llmarques
        if ((editingContext.getActiveService() == null)
203
            || (adjustedPoint == null)) {
204 109 llmarques
            return;
205
        }
206
207
        DrawingStatus helperGeo = null;
208 140 llmarques
209 89 llmarques
        try {
210 226 llmarques
            helperGeo =
211
                editingContext.getActiveService().getDrawingStatus(
212
                    adjustedPoint);
213 109 llmarques
        } catch (VectorEditingException e) {
214 130 llmarques
            logger.info("An error ocurred when draw service geometries", e);
215 89 llmarques
        }
216 140 llmarques
217 109 llmarques
        if (helperGeo != null) {
218 307 fdiaz
            for (Iterator iterator = helperGeo.getStatus().iterator(); iterator
219 109 llmarques
                .hasNext();) {
220 307 fdiaz
                Status status = (Status) iterator.next();
221
                ISymbol symbol = status.getSymbol();
222 365 llmarques
                if (symbol == null) {
223 372 llmarques
                    symbol =
224
                        MapControlLocator.getMapControlManager()
225 365 llmarques
                            .getAxisReferenceSymbol();
226 307 fdiaz
                }
227
                if (symbol instanceof ITextSymbol) {
228
                    ((ITextSymbol) symbol).setText(status.getText());
229
                }
230 365 llmarques
                editingContext.getMapControl().getMapControlDrawer()
231
                    .draw(status.getGeometry(), symbol);
232 109 llmarques
            }
233 89 llmarques
        }
234 80 llmarques
    }
235 9 llmarques
}