svn-gvsig-desktop / tags / v1_0_1_RELEASE / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 9531
History | View | Annotate | Download (12.3 KB)
1 | 3831 | 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; |
||
42 | |||
43 | 3997 | fjp | import java.awt.Color; |
44 | 3883 | caballero | import java.awt.Graphics; |
45 | import java.awt.Graphics2D; |
||
46 | import java.awt.geom.AffineTransform; |
||
47 | import java.awt.geom.Point2D; |
||
48 | import java.awt.image.BufferedImage; |
||
49 | import java.io.IOException; |
||
50 | 4144 | fjp | import java.util.ArrayList; |
51 | 3883 | caballero | |
52 | 5893 | fjp | import org.apache.log4j.Logger; |
53 | |||
54 | 4024 | caballero | import com.hardcode.driverManager.DriverLoadException; |
55 | import com.hardcode.gdbms.engine.data.driver.DriverException; |
||
56 | 3831 | caballero | import com.hardcode.gdbms.engine.values.Value; |
57 | 4118 | caballero | import com.hardcode.gdbms.engine.values.ValueFactory; |
58 | 3883 | caballero | import com.iver.andami.PluginServices; |
59 | 5923 | fjp | import com.iver.andami.messages.NotificationManager; |
60 | 3883 | caballero | import com.iver.cit.gvsig.CADExtension; |
61 | 3831 | caballero | import com.iver.cit.gvsig.fmap.ViewPort; |
62 | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
||
63 | 3952 | fjp | import com.iver.cit.gvsig.fmap.core.FShape; |
64 | 3831 | caballero | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
65 | import com.iver.cit.gvsig.fmap.core.Handler; |
||
66 | 4144 | fjp | import com.iver.cit.gvsig.fmap.core.IFeature; |
67 | 3831 | caballero | import com.iver.cit.gvsig.fmap.core.IGeometry; |
68 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
69 | import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
||
70 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
71 | 4550 | caballero | import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
72 | 5184 | caballero | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
73 | 4144 | fjp | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
74 | 3831 | caballero | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
75 | 4584 | caballero | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
76 | 5170 | caballero | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
77 | 5735 | caballero | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
78 | 9065 | caballero | import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool; |
79 | 4465 | caballero | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
80 | 8765 | jjdelcerro | import com.iver.cit.gvsig.project.documents.view.gui.View; |
81 | 4892 | caballero | import com.iver.utiles.console.JConsole; |
82 | 3831 | caballero | |
83 | /**
|
||
84 | * DOCUMENT ME!
|
||
85 | 5985 | caballero | *
|
86 | 3831 | caballero | * @author Vicente Caballero Navarro
|
87 | */
|
||
88 | public abstract class DefaultCADTool implements CADTool { |
||
89 | 5923 | fjp | private static Logger logger = Logger.getLogger(DefaultCADTool.class |
90 | .getName()); |
||
91 | |||
92 | 4445 | fjp | private CADToolAdapter cadToolAdapter;
|
93 | 3831 | caballero | |
94 | 4445 | fjp | private String question; |
95 | 3831 | caballero | |
96 | 4445 | fjp | private String[] currentdescriptions; |
97 | 3831 | caballero | |
98 | 4708 | caballero | private String tool = "selection"; |
99 | |||
100 | 9065 | caballero | private DefaultCADTool previousTool;
|
101 | |||
102 | 4445 | fjp | /**
|
103 | * DOCUMENT ME!
|
||
104 | */
|
||
105 | public void draw(IGeometry geometry) { |
||
106 | if (geometry != null) { |
||
107 | BufferedImage img = getCadToolAdapter().getMapControl().getImage();
|
||
108 | Graphics2D gImag = (Graphics2D) img.getGraphics(); |
||
109 | ViewPort vp = getCadToolAdapter().getMapControl().getViewPort(); |
||
110 | geometry.draw(gImag, vp, CADTool.drawingSymbol); |
||
111 | } |
||
112 | } |
||
113 | 3831 | caballero | |
114 | 4445 | fjp | /**
|
115 | * DOCUMENT ME!
|
||
116 | 5985 | caballero | *
|
117 | 4445 | fjp | * @param cta
|
118 | * DOCUMENT ME!
|
||
119 | */
|
||
120 | public void setCadToolAdapter(CADToolAdapter cta) { |
||
121 | cadToolAdapter = cta; |
||
122 | } |
||
123 | 3831 | caballero | |
124 | 4445 | fjp | /**
|
125 | * DOCUMENT ME!
|
||
126 | 5985 | caballero | *
|
127 | 4445 | fjp | * @return DOCUMENT ME!
|
128 | */
|
||
129 | public CADToolAdapter getCadToolAdapter() {
|
||
130 | return cadToolAdapter;
|
||
131 | } |
||
132 | 5923 | fjp | |
133 | public VectorialLayerEdited getVLE() {
|
||
134 | return (VectorialLayerEdited) CADExtension.getEditionManager()
|
||
135 | .getActiveLayerEdited(); |
||
136 | 4637 | caballero | } |
137 | 3831 | caballero | |
138 | 4445 | fjp | /**
|
139 | * DOCUMENT ME!
|
||
140 | 5985 | caballero | *
|
141 | 4445 | fjp | * @param g
|
142 | * DOCUMENT ME!
|
||
143 | * @param firstPoint
|
||
144 | * DOCUMENT ME!
|
||
145 | * @param endPoint
|
||
146 | * DOCUMENT ME!
|
||
147 | */
|
||
148 | public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint) { |
||
149 | GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2); |
||
150 | elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
||
151 | elShape.lineTo(endPoint.getX(), endPoint.getY()); |
||
152 | ShapeFactory.createPolyline2D(elShape).draw(g, |
||
153 | getCadToolAdapter().getMapControl().getViewPort(), |
||
154 | CADTool.drawingSymbol); |
||
155 | } |
||
156 | |||
157 | /**
|
||
158 | * DOCUMENT ME!
|
||
159 | 5985 | caballero | *
|
160 | 4445 | fjp | * @param geometry
|
161 | * DOCUMENT ME!
|
||
162 | */
|
||
163 | public void addGeometry(IGeometry geometry) { |
||
164 | 4971 | caballero | VectorialEditableAdapter vea = getVLE().getVEA(); |
165 | 4445 | fjp | try {
|
166 | 5893 | fjp | // Deber?amos comprobar que lo que escribimos es correcto:
|
167 | // Lo hacemos en el VectorialAdapter, justo antes de
|
||
168 | // a?adir, borrar o modificar una feature
|
||
169 | |||
170 | 4971 | caballero | int numAttr = vea.getRecordset().getFieldCount();
|
171 | 4445 | fjp | Value[] values = new Value[numAttr]; |
172 | for (int i = 0; i < numAttr; i++) { |
||
173 | values[i] = ValueFactory.createNullValue(); |
||
174 | } |
||
175 | 5923 | fjp | int num;
|
176 | try {
|
||
177 | num = vea.getRowCount(); |
||
178 | DefaultFeature df = new DefaultFeature(geometry, values, String |
||
179 | .valueOf(num)); |
||
180 | int index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
|
||
181 | VectorialLayerEdited vle = getVLE(); |
||
182 | 5985 | caballero | clearSelection(); |
183 | 5923 | fjp | ArrayList selectedRow = vle.getSelectedRow();
|
184 | 4637 | caballero | |
185 | 5985 | caballero | |
186 | 6881 | cesar | ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
187 | 5923 | fjp | BufferedImage selectionImage = new BufferedImage(vp |
188 | .getImageWidth(), vp.getImageHeight(), |
||
189 | BufferedImage.TYPE_INT_ARGB);
|
||
190 | Graphics2D gs = selectionImage.createGraphics();
|
||
191 | 5985 | caballero | int inversedIndex=vea.getInversedIndex(index);
|
192 | 5923 | fjp | selectedRow.add(new DefaultRowEdited(df,
|
193 | 5985 | caballero | IRowEdited.STATUS_ADDED, inversedIndex )); |
194 | vea.getSelection().set(inversedIndex); |
||
195 | 5923 | fjp | IGeometry geom = df.getGeometry(); |
196 | geom.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol); |
||
197 | vle.drawHandlers(geom.cloneGeometry(), gs, vp); |
||
198 | vea.setSelectionImage(selectionImage); |
||
199 | } catch (IOException e) { |
||
200 | logger.debug(e); |
||
201 | NotificationManager.addError(e); |
||
202 | return;
|
||
203 | } |
||
204 | 4637 | caballero | |
205 | 4445 | fjp | } catch (DriverIOException e) {
|
206 | 4024 | caballero | e.printStackTrace(); |
207 | 4445 | fjp | } catch (DriverException e) {
|
208 | e.printStackTrace(); |
||
209 | 4024 | caballero | } catch (DriverLoadException e) {
|
210 | e.printStackTrace(); |
||
211 | } |
||
212 | 3831 | caballero | |
213 | 4445 | fjp | draw(geometry.cloneGeometry()); |
214 | } |
||
215 | |||
216 | /**
|
||
217 | * DOCUMENT ME!
|
||
218 | 5985 | caballero | *
|
219 | 4445 | fjp | * @param geometry
|
220 | * DOCUMENT ME!
|
||
221 | */
|
||
222 | public void modifyFeature(int index, IFeature row) { |
||
223 | try {
|
||
224 | 5923 | fjp | getVLE().getVEA().modifyRow(index, row, getName(), |
225 | EditionEvent.GRAPHIC); |
||
226 | 3831 | caballero | } catch (IOException e1) { |
227 | e1.printStackTrace(); |
||
228 | } catch (DriverIOException e1) {
|
||
229 | e1.printStackTrace(); |
||
230 | } |
||
231 | 4445 | fjp | draw(row.getGeometry().cloneGeometry()); |
232 | } |
||
233 | 3831 | caballero | |
234 | 4445 | fjp | /**
|
235 | * DOCUMENT ME!
|
||
236 | 5985 | caballero | *
|
237 | 4445 | fjp | * @param geometry
|
238 | * DOCUMENT ME!
|
||
239 | * @param values
|
||
240 | * DOCUMENT ME!
|
||
241 | */
|
||
242 | 5884 | caballero | public int addGeometry(IGeometry geometry, Value[] values) { |
243 | 5923 | fjp | int index = 0; |
244 | 5884 | caballero | VectorialEditableAdapter vea = getVLE().getVEA(); |
245 | try {
|
||
246 | 5923 | fjp | int num = vea.getRowCount();
|
247 | DefaultFeature df = new DefaultFeature(geometry, values, String |
||
248 | .valueOf(num)); |
||
249 | index = vea.addRow(df, getName(), EditionEvent.GRAPHIC); |
||
250 | 5884 | caballero | } catch (DriverIOException e) {
|
251 | e.printStackTrace(); |
||
252 | } catch (IOException e) { |
||
253 | e.printStackTrace(); |
||
254 | } catch (DriverLoadException e) {
|
||
255 | e.printStackTrace(); |
||
256 | } |
||
257 | 5985 | caballero | return vea.getInversedIndex(index);
|
258 | 4445 | fjp | } |
259 | 3831 | caballero | |
260 | 4445 | fjp | /**
|
261 | * Devuelve la cadena que corresponde al estado en el que nos encontramos.
|
||
262 | 5985 | caballero | *
|
263 | 4445 | fjp | * @return Cadena para mostrar por consola.
|
264 | */
|
||
265 | public String getQuestion() { |
||
266 | return question;
|
||
267 | } |
||
268 | 3831 | caballero | |
269 | 4445 | fjp | /**
|
270 | * Actualiza la cadena que corresponde al estado actual.
|
||
271 | 5985 | caballero | *
|
272 | 4445 | fjp | * @param s
|
273 | * Cadena que aparecer? en consola.
|
||
274 | */
|
||
275 | public void setQuestion(String s) { |
||
276 | question = s; |
||
277 | 5923 | fjp | // ConsoleToken.addQuestion(s);
|
278 | 4445 | fjp | } |
279 | 3831 | caballero | |
280 | 4445 | fjp | /**
|
281 | 5923 | fjp | * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
|
282 | * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
|
||
283 | * edici?n y las que est?n por encima de ella en el TOC.
|
||
284 | 4445 | fjp | */
|
285 | public void refresh() { |
||
286 | 5923 | fjp | // getCadToolAdapter().getMapControl().drawMap(false);
|
287 | getVLE().getLayer().setDirty(true);
|
||
288 | |||
289 | getCadToolAdapter().getMapControl().rePaintDirtyLayers(); |
||
290 | 4445 | fjp | } |
291 | |||
292 | 5923 | fjp | /*
|
293 | * public void drawHandlers(Graphics g, FBitSet sel, AffineTransform at)
|
||
294 | * throws DriverIOException { for (int i = sel.nextSetBit(0); i >= 0; i =
|
||
295 | * sel.nextSetBit(i + 1)) { IGeometry ig =
|
||
296 | * getCadToolAdapter().getVectorialAdapter() .getShape(i).cloneGeometry();
|
||
297 | * if (ig == null) continue; Handler[] handlers =
|
||
298 | * ig.getHandlers(IGeometry.SELECTHANDLER);
|
||
299 | * FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers); } }
|
||
300 | */
|
||
301 | 4445 | fjp | public void drawHandlers(Graphics g, ArrayList selectedRows, |
302 | AffineTransform at) {
|
||
303 | for (int i = 0; i < selectedRows.size(); i++) { |
||
304 | IRowEdited edRow = (IRowEdited) selectedRows.get(i); |
||
305 | IFeature feat = (IFeature) edRow.getLinkedRow(); |
||
306 | // IFeature feat = (IFeature) selectedRows.get(i);
|
||
307 | 4144 | fjp | IGeometry ig = feat.getGeometry().cloneGeometry(); |
308 | 4445 | fjp | if (ig == null) |
309 | continue;
|
||
310 | Handler[] handlers = ig.getHandlers(IGeometry.SELECTHANDLER); |
||
311 | FGraphicUtilities.DrawHandlers((Graphics2D) g, at, handlers);
|
||
312 | } |
||
313 | } |
||
314 | 3883 | caballero | |
315 | public void setDescription(String[] currentdescriptions) { |
||
316 | this.currentdescriptions = currentdescriptions;
|
||
317 | } |
||
318 | 4445 | fjp | |
319 | public String[] getDescriptions() { |
||
320 | 3883 | caballero | return currentdescriptions;
|
321 | } |
||
322 | |||
323 | 4445 | fjp | /*
|
324 | * (non-Javadoc)
|
||
325 | 5985 | caballero | *
|
326 | 4445 | fjp | * @see com.iver.cit.gvsig.gui.cad.CADTool#end()
|
327 | */
|
||
328 | public void end() { |
||
329 | 5923 | fjp | CADExtension.setCADTool("_selection", true); |
330 | 5016 | caballero | PluginServices.getMainFrame().setSelectedTool("_selection");
|
331 | 9065 | caballero | CADTool cadtool=CADExtension.getCADTool(); |
332 | cadtool.setPreviosTool(this);
|
||
333 | 4445 | fjp | } |
334 | |||
335 | 3997 | fjp | public void init() { |
336 | CADTool.drawingSymbol.setOutlined(true);
|
||
337 | CADTool.drawingSymbol.setOutlineColor(Color.GREEN);
|
||
338 | 4024 | caballero | |
339 | 3997 | fjp | } |
340 | 5923 | fjp | |
341 | protected ArrayList getSelectedRows() { |
||
342 | 4637 | caballero | VectorialLayerEdited vle = getVLE(); |
343 | 5923 | fjp | ArrayList selectedRow = vle.getSelectedRow();
|
344 | return selectedRow;
|
||
345 | 4465 | caballero | } |
346 | 5923 | fjp | |
347 | protected ArrayList getSelectedHandlers() { |
||
348 | 4637 | caballero | VectorialLayerEdited vle = getVLE(); |
349 | 5923 | fjp | ArrayList selectedHandlers = vle.getSelectedHandler();
|
350 | return selectedHandlers;
|
||
351 | 4584 | caballero | } |
352 | 5923 | fjp | |
353 | public void clearSelection() { |
||
354 | 4637 | caballero | VectorialLayerEdited vle = getVLE(); |
355 | 4584 | caballero | ArrayList selectedRow = vle.getSelectedRow();
|
356 | 5923 | fjp | ArrayList selectedHandlers = vle.getSelectedHandler();
|
357 | selectedRow.clear(); |
||
358 | selectedHandlers.clear(); |
||
359 | VectorialEditableAdapter vea = vle.getVEA(); |
||
360 | FBitSet selection = vea.getSelection(); |
||
361 | selection.clear(); |
||
362 | vea.setSelectionImage(null);
|
||
363 | vea.setHandlersImage(null);
|
||
364 | 4637 | caballero | |
365 | 4584 | caballero | } |
366 | 5923 | fjp | |
367 | 4708 | caballero | public String getNextTool() { |
368 | return tool;
|
||
369 | } |
||
370 | |||
371 | public void setNextTool(String tool) { |
||
372 | this.tool = tool;
|
||
373 | } |
||
374 | 5923 | fjp | |
375 | public boolean changeCommand(String name) throws CommandException { |
||
376 | CADTool[] cadtools = CADExtension.getCADTools();
|
||
377 | for (int i = 0; i < cadtools.length; i++) { |
||
378 | CADTool ct = cadtools[i]; |
||
379 | if (name.equalsIgnoreCase(ct.getName())
|
||
380 | || name.equalsIgnoreCase(ct.toString())) { |
||
381 | int type = FShape.POINT;
|
||
382 | 5170 | caballero | try {
|
383 | 5923 | fjp | type = ((FLyrVect) getVLE().getLayer()).getShapeType(); |
384 | 5170 | caballero | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
385 | e.printStackTrace(); |
||
386 | } |
||
387 | if (ct.isApplicable(type)) {
|
||
388 | getCadToolAdapter().setCadTool(ct); |
||
389 | ct.init(); |
||
390 | View vista = (View) PluginServices.getMDIManager() |
||
391 | 6880 | cesar | .getActiveWindow(); |
392 | 5170 | caballero | vista.getConsolePanel().addText("\n" + ct.getName(),
|
393 | JConsole.COMMAND); |
||
394 | return true; |
||
395 | 5923 | fjp | } else {
|
396 | 5735 | caballero | throw new CommandException(name); |
397 | 5170 | caballero | } |
398 | 4892 | caballero | } |
399 | } |
||
400 | return false; |
||
401 | } |
||
402 | 5923 | fjp | |
403 | 5170 | caballero | public boolean isApplicable(int shapeType) { |
404 | return true; |
||
405 | } |
||
406 | 5923 | fjp | |
407 | 4892 | caballero | public abstract String toString(); |
408 | 5735 | caballero | |
409 | 5923 | fjp | public void throwValueException(String s, double d) { |
410 | 6880 | cesar | View vista = (View) PluginServices.getMDIManager().getActiveWindow(); |
411 | 5923 | fjp | vista.getConsolePanel().addText(s + " : " + d, JConsole.ERROR);
|
412 | 5735 | caballero | } |
413 | 5923 | fjp | |
414 | public void throwOptionException(String s, String o) { |
||
415 | 6880 | cesar | View vista = (View) PluginServices.getMDIManager().getActiveWindow(); |
416 | 5923 | fjp | vista.getConsolePanel().addText(s + " : " + o, JConsole.ERROR);
|
417 | 5735 | caballero | } |
418 | 5923 | fjp | |
419 | public void throwPointException(String s, double x, double y) { |
||
420 | 6880 | cesar | View vista = (View) PluginServices.getMDIManager().getActiveWindow(); |
421 | 5923 | fjp | vista.getConsolePanel().addText(s + " : " + " X = " + x + ", Y = " + y, |
422 | JConsole.ERROR); |
||
423 | 5735 | caballero | } |
424 | |||
425 | 9065 | caballero | public void setPreviosTool(DefaultCADTool tool) { |
426 | previousTool=tool; |
||
427 | } |
||
428 | public void restorePreviousTool() { |
||
429 | CADExtension.setCADTool(previousTool.toString(), true);
|
||
430 | PluginServices.getMainFrame().setSelectedTool(previousTool.toString()); |
||
431 | } |
||
432 | |||
433 | 3831 | caballero | } |