root / org.gvsig.legend.urbanhorizontalsignage / trunk / org.gvsig.legend.urbanhorizontalsignage / org.gvsig.legend.urbanhorizontalsignage.app / org.gvsig.legend.urbanhorizontalsignage.app.mainplugin / src / main / java / org / gvsig / legend / urbanhorizontalsignage / app / rotate / RotatePointBehavior.java @ 5239
History | View | Annotate | Download (6.15 KB)
1 |
package org.gvsig.legend.urbanhorizontalsignage.app.rotate; |
---|---|
2 |
|
3 |
import java.awt.event.InputEvent; |
4 |
import java.awt.event.MouseEvent; |
5 |
import java.awt.geom.AffineTransform; |
6 |
import java.awt.geom.Point2D; |
7 |
import javax.swing.SwingUtilities; |
8 |
import org.gvsig.euclidean.EuclideanLine2D; |
9 |
import org.gvsig.euclidean.EuclideanManager; |
10 |
import org.gvsig.fmap.dal.feature.Feature; |
11 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
12 |
import org.gvsig.fmap.geom.Geometry; |
13 |
import org.gvsig.fmap.geom.primitive.Point; |
14 |
import org.gvsig.fmap.mapcontext.ViewPort; |
15 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
16 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
17 |
import org.gvsig.fmap.mapcontrol.MapControl; |
18 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
19 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
20 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
21 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
22 |
import org.gvsig.legend.urbanverticalsignage.lib.api.PictureMarkerSymbolTableLegend; |
23 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
24 |
import org.gvsig.tools.util.ToolsUtilLocator; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author gvSIG Team
|
31 |
*/
|
32 |
@SuppressWarnings("UseSpecificCatch") |
33 |
public class RotatePointBehavior extends Behavior { |
34 |
|
35 |
protected static final Logger LOGGER = LoggerFactory.getLogger(RotatePointBehavior.class); |
36 |
|
37 |
protected RotatePointListener listener;
|
38 |
|
39 |
private double angle; |
40 |
private Point2D currentPoint; |
41 |
private Point2D centerPoint; |
42 |
private IMarkerSymbol symbol;
|
43 |
private Feature feature;
|
44 |
private String layerName; |
45 |
private PictureMarkerSymbolTableLegend legend;
|
46 |
|
47 |
public RotatePointBehavior(RotatePointListener listener) {
|
48 |
this.listener = listener;
|
49 |
} |
50 |
|
51 |
@Override
|
52 |
public void setMapControl(MapControl mc) { |
53 |
super.setMapControl(mc);
|
54 |
updateCurrentValues(); |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public void paintComponent(MapControlDrawer drawer) { |
59 |
if (this.feature == null || this.symbol == null || this.currentPoint==null) { |
60 |
return;
|
61 |
} |
62 |
Geometry geom = this.feature.getDefaultGeometry();
|
63 |
if (geom == null) { |
64 |
return;
|
65 |
} |
66 |
this.symbol.setRotation(angle);
|
67 |
drawer.draw(geom, this.symbol);
|
68 |
|
69 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
70 |
double d = distance(this.centerPoint, this.currentPoint); |
71 |
drawer.drawLine( |
72 |
(int) this.centerPoint.getX(), (int) this.centerPoint.getY(), |
73 |
(int) this.currentPoint.getX(), (int) this.currentPoint.getY() |
74 |
); |
75 |
drawer.drawLine( |
76 |
(int) this.centerPoint.getX(), (int) this.centerPoint.getY(), |
77 |
(int) (this.centerPoint.getX()+d), (int) this.centerPoint.getY() |
78 |
); |
79 |
} |
80 |
|
81 |
private void updateCurrentValues() { |
82 |
this.angle = 0; |
83 |
this.centerPoint = null; |
84 |
this.currentPoint = null; |
85 |
|
86 |
this.feature = null; |
87 |
this.layerName = null; |
88 |
this.legend = null; |
89 |
this.symbol = null; |
90 |
try {
|
91 |
FLayer[] layers = this.getMapControl().getMapContext().getLayers().getActives(); |
92 |
if (layers == null || layers.length != 1) { |
93 |
return;
|
94 |
} |
95 |
FLyrVect layer = (FLyrVect) layers[0];
|
96 |
FeatureSelection selection = layer.getFeatureStore().getFeatureSelection(); |
97 |
feature = selection.first(); |
98 |
if (symbol == null && feature != null) { |
99 |
this.legend = (PictureMarkerSymbolTableLegend) layer.getLegend();
|
100 |
symbol = (IMarkerSymbol) legend.getSymbolByFeature(feature); |
101 |
} |
102 |
Point point = (Point) feature.getDefaultGeometry(); |
103 |
ViewPort vp = this.getMapControl().getViewPort();
|
104 |
this.centerPoint = vp.fromMapPoint(point.getX(), point.getY());
|
105 |
} catch (Exception ex) { |
106 |
this.angle = 0; |
107 |
this.currentPoint = null; |
108 |
|
109 |
this.centerPoint = null; |
110 |
this.legend = null; |
111 |
this.feature = null; |
112 |
this.layerName = null; |
113 |
this.symbol = null; |
114 |
} |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void mouseMoved(MouseEvent mouseEvent) throws BehaviorException { |
119 |
|
120 |
if (((mouseEvent.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK |
121 |
| InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.META_DOWN_MASK)) != 0) |
122 |
|| SwingUtilities.isLeftMouseButton(mouseEvent)) {
|
123 |
return;
|
124 |
} |
125 |
try {
|
126 |
if (mouseEvent.getClickCount() == 1) { |
127 |
listener.rotate(feature, legend.getRotationFieldName(), angle); |
128 |
return;
|
129 |
} |
130 |
this.currentPoint = mouseEvent.getPoint();
|
131 |
|
132 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
133 |
double d = distance(this.centerPoint, this.currentPoint); |
134 |
EuclideanLine2D line = euclideanManager.createLine2D( |
135 |
this.centerPoint.getX(), this.centerPoint.getY(), |
136 |
this.currentPoint.getX(), this.currentPoint.getY() |
137 |
); |
138 |
// EuclideanLine2D line0 = euclideanManager.createLine2D(
|
139 |
// this.centerPoint.getX(), this.centerPoint.getY(),
|
140 |
// this.centerPoint.getX()+d, this.centerPoint.getY()
|
141 |
// );
|
142 |
// Point2D.Double p = new Point2D.Double(this.centerPoint.getX()+d, this.centerPoint.getY()-1);
|
143 |
this.angle = Math.toDegrees(line.getAngle()); |
144 |
LOGGER.info("Angulo: " + angle);
|
145 |
|
146 |
} catch (Exception ex) { |
147 |
LOGGER.warn("Can't rotate from point of the layer " + layerName, ex);
|
148 |
} |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
153 |
mouseMoved(e); |
154 |
} |
155 |
|
156 |
public void setListener(ToolListener listener) { |
157 |
this.listener = (RotatePointListener) listener;
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public ToolListener getListener() {
|
162 |
return listener;
|
163 |
} |
164 |
|
165 |
public double distance(Point2D p1, Point2D p2) { |
166 |
double x = Math.sqrt( Math.pow(p2.getX()-p1.getX(), 2)+Math.pow(p2.getY()-p1.getY(), 2)); |
167 |
return x;
|
168 |
} |
169 |
} |