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 @ 2204
History | View | Annotate | Download (8.44 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 |
//FIXME: Estar?a bien hacer esto
|
151 |
editingContext.setValue(currentParam, point); |
152 |
// editingContext.getActiveService().setValue(point);
|
153 |
} catch (VectorEditingException ex) {
|
154 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
155 |
editingContext.showConsoleMessage(i18nManager |
156 |
.getTranslation("invalid_option"));
|
157 |
} |
158 |
|
159 |
// editingContext.nextParameter();
|
160 |
|
161 |
} else if (SwingUtilities.isRightMouseButton(e)) { |
162 |
|
163 |
EditingSwingManager swingManager = |
164 |
EditingSwingLocator.getSwingManager(); |
165 |
|
166 |
EditingContextMenu contextMenu = |
167 |
swingManager.getContextualMenu(getMapControl(), |
168 |
new AcceptValueListener() {
|
169 |
|
170 |
public void acceptValue(Object value) { |
171 |
editingContext.textEntered((String) value);
|
172 |
|
173 |
} |
174 |
}, currentParam, editingContext); |
175 |
|
176 |
contextMenu.show(getMapControl(), e.getX(), e.getY()); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
187 |
ViewPort vp = editingContext.getMapControl().getViewPort(); |
188 |
adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY()); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
197 |
} |
198 |
|
199 |
@Override
|
200 |
@SuppressWarnings("rawtypes") |
201 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
202 |
super.paintComponent(mapControlDrawer);
|
203 |
|
204 |
if ((editingContext.getActiveService() == null) |
205 |
|| (adjustedPoint == null)) {
|
206 |
return;
|
207 |
} |
208 |
|
209 |
DrawingStatus helperGeo = null;
|
210 |
|
211 |
try {
|
212 |
helperGeo = |
213 |
editingContext.getActiveService().getDrawingStatus( |
214 |
adjustedPoint); |
215 |
} catch (VectorEditingException e) {
|
216 |
logger.info("An error ocurred when draw service geometries", e);
|
217 |
} |
218 |
|
219 |
if (helperGeo != null) { |
220 |
for (Iterator iterator = helperGeo.getStatus().iterator(); iterator |
221 |
.hasNext();) { |
222 |
Status status = (Status) iterator.next(); |
223 |
ISymbol symbol = status.getSymbol(); |
224 |
if (symbol == null) { |
225 |
symbol = |
226 |
MapControlLocator.getMapControlManager() |
227 |
.getAxisReferenceSymbol(); |
228 |
} |
229 |
if (symbol instanceof ITextSymbol) { |
230 |
((ITextSymbol) symbol).setText(status.getText()); |
231 |
} |
232 |
editingContext.getMapControl().getMapControlDrawer() |
233 |
.draw(status.getGeometry(), symbol); |
234 |
} |
235 |
} |
236 |
} |
237 |
} |