Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / MoveBehavior.java @ 10627

History | View | Annotate | Download (4.36 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 com.iver.cit.gvsig.fmap.tools.Behavior;
42

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

    
49
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
50
import com.iver.cit.gvsig.fmap.tools.Events.MoveEvent;
51
import com.iver.cit.gvsig.fmap.tools.Listeners.PanListener;
52
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
53

    
54

    
55
/**
56
 * Behaviour que espera un listener de tipo MoveListener.
57
 *
58
 * @author Vicente Caballero Navarro
59
 */
60
public class MoveBehavior extends Behavior {
61
        private Point2D m_FirstPoint;
62
        private Point2D m_LastPoint;
63
        private Point2D m_PointAnt;
64
        private PanListener listener;
65

    
66
        /**
67
         * Crea un nuevo MoveBehavior.
68
         *
69
         * @param pli listener.
70
         */
71
        public MoveBehavior(PanListener pli) {
72
                listener = pli;
73
        }
74

    
75
        /**
76
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
77
         */
78
        public void paintComponent(Graphics g) {
79
            Color theBackColor = getMapControl().getMapContext().getViewPort().getBackColor();
80
            if (theBackColor == null)
81
                g.setColor(Color.WHITE);
82
            else
83
                g.setColor(theBackColor);
84
                
85
                g.setColor(getMapControl().getMapContext().getViewPort().getBackColor());
86
                g.fillRect(0, 0, getMapControl().getWidth(), getMapControl().getHeight());
87

    
88
                if (getMapControl().getImage() != null) {
89
                        if ((m_FirstPoint != null) && (m_LastPoint != null)) {
90
                                if ((m_LastPoint == null) && (m_PointAnt == null)) {
91
                                        m_LastPoint = new Point();
92
                                        m_PointAnt = new Point();
93
                                }
94

    
95
                                g.drawImage(getMapControl().getImage(),
96
                                        (int) (m_LastPoint.getX() - m_PointAnt.getX()),
97
                                        (int) (m_LastPoint.getY() - m_PointAnt.getY()), null);
98
                        } else {
99
                                super.paintComponent(g);
100
                        }
101
                }
102
        }
103

    
104
        /**
105
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
106
         */
107
        public void mousePressed(MouseEvent e) {
108
                Point pScreen = e.getPoint();
109
                
110
                m_PointAnt = pScreen;
111
                m_FirstPoint = m_PointAnt;
112

    
113
                if (e.getButton() == MouseEvent.BUTTON1) {
114
                        m_PointAnt = pScreen;
115
                        m_FirstPoint = m_PointAnt;
116
                }
117

    
118
                if (listener.cancelDrawing()) {
119
                        getMapControl().cancelDrawing();
120
                }
121
        }
122

    
123
        /**
124
         * Reimplementaci?n del m?todo mouseReleased de Behavior.
125
         *
126
         * @param e MouseEvent
127
         *
128
         * @throws BehaviorException Excepci?n lanzada cuando el Behavior.
129
         */
130
        public void mouseReleased(MouseEvent e) throws BehaviorException {
131
                Point2D p1;
132
                Point2D p2;
133

    
134
                if (e.getButton() == MouseEvent.BUTTON1) {
135
                        MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
136
                        listener.move(event);
137

    
138
                        // System.out.println("mouseReleased");
139
                        getMapControl().drawMap(true);
140
                }
141

    
142
                m_FirstPoint = null;
143
        }
144

    
145
        /**
146
         * Reimplementaci?n del m?todo mouseDragged de Behavior.
147
         *
148
         * @param e MouseEvent
149
         */
150
        public void mouseDragged(MouseEvent e) {
151
                m_LastPoint = e.getPoint();
152
                getMapControl().repaint();
153
        }
154

    
155
        /**
156
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setListener(com.iver.cit.gvsig.fmap.tools.ToolListener)
157
         */
158
        public void setListener(ToolListener listener) {
159
                this.listener = (PanListener) listener;
160
        }
161

    
162
        /**
163
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
164
         */
165
        public ToolListener getListener() {
166
                return listener;
167
        }
168
}