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

History | View | Annotate | Download (7.67 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
                    if (e.getClickCount() == 2) {
123
                        editingContext.finishService();
124
                        return;
125
                    }
126
                }
127

    
128
                Point point; 
129
                if(getMapControl().getAdjustedPoint() != null){
130
                    point = vp.convertToMapPoint(getMapControl().getAdjustedPoint());
131
                } else {
132
                    point = vp.convertToMapPoint(e.getX(), e.getY());
133
                }
134

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

    
143
                editingContext.getNextParameter();
144

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

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

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

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

    
157
                            }
158
                        }, currentParam);
159

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

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

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

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

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

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

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

    
193
        DrawingStatus helperGeo = null;
194

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

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