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 @ 41974

History | View | Annotate | Download (4.76 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

    
68
    /**
69
     * <p>
70
     * Creates a new behavior for moving the mouse.
71
     * </p>
72
     *
73
     * @param pli
74
     *            listener used to permit this object to work with the
75
     *            associated <code>MapControl</code>
76
     * @param mouseButton
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
    @Override
88
    public void paintComponent(MapControlDrawer renderer) {
89
        if( ! this.isMyButton() ) {
90
            return;
91
        }
92
        super.paintComponent(renderer);
93
        BufferedImage image = getMapControl().getImage();
94
        if (image != null) {
95
            if ((m_FirstPoint != null) && (m_LastPoint != null)) {
96
                renderer.setComposite(AlphaComposite.getInstance(
97
                    AlphaComposite.SRC_OVER, 0.5f));
98
                renderer.drawImage(image,
99
                    (int) (m_LastPoint.getX() - m_FirstPoint.getX()),
100
                    (int) (m_LastPoint.getY() - m_FirstPoint.getY()));
101
            }
102
        }
103
    }
104

    
105
    @Override
106
    public void mousePressed(MouseEvent e) {
107
        if (this.isMyButton(e)) {
108
            m_FirstPoint = e.getPoint();
109
            if (listener.cancelDrawing()) {
110
               getMapControl().cancelDrawing();
111
           }
112
        }
113

    
114

    
115
    }
116

    
117
    @Override
118
    public void mouseReleased(MouseEvent e) throws BehaviorException {
119
        if (this.isMyButton(e) && m_FirstPoint != null) {
120
            doMouseReleased(e);
121
        }
122
    }
123

    
124
    protected void doMouseReleased(MouseEvent e) throws BehaviorException {
125
        MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
126
        listener.move(event);
127

    
128
        getMapControl().drawMap(true);
129

    
130
        m_FirstPoint = null;
131
    }
132

    
133

    
134

    
135
    @Override
136
    public void mouseDragged(MouseEvent e) {
137
        if (this.isMyButton(e)) {
138
            m_LastPoint = e.getPoint();
139
            getMapControl().repaint();
140
        }
141
    }
142

    
143
    /**
144
     * <p>
145
     * Sets a tool listener to work with the <code>MapControl</code> using this
146
     * behavior.
147
     * </p>
148
     *
149
     * @param listener
150
     *            a <code>PanListener</code> object for this behavior
151
     */
152
    public void setListener(ToolListener listener) {
153
        this.listener = (PanListener) listener;
154
    }
155

    
156
    public ToolListener getListener() {
157
        return listener;
158
    }
159
}