svn-gvsig-desktop / tags / v1_0_2_Build_915 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / JoinCADTool.java @ 12217
History | View | Annotate | Download (9.2 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 | import com.iver.andami.PluginServices; |
||
51 | import com.iver.cit.gvsig.CADExtension; |
||
52 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
53 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
54 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
55 | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
||
56 | import com.iver.cit.gvsig.fmap.core.Handler; |
||
57 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
58 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
59 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
60 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
61 | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
||
62 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
63 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
64 | import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
||
65 | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
||
66 | import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
||
67 | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
||
68 | import com.iver.cit.gvsig.gui.cad.tools.smc.JoinCADToolContext; |
||
69 | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
||
70 | |||
71 | |||
72 | /**
|
||
73 | * DOCUMENT ME!
|
||
74 | *
|
||
75 | * @author Vicente Caballero Navarro
|
||
76 | */
|
||
77 | public class JoinCADTool extends DefaultCADTool { |
||
78 | private JoinCADToolContext _fsm;
|
||
79 | private SelectionCADTool selectionCADTool;
|
||
80 | /**
|
||
81 | * Crea un nuevo JoinCADTool.
|
||
82 | */
|
||
83 | public JoinCADTool() {
|
||
84 | } |
||
85 | |||
86 | /**
|
||
87 | * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
||
88 | * carga previa a la utilizaci?n de la herramienta.
|
||
89 | */
|
||
90 | public void init() { |
||
91 | _fsm = new JoinCADToolContext(this); |
||
92 | clearSelection(); |
||
93 | selectionCADTool=new SelectionCADTool();//(SelectionCADTool) CADExtension.getCADTool(); |
||
94 | selectionCADTool.init(); |
||
95 | selectionCADTool.multipleSelection(true);
|
||
96 | } |
||
97 | |||
98 | /* (non-Javadoc)
|
||
99 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
||
100 | */
|
||
101 | public void transition(double x, double y, InputEvent event) { |
||
102 | _fsm.addPoint(x, y, event); |
||
103 | } |
||
104 | |||
105 | /* (non-Javadoc)
|
||
106 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
||
107 | */
|
||
108 | public void transition(double d) { |
||
109 | _fsm.addValue(d); |
||
110 | } |
||
111 | |||
112 | /* (non-Javadoc)
|
||
113 | * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
||
114 | */
|
||
115 | public void transition(String s) throws CommandException { |
||
116 | if (!super.changeCommand(s)){ |
||
117 | _fsm.addOption(s); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /**
|
||
122 | * DOCUMENT ME!
|
||
123 | */
|
||
124 | public void selection() { |
||
125 | if (!CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
||
126 | CADExtension.setCADTool("_selection",false); |
||
127 | ((SelectionCADTool)CADExtension.getCADTool()).multipleSelection(true);
|
||
128 | ((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
||
129 | "_join");
|
||
130 | } |
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * Equivale al transition del prototipo pero sin pasarle como par?metro el
|
||
135 | * editableFeatureSource que ya estar? creado.
|
||
136 | *
|
||
137 | * @param x par?metro x del punto que se pase en esta transici?n.
|
||
138 | * @param y par?metro y del punto que se pase en esta transici?n.
|
||
139 | */
|
||
140 | public void addPoint(double x, double y,InputEvent event) { |
||
141 | selectionCADTool.selectFeatures(x,y,event); |
||
142 | } |
||
143 | |||
144 | /**
|
||
145 | * M?todo para dibujar la lo necesario para el estado en el que nos
|
||
146 | * encontremos.
|
||
147 | *
|
||
148 | * @param g Graphics sobre el que dibujar.
|
||
149 | * @param x par?metro x del punto que se pase para dibujar.
|
||
150 | * @param y par?metro x del punto que se pase para dibujar.
|
||
151 | */
|
||
152 | public void drawOperation(Graphics g, double x, double y) { |
||
153 | VectorialLayerEdited vle=getVLE(); |
||
154 | ArrayList selectedRows=vle.getSelectedRow();
|
||
155 | ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort(); |
||
156 | DefaultRowEdited[] dres=(DefaultRowEdited[])selectedRows.toArray(new DefaultRowEdited[0]); |
||
157 | for (int i=0;i<dres.length;i++) { |
||
158 | IGeometry geom=((IFeature)dres[i].getLinkedRow()).getGeometry().cloneGeometry(); |
||
159 | 11427 | caballero | geom.draw((Graphics2D)g,vp,DefaultCADTool.selectSymbol);
|
160 | 5984 | caballero | } |
161 | } |
||
162 | /**
|
||
163 | * Add a diferent option.
|
||
164 | *
|
||
165 | * @param s Diferent option.
|
||
166 | */
|
||
167 | public void addOption(String s) { |
||
168 | if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){ |
||
169 | init(); |
||
170 | }else {
|
||
171 | try {
|
||
172 | joinGeometries(); |
||
173 | } catch (IOException e) { |
||
174 | e.printStackTrace(); |
||
175 | } catch (DriverIOException e) {
|
||
176 | e.printStackTrace(); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | private Handler[] getHandlers(DefaultRowEdited[] selectedRows) { |
||
181 | ArrayList handlers=new ArrayList(); |
||
182 | for (int i=0;i<selectedRows.length;i++) { |
||
183 | IGeometry geometry=((IFeature)selectedRows[i].getLinkedRow()).getGeometry(); |
||
184 | Handler[] hs=geometry.getHandlers(IGeometry.SELECTHANDLER); |
||
185 | for (int j=0;j<hs.length;j++) { |
||
186 | handlers.add(hs[j]); |
||
187 | } |
||
188 | } |
||
189 | return (Handler[])handlers.toArray(new Handler[0]); |
||
190 | } |
||
191 | private boolean isIntoRect(Point2D p,Handler[] handlers) { |
||
192 | double tol=0.02; |
||
193 | 11226 | caballero | double angle=Double.NEGATIVE_INFINITY; |
194 | 5984 | caballero | |
195 | //for (int i=0;i<handlers.length;i++) {
|
||
196 | //if (handlers.length>0) {
|
||
197 | Point2D p0=p;
|
||
198 | for(int j=0;j<handlers.length;j++) { |
||
199 | Point2D p1Aux=handlers[j].getPoint();
|
||
200 | if (!handlers[0].equalsPoint(handlers[j])) { |
||
201 | double angleAux=UtilFunctions.getAngle(p0,p1Aux);
|
||
202 | 11226 | caballero | if (angle!=Double.NEGATIVE_INFINITY && (angle>angleAux+tol || angle<angleAux-tol)) { |
203 | 5984 | caballero | return false; |
204 | } |
||
205 | angle=angleAux; |
||
206 | } |
||
207 | } |
||
208 | //}
|
||
209 | return true; |
||
210 | } |
||
211 | private Point2D[] startAndEndPoints(Handler[] handlers) { |
||
212 | Point2D first=null; |
||
213 | Point2D end=null; |
||
214 | for (int i=0;i<handlers.length;i++) { |
||
215 | Point2D aux=(Point2D)handlers[i].getPoint().clone(); |
||
216 | if (first == null || aux.getX()<first.getX()) { |
||
217 | first=aux; |
||
218 | } |
||
219 | if (end == null || aux.getX()>end.getX()) { |
||
220 | end=aux; |
||
221 | } |
||
222 | } |
||
223 | if (first.getX()==end.getX()) {
|
||
224 | for (int i=0;i<handlers.length;i++) { |
||
225 | Point2D aux=(Point2D)handlers[i].getPoint().clone(); |
||
226 | if (first == null || aux.getY()<first.getY()) { |
||
227 | first=aux; |
||
228 | } |
||
229 | if (end == null || aux.getY()>end.getY()) { |
||
230 | end=aux; |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | return new Point2D[] {first,end}; |
||
235 | } |
||
236 | |||
237 | |||
238 | |||
239 | private void joinGeometries() throws IOException, DriverIOException { |
||
240 | DefaultRowEdited[] rows = (DefaultRowEdited[]) getSelectedRows() |
||
241 | .toArray(new DefaultRowEdited[0]); |
||
242 | Handler[] handlers = getHandlers(rows); |
||
243 | if (handlers.length < 2) { |
||
244 | return;
|
||
245 | } |
||
246 | Point2D[] points = startAndEndPoints(handlers); |
||
247 | |||
248 | if (!isIntoRect(points[0], handlers)) |
||
249 | return;
|
||
250 | |||
251 | VectorialLayerEdited vle = getVLE(); |
||
252 | VectorialEditableAdapter vea = vle.getVEA(); |
||
253 | vea.startComplexRow(); |
||
254 | |||
255 | int num = vea.getRowCount();
|
||
256 | GeneralPathX gpx = new GeneralPathX();
|
||
257 | gpx.moveTo(points[0].getX(), points[0].getY()); |
||
258 | gpx.lineTo(points[1].getX(), points[1].getY()); |
||
259 | |||
260 | IGeometry geom = ShapeFactory.createPolyline2D(gpx); |
||
261 | DefaultFeature df1 = new DefaultFeature(geom, null, String.valueOf(num)); |
||
262 | |||
263 | for (int i = rows.length - 1; i >= 0; i--) { |
||
264 | vea.removeRow(rows[i].getIndex(), getName(), EditionEvent.GRAPHIC); |
||
265 | } |
||
266 | int index1 = vea.addRow(df1, PluginServices.getText(this, "parte1"), |
||
267 | EditionEvent.GRAPHIC); |
||
268 | 6008 | caballero | //clearSelection();
|
269 | ArrayList selectedRowAux = new ArrayList(); |
||
270 | selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
|
||
271 | 5984 | caballero | vea.getInversedIndex(index1))); |
272 | 6008 | caballero | vle.setSelectionCache(selectedRowAux); |
273 | 6071 | caballero | vea.endComplexRow(getName()); |
274 | 5984 | caballero | |
275 | } |
||
276 | |||
277 | /*
|
||
278 | * (non-Javadoc)
|
||
279 | *
|
||
280 | * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
||
281 | */
|
||
282 | public void addValue(double d) { |
||
283 | } |
||
284 | public String getName() { |
||
285 | return PluginServices.getText(this,"join_"); |
||
286 | } |
||
287 | |||
288 | public String toString() { |
||
289 | return "_join"; |
||
290 | } |
||
291 | |||
292 | public boolean isApplicable(int shapeType) { |
||
293 | switch (shapeType) {
|
||
294 | case FShape.MULTI:
|
||
295 | case FShape.LINE:
|
||
296 | return true; |
||
297 | } |
||
298 | return false; |
||
299 | } |
||
300 | |||
301 | |||
302 | } |