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

History | View | Annotate | Download (10.4 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.DefaultLayoutNotification;
32
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
33
import org.gvsig.app.project.documents.layout.LayoutContext;
34
import org.gvsig.app.project.documents.layout.LayoutNotification;
35
import org.gvsig.app.project.documents.layout.fframes.FFrame;
36
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
37
import org.gvsig.app.project.documents.layout.tools.behavior.*;
38
import org.gvsig.app.project.documents.layout.tools.listener.LayoutToolListener;
39
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
40
import org.gvsig.layout.mapbox.fframe.FFrameMapBox;
41
import static org.gvsig.layout.mapbox.fframe.FFrameMapBox.PIXELS_TOLERANCE;
42
import org.gvsig.layout.mapbox.model.Cell;
43
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_AT_BOTOM_OF_CELL;
44
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_AT_TOP_OF_CELL;
45
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_ON_LEFT_EDGE_OF_CELL;
46
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_ON_RIGHT_EDGE_OF_CELL;
47
import static org.gvsig.layout.mapbox.model.MapBoxModel.POSITION_OUT_OF_MAPBOX;
48
import org.gvsig.tools.lang.CloneableUtils;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

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

    
68

    
69

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

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

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

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

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

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

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

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

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

    
272
    /**
273
     * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#getListener()
274
     */
275
    @Override
276
    public LayoutToolListener getListener() {
277
        return listener;
278
    }
279

    
280
    @Override
281
    public boolean isAdjustable() {
282
        return true;
283
    }
284
}