Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / com / iver / cit / gvsig / graph / tools / FlagListener.java @ 20103

History | View | Annotate | Download (4.02 KB)

1 8050 fjp
/* 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 com.iver.cit.gvsig.graph.tools;
42
43
import java.awt.Cursor;
44
import java.awt.geom.Point2D;
45
46
import javax.swing.JOptionPane;
47
48 8357 fjp
import com.iver.andami.PluginServices;
49 8261 fjp
import com.iver.andami.messages.NotificationManager;
50 8050 fjp
import com.iver.cit.gvsig.fmap.MapControl;
51
import com.iver.cit.gvsig.fmap.layers.FLayer;
52
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
53
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
54
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
55
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
56 13583 fjp
import com.iver.cit.gvsig.graph.core.GraphException;
57 8050 fjp
import com.iver.cit.gvsig.graph.core.GvFlag;
58
import com.iver.cit.gvsig.graph.core.Network;
59 20103 fpenarrubia
import com.iver.cit.gvsig.graph.core.NetworkUtils;
60 8050 fjp
61
public class FlagListener implements PointListener {
62 8567 fjp
        public static final int TO_ARC = 0;
63
        public static final int TO_NODE = 1;
64 8123 fjp
        public static int pixelTolerance = 10;
65 8050 fjp
    private MapControl mapCtrl;
66
    private int idSymbolFlag = -1;
67
    private Cursor cur = java.awt.Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
68 8567 fjp
        private int mode;
69 8050 fjp
70
71
    public FlagListener(MapControl mc) {
72
        this.mapCtrl = mc;
73
    }
74
75
76
    /* (non-Javadoc)
77
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
78
     * The PointEvent method bring you a point in pixel coordinates. You
79
     * need to transform it to world coordinates. The class to do conversions
80
     * is ViewPort, obtained thru the MapContext of mapCtrl.
81
     */
82
    public void point(PointEvent event) throws BehaviorException {
83
84
        Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
85
86
                SingleLayerIterator it = new SingleLayerIterator(mapCtrl.getMapContext().getLayers());
87
                while (it.hasNext())
88
                {
89
                        FLayer aux = it.next();
90
                        if (!aux.isActive())
91
                                continue;
92
                        Network net = (Network) aux.getProperty("network");
93
94
                        if ( net != null)
95
                        {
96 8123 fjp
                                double realTol = mapCtrl.getViewPort().toMapDistance(pixelTolerance);
97 8261 fjp
                                GvFlag flag;
98
                                try {
99 8567 fjp
                                        if (mode == TO_ARC)
100
                                                flag = net.addFlag(pReal.getX(), pReal.getY(), realTol);
101
                                        else
102
                                                flag = net.addFlagToNode(pReal.getX(), pReal.getY(), realTol);
103 8261 fjp
                                        if (flag == null)
104
                                        {
105
                                                JOptionPane.showMessageDialog(null, "No est? sobre la red");
106
                                                return;
107
                                        }
108 8673 fjp
                                        NetworkUtils.addGraphicFlag(mapCtrl, flag);
109 8261 fjp
                                        mapCtrl.drawGraphics();
110 8357 fjp
                                        PluginServices.getMainFrame().enableControls();
111 8261 fjp
                                } catch (GraphException e) {
112
                                        e.printStackTrace();
113
                                        NotificationManager.addError(e);
114 8050 fjp
                                }
115
116
                        }
117
                }
118
119
120
    }
121
122
    public Cursor getCursor() {
123
        return cur;
124
    }
125
126
    public boolean cancelDrawing() {
127
        return false;
128
    }
129
130
131
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
132
133
        }
134
135 8567 fjp
136
        public void setMode(int mode) {
137
                this.mode = mode;
138
139
        }
140
141 8050 fjp
}
142