Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / tools / BarrierListener.java @ 31809

History | View | Annotate | Download (4.55 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
import java.io.IOException;
46

    
47
import javax.swing.ImageIcon;
48
import javax.swing.JOptionPane;
49

    
50
import org.gvsig.graph.core.GvModifiedCost;
51
import org.gvsig.graph.core.Network;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.cit.gvsig.fmap.MapControl;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
57
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
58
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
61
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
62
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
63
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
64
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
65
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
66

    
67
public class BarrierListener implements PointListener {
68
        private MapControl mapCtrl;
69

    
70
        private int idSymbolBarrier = -1;
71

    
72
        private Cursor cur = java.awt.Cursor
73
                        .getPredefinedCursor(Cursor.HAND_CURSOR);
74

    
75
        public BarrierListener(MapControl mc) {
76
                this.mapCtrl = mc;
77
        }
78

    
79
        /*
80
         * (non-Javadoc)
81
         * 
82
         * @see org.gvsig.fmap.tools.Listeners.PointListener#point(org.gvsig.fmap.tools.Events.PointEvent)
83
         *      The PointEvent method bring you a point in pixel coordinates. You
84
         *      need to transform it to world coordinates. The class to do
85
         *      conversions is ViewPort, obtained thru the MapContext of mapCtrl.
86
         */
87
        public void point(PointEvent event) throws BehaviorException {
88

    
89
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(
90
                                event.getPoint());
91

    
92
                SingleLayerIterator it = new SingleLayerIterator(mapCtrl
93
                                .getMapContext().getLayers());
94
                while (it.hasNext()) {
95
                        FLayer aux = it.next();
96
                        if (!aux.isActive())
97
                                continue;
98
                        Network net = (Network) aux.getProperty("network");                        
99

    
100
                        if (net != null) {
101
                                Point2D nearestPoint = new Point2D.Double();
102
                                double realTol = mapCtrl.getViewPort().toMapDistance(
103
                                                FlagListener.pixelTolerance);
104
                                int idArc = net.findClosestArc(pReal.getX(), pReal.getY(),
105
                                                realTol, nearestPoint);
106
                                if (idArc == -1) {
107
                                        JOptionPane.showMessageDialog(null, "No est? sobre la red");
108
                                        return;
109
                                }
110
                                
111
                                GvModifiedCost modCost = net.addModifiedCost(idArc, -1.0, 3);
112
                                GraphicLayer graphicLayer = mapCtrl.getMapContext()
113
                                                .getGraphicsLayer();
114
                                if (idSymbolBarrier == -1) {
115
                                        FSymbol simFlag = new FSymbol(FConstant.SYMBOL_TYPE_ICON);
116
                                        simFlag.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN);
117
                                        simFlag.setSizeInPixels(true);
118
                                        simFlag.setSize(16);
119
                                        ImageIcon icon = new ImageIcon(this.getClass()
120
                                                        .getClassLoader().getResource("images/barrier.png"));
121
                                        simFlag.setIcon(icon.getImage());
122

    
123
                                        idSymbolBarrier = graphicLayer.addSymbol(simFlag);
124
                                }
125
                                IGeometry gAux = ShapeFactory.createPoint2D(nearestPoint.getX(),
126
                                                nearestPoint.getY());
127
                                FGraphic graphic = new FGraphic(gAux, idSymbolBarrier);
128
                                graphic.setTag("BARRIER");
129
                                graphic.setObjectTag(modCost);
130
                                graphicLayer.addGraphic(graphic);
131
                                mapCtrl.drawGraphics();
132
                                PluginServices.getMainFrame().enableControls();
133

    
134
                        }
135
                }
136

    
137
        }
138

    
139
        public Cursor getCursor() {
140
                return cur;
141
        }
142

    
143
        public boolean cancelDrawing() {
144
                return false;
145
        }
146

    
147
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
148

    
149
        }
150

    
151
}