Statistics
| Revision:

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

History | View | Annotate | Download (4.57 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.fmap.mapcontrol.tools.Behavior;
23

    
24
import java.awt.Color;
25
import java.awt.event.MouseEvent;
26
import java.awt.geom.Point2D;
27

    
28
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
29
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
30
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent;
31
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener;
32
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
33

    
34
/**
35
 * <p>
36
 * Behavior that permits user to move the image of the associated
37
 * <code>MapControl</code> using a {@link PanListener PanListener}.
38
 * </p>
39
 * 
40
 * @author Vicente Caballero Navarro
41
 * @author Pablo Piqueras Bartolom?
42
 */
43
public class MoveBehavior extends Behavior {
44

    
45
    /**
46
     * First point of the path in image coordinates.
47
     */
48
    private Point2D m_FirstPoint;
49

    
50
    /**
51
     * Last point of the path in image coordinates.
52
     */
53
    private Point2D m_LastPoint;
54

    
55
    /**
56
     * Tool listener used to work with the <code>MapControl</code> object.
57
     * 
58
     * @see #getListener()
59
     * @see #setListener(ToolListener)
60
     */
61
    private PanListener listener;
62
    private boolean isButton1;
63

    
64
    /**
65
     * <p>
66
     * Creates a new behavior for moving the mouse.
67
     * </p>
68
     * 
69
     * @param pli
70
     *            listener used to permit this object to work with the
71
     *            associated <code>MapControl</code>
72
     */
73
    public MoveBehavior(PanListener pli) {
74
        listener = pli;
75
    }
76

    
77
    public void paintComponent(MapControlDrawer renderer) {
78
        Color theBackColor =
79
            getMapControl().getMapContext().getViewPort().getBackColor();
80
        if (theBackColor == null) {
81
            renderer.setColor(Color.WHITE);
82
        } else {
83
            renderer.setColor(theBackColor);
84
        }
85
        renderer.fillRect(0, 0, getMapControl().getWidth(), getMapControl()
86
            .getHeight());
87

    
88
        if (getMapControl().getImage() != null) {
89
            if ((m_FirstPoint != null) && (m_LastPoint != null)) {
90
                renderer.drawImage(getMapControl().getImage(),
91
                    (int) (m_LastPoint.getX() - m_FirstPoint.getX()),
92
                    (int) (m_LastPoint.getY() - m_FirstPoint.getY()));
93
            } else {
94
                super.paintComponent(renderer);
95
            }
96
        }
97
    }
98

    
99
    public void mousePressed(MouseEvent e) {
100
        if (e.getButton() == MouseEvent.BUTTON1) {
101
            isButton1 = true;
102
            m_FirstPoint = e.getPoint();
103
        } else {
104
            isButton1 = false;
105
        }
106

    
107
        if (listener.cancelDrawing()) {
108
            getMapControl().cancelDrawing();
109
        }
110
    }
111

    
112
    public void mouseReleased(MouseEvent e) throws BehaviorException {
113
        if (e.getButton() == MouseEvent.BUTTON1 && m_FirstPoint != null) {
114
            MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
115
            listener.move(event);
116

    
117
            // System.out.println("mouseReleased");
118
            getMapControl().drawMap(true);
119
        }
120

    
121
        m_FirstPoint = null;
122
    }
123

    
124
    public void mouseDragged(MouseEvent e) {
125
        if (isButton1) {
126
            m_LastPoint = e.getPoint();
127
            getMapControl().repaint();
128
        }
129
    }
130

    
131
    /**
132
     * <p>
133
     * Sets a tool listener to work with the <code>MapControl</code> using this
134
     * behavior.
135
     * </p>
136
     * 
137
     * @param listener
138
     *            a <code>PanListener</code> object for this behavior
139
     */
140
    public void setListener(ToolListener listener) {
141
        this.listener = (PanListener) listener;
142
    }
143

    
144
    public ToolListener getListener() {
145
        return listener;
146
    }
147
}