Revision 5223 trunk/extensions/extCAD/src/com/iver/cit/gvsig/gui/cad/CADToolAdapter.java
CADToolAdapter.java | ||
---|---|---|
17 | 17 |
|
18 | 18 |
import com.iver.andami.PluginServices; |
19 | 19 |
import com.iver.cit.gvsig.CADExtension; |
20 |
import com.iver.cit.gvsig.EditionManager; |
|
20 | 21 |
import com.iver.cit.gvsig.fmap.DriverException; |
21 | 22 |
import com.iver.cit.gvsig.fmap.ViewPort; |
22 | 23 |
import com.iver.cit.gvsig.fmap.core.Handler; |
... | ... | |
31 | 32 |
import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
32 | 33 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
33 | 34 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
35 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
36 |
import com.iver.cit.gvsig.fmap.layers.SpatialCache; |
|
34 | 37 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
35 | 38 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
36 | 39 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
37 | 40 |
import com.iver.cit.gvsig.gui.View; |
41 |
import com.iver.cit.gvsig.gui.cad.snapping.NearestPointSnapper; |
|
42 |
import com.iver.cit.gvsig.gui.cad.snapping.SnappingVisitor; |
|
38 | 43 |
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool; |
44 |
import com.iver.cit.gvsig.layers.ILayerEdited; |
|
39 | 45 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
40 | 46 |
import com.iver.utiles.console.JConsole; |
41 | 47 |
import com.vividsolutions.jts.geom.Envelope; |
42 |
import com.vividsolutions.jts.index.SpatialIndex; |
|
43 | 48 |
|
44 | 49 |
public class CADToolAdapter extends Behavior { |
45 |
public static int MAX_ENTITIES_IN_SPATIAL_CACHE = 5000; |
|
46 | 50 |
|
47 | 51 |
public static final int ABSOLUTE = 0; |
48 | 52 |
|
... | ... | |
75 | 79 |
|
76 | 80 |
private boolean adjustSnapping = false; |
77 | 81 |
|
78 |
private VectorialEditableAdapter vea; |
|
79 |
|
|
80 | 82 |
private CADGrid cadgrid = new CADGrid(); |
81 | 83 |
|
82 |
private SpatialIndex spatialCache; |
|
83 |
|
|
84 | 84 |
/** |
85 | 85 |
* Pinta de alguna manera especial las geometrias seleccionadas para la |
86 | 86 |
* edici?n. En caso de que el snapping est? activado, pintar? el efecto del |
87 | 87 |
* mismo. |
88 |
* |
|
88 |
*
|
|
89 | 89 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics) |
90 | 90 |
*/ |
91 | 91 |
public void paintComponent(Graphics g) { |
... | ... | |
146 | 146 |
/** |
147 | 147 |
* Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste |
148 | 148 |
* est? activo y devuelve la distancia de un punto al punto ajustado |
149 |
* |
|
149 |
*
|
|
150 | 150 |
* @param point |
151 | 151 |
* @param mapHandlerAdjustedPoint |
152 | 152 |
* DOCUMENT ME! |
153 |
* |
|
154 |
* @return Distancia del punto que se pasa como par?metro al punto ajustado |
|
153 |
* |
|
154 |
* @return Distancia del punto que se pasa como |
|
155 |
* par?metro al punto ajustado. Si no hay ajuste, |
|
156 |
* devuelve Double.MAX_VALUE |
|
155 | 157 |
*/ |
156 | 158 |
private double adjustToHandler(Point2D point, |
157 | 159 |
Point2D mapHandlerAdjustedPoint) { |
158 | 160 |
|
161 |
ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited(); |
|
162 |
if (!(aux instanceof VectorialLayerEdited)) |
|
163 |
return Double.MAX_VALUE; |
|
164 |
VectorialLayerEdited vle = (VectorialLayerEdited) aux; |
|
165 |
VectorialEditableAdapter vea = vle.getVEA(); |
|
166 |
FLyrVect lyrVect = (FLyrVect) vle.getLayer(); |
|
167 |
SpatialCache cache = lyrVect.getSpatialCache(); |
|
168 |
ViewPort vp = getMapControl().getViewPort(); |
|
169 |
if (cache == null) |
|
170 |
return Double.MAX_VALUE; |
|
171 |
|
|
159 | 172 |
double rw = getMapControl().getViewPort().toMapDistance(5); |
160 | 173 |
Point2D mapPoint = point; |
161 | 174 |
Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw / 2, |
162 | 175 |
mapPoint.getY() - rw / 2, rw, rw); |
163 | 176 |
|
164 | 177 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(r); |
165 |
double min = Double.MAX_VALUE; |
|
166 |
Point2D argmin = null; |
|
167 |
Point2D mapArgmin = null; |
|
178 |
|
|
179 |
// TODO: Poner en VectorialLayerEdited los ISnappers que queremos |
|
180 |
// usar e iterar por ellos para obtener el mejor punto. |
|
181 |
NearestPointSnapper defaultSnap = new NearestPointSnapper(); |
|
182 |
double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance); |
|
183 |
SnappingVisitor snapVisitor = new SnappingVisitor(defaultSnap, point, mapTolerance); |
|
184 |
cache.query(e, snapVisitor); |
|
185 |
|
|
168 | 186 |
|
169 |
IRowEdited[] feats; |
|
170 |
ViewPort vp = getMapControl().getViewPort(); |
|
171 |
String strEPSG = vp.getProjection().getAbrev() |
|
172 |
.substring(5); |
|
173 |
try { |
|
174 |
feats = vea.getFeatures(r, strEPSG); |
|
175 |
IGeometry geometry = null; |
|
176 |
for (int i = 0; i < feats.length; i++) { |
|
177 |
IFeature feat = (IFeature) feats[i].getLinkedRow(); |
|
178 |
geometry = feat.getGeometry(); |
|
179 |
// TODO: PROVISIONAL. AVERIGUAR PORQU? DA FALLO EL DRIVER INDEXEDSHAPEDRIVER |
|
180 |
|
|
181 |
if (geometry == null) break; |
|
182 |
Handler[] handlers = geometry.getHandlers(IGeometry.SELECTHANDLER); |
|
183 |
|
|
184 |
for (int j = 0; j < handlers.length; j++) { |
|
185 |
Point2D handlerPoint = handlers[j].getPoint(); |
|
186 |
// System.err.println("handlerPoint= "+ handlerPoint); |
|
187 |
Point2D handlerImagePoint = handlerPoint; |
|
188 |
double dist = handlerImagePoint.distance(point); |
|
189 |
if ((dist < vp.toMapDistance( |
|
190 |
SelectionCADTool.tolerance)) |
|
191 |
&& (dist < min)) { |
|
192 |
min = dist; |
|
193 |
argmin = handlerImagePoint; |
|
194 |
mapArgmin = handlerPoint; |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
} catch (DriverException e1) { |
|
199 |
// TODO Auto-generated catch block |
|
200 |
e1.printStackTrace(); |
|
187 |
if (snapVisitor.getSnapPoint() != null) { |
|
188 |
mapHandlerAdjustedPoint.setLocation(snapVisitor.getSnapPoint()); |
|
189 |
return snapVisitor.getMinDist(); |
|
201 | 190 |
} |
202 | 191 |
|
203 |
if (argmin != null) { |
|
204 |
point.setLocation(argmin); |
|
205 |
|
|
206 |
// Se hace el casting porque no se quiere redondeo |
|
207 |
point.setLocation(argmin.getX(), argmin.getY()); |
|
208 |
|
|
209 |
mapHandlerAdjustedPoint.setLocation(mapArgmin); |
|
210 |
|
|
211 |
return min; |
|
212 |
} |
|
213 |
|
|
214 | 192 |
return Double.MAX_VALUE; |
215 | 193 |
|
216 | 194 |
} |
... | ... | |
257 | 235 |
|
258 | 236 |
/** |
259 | 237 |
* DOCUMENT ME! |
260 |
* |
|
238 |
*
|
|
261 | 239 |
* @param g |
262 | 240 |
* DOCUMENT ME! |
263 | 241 |
*/ |
... | ... | |
299 | 277 |
|
300 | 278 |
/** |
301 | 279 |
* DOCUMENT ME! |
302 |
* |
|
280 |
*
|
|
303 | 281 |
* @param point |
304 | 282 |
*/ |
305 | 283 |
private void calculateSnapPoint(Point point) { |
... | ... | |
340 | 318 |
|
341 | 319 |
if (distance < minDistance) { |
342 | 320 |
adjustSnapping = true; |
343 |
adjustedPoint = getMapControl().getViewPort().fromMapPoint( |
|
344 |
handlerAdjustedPoint); |
|
321 |
adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint); |
|
345 | 322 |
mapAdjustedPoint = mapPoint; |
346 | 323 |
minDistance = distance; |
347 | 324 |
} |
... | ... | |
394 | 371 |
/** |
395 | 372 |
* M?todo que realiza las transiciones en las herramientas en funci?n de un |
396 | 373 |
* texto introducido en la consola |
397 |
* |
|
374 |
*
|
|
398 | 375 |
* @param text |
399 | 376 |
* DOCUMENT ME! |
400 | 377 |
*/ |
... | ... | |
406 | 383 |
* if ("".equals(text)) { transition("aceptar"); } else { |
407 | 384 |
*/ |
408 | 385 |
text = text.trim(); |
409 |
int type=ABSOLUTE;
|
|
410 |
String[] numbers=new String[1];
|
|
411 |
numbers[0]=text;
|
|
412 |
if (text.indexOf(",")!=-1){
|
|
386 |
int type = ABSOLUTE;
|
|
387 |
String[] numbers = new String[1];
|
|
388 |
numbers[0] = text;
|
|
389 |
if (text.indexOf(",") != -1) {
|
|
413 | 390 |
|
414 | 391 |
numbers = text.split(","); |
415 | 392 |
if (numbers[0].substring(0, 1).equals("@")) { |
... | ... | |
471 | 448 |
if (desc[i] != null) { |
472 | 449 |
CADExtension |
473 | 450 |
.addMenuEntry(PluginServices.getText(this, desc[i]));// , |
474 |
// labels[i]);
|
|
451 |
// labels[i]); |
|
475 | 452 |
} |
476 | 453 |
} |
477 | 454 |
|
... | ... | |
482 | 459 |
* con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene en el |
483 | 460 |
* pressed) y si es de teclado, ser? un KeyEvent. Del evento se puede sacar |
484 | 461 |
* informaci?n acerca de si estaba pulsada la tecla CTRL, o Alt, etc. |
485 |
* |
|
462 |
*
|
|
486 | 463 |
* @param values |
487 | 464 |
* @param event |
488 | 465 |
*/ |
... | ... | |
562 | 539 |
|
563 | 540 |
/** |
564 | 541 |
* DOCUMENT ME! |
565 |
* |
|
542 |
*
|
|
566 | 543 |
* @param text |
567 | 544 |
* DOCUMENT ME! |
568 | 545 |
* @param source |
... | ... | |
604 | 581 |
|
605 | 582 |
/** |
606 | 583 |
* DOCUMENT ME! |
607 |
* |
|
584 |
*
|
|
608 | 585 |
* @param value |
609 | 586 |
* DOCUMENT ME! |
610 | 587 |
*/ |
... | ... | |
616 | 593 |
|
617 | 594 |
/** |
618 | 595 |
* DOCUMENT ME! |
619 |
* |
|
596 |
*
|
|
620 | 597 |
* @param activated |
621 | 598 |
* DOCUMENT ME! |
622 | 599 |
*/ |
... | ... | |
625 | 602 |
} |
626 | 603 |
|
627 | 604 |
/** |
628 |
* DOCUMENT ME! |
|
629 |
* |
|
630 |
* @param x |
|
631 |
* DOCUMENT ME! |
|
632 |
* @param y |
|
633 |
* DOCUMENT ME! |
|
634 |
* @param dist |
|
635 |
* DOCUMENT ME! |
|
636 |
*/ |
|
637 |
public void getSnapPoint(double x, double y, double dist) { |
|
638 |
} |
|
639 |
|
|
640 |
/** |
|
641 | 605 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener() |
642 | 606 |
*/ |
643 | 607 |
public ToolListener getListener() { |
... | ... | |
660 | 624 |
|
661 | 625 |
/** |
662 | 626 |
* DOCUMENT ME! |
663 |
* |
|
627 |
*
|
|
664 | 628 |
* @return DOCUMENT ME! |
665 | 629 |
*/ |
666 | 630 |
public CADTool getCadTool() { |
... | ... | |
669 | 633 |
|
670 | 634 |
/** |
671 | 635 |
* DOCUMENT ME! |
672 |
* |
|
636 |
*
|
|
673 | 637 |
* @param cadTool |
674 | 638 |
* DOCUMENT ME! |
675 | 639 |
*/ |
... | ... | |
681 | 645 |
/* |
682 | 646 |
* int ret = cadTool.transition(null, editableFeatureSource, selection, |
683 | 647 |
* new double[0]); |
684 |
* |
|
648 |
*
|
|
685 | 649 |
* if ((ret & Automaton.AUTOMATON_FINISHED) == |
686 | 650 |
* Automaton.AUTOMATON_FINISHED) { popCadTool(); |
687 |
* |
|
651 |
*
|
|
688 | 652 |
* if (cadToolStack.isEmpty()) { pushCadTool(new |
689 | 653 |
* com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new |
690 | 654 |
* SelectionCadTool()); |
691 | 655 |
* PluginServices.getMainFrame().setSelectedTool("selection"); } |
692 |
* |
|
656 |
*
|
|
693 | 657 |
* askQuestion(); |
694 |
* |
|
658 |
*
|
|
695 | 659 |
* getMapControl().drawMap(false); } |
696 | 660 |
*/ |
697 | 661 |
} |
... | ... | |
724 | 688 |
|
725 | 689 |
/** |
726 | 690 |
* DOCUMENT ME! |
727 |
* |
|
691 |
*
|
|
728 | 692 |
* @param cadTool |
729 | 693 |
* DOCUMENT ME! |
730 | 694 |
*/ |
... | ... | |
734 | 698 |
// askQuestion(); |
735 | 699 |
} |
736 | 700 |
|
737 |
/** |
|
738 |
* DOCUMENT ME! |
|
739 |
* |
|
740 |
* @return DOCUMENT ME! |
|
741 |
*/ |
|
742 |
public VectorialEditableAdapter getVectorialAdapter() { |
|
743 |
return vea; |
|
744 |
} |
|
745 | 701 |
|
746 | 702 |
/** |
747 |
* DOCUMENT ME! |
|
748 |
* |
|
749 |
* @param editableFeatureSource |
|
750 |
* DOCUMENT ME! |
|
751 |
* @param selection |
|
752 |
* DOCUMENT ME! |
|
753 |
*/ |
|
754 |
public void setVectorialAdapter(VectorialEditableAdapter vea) { |
|
755 |
this.vea = vea; |
|
756 |
} |
|
757 |
|
|
758 |
/** |
|
759 |
* DOCUMENT ME! |
|
760 |
* |
|
761 |
* @return DOCUMENT ME! |
|
762 |
*/ |
|
763 |
/* |
|
764 |
* public CadMapControl getCadMapControl() { return cadMapControl; } |
|
765 |
*/ |
|
766 |
/** |
|
767 |
* DOCUMENT ME! |
|
768 |
* |
|
769 |
* @param cadMapControl |
|
770 |
* DOCUMENT ME! |
|
771 |
*/ |
|
772 |
/* |
|
773 |
* public void setCadMapControl(CadMapControl cadMapControl) { |
|
774 |
* this.cadMapControl = cadMapControl; } |
|
775 |
*/ |
|
776 |
|
|
777 |
/** |
|
778 | 703 |
* Elimina las geometr?as seleccionadas actualmente |
779 | 704 |
*/ |
780 | 705 |
private void delete() { |
706 |
ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited(); |
|
707 |
if (!(aux instanceof VectorialLayerEdited)) |
|
708 |
return; |
|
709 |
VectorialLayerEdited vle = (VectorialLayerEdited) aux; |
|
710 |
VectorialEditableAdapter vea = vle.getVEA(); |
|
711 |
FLyrVect lyrVect = (FLyrVect) vle.getLayer(); |
|
712 |
|
|
781 | 713 |
vea.startComplexRow(); |
782 |
FBitSet selection = getVectorialAdapter().getSelection();
|
|
714 |
FBitSet selection = vea.getSelection();
|
|
783 | 715 |
try { |
784 | 716 |
int[] indexesToDel = new int[selection.cardinality()]; |
785 | 717 |
int j = 0; |
... | ... | |
792 | 724 |
* VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
793 | 725 |
* .getEditionManager().getActiveLayerEdited(); ArrayList |
794 | 726 |
* selectedRow = vle.getSelectedRow(); |
795 |
* |
|
727 |
*
|
|
796 | 728 |
* int[] indexesToDel = new int[selectedRow.size()]; for (int i = 0; |
797 | 729 |
* i < selectedRow.size(); i++) { IRowEdited edRow = (IRowEdited) |
798 | 730 |
* selectedRow.get(i); indexesToDel[i] = edRow.getIndex(); } |
... | ... | |
816 | 748 |
} |
817 | 749 |
System.out.println("clear Selection"); |
818 | 750 |
selection.clear(); |
819 |
VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
|
820 |
.getEditionManager().getActiveLayerEdited(); |
|
821 | 751 |
vle.clearSelection(); |
822 | 752 |
/* |
823 | 753 |
* if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool |
... | ... | |
828 | 758 |
|
829 | 759 |
/** |
830 | 760 |
* DOCUMENT ME! |
831 |
* |
|
761 |
*
|
|
832 | 762 |
* @param b |
833 | 763 |
*/ |
834 | 764 |
public void setAdjustGrid(boolean b) { |
... | ... | |
837 | 767 |
|
838 | 768 |
/** |
839 | 769 |
* DOCUMENT ME! |
840 |
* |
|
770 |
*
|
|
841 | 771 |
* @param actionCommand |
842 | 772 |
*/ |
843 | 773 |
public void keyPressed(String actionCommand) { |
... | ... | |
872 | 802 |
return cadgrid; |
873 | 803 |
} |
874 | 804 |
|
875 |
/** |
|
876 |
* @return Returns the spatialCache. |
|
877 |
*/ |
|
878 |
public SpatialIndex getSpatialCache() { |
|
879 |
return spatialCache; |
|
880 |
} |
|
881 | 805 |
|
882 |
/** |
|
883 |
* Se usa para rellenar la cache de entidades con la que queremos trabajar |
|
884 |
* (para hacer snapping, por ejemplo. Lo normal ser? rellenarla cada vez que |
|
885 |
* cambie el extent, y bas?ndonos en el futuro EditionManager para saber de |
|
886 |
* cu?ntos temas hay que leer. Si el numero de entidades supera |
|
887 |
* MAX_ENTITIES_IN_SPATIAL_CACHE, lo pondremos a nulo. |
|
888 |
* |
|
889 |
* @throws DriverException |
|
890 |
*/ |
|
891 |
/* |
|
892 |
* public void createSpatialCache() throws DriverException { ViewPort vp = |
|
893 |
* getMapControl().getViewPort(); Rectangle2D extent = |
|
894 |
* vp.getAdjustedExtent(); // TODO: Por ahora cogemos el VectorialAdapter // |
|
895 |
* de aqu?, pero deber?amos tener un m?todo que // le pregunte al |
|
896 |
* EditionManager el tema sobre // el que estamos pintando. String strEPSG = |
|
897 |
* vp.getProjection().getAbrev().substring(5); IRowEdited[] feats = |
|
898 |
* getVectorialAdapter().getFeatures(extent, strEPSG); if (feats.length > |
|
899 |
* MAX_ENTITIES_IN_SPATIAL_CACHE) this.spatialCache = null; else |
|
900 |
* this.spatialCache = new Quadtree(); for (int i=0; i < feats.length; i++) { |
|
901 |
* IFeature feat = (IFeature)feats[i].getLinkedRow(); IGeometry geom = |
|
902 |
* feat.getGeometry(); // TODO: EL getBounds2D del IGeometry ralentiza |
|
903 |
* innecesariamente // Podr?amos hacer que GeneralPathX lo tenga guardado, // |
|
904 |
* y tenga un constructor en el que se lo fijes. // De esta forma, el |
|
905 |
* driver, a la vez que recupera // las geometr?as podr?a calcular el |
|
906 |
* boundingbox // y asignarlo. Luego habr?a que poner un m?todo // que |
|
907 |
* recalcule el bounding box bajo demanda. |
|
908 |
* |
|
909 |
* Envelope e = FConverter.convertRectangle2DtoEnvelope(geom.getBounds2D()); |
|
910 |
* spatialCache.insert(e, geom); } } |
|
911 |
*/ |
|
912 | 806 |
} |
Also available in: Unified diff