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

History | View | Annotate | Download (8.32 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

    
36
import javax.swing.SwingUtilities;
37

    
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.GeometryLocator;
43
import org.gvsig.fmap.geom.primitive.Point;
44
import org.gvsig.fmap.mapcontext.ViewPort;
45
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
47
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.fmap.mapcontrol.MapControlLocator;
49
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
50
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
51
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.exception.BaseException;
54
import org.gvsig.tools.i18n.I18nManager;
55
import org.gvsig.vectorediting.lib.api.DrawingStatus;
56
import org.gvsig.vectorediting.lib.api.DrawingStatus.Status;
57
import org.gvsig.vectorediting.lib.api.EditingService;
58
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
59
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
60
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException;
61
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
62
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

    
67
public class DefaultEditingBehavior extends Behavior {
68

    
69
    private static final Logger logger = LoggerFactory
70
        .getLogger(DefaultEditingBehavior.class);
71

    
72
    private DefaultEditingContext editingContext;
73

    
74
    private Point adjustedPoint;
75

    
76
    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

    
90
    public DefaultEditingBehavior(DefaultEditingContext editingContext)
91
        throws CreateEditingBehaviorException {
92

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

    
99
    @Override
100
    public ToolListener getListener() {
101
        return new ToolListener() {
102

    
103
            public boolean cancelDrawing() {
104
                return false;
105
            }
106

    
107
            public Image getImageCursor() {
108
                return imageCursor;
109
            }
110
        };
111
    }
112

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

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

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

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

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

    
132
                Point point = null;
133
                Point2D point2D = getMapControl().getMapAdjustedPoint();
134
                if (point2D == null) {
135
                    point2D = getMapControl().getAdjustedPoint();
136
                    point = vp.convertToMapPoint(point2D);
137
                } else {
138
                    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
                }
148

    
149
                try {
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
                editingContext.getMapControl().getMapControlDrawer()
231
                    .draw(status.getGeometry(), symbol);
232
            }
233
        }
234
    }
235
}