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 | 5 | jldominguez | /* 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 | 1714 | fdiaz | package org.gvsig.layout.mapbox.tools; |
23 | 5 | jldominguez | |
24 | import java.awt.Color; |
||
25 | import java.awt.Graphics; |
||
26 | import java.awt.Graphics2D; |
||
27 | import java.awt.event.MouseEvent; |
||
28 | 1714 | fdiaz | 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 | 5 | jldominguez | import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
37 | 1714 | fdiaz | import org.gvsig.app.project.documents.layout.tools.behavior.*; |
38 | 5 | jldominguez | import org.gvsig.app.project.documents.layout.tools.listener.LayoutToolListener; |
39 | import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
||
40 | 1714 | fdiaz | 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 | 5 | jldominguez | |
52 | /**
|
||
53 | * Behaviour que espera un listener de tipo LayoutMoveListener.
|
||
54 | *
|
||
55 | 1714 | fdiaz | * @author gvSIG team
|
56 | 5 | jldominguez | */
|
57 | 1714 | fdiaz | 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 | 5 | jldominguez | |
68 | |||
69 | 1714 | fdiaz | |
70 | 5 | jldominguez | /**
|
71 | 1714 | fdiaz | * Crea un nuevo MoveBehavior.
|
72 | 5 | jldominguez | *
|
73 | * @param pli
|
||
74 | * listener.
|
||
75 | */
|
||
76 | 1714 | fdiaz | public LayoutEditMapBoxBehavior(LayoutEditMapBoxListenerImpl lpl) {
|
77 | 5 | jldominguez | listener = lpl; |
78 | } |
||
79 | |||
80 | /**
|
||
81 | * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
||
82 | */
|
||
83 | 1714 | fdiaz | @Override
|
84 | 5 | jldominguez | public void paintComponent(Graphics g) { |
85 | getLayoutControl().getLayoutDraw().drawRectangle((Graphics2D) g);
|
||
86 | g.drawImage(getLayoutControl().getImage(), 0, 0, getLayoutControl() |
||
87 | .getComponent()); |
||
88 | 1714 | fdiaz | getLayoutControl().getLayoutDraw().drawHandlers((Graphics2D) g,
|
89 | Color.black);
|
||
90 | 5 | jldominguez | |
91 | 1714 | fdiaz | if(dragFrame != null){ |
92 | dragFrame.draw((Graphics2D) g, getLayoutControl().getAT());
|
||
93 | 5 | jldominguez | } |
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 | 1714 | fdiaz | @Override
|
103 | 5 | jldominguez | public void mousePressed(MouseEvent e) throws BehaviorException { |
104 | super.mousePressed(e);
|
||
105 | 1714 | fdiaz | |
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 | 5 | jldominguez | } |
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 | 1714 | fdiaz | @Override
|
145 | 5 | jldominguez | public void mouseReleased(MouseEvent e) throws BehaviorException { |
146 | super.mouseReleased(e);
|
||
147 | |||
148 | 1714 | fdiaz | 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 | 5 | jldominguez | } |
184 | |||
185 | /**
|
||
186 | * Reimplementaci?n del m?todo mouseDragged de Behavior.
|
||
187 | *
|
||
188 | * @param e
|
||
189 | * MouseEvent
|
||
190 | * @throws BehaviorException
|
||
191 | */
|
||
192 | 1714 | fdiaz | @Override
|
193 | 5 | jldominguez | public void mouseDragged(MouseEvent e) throws BehaviorException { |
194 | super.mouseDragged(e);
|
||
195 | 1714 | fdiaz | |
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 | 5 | jldominguez | } |
223 | |||
224 | 1714 | fdiaz | @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 | 5 | jldominguez | } |
269 | 1714 | fdiaz | |
270 | |||
271 | 5 | jldominguez | |
272 | /**
|
||
273 | * @see org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior#getListener()
|
||
274 | */
|
||
275 | 1714 | fdiaz | @Override
|
276 | 5 | jldominguez | public LayoutToolListener getListener() {
|
277 | return listener;
|
||
278 | } |
||
279 | |||
280 | 1714 | fdiaz | @Override
|
281 | public boolean isAdjustable() { |
||
282 | return true; |
||
283 | 5 | jldominguez | } |
284 | } |