root / trunk / extensions / extGraph / src / org / gvsig / graph / tools / BarrierListener.java @ 39203
History | View | Annotate | Download (4.52 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.graph.tools; |
42 |
|
43 |
import java.awt.Cursor; |
44 |
import java.awt.geom.Point2D; |
45 |
|
46 |
import javax.swing.ImageIcon; |
47 |
import javax.swing.JOptionPane; |
48 |
|
49 |
import org.gvsig.graph.core.GvModifiedCost; |
50 |
import org.gvsig.graph.core.Network; |
51 |
|
52 |
import com.iver.andami.PluginServices; |
53 |
import com.iver.cit.gvsig.fmap.MapControl; |
54 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
55 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
56 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
57 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
58 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
59 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
60 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
61 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
62 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
63 |
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent; |
64 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener; |
65 |
|
66 |
public class BarrierListener implements PointListener { |
67 |
private MapControl mapCtrl;
|
68 |
|
69 |
private int idSymbolBarrier = -1; |
70 |
|
71 |
private Cursor cur = java.awt.Cursor |
72 |
.getPredefinedCursor(Cursor.HAND_CURSOR);
|
73 |
|
74 |
public BarrierListener(MapControl mc) {
|
75 |
this.mapCtrl = mc;
|
76 |
} |
77 |
|
78 |
/*
|
79 |
* (non-Javadoc)
|
80 |
*
|
81 |
* @see org.gvsig.fmap.tools.Listeners.PointListener#point(org.gvsig.fmap.tools.Events.PointEvent)
|
82 |
* The PointEvent method bring you a point in pixel coordinates. You
|
83 |
* need to transform it to world coordinates. The class to do
|
84 |
* conversions is ViewPort, obtained thru the MapContext of mapCtrl.
|
85 |
*/
|
86 |
public void point(PointEvent event) throws BehaviorException { |
87 |
|
88 |
Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
|
89 |
event.getPoint()); |
90 |
|
91 |
SingleLayerIterator it = new SingleLayerIterator(mapCtrl
|
92 |
.getMapContext().getLayers()); |
93 |
while (it.hasNext()) {
|
94 |
FLayer aux = it.next(); |
95 |
if (!aux.isActive())
|
96 |
continue;
|
97 |
Network net = (Network) aux.getProperty("network");
|
98 |
|
99 |
if (net != null) { |
100 |
Point2D nearestPoint = new Point2D.Double(); |
101 |
double realTol = mapCtrl.getViewPort().toMapDistance(
|
102 |
FlagListener.pixelTolerance); |
103 |
int idArc = net.findClosestArc(pReal.getX(), pReal.getY(),
|
104 |
realTol, nearestPoint); |
105 |
if (idArc == -1) { |
106 |
JOptionPane.showMessageDialog(null, "No est? sobre la red"); |
107 |
return;
|
108 |
} |
109 |
|
110 |
GvModifiedCost modCost = net.addModifiedCost(idArc, -1.0, 3); |
111 |
GraphicLayer graphicLayer = mapCtrl.getMapContext() |
112 |
.getGraphicsLayer(); |
113 |
if (idSymbolBarrier == -1) { |
114 |
FSymbol simFlag = new FSymbol(FConstant.SYMBOL_TYPE_ICON);
|
115 |
simFlag.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN); |
116 |
simFlag.setSizeInPixels(true);
|
117 |
simFlag.setSize(16);
|
118 |
ImageIcon icon = new ImageIcon(this.getClass() |
119 |
.getClassLoader().getResource("images/barrier.png"));
|
120 |
simFlag.setIcon(icon.getImage()); |
121 |
|
122 |
idSymbolBarrier = graphicLayer.addSymbol(simFlag); |
123 |
} |
124 |
IGeometry gAux = ShapeFactory.createPoint2D(nearestPoint.getX(), |
125 |
nearestPoint.getY()); |
126 |
FGraphic graphic = new FGraphic(gAux, idSymbolBarrier);
|
127 |
graphic.setTag("BARRIER");
|
128 |
graphic.setObjectTag(modCost); |
129 |
graphicLayer.addGraphic(graphic); |
130 |
mapCtrl.drawGraphics(); |
131 |
PluginServices.getMainFrame().enableControls(); |
132 |
|
133 |
} |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
public Cursor getCursor() { |
139 |
return cur;
|
140 |
} |
141 |
|
142 |
public boolean cancelDrawing() { |
143 |
return false; |
144 |
} |
145 |
|
146 |
public void pointDoubleClick(PointEvent event) throws BehaviorException { |
147 |
|
148 |
} |
149 |
|
150 |
} |