svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / AnnotationEditableAdapter.java @ 4523
History | View | Annotate | Download (10 KB)
1 |
package com.iver.cit.gvsig.fmap.edition; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.awt.geom.Rectangle2D; |
5 |
import java.io.IOException; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
|
9 |
import com.hardcode.gdbms.engine.values.NumericValue; |
10 |
import com.hardcode.gdbms.engine.values.Value; |
11 |
import com.iver.cit.gvsig.fmap.DriverException; |
12 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
13 |
import com.iver.cit.gvsig.fmap.core.FGeometry; |
14 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
15 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
16 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
17 |
import com.iver.cit.gvsig.fmap.core.IRow; |
18 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
19 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
20 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
21 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
22 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
23 |
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation; |
24 |
import com.iver.cit.gvsig.fmap.layers.MappingAnnotation; |
25 |
import com.vividsolutions.jts.geom.Envelope; |
26 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
27 |
|
28 |
public class AnnotationEditableAdapter extends VectorialEditableAdapter{ |
29 |
private ArrayList labels; |
30 |
private MappingAnnotation mapping;
|
31 |
|
32 |
public int doAddRow(IRow feat) throws DriverIOException, IOException { |
33 |
return super.doAddRow(feat); |
34 |
} |
35 |
|
36 |
public int doModifyRow(int calculatedIndex, IRow feat) throws IOException, DriverIOException { |
37 |
FLabel label=(FLabel)labels.get(calculatedIndex); |
38 |
Value value=feat.getAttribute(mapping.getColumnText()); |
39 |
Rectangle2D rLabelAnt=(Rectangle2D)label.getBoundBox().clone(); |
40 |
Rectangle2D rLabel=(Rectangle2D)label.getBoundBox(); |
41 |
label.setString(value.toString()); |
42 |
|
43 |
if (mapping.getColumnRotate()==mapping.getColumnText()){
|
44 |
label.setRotation(((NumericValue)value).doubleValue()); |
45 |
} //Aqu? hay que comprobar, si se quiere, el resto de columnas, si son iguales a la de Texto.
|
46 |
|
47 |
|
48 |
int posAnteriorInExpansionFile = -1; |
49 |
Integer integer = new Integer(calculatedIndex); |
50 |
|
51 |
IFeature featAnt = null;
|
52 |
System.err.println("Modifica una Row en la posici?n: " |
53 |
+ calculatedIndex); |
54 |
// Si la geometr?a no ha sido modificada
|
55 |
if (!relations.containsKey(integer)) {
|
56 |
int newPosition = expansionFile.addRow(feat);
|
57 |
relations.put(integer, new Integer(newPosition)); |
58 |
|
59 |
// Se actualiza el ?ndice espacial
|
60 |
try {
|
61 |
featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex)); |
62 |
} catch (DriverException e) {
|
63 |
throw new DriverIOException(e); |
64 |
} |
65 |
|
66 |
Rectangle2D rAntAux = featAnt.getGeometry().getBounds2D();
|
67 |
Rectangle2D rAnt=label.getBoundBox();
|
68 |
rAnt.setFrame(rAntAux.getX(),rAntAux.getY(),rAnt.getWidth(),rAnt.getHeight()); |
69 |
Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
|
70 |
double difX=r.getX()-rAnt.getX();
|
71 |
double difY=r.getY()-rAnt.getY();
|
72 |
Point2D p=new Point2D.Double(rLabel.getX()+difX,rLabel.getY()+difY); |
73 |
label.setBoundBox(new Rectangle2D.Double(p.getX(),p.getY(),rLabel.getWidth(),rLabel.getHeight())); |
74 |
label.setOrig(p); |
75 |
index.remove(new Envelope(rLabelAnt.getX(), rLabelAnt.getX()
|
76 |
+ rLabelAnt.getWidth(), rLabelAnt.getY(), rLabelAnt.getY() |
77 |
+ rLabelAnt.getHeight()), new Integer(calculatedIndex)); |
78 |
index.insert(new Envelope(rLabel.getX(), rLabel.getX() + rLabel.getWidth(), rLabel
|
79 |
.getY(), rLabel.getY() + rLabel.getHeight()), new Integer( |
80 |
calculatedIndex)); |
81 |
} else {
|
82 |
// Obtenemos el ?ndice en el fichero de expansi?n
|
83 |
int num = ((Integer) relations.get(integer)).intValue(); |
84 |
posAnteriorInExpansionFile = num; |
85 |
|
86 |
// Obtenemos la geometr?a para actualiza el ?ndice
|
87 |
// espacialposteriormente
|
88 |
featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow(); |
89 |
|
90 |
/*
|
91 |
* Se modifica la geometr?a y nos guardamos el ?ndice dentro del
|
92 |
* fichero de expansi?n en el que se encuentra la geometr?a
|
93 |
* modificada
|
94 |
*/
|
95 |
num = expansionFile.modifyRow(num, feat); |
96 |
|
97 |
/*
|
98 |
* Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
|
99 |
* fichero de expansi?n.
|
100 |
*/
|
101 |
relations.put(integer, new Integer(num)); |
102 |
|
103 |
// Se modifica el ?ndice espacial
|
104 |
Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
|
105 |
Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
|
106 |
|
107 |
double difX=r.getX()-rAnt.getX();
|
108 |
double difY=r.getY()-rAnt.getY();
|
109 |
Point2D p=new Point2D.Double(rLabel.getX()+difX,rLabel.getY()+difY); |
110 |
|
111 |
label.setBoundBox(new Rectangle2D.Double(p.getX(),p.getY(),rLabel.getWidth(),rLabel.getHeight())); |
112 |
label.setOrig(p); |
113 |
index.remove(new Envelope(rLabelAnt.getX(), rLabelAnt.getX()
|
114 |
+ rLabelAnt.getWidth(), rLabelAnt.getY(), rLabelAnt.getY() |
115 |
+ rLabelAnt.getHeight()), new Integer(calculatedIndex)); |
116 |
index.insert(new Envelope(rLabel.getX(), rLabel.getX() + rLabel.getWidth(), rLabel
|
117 |
.getY(), rLabel.getY() + rLabel.getHeight()), new Integer( |
118 |
calculatedIndex)); |
119 |
} |
120 |
return posAnteriorInExpansionFile;
|
121 |
} |
122 |
|
123 |
public IRow doRemoveRow(int index) throws DriverIOException, IOException { |
124 |
return super.doRemoveRow(index); |
125 |
} |
126 |
|
127 |
public void undoAddRow(int calculatedIndex) throws DriverIOException, IOException { |
128 |
super.undoAddRow(calculatedIndex);
|
129 |
} |
130 |
|
131 |
public void undoModifyRow(int calculatedIndex, int previousExpansionFileIndex) throws IOException, DriverIOException { |
132 |
super.undoModifyRow(calculatedIndex, previousExpansionFileIndex);
|
133 |
Value value=this.getRow(calculatedIndex).getAttribute(mapping.getColumnText());
|
134 |
((FLabel)labels.get(calculatedIndex)).setString(value.toString()); |
135 |
if (mapping.getColumnRotate()==mapping.getColumnText()){
|
136 |
((FLabel)labels.get(calculatedIndex)).setRotation(((NumericValue)value).doubleValue()); |
137 |
} |
138 |
} |
139 |
|
140 |
public void undoRemoveRow(int index) throws IOException, DriverIOException { |
141 |
super.undoRemoveRow(index);
|
142 |
} |
143 |
|
144 |
public AnnotationEditableAdapter(ArrayList labels,MappingAnnotation mapping) { |
145 |
super();
|
146 |
this.labels=labels;
|
147 |
this.mapping=mapping;
|
148 |
} |
149 |
public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws DriverException { |
150 |
// En esta clase suponemos random access.
|
151 |
// Luego tendremos otra clase que sea VectorialEditableDBAdapter
|
152 |
// que reescribir? este m?todo.
|
153 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(r); |
154 |
List l = index.query(e);
|
155 |
IRowEdited[] feats = new IRowEdited[l.size()]; |
156 |
try {
|
157 |
for (int index = 0; index < l.size(); index++) { |
158 |
Integer i = (Integer) l.get(index); |
159 |
int inverse = getInversedIndex(i.intValue());
|
160 |
feats[index] = (IRowEdited) getRow(inverse); |
161 |
} |
162 |
} catch (DriverIOException e1) {
|
163 |
throw new DriverException(e1); |
164 |
} catch (IOException e1) { |
165 |
throw new DriverException(e1); |
166 |
} |
167 |
|
168 |
return feats;
|
169 |
} |
170 |
/* (non-Javadoc)
|
171 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
|
172 |
*/
|
173 |
public IRowEdited getRow(int index) throws DriverIOException, IOException { |
174 |
int calculatedIndex=getCalculatedIndex(index);
|
175 |
Integer integer = new Integer(calculatedIndex); |
176 |
//Si no est? en el fichero de expansi?n
|
177 |
DefaultRowEdited edRow=null;
|
178 |
if (!relations.containsKey(integer)) {
|
179 |
try {
|
180 |
IFeature f=ova.getFeature(calculatedIndex); |
181 |
//IGeometry g=f.getGeometry().cloneGeometry();
|
182 |
IGeometry geom=getGeometry(((FLabel)labels.get(index)).getBoundBox()); |
183 |
f=new DefaultFeature(geom,f.getAttributes());
|
184 |
edRow = new DefaultRowEdited(f,
|
185 |
DefaultRowEdited.STATUS_ORIGINAL, index); |
186 |
System.out.println("Piden la feature con ID= " + index); |
187 |
// Exception e = new Exception();
|
188 |
// e.printStackTrace();
|
189 |
} catch (DriverException e) {
|
190 |
// TODO Auto-generated catch block
|
191 |
e.printStackTrace(); |
192 |
} |
193 |
|
194 |
return edRow;
|
195 |
} else {
|
196 |
int num = ((Integer) relations.get(integer)).intValue(); |
197 |
IRowEdited aux = expansionFile.getRow(num); |
198 |
IFeature f=(IFeature)aux.getLinkedRow().cloneRow(); |
199 |
IGeometry geom=getGeometry(((FLabel)labels.get(index)).getBoundBox()); |
200 |
f=new DefaultFeature(geom,f.getAttributes());
|
201 |
edRow = new DefaultRowEdited(f, aux.getStatus(), index);
|
202 |
return edRow;
|
203 |
} |
204 |
} |
205 |
private IGeometry getGeometry(Rectangle2D r){ |
206 |
GeneralPathX resul = new GeneralPathX();
|
207 |
Point2D[] vs=new Point2D[5]; |
208 |
vs[0]=new Point2D.Double(r.getX(),r.getY()); |
209 |
vs[1]=new Point2D.Double(r.getMaxX(),r.getY()); |
210 |
vs[2]=new Point2D.Double(r.getMaxX(),r.getMaxY()); |
211 |
vs[3]=new Point2D.Double(r.getX(),r.getMaxY()); |
212 |
vs[4]=new Point2D.Double(r.getX(),r.getY()); |
213 |
for (int i = 0; i < vs.length; i++) { |
214 |
if (i == 0) { |
215 |
resul.moveTo(vs[i].getX(),vs[i].getY()); |
216 |
} else {
|
217 |
resul.lineTo(vs[i].getX(),vs[i].getY()); |
218 |
} |
219 |
} |
220 |
return ShapeFactory.createPolygon2D(resul);
|
221 |
} |
222 |
public void startEdition() throws EditionException { |
223 |
isEditing = true;
|
224 |
try {
|
225 |
expansionFile.open(); |
226 |
index = new Quadtree();
|
227 |
|
228 |
for (int i = 0; i < labels.size(); i++) { |
229 |
Rectangle2D r=((FLabel)labels.get(i)).getBoundBox();
|
230 |
Envelope e = new Envelope(r.getX(),
|
231 |
r.getX() + r.getWidth(), r.getY(), r.getY() |
232 |
+ r.getHeight()); |
233 |
index.insert(e, new Integer(i)); |
234 |
if (fullExtent == null) { |
235 |
fullExtent = r; |
236 |
} else {
|
237 |
fullExtent = fullExtent.createUnion(r); |
238 |
} |
239 |
} |
240 |
} catch (IOException e) { |
241 |
throw new EditionException(e); |
242 |
} |
243 |
|
244 |
System.err.println("Se han metido en el ?ndice " |
245 |
+ index.queryAll().size() + " labels");
|
246 |
} |
247 |
|
248 |
public Rectangle2D getShapeBounds(int index) throws IOException { |
249 |
//Solo se utiliza cuando el driver es BoundedShapes
|
250 |
// Si no est? en el fichero de expansi?n
|
251 |
Integer integer = new Integer((int) index); |
252 |
if (!relations.containsKey(integer)) {
|
253 |
if (ova.getDriver() instanceof BoundedShapes){ |
254 |
BoundedShapes bs = (BoundedShapes) ova.getDriver(); |
255 |
return bs.getShapeBounds(index);
|
256 |
}else{
|
257 |
return ova.getDriver().getShape(index).getBounds2D();
|
258 |
} |
259 |
|
260 |
} else {
|
261 |
int num = ((Integer) relations.get(integer)).intValue(); |
262 |
DefaultRowEdited feat; |
263 |
feat = (DefaultRowEdited) expansionFile.getRow(num); |
264 |
if (feat.getStatus() == IRowEdited.STATUS_DELETED)
|
265 |
return null; |
266 |
IGeometry geom = ((IFeature)feat.getLinkedRow()).getGeometry(); |
267 |
return geom.getBounds2D();//getGeometry(); |
268 |
} |
269 |
|
270 |
} |
271 |
} |