root / tags / v1_0_2_Build_912 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / DefaultCADTool.java @ 11422
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 | 10656 | caballero | import com.iver.cit.gvsig.fmap.layers.FLayer; |
77 | 5170 | caballero | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
78 | 5735 | caballero | import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
79 | 9065 | caballero | import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool; |
80 | 4465 | caballero | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
81 | 8765 | jjdelcerro | import com.iver.cit.gvsig.project.documents.view.gui.View; |
82 | 4892 | caballero | import com.iver.utiles.console.JConsole; |
83 | 3831 | caballero | |
84 | /**
|
||
85 | * DOCUMENT ME!
|
||
86 | 5985 | caballero | *
|
87 | 3831 | caballero | * @author Vicente Caballero Navarro
|
88 | */
|
||
89 | public abstract class DefaultCADTool implements CADTool { |
||
90 | 5923 | fjp | private static Logger logger = Logger.getLogger(DefaultCADTool.class |
91 | .getName()); |
||
92 | |||
93 | 4445 | fjp | private CADToolAdapter cadToolAdapter;
|
94 | 3831 | caballero | |
95 | 4445 | fjp | private String question; |
96 | 3831 | caballero | |
97 | 4445 | fjp | private String[] currentdescriptions; |
98 | 3831 | caballero | |
99 | 4708 | caballero | private String tool = "selection"; |
100 | |||
101 | 9065 | caballero | private DefaultCADTool previousTool;
|
102 | |||
103 | 4445 | fjp | /**
|
104 | * DOCUMENT ME!
|
||
105 | */
|
||
106 | public void draw(IGeometry geometry) { |
||
107 | if (geometry != null) { |
||
108 | BufferedImage img = getCadToolAdapter().getMapControl().getImage();
|
||
109 | Graphics2D gImag = (Graphics2D) img.getGraphics(); |
||
110 | ViewPort vp = getCadToolAdapter().getMapControl().getViewPort(); |
||
111 | geometry.draw(gImag, vp, CADTool.drawingSymbol); |
||
112 | } |
||
113 | } |
||
114 | 3831 | caballero | |
115 | 4445 | fjp | /**
|
116 | * DOCUMENT ME!
|
||
117 | 5985 | caballero | *
|
118 | 4445 | fjp | * @param cta
|
119 | * DOCUMENT ME!
|
||
120 | */
|
||
121 | public void setCadToolAdapter(CADToolAdapter cta) { |
||
122 | cadToolAdapter = cta; |
||
123 | } |
||
124 | 3831 | caballero | |
125 | 4445 | fjp | /**
|
126 | * DOCUMENT ME!
|
||
127 | 5985 | caballero | *
|
128 | 4445 | fjp | * @return DOCUMENT ME!
|
129 | */
|
||
130 | public CADToolAdapter getCadToolAdapter() {
|
||
131 | return cadToolAdapter;
|
||
132 | } |
||
133 | 5923 | fjp | |
134 | public VectorialLayerEdited getVLE() {
|
||
135 | return (VectorialLayerEdited) CADExtension.getEditionManager()
|
||
136 | .getActiveLayerEdited(); |
||
137 | 4637 | caballero | } |
138 | 3831 | caballero | |
139 | 4445 | fjp | /**
|
140 | * DOCUMENT ME!
|
||
141 | 5985 | caballero | *
|
142 | 4445 | fjp | * @param g
|
143 | * DOCUMENT ME!
|
||
144 | * @param firstPoint
|
||
145 | * DOCUMENT ME!
|
||
146 | * @param endPoint
|
||
147 | * DOCUMENT ME!
|
||
148 | */
|
||
149 | public void drawLine(Graphics2D g, Point2D firstPoint, Point2D endPoint) { |
||
150 | GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2); |
||
151 | elShape.moveTo(firstPoint.getX(), firstPoint.getY()); |
||
152 | elShape.lineTo(endPoint.getX(), endPoint.getY()); |
||
153 | ShapeFactory.createPolyline2D(elShape).draw(g, |
||
154 | getCadToolAdapter().getMapControl().getViewPort(), |
||
155 | CADTool.drawingSymbol); |
||
156 | } |
||
157 | |||
158 | /**
|
||
159 | * DOCUMENT ME!
|
||
160 | 5985 | caballero | *
|
161 | 4445 | fjp | * @param geometry
|
162 | * DOCUMENT ME!
|
||
163 | */
|
||
164 | public void addGeometry(IGeometry geometry) { |
||
165 | 4971 | caballero | VectorialEditableAdapter vea = getVLE().getVEA(); |
166 | 4445 | fjp | try {
|
167 | 5893 | fjp | // Deber?amos comprobar que lo que escribimos es correcto:
|
168 | // Lo hacemos en el VectorialAdapter, justo antes de
|
||
169 | // a?adir, borrar o modificar una feature
|
||
170 | |||
171 | 4971 | caballero | int numAttr = vea.getRecordset().getFieldCount();
|
172 | 4445 | fjp | Value[] values = new Value[numAttr]; |
173 | for (int i = 0; i < numAttr; i++) { |
||
174 | values[i] = ValueFactory.createNullValue(); |
||
175 | } |
||
176 | 5923 | fjp | int num;
|
177 | try {
|
||
178 | num = vea.getRowCount(); |
||
179 | DefaultFeature df = new DefaultFeature(geometry, values, String |
||
180 | .valueOf(num)); |
||
181 | int index = vea.addRow(df, getName(), EditionEvent.GRAPHIC);
|
||
182 | VectorialLayerEdited vle = getVLE(); |
||
183 | 5985 | caballero | clearSelection(); |
184 | 5923 | fjp | ArrayList selectedRow = vle.getSelectedRow();
|
185 | 4637 | caballero | |
186 | 5985 | caballero | |
187 | 6881 | cesar | ViewPort vp = vle.getLayer().getMapContext().getViewPort(); |
188 | 5923 | fjp | BufferedImage selectionImage = new BufferedImage(vp |
189 | .getImageWidth(), vp.getImageHeight(), |
||
190 | BufferedImage.TYPE_INT_ARGB);
|
||
191 | Graphics2D gs = selectionImage.createGraphics();
|
||
192 | 5985 | caballero | int inversedIndex=vea.getInversedIndex(index);
|
193 | 5923 | fjp | selectedRow.add(new DefaultRowEdited(df,
|
194 | 5985 | caballero | IRowEdited.STATUS_ADDED, inversedIndex )); |
195 | vea.getSelection().set(inversedIndex); |
||
196 | 5923 | fjp | IGeometry geom = df.getGeometry(); |
197 | geom.cloneGeometry().draw(gs, vp, CADTool.drawingSymbol); |
||
198 | vle.drawHandlers(geom.cloneGeometry(), gs, vp); |
||
199 | vea.setSelectionImage(selectionImage); |
||
200 | } catch (IOException e) { |
||
201 | logger.debug(e); |
||
202 | NotificationManager.addError(e); |
||
203 | return;
|
||
204 | } |
||
205 | 4637 | caballero | |
206 | 4445 | fjp | } catch (DriverIOException e) {
|
207 | 4024 | caballero | e.printStackTrace(); |
208 | 4445 | fjp | } catch (DriverException e) {
|
209 | e.printStackTrace(); |
||
210 | 4024 | caballero | } catch (DriverLoadException e) {
|
211 | e.printStackTrace(); |
||
212 | } |
||
213 | 3831 | caballero | |
214 | 4445 | fjp | draw(geometry.cloneGeometry()); |
215 | } |
||
216 | |||
217 | /**
|
||
218 | * DOCUMENT ME!
|
||
219 | 5985 | caballero | *
|
220 | 4445 | fjp | * @param geometry
|
221 | * DOCUMENT ME!
|
||
222 | */
|
||
223 | public void modifyFeature(int index, IFeature row) { |
||
224 | try {
|
||
225 | 5923 | fjp | getVLE().getVEA().modifyRow(index, row, getName(), |
226 | EditionEvent.GRAPHIC); |
||
227 | 3831 | caballero | } catch (IOException e1) { |
228 | e1.printStackTrace(); |
||
229 | } catch (DriverIOException e1) {
|
||
230 | e1.printStackTrace(); |
||
231 | } |
||
232 | 4445 | fjp | draw(row.getGeometry().cloneGeometry()); |
233 | } |
||
234 | 3831 | caballero | |
235 | 4445 | fjp | /**
|
236 | * DOCUMENT ME!
|
||
237 | 5985 | caballero | *
|
238 | 4445 | fjp | * @param geometry
|
239 | * DOCUMENT ME!
|
||
240 | * @param values
|
||
241 | * DOCUMENT ME!
|
||
242 | */
|
||
243 | 5884 | caballero | public int addGeometry(IGeometry geometry, Value[] values) { |
244 | 5923 | fjp | int index = 0; |
245 | 5884 | caballero | VectorialEditableAdapter vea = getVLE().getVEA(); |
246 | try {
|
||
247 | 5923 | fjp | int num = vea.getRowCount();
|
248 | DefaultFeature df = new DefaultFeature(geometry, values, String |
||
249 | .valueOf(num)); |
||
250 | index = vea.addRow(df, getName(), EditionEvent.GRAPHIC); |
||
251 | 5884 | caballero | } catch (DriverIOException e) {
|
252 | e.printStackTrace(); |
||
253 | } catch (IOException e) { |
||
254 | e.printStackTrace(); |
||
255 | } catch (DriverLoadException e) {
|
||
256 | e.printStackTrace(); |
||
257 | } |
||
258 | 5985 | caballero | return vea.getInversedIndex(index);
|
259 | 4445 | fjp | } |
260 | 3831 | caballero | |
261 | 4445 | fjp | /**
|
262 | * Devuelve la cadena que corresponde al estado en el que nos encontramos.
|
||
263 | 5985 | caballero | *
|
264 | 4445 | fjp | * @return Cadena para mostrar por consola.
|
265 | */
|
||
266 | public String getQuestion() { |
||
267 | return question;
|
||
268 | } |
||
269 | 3831 | caballero | |
270 | 4445 | fjp | /**
|
271 | * Actualiza la cadena que corresponde al estado actual.
|
||
272 | 5985 | caballero | *
|
273 | 4445 | fjp | * @param s
|
274 | * Cadena que aparecer? en consola.
|
||
275 | */
|
||
276 | public void setQuestion(String s) { |
||
277 | question = s; |
||
278 | 5923 | fjp | // ConsoleToken.addQuestion(s);
|
279 | 4445 | fjp | } |
280 | 3831 | caballero | |
281 | 4445 | fjp | /**
|
282 | 5923 | fjp | * Provoca un repintado "soft" de la capa activa en edici?n. Las capas por
|
283 | * debajo de ella no se dibujan de verdad, solo se dibuja la que est? en
|
||
284 | * edici?n y las que est?n por encima de ella en el TOC.
|
||
285 | 4445 | fjp | */
|
286 | public void refresh() { |
||
287 | 10656 | caballero | FLayer layer=getVLE().getLayer(); |
288 | layer.setDirty(true);
|
||
289 | 5923 | fjp | 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 | 10292 | caballero | //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 | } |