Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / MoveBehavior.java @ 30327

History | View | Annotate | Download (5.04 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.fmap.mapcontrol.tools.Behavior;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.event.MouseEvent;
46
import java.awt.geom.Point2D;
47

    
48
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
49
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
50
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent;
51
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener;
52
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
53

    
54

    
55

    
56
/**
57
 * <p>Behavior that permits user to move the image of the associated
58
 *  <code>MapControl</code> using a {@link PanListener PanListener}.</p>
59
 *
60
 * @author Vicente Caballero Navarro
61
 * @author Pablo Piqueras Bartolom?
62
 */
63
public class MoveBehavior extends Behavior {
64
        /**
65
         * First point of the path in image coordinates.
66
         */
67
        private Point2D m_FirstPoint;
68

    
69
        /**
70
         * Last point of the path in image coordinates.
71
         */
72
        private Point2D m_LastPoint;
73

    
74
        /**
75
         * Tool listener used to work with the <code>MapControl</code> object.
76
         *
77
         * @see #getListener()
78
         * @see #setListener(ToolListener)
79
         */
80
        private PanListener listener;
81
        private boolean isButton1;
82

    
83
        /**
84
          * <p>Creates a new behavior for moving the mouse.</p>
85
          *
86
         * @param pli listener used to permit this object to work with the associated <code>MapControl</code>
87
         */
88
        public MoveBehavior(PanListener pli) {
89
                listener = pli;
90
        }
91

    
92
        /*
93
         * (non-Javadoc)
94
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
95
         */
96
        public void paintComponent(MapControlDrawer renderer) {
97
            Color theBackColor = getMapControl().getMapContext().getViewPort().getBackColor();
98
            if (theBackColor == null)
99
                renderer.setColor(Color.WHITE);
100
            else
101
                renderer.setColor(theBackColor);
102
                renderer.setColor(getMapControl().getMapContext().getViewPort().getBackColor());
103
                renderer.fillRect(0, 0, getMapControl().getWidth(), getMapControl().getHeight());
104

    
105
                if (getMapControl().getImage() != null) {
106
                        if ((m_FirstPoint != null) && (m_LastPoint != null)) {
107
                                renderer.drawImage(getMapControl().getImage(),
108
                                        (int) (m_LastPoint.getX() - m_FirstPoint.getX()),
109
                                        (int) (m_LastPoint.getY() - m_FirstPoint.getY()));
110
                        } else {
111
                                super.paintComponent(renderer);
112
                        }
113
                }
114
        }
115

    
116
        /*
117
         * (non-Javadoc)
118
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
119
         */
120
        public void mousePressed(MouseEvent e) {
121
                if (e.getButton() == MouseEvent.BUTTON1) {
122
                        isButton1 = true;
123
                        m_FirstPoint = e.getPoint();
124
                }else{
125
                        isButton1 = false;
126
                }
127

    
128
                if (listener.cancelDrawing()) {
129
                        getMapControl().cancelDrawing();
130
                }
131
        }
132

    
133
        /*
134
         * (non-Javadoc)
135
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
136
         */
137
        public void mouseReleased(MouseEvent e) throws BehaviorException {
138
                if (e.getButton() == MouseEvent.BUTTON1 && m_FirstPoint!=null) {
139
                        MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
140
                        listener.move(event);
141

    
142
                        // System.out.println("mouseReleased");
143
                        getMapControl().drawMap(true);
144
                }
145

    
146
                m_FirstPoint = null;
147
        }
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
152
         */
153
        public void mouseDragged(MouseEvent e) {
154
                if (isButton1){
155
                        m_LastPoint = e.getPoint();
156
                        getMapControl().repaint();
157
                }
158
        }
159

    
160
        /**
161
         * <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
162
         *
163
         * @param listener a <code>PanListener</code> object for this behavior
164
         */
165
        public void setListener(ToolListener listener) {
166
                this.listener = (PanListener) listener;
167
        }
168

    
169
        /*
170
         * (non-Javadoc)
171
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
172
         */
173
        public ToolListener getListener() {
174
                return listener;
175
        }
176
}