Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / layout / mapbox / tools / LayoutEditMapBoxBehavior.java @ 1757

History | View | Annotate | Download (10.1 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.layout.mapbox.tools;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.event.MouseEvent;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.Point2D;
30
import org.apache.commons.lang3.tuple.Pair;
31
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
32
import org.gvsig.app.project.documents.layout.LayoutContext;
33
import org.gvsig.app.project.documents.layout.fframes.FFrame;
34
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
35
import org.gvsig.app.project.documents.layout.tools.behavior.*;
36
import org.gvsig.app.project.documents.layout.tools.listener.LayoutToolListener;
37
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
38
import org.gvsig.layout.mapbox.fframe.FFrameMapBox;
39
import static org.gvsig.layout.mapbox.fframe.FFrameMapBox.PIXELS_TOLERANCE;
40
import org.gvsig.layout.mapbox.model.Cell;
41
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_AT_BOTOM_OF_CELL;
42
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_AT_TOP_OF_CELL;
43
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_ON_LEFT_EDGE_OF_CELL;
44
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_ON_RIGHT_EDGE_OF_CELL;
45
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_OUT_OF_MAPBOX;
46
import org.gvsig.tools.lang.CloneableUtils;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
/**
51
 * Behaviour que espera un listener de tipo LayoutMoveListener.
52
 * 
53
 * @author gvSIG team
54
 */
55
public class LayoutEditMapBoxBehavior extends LayoutBehavior {
56
    
57
    private static final Logger LOGGER = LoggerFactory.getLogger(LayoutEditMapBoxBehavior.class);
58
    private final LayoutEditMapBoxListenerImpl listener;
59
    private boolean dragging = false;
60
    
61
    private Point2D.Double pointPressed;
62
    private Pair<Integer, Cell> relativePosition;
63
//    private FFrameMapBox currFrame;
64
    private FFrameMapBox dragFrame;
65

    
66

    
67

    
68
    /**
69
     * Crea un nuevo MoveBehavior.
70
     * 
71
     * @param pli
72
     *            listener.
73
     */
74
    public LayoutEditMapBoxBehavior(LayoutEditMapBoxListenerImpl lpl) {
75
        listener = lpl;
76
    }
77

    
78
    /**
79
     * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
80
     */
81
    @Override
82
    public void paintComponent(Graphics g) {
83
        getLayoutControl().getLayoutDraw().drawRectangle((Graphics2D) g);
84
        g.drawImage(getLayoutControl().getImage(), 0, 0, getLayoutControl()
85
            .getComponent());
86
        getLayoutControl().getLayoutDraw().drawHandlers((Graphics2D) g,
87
            Color.black);
88

    
89
        if(dragFrame != null){
90
                dragFrame.draw((Graphics2D) g, getLayoutControl().getAT());
91
        }
92
        g.drawImage(getLayoutControl().getImgRuler(), 0, 0, getLayoutControl()
93
            .getComponent());
94
    }
95

    
96
    /**
97
     * @throws BehaviorException
98
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
99
     */
100
    @Override
101
    public void mousePressed(MouseEvent e) throws BehaviorException {
102
        super.mousePressed(e);
103

    
104
        this.dragFrame = null;
105
        this.dragging = false;
106
        IFFrame[] fframes = getLayoutControl().getLayoutContext().getSelectedFFrames();
107
        if(fframes.length != 1 || !(fframes[0] instanceof FFrameMapBox)){
108
            return;
109
        }
110
        AffineTransform at = getLayoutControl().getAT();
111
        for (IFFrame frame : fframes) {
112
            if (frame instanceof FFrameMapBox) {
113
                this.pointPressed = 
114
                    FLayoutUtilities.toSheetPoint(
115
                        e.getPoint(),
116
                        at
117
                );
118
                this.relativePosition = ((FFrameMapBox)frame).getModel().getRelativePosition(
119
                    ((FFrameMapBox)frame).fromSheetPointToMapBoxPoint(this.pointPressed),
120
                    FLayoutUtilities.toSheetDistance(PIXELS_TOLERANCE, at)
121
                );
122
                if(this.relativePosition.getLeft() == POSITION_OUT_OF_MAPBOX 
123
//                    || this.cursorPosition.getLeft() == POSITION_IN_CELL
124
                    ){
125
                    continue;
126
                }
127
//                this.currFrame = (FFrameMapBox)frame;
128
                break;
129
            }
130
        }
131
    }
132

    
133
    /**
134
     * Reimplementaci?n del m?todo mouseReleased de Behavior.
135
     * 
136
     * @param e
137
     *            MouseEvent
138
     * 
139
     * @throws BehaviorException
140
     *             Excepci?n lanzada cuando el Behavior.
141
     */
142
    @Override
143
    public void mouseReleased(MouseEvent e) throws BehaviorException {
144
        super.mouseReleased(e);
145

    
146
        LayoutContext layoutContext = getLayoutControl().getLayoutContext();
147
        IFFrame[] fframes = layoutContext.getSelectedFFrames();
148
        if (fframes.length != 1 || !(fframes[0] instanceof FFrameMapBox)) {
149
            return;
150
        }
151
        FFrameMapBox currFrame = (FFrameMapBox) fframes[0];
152
        Point2D.Double point
153
            = FLayoutUtilities.toSheetPoint(
154
                e.getPoint(),
155
                getLayoutControl().getAT()
156
            );
157
        try {
158
            FFrameMapBox newFrame = (FFrameMapBox) CloneableUtils.cloneQuietly(currFrame);
159
            if (this.dragging) {
160
                this.listener.drag(newFrame, relativePosition.getLeft(), relativePosition.getRight(), point);
161
            } else {
162
                Point2D.Double mapPoint = newFrame.fromSheetPointToMapBoxPoint(point);
163
                Cell cell = newFrame.getModel().getCell(mapPoint.getX(), mapPoint.getY());
164
                if (cell == null) {
165
                    return;
166
                }
167
                this.listener.selectCell(newFrame, cell);
168
            }
169
            layoutContext.getFrameCommandsRecord().update(currFrame, newFrame);
170
            layoutContext.updateFFrames();
171
        } catch (Exception ex) {
172
            LOGGER.warn("Failed releasing mouse over FFrameMapBox", ex);
173
            //FIXME: Do nothing?
174
        } finally {
175
            dragging = false;
176
            dragFrame = null;
177
        }
178
        getLayoutControl().refresh();
179
    }
180

    
181
    /**
182
     * Reimplementaci?n del m?todo mouseDragged de Behavior.
183
     * 
184
     * @param e
185
     *            MouseEvent
186
     * @throws BehaviorException
187
     */
188
    @Override
189
    public void mouseDragged(MouseEvent e) throws BehaviorException {
190
        super.mouseDragged(e);
191
        
192
        IFFrame[] fframes = getLayoutControl().getLayoutContext().getSelectedFFrames();
193
        if(fframes.length != 1 || !(fframes[0] instanceof FFrameMapBox)){
194
            return;
195
        }
196
        FFrameMapBox currFrame = (FFrameMapBox) fframes[0];
197
        
198
        Pair<Integer, Cell>  rP;
199
        this.dragFrame = (FFrameMapBox) CloneableUtils.cloneQuietly(currFrame);
200
        AffineTransform at = getLayoutControl().getAT();
201
        Point2D.Double point = FLayoutUtilities.toSheetPoint(
202
            e.getPoint(),
203
            at
204
        );
205
        rP = dragFrame.getModel().getRelativePosition(
206
            dragFrame.fromSheetPointToMapBoxPoint(this.pointPressed),
207
            FLayoutUtilities.toSheetDistance(PIXELS_TOLERANCE, at)
208
        );
209
        
210
        this.dragging = true;
211
        Point2D.Double dragPoint
212
            = FLayoutUtilities.toSheetPoint(
213
                e.getPoint(),
214
                getLayoutControl().getAT()
215
            );
216
        this.listener.drag(dragFrame, rP.getLeft(), rP.getRight(), dragPoint);
217
        getLayoutControl().refresh();
218
    }
219

    
220
    @Override
221
    public void mouseMoved(MouseEvent e) throws BehaviorException {
222
        super.mouseMoved(e);
223
        
224
        IFFrame[] fframes = getLayoutControl().getLayoutContext().getSelectedFFrames();
225
        if(fframes.length != 1 || !(fframes[0] instanceof FFrameMapBox)){
226
            return;
227
        }
228
        FFrameMapBox currFrame = (FFrameMapBox) fframes[0];
229
//        if(currFrame == null){
230
//            return;
231
//        }
232
        
233
        if(this.dragging) {
234
            return;
235
        }
236
        
237
        AffineTransform at = getLayoutControl().getAT();
238
        
239
//        if (this.currFrame.contains(e.getPoint())) {
240
            Point2D.Double point = FLayoutUtilities.toSheetPoint(
241
                e.getPoint(),
242
                at
243
            );
244
            Point2D.Double mapBoxPoint = currFrame.fromSheetPointToMapBoxPoint(point);
245
            Pair<Integer, Cell> cursorPosition = currFrame.getModel().getRelativePosition(mapBoxPoint,
246
                FLayoutUtilities.toSheetDistance(PIXELS_TOLERANCE, at)
247
            );
248
            
249
            switch (cursorPosition.getLeft()) {
250
                case POSITION_ON_LEFT_EDGE_OF_CELL:
251
                case POSITION_ON_RIGHT_EDGE_OF_CELL:
252
                    getLayoutControl().setMapCursor(FFrame.iEResize);
253
                    break;
254
                case POSITION_AT_TOP_OF_CELL:
255
                case POSITION_AT_BOTOM_OF_CELL:
256
                    getLayoutControl().setMapCursor(FFrame.iNResize);
257
                    break;
258
                default:
259
                    getLayoutControl().setMapCursor(this.listener.getImageCursor());
260
            }
261
//        } else {
262
//            getLayoutControl().setMapCursor(this.listener.getImageCursor());
263
//        }
264
    }
265
    
266
    
267

    
268
    /**
269
     * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#getListener()
270
     */
271
    @Override
272
    public LayoutToolListener getListener() {
273
        return listener;
274
    }
275

    
276
    @Override
277
    public boolean isAdjustable() {
278
        return true;
279
    }
280
}