Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / MoveBehavior.java @ 41971

History | View | Annotate | Download (4.64 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools.Behavior;
25

    
26
import java.awt.AlphaComposite;
27
import java.awt.event.MouseEvent;
28
import java.awt.geom.Point2D;
29
import java.awt.image.BufferedImage;
30

    
31
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
32
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
33
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent;
34
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener;
35
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
/**
40
 * <p>
41
 * Behavior that permits user to move the image of the associated
42
 * <code>MapControl</code> using a {@link PanListener PanListener}.
43
 * </p>
44
 *
45
 * @author Vicente Caballero Navarro
46
 * @author Pablo Piqueras Bartolom?
47
 */
48
public class MoveBehavior extends Behavior {
49
    private static final Logger logger = LoggerFactory.getLogger(MoveBehavior.class);
50
    /**
51
     * First point of the path in image coordinates.
52
     */
53
    protected Point2D m_FirstPoint;
54

    
55
    /**
56
     * Last point of the path in image coordinates.
57
     */
58
    protected Point2D m_LastPoint;
59

    
60
    /**
61
     * Tool listener used to work with the <code>MapControl</code> object.
62
     *
63
     * @see #getListener()
64
     * @see #setListener(ToolListener)
65
     */
66
    private PanListener listener;
67
    private boolean isMyButton;
68

    
69
    /**
70
     * <p>
71
     * Creates a new behavior for moving the mouse.
72
     * </p>
73
     *
74
     * @param pli
75
     *            listener used to permit this object to work with the
76
     *            associated <code>MapControl</code>
77
     */
78
    public MoveBehavior(PanListener pli, int mouseButton) {
79
        super(mouseButton);
80
        listener = pli;
81
    }
82
    
83
    public MoveBehavior(PanListener pli) {
84
        this(pli,BUTTON_LEFT);
85
    }
86

    
87
    public void paintComponent(MapControlDrawer renderer) {
88
        BufferedImage image = getMapControl().getImage();
89
        if (image != null) {
90
            if ((m_FirstPoint != null) && (m_LastPoint != null)) {
91
                renderer.setComposite(AlphaComposite.getInstance(
92
                    AlphaComposite.SRC_OVER, 0.5f));
93
                renderer.drawImage(image,
94
                    (int) (m_LastPoint.getX() - m_FirstPoint.getX()),
95
                    (int) (m_LastPoint.getY() - m_FirstPoint.getY()));
96
            }
97
        }
98
    }
99

    
100
    public void mousePressed(MouseEvent e) {
101
        if (isMyButton(e)) {
102
            isMyButton = true;
103
            m_FirstPoint = e.getPoint();
104
            if (listener.cancelDrawing()) {
105
               getMapControl().cancelDrawing();
106
           }
107
        }
108

    
109

    
110
    }
111

    
112
    public void mouseReleased(MouseEvent e) throws BehaviorException {
113
        if (isMyButton(e) && m_FirstPoint != null) {
114
            doMouseReleased(e);
115
        }
116
    }
117

    
118
    protected void doMouseReleased(MouseEvent e) throws BehaviorException {
119
        MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
120
        listener.move(event);
121

    
122
        getMapControl().drawMap(true);
123

    
124
        m_FirstPoint = null;
125
        isMyButton = false;
126
    }
127

    
128

    
129

    
130
    public void mouseDragged(MouseEvent e) {
131
        if (isMyButton) {
132
            m_LastPoint = e.getPoint();
133
            getMapControl().repaint();
134
        }
135
    }
136

    
137
    /**
138
     * <p>
139
     * Sets a tool listener to work with the <code>MapControl</code> using this
140
     * behavior.
141
     * </p>
142
     *
143
     * @param listener
144
     *            a <code>PanListener</code> object for this behavior
145
     */
146
    public void setListener(ToolListener listener) {
147
        this.listener = (PanListener) listener;
148
    }
149

    
150
    public ToolListener getListener() {
151
        return listener;
152
    }
153
}