Statistics
| Revision:

root / trunk / extensions / extGraph / src / org / gvsig / graph / tools / FlagListener.java @ 29993

History | View | Annotate | Download (3.99 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.JOptionPane;
47

    
48
import org.gvsig.graph.core.GraphException;
49
import org.gvsig.graph.core.GvFlag;
50
import org.gvsig.graph.core.Network;
51
import org.gvsig.graph.core.NetworkUtils;
52

    
53
import com.iver.andami.PluginServices;
54
import com.iver.andami.messages.NotificationManager;
55
import com.iver.cit.gvsig.fmap.MapControl;
56
import com.iver.cit.gvsig.fmap.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
58
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
59
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
60
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
61

    
62
public class FlagListener implements PointListener {
63
        public static final int TO_ARC = 0;
64
        public static final int TO_NODE = 1;
65
        public static int pixelTolerance = 10;
66
    private MapControl mapCtrl;
67
    private int idSymbolFlag = -1;
68
    private Cursor cur = java.awt.Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
69
        private int mode;
70

    
71
    
72
    public FlagListener(MapControl mc) {
73
        this.mapCtrl = mc;
74
    }
75

    
76

    
77
    /* (non-Javadoc)
78
     * @see org.gvsig.fmap.tools.Listeners.PointListener#point(org.gvsig.fmap.tools.Events.PointEvent)
79
     * The PointEvent method bring you a point in pixel coordinates. You
80
     * need to transform it to world coordinates. The class to do conversions
81
     * is ViewPort, obtained thru the MapContext of mapCtrl.
82
     */
83
    public void point(PointEvent event) throws BehaviorException {
84
        
85
        Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
86
        
87
                SingleLayerIterator it = new SingleLayerIterator(mapCtrl.getMapContext().getLayers());
88
                while (it.hasNext())
89
                {
90
                        FLayer aux = it.next();
91
                        if (!aux.isActive())
92
                                continue;
93
                        Network net = (Network) aux.getProperty("network");
94
                        
95
                        if ( net != null)
96
                        {
97
                                double realTol = mapCtrl.getViewPort().toMapDistance(pixelTolerance);
98
                                GvFlag flag;
99
                                try {
100
                                        if (mode == TO_ARC)
101
                                                flag = net.addFlag(pReal.getX(), pReal.getY(), realTol);
102
                                        else
103
                                                flag = net.addFlagToNode(pReal.getX(), pReal.getY(), realTol);
104
                                        if (flag == null)
105
                                        {
106
                                                JOptionPane.showMessageDialog(null, PluginServices.getText(this, "No est? sobre la red"));
107
                                                return;
108
                                        }
109
                                        NetworkUtils.addGraphicFlag(mapCtrl, flag);
110
                                        mapCtrl.drawGraphics();
111
                                        PluginServices.getMainFrame().enableControls();
112
                                } catch (GraphException e) {
113
                                        e.printStackTrace();
114
                                        NotificationManager.addError(e);
115
                                }
116
                                
117
                        }
118
                }
119
        
120
        
121
    }
122

    
123
    public Cursor getCursor() {
124
        return cur;
125
    }
126

    
127
    public boolean cancelDrawing() {
128
        return false;
129
    }
130

    
131

    
132
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
133
                
134
        }
135

    
136

    
137
        public void setMode(int mode) {
138
                this.mode = mode;
139
                
140
        }
141

    
142
}
143

    
144