svn-gvsig-desktop / tags / v1_1_Build_1006 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / JoinCADTool.java @ 12458
History | View | Annotate | Download (9.35 KB)
1 | 5984 | caballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package com.iver.cit.gvsig.gui.cad.tools; |
||
42 | |||
43 | import java.awt.Graphics; |
||
44 | import java.awt.Graphics2D; |
||
45 | import java.awt.event.InputEvent; |
||
46 | import java.awt.geom.Point2D; |
||
47 | import java.io.IOException; |
||
48 | import java.util.ArrayList; |
||
49 | |||
50 | 12042 | caballero | import com.hardcode.gdbms.engine.data.driver.DriverException; |
51 | 5984 | caballero | import com.iver.andami.PluginServices; |
52 | import com.iver.cit.gvsig.CADExtension; |
||
53 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
54 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
55 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
56 | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
||
57 | import com.iver.cit.gvsig.fmap.core.Handler; |
||
58 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
59 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
60 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
61 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
62 | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
||
63 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
64 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
65 | import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
||
66 | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
||
67 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
68 | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
||
69 | import com.iver.cit.gvsig.gui.cad.tools.smc.JoinCADToolContext; |
||
70 | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
||
71 | |||
72 | |||
73 | /**
|
||
74 | * DOCUMENT ME!
|
||
75 | *
|
||
76 | * @author Vicente Caballero Navarro
|
||
77 | */
|
||
78 | public class JoinCADTool extends DefaultCADTool { |
||
79 | private JoinCADToolContext _fsm;
|
||
80 | private SelectionCADTool selectionCADTool;
|
||
81 | /**
|
||
82 | * Crea un nuevo JoinCADTool.
|
||
83 | */
|
||
84 | public JoinCADTool() {
|
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
||
89 | * carga previa a la utilizaci?n de la herramienta.
|
||
90 | */
|
||
91 | public void init() { |
||
92 | _fsm = new JoinCADToolContext(this); |
||
93 | clearSelection(); |
||
94 | selectionCADTool=new SelectionCADTool();//(SelectionCADTool) CADExtension.getCADTool(); |
||
95 | selectionCADTool.init(); |
||
96 | selectionCADTool.multipleSelection(true);
|
||
97 | } |
||
98 | |||
99 | /* (non-Javadoc)
|
||
100 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
101 | */
|
||
102 | public void transition(double x, double y, InputEvent event) { |
||
103 | _fsm.addPoint(x, y, event); |
||
104 | } |
||
105 | |||
106 | /* (non-Javadoc)
|
||
107 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
108 | */
|
||
109 | public void transition(double d) { |
||
110 | _fsm.addValue(d); |
||
111 | } |
||
112 | |||
113 | /* (non-Javadoc)
|
||
114 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
115 | */
|
||
116 | public void transition(String s) throws CommandException { |
||
117 | if (!super.changeCommand(s)){ |
||
118 | _fsm.addOption(s); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /**
|
||
123 | * DOCUMENT ME!
|
||
124 | */
|
||
125 | public void selection() { |
||
126 | if (!CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
||
127 | CADExtension.setCADTool("_selection",false); |
||
128 | ((SelectionCADTool)CADExtension.getCADTool()).multipleSelection(true);
|
||
129 | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
||
130 | "_join");
|
||
131 | } |
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
136 | * editableFeatureSource que ya estar? creado.
|
||
137 | *
|
||
138 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
139 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
140 | */
|
||
141 | public void addPoint(double x, double y,InputEvent event) { |
||
142 | selectionCADTool.selectFeatures(x,y,event); |
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
147 | * encontremos.
|
||
148 | *
|
||
149 | * @param g Graphics sobre el que dibujar.
|
||
150 | * @param x par?metro x del punto que se pase para dibujar.
|
||
151 | * @param y par?metro x del punto que se pase para dibujar.
|
||
152 | */
|
||
153 | public void drawOperation(Graphics g, double x, double y) { |
||
154 | VectorialLayerEdited vle=getVLE(); |
||
155 | ArrayList selectedRows=vle.getSelectedRow();
|
||
156 | ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort(); |
||
157 | DefaultRowEdited[] dres=(DefaultRowEdited[])selectedRows.toArray(new DefaultRowEdited[0]); |
||
158 | for (int i=0;i<dres.length;i++) { |
||
159 | IGeometry geom=((IFeature)dres[i].getLinkedRow()).getGeometry().cloneGeometry(); |
||
160 | 11436 | caballero | geom.draw((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
|
161 | 5984 | caballero | } |
162 | } |
||
163 | /**
|
||
164 | * Add a diferent option.
|
||
165 | *
|
||
166 | * @param s Diferent option.
|
||
167 | */
|
||
168 | public void addOption(String s) { |
||
169 | if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){ |
||
170 | init(); |
||
171 | }else {
|
||
172 | try {
|
||
173 | joinGeometries(); |
||
174 | } catch (IOException e) { |
||
175 | e.printStackTrace(); |
||
176 | } catch (DriverIOException e) {
|
||
177 | e.printStackTrace(); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | private Handler[] getHandlers(DefaultRowEdited[] selectedRows) { |
||
182 | ArrayList handlers=new ArrayList(); |
||
183 | for (int i=0;i<selectedRows.length;i++) { |
||
184 | IGeometry geometry=((IFeature)selectedRows[i].getLinkedRow()).getGeometry(); |
||
185 | Handler[] hs=geometry.getHandlers(IGeometry.SELECTHANDLER); |
||
186 | for (int j=0;j<hs.length;j++) { |
||
187 | handlers.add(hs[j]); |
||
188 | } |
||
189 | } |
||
190 | return (Handler[])handlers.toArray(new Handler[0]); |
||
191 | } |
||
192 | private boolean isIntoRect(Point2D p,Handler[] handlers) { |
||
193 | double tol=0.02; |
||
194 | 11226 | caballero | double angle=Double.NEGATIVE_INFINITY; |
195 | 5984 | caballero | |
196 | //for (int i=0;i<handlers.length;i++) {
|
||
197 | //if (handlers.length>0) {
|
||
198 | Point2D p0=p;
|
||
199 | for(int j=0;j<handlers.length;j++) { |
||
200 | Point2D p1Aux=handlers[j].getPoint();
|
||
201 | if (!handlers[0].equalsPoint(handlers[j])) { |
||
202 | double angleAux=UtilFunctions.getAngle(p0,p1Aux);
|
||
203 | 11226 | caballero | if (angle!=Double.NEGATIVE_INFINITY && (angle>angleAux+tol || angle<angleAux-tol)) { |
204 | 5984 | caballero | return false; |
205 | } |
||
206 | angle=angleAux; |
||
207 | } |
||
208 | } |
||
209 | //}
|
||
210 | return true; |
||
211 | } |
||
212 | private Point2D[] startAndEndPoints(Handler[] handlers) { |
||
213 | Point2D first=null; |
||
214 | Point2D end=null; |
||
215 | for (int i=0;i<handlers.length;i++) { |
||
216 | Point2D aux=(Point2D)handlers[i].getPoint().clone(); |
||
217 | if (first == null || aux.getX()<first.getX()) { |
||
218 | first=aux; |
||
219 | } |
||
220 | if (end == null || aux.getX()>end.getX()) { |
||
221 | end=aux; |
||
222 | } |
||
223 | } |
||
224 | if (first.getX()==end.getX()) {
|
||
225 | for (int i=0;i<handlers.length;i++) { |
||
226 | Point2D aux=(Point2D)handlers[i].getPoint().clone(); |
||
227 | if (first == null || aux.getY()<first.getY()) { |
||
228 | first=aux; |
||
229 | } |
||
230 | if (end == null || aux.getY()>end.getY()) { |
||
231 | end=aux; |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | return new Point2D[] {first,end}; |
||
236 | } |
||
237 | |||
238 | |||
239 | |||
240 | private void joinGeometries() throws IOException, DriverIOException { |
||
241 | DefaultRowEdited[] rows = (DefaultRowEdited[]) getSelectedRows() |
||
242 | .toArray(new DefaultRowEdited[0]); |
||
243 | Handler[] handlers = getHandlers(rows); |
||
244 | if (handlers.length < 2) { |
||
245 | return;
|
||
246 | } |
||
247 | Point2D[] points = startAndEndPoints(handlers); |
||
248 | |||
249 | if (!isIntoRect(points[0], handlers)) |
||
250 | return;
|
||
251 | |||
252 | VectorialLayerEdited vle = getVLE(); |
||
253 | VectorialEditableAdapter vea = vle.getVEA(); |
||
254 | vea.startComplexRow(); |
||
255 | |||
256 | 12134 | caballero | String newFID;
|
257 | // try {
|
||
258 | newFID = vea.getNewFID(); |
||
259 | // } catch (DriverException e) {
|
||
260 | // throw new DriverIOException(e);
|
||
261 | // }
|
||
262 | 5984 | caballero | GeneralPathX gpx = new GeneralPathX();
|
263 | gpx.moveTo(points[0].getX(), points[0].getY()); |
||
264 | gpx.lineTo(points[1].getX(), points[1].getY()); |
||
265 | |||
266 | IGeometry geom = ShapeFactory.createPolyline2D(gpx); |
||
267 | 12134 | caballero | DefaultFeature df1 = new DefaultFeature(geom, null, newFID); |
268 | 5984 | caballero | |
269 | for (int i = rows.length - 1; i >= 0; i--) { |
||
270 | vea.removeRow(rows[i].getIndex(), getName(), EditionEvent.GRAPHIC); |
||
271 | } |
||
272 | int index1 = vea.addRow(df1, PluginServices.getText(this, "parte1"), |
||
273 | EditionEvent.GRAPHIC); |
||
274 | 6008 | caballero | //clearSelection();
|
275 | ArrayList selectedRowAux = new ArrayList(); |
||
276 | selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
|
||
277 | 5984 | caballero | vea.getInversedIndex(index1))); |
278 | 6008 | caballero | vle.setSelectionCache(selectedRowAux); |
279 | 6071 | caballero | vea.endComplexRow(getName()); |
280 | 5984 | caballero | |
281 | } |
||
282 | |||
283 | /*
|
||
284 | * (non-Javadoc)
|
||
285 | *
|
||
286 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
287 | */
|
||
288 | public void addValue(double d) { |
||
289 | } |
||
290 | public String getName() { |
||
291 | return PluginServices.getText(this,"join_"); |
||
292 | } |
||
293 | |||
294 | public String toString() { |
||
295 | return "_join"; |
||
296 | } |
||
297 | |||
298 | public boolean isApplicable(int shapeType) { |
||
299 | switch (shapeType) {
|
||
300 | case FShape.MULTI:
|
||
301 | case FShape.LINE:
|
||
302 | return true; |
||
303 | } |
||
304 | return false; |
||
305 | } |
||
306 | |||
307 | |||
308 | } |