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

History | View | Annotate | Download (7.76 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.image.BufferedImage;
32
import java.util.Iterator;
33
import java.util.Set;
34

    
35
import javax.swing.SwingUtilities;
36

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

    
40
import org.gvsig.fmap.geom.primitive.Point;
41
import org.gvsig.fmap.mapcontext.ViewPort;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
44
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
45
import org.gvsig.fmap.mapcontrol.MapControlLocator;
46
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
47
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
48
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
49
import org.gvsig.tools.ToolsLocator;
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

    
63
public class DefaultEditingBehavior extends Behavior {
64

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

    
68
    private DefaultEditingContext editingContext;
69

    
70
    private Point adjustedPoint;
71

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

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

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

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

    
99
            public boolean cancelDrawing() {
100
                return false;
101
            }
102

    
103
            public Image getImageCursor() {
104
                return imageCursor;
105
            }
106
        };
107
    }
108

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

    
115
        if ((activeService != null) && (currentParam != null)) {
116

    
117
            if (SwingUtilities.isLeftMouseButton(e)) {
118

    
119
                Set<TYPE> typesOfParam = currentParam.getTypes();
120

    
121
                if (typesOfParam.contains(TYPE.LIST_POSITIONS)
122
                    || typesOfParam.contains(TYPE.POSITION)) {
123

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

    
131
                    Point point;
132
                    point = vp.convertToMapPoint(e.getX(), e.getY());
133

    
134
                    try {
135
                        editingContext.getActiveService().setValue(point);
136
                    } catch (VectorEditingException ex) {
137
                        I18nManager i18nManager = ToolsLocator.getI18nManager();
138
                        editingContext.showConsoleMessage(i18nManager
139
                            .getTranslation("invalid_option"));
140
                    }
141

    
142
                    editingContext.getNextParameter();
143
                }
144

    
145

    
146
            } else if (SwingUtilities.isRightMouseButton(e)) {
147

    
148
                EditingSwingManager swingManager =
149
                    EditingSwingLocator.getSwingManager();
150

    
151
                EditingContextMenu contextMenu =
152
                    swingManager.getContextualMenu(getMapControl(),
153
                        new AcceptValueListener() {
154

    
155
                            public void acceptValue(Object value) {
156
                                editingContext.textEntered((String) value);
157

    
158
                            }
159
                        }, currentParam);
160

    
161
                contextMenu.show(getMapControl(), e.getX(), e.getY());
162
            }
163
        }
164
    }
165

    
166
    @Override
167
    public void mouseEntered(MouseEvent e) throws BehaviorException {
168
    }
169

    
170
    @Override
171
    public void mouseMoved(MouseEvent e) throws BehaviorException {
172
        ViewPort vp = editingContext.getMapControl().getViewPort();
173
        adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY());
174
    }
175

    
176
    @Override
177
    public void mousePressed(MouseEvent e) throws BehaviorException {
178
    }
179

    
180
    @Override
181
    public void mouseReleased(MouseEvent e) throws BehaviorException {
182
    }
183

    
184
    @Override
185
    @SuppressWarnings("rawtypes")
186
    public void paintComponent(MapControlDrawer mapControlDrawer) {
187
        super.paintComponent(mapControlDrawer);
188

    
189
        if ((editingContext.getActiveService() == null)
190
            || (adjustedPoint == null)) {
191
            return;
192
        }
193

    
194
        DrawingStatus helperGeo = null;
195

    
196
        try {
197
            helperGeo =
198
                editingContext.getActiveService().getDrawingStatus(
199
                    adjustedPoint);
200
        } catch (VectorEditingException e) {
201
            logger.info("An error ocurred when draw service geometries", e);
202
        }
203

    
204
        if (helperGeo != null) {
205
            for (Iterator iterator = helperGeo.getStatus().iterator(); iterator
206
                .hasNext();) {
207
                Status status = (Status) iterator.next();
208
                ISymbol symbol = status.getSymbol();
209
                if (symbol==null){
210
                    symbol = MapControlLocator.getMapControlManager()
211
                        .getAxisReferenceSymbol();
212
                }
213
                if (symbol instanceof ITextSymbol) {
214
                    ((ITextSymbol) symbol).setText(status.getText());
215
                }
216
                editingContext
217
                    .getMapControl()
218
                    .getMapControlDrawer()
219
                    .draw(
220
                        status.getGeometry(),
221
                        symbol);
222
            }
223
        }
224
    }
225
}