svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / tools / listener / LayoutAddRectangleListener.java @ 1714
History | View | Annotate | Download (5.37 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 | package org.gvsig.app.project.documents.layout.tools.listener; |
||
23 | |||
24 | import java.awt.Image; |
||
25 | import java.awt.geom.Point2D; |
||
26 | import java.awt.geom.Rectangle2D; |
||
27 | |||
28 | import org.gvsig.andami.PluginServices; |
||
29 | import org.gvsig.app.project.documents.layout.fframes.FFrameGraphics; |
||
30 | import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
||
31 | import org.gvsig.app.project.documents.layout.tools.AbstractLayoutToolListener; |
||
32 | import org.gvsig.fmap.geom.Geometry; |
||
33 | import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
||
34 | import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
||
35 | |||
36 | /**
|
||
37 | * Implementaci?n de la interfaz LayoutRectangleListener como herramienta para
|
||
38 | * realizar una inserci?n por rect?ngulo.
|
||
39 | *
|
||
40 | * @author Vicente Caballero Navarro
|
||
41 | */
|
||
42 | public class LayoutAddRectangleListener extends AbstractLayoutToolListener |
||
43 | implements LayoutRectangleListener {
|
||
44 | |||
45 | private final Image img = PluginServices.getIconTheme() |
||
46 | 24 | jldominguez | .get("cursor-selection-by-rectangle").getImage();
|
47 | 5 | jldominguez | protected static int TOLERANCE = 15; |
48 | |||
49 | /**
|
||
50 | * Crea un nuevo LayoutAddRectangleListener.
|
||
51 | *
|
||
52 | * @param l
|
||
53 | * Layout.
|
||
54 | */
|
||
55 | public LayoutAddRectangleListener(LayoutPanel layoutPanel) {
|
||
56 | super(layoutPanel);
|
||
57 | } |
||
58 | |||
59 | /**
|
||
60 | * @see org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener#rectangle(org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent)
|
||
61 | */
|
||
62 | public void rectangle(EnvelopeEvent event) throws BehaviorException { |
||
63 | FFrameGraphics fframe = |
||
64 | (FFrameGraphics) layoutManager |
||
65 | .createFrame(FFrameGraphics.PERSISTENCE_DEFINITION_NAME); |
||
66 | |||
67 | fframe.setGeometryAdapter(layoutPanel.getLayoutControl() |
||
68 | .getGeometryAdapter()); |
||
69 | fframe.update(Geometry.TYPES.SURFACE, layoutPanel.getLayoutControl() |
||
70 | .getAT()); |
||
71 | fframe.setBoundBox(layoutPanel.getLayoutControl().getGeometryAdapter() |
||
72 | .getBounds2D()); |
||
73 | layoutPanel.getLayoutContext().addFFrame(fframe, true, true); |
||
74 | PluginServices.getMainFrame().enableControls(); |
||
75 | layoutPanel.getLayoutControl().refresh(); |
||
76 | } |
||
77 | |||
78 | /**
|
||
79 | * @see org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener#getCursor()
|
||
80 | */
|
||
81 | public Image getImageCursor() { |
||
82 | return img;
|
||
83 | } |
||
84 | |||
85 | /**
|
||
86 | * @see org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener#cancelDrawing()
|
||
87 | */
|
||
88 | public boolean cancelDrawing() { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | protected Rectangle2D getRectangle(int tolerance) { |
||
93 | Rectangle2D r = new Rectangle2D.Double(); |
||
94 | if (isCorrectSize(tolerance, layoutPanel.getLayoutControl()
|
||
95 | .getFirstPoint(), layoutPanel.getLayoutControl().getLastPoint())) { |
||
96 | Point2D fp = layoutPanel.getLayoutControl().getFirstPoint();
|
||
97 | Point2D lp = layoutPanel.getLayoutControl().getLastPoint();
|
||
98 | if (fp.getX() < lp.getX()) {
|
||
99 | if (fp.getY() < lp.getY()) {
|
||
100 | r.setRect(fp.getX(), fp.getY(), lp.getX() - fp.getX(), |
||
101 | lp.getY() - fp.getY()); |
||
102 | } else {
|
||
103 | r.setRect(fp.getX(), lp.getY(), lp.getX() - fp.getX(), |
||
104 | fp.getY() - lp.getY()); |
||
105 | } |
||
106 | } else {
|
||
107 | if (fp.getY() > lp.getY()) {
|
||
108 | r.setRect(lp.getX(), lp.getY(), fp.getX() - lp.getX(), |
||
109 | fp.getY() - lp.getY()); |
||
110 | } else {
|
||
111 | r.setRect(lp.getX(), fp.getY(), lp.getX() - fp.getX(), |
||
112 | lp.getY() - fp.getY()); |
||
113 | } |
||
114 | } |
||
115 | } else {
|
||
116 | Point2D p1 = layoutPanel.getLayoutControl().getFirstPoint();
|
||
117 | p1 = |
||
118 | new Point2D.Double(p1.getX() + tolerance, p1.getY() + tolerance); |
||
119 | r.setFrameFromDiagonal(layoutPanel.getLayoutControl() |
||
120 | .getFirstPoint(), p1); |
||
121 | } |
||
122 | return r;
|
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * Devuelve true si el rectangulo formado por los dos puntos que se pasan
|
||
127 | * como par?metro es superior a la tolerancia.
|
||
128 | *
|
||
129 | * @param tolerance
|
||
130 | * Tolerancia
|
||
131 | * @param p1
|
||
132 | * Punto inicial del rect?ngulo.
|
||
133 | * @param p2
|
||
134 | * Punto final del rect?ngulo.
|
||
135 | *
|
||
136 | * @return True si el tama?o es correcto.
|
||
137 | */
|
||
138 | private boolean isCorrectSize(int tolerance, Point2D p1, Point2D p2) { |
||
139 | if (Math.abs(p2.getX() - p1.getX()) < tolerance) { |
||
140 | return false; |
||
141 | } else
|
||
142 | if (Math.abs(p2.getY() - p1.getY()) < tolerance) { |
||
143 | return false; |
||
144 | } |
||
145 | return true; |
||
146 | } |
||
147 | } |