Revision 61 org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.polyline/src/main/java/org/gvsig/vectorediting/lib/prov/polyline/PolylineEditingProvider.java

View differences:

PolylineEditingProvider.java
26 26
import org.gvsig.fmap.geom.primitive.Surface;
27 27
import org.gvsig.fmap.geom.type.GeometryType;
28 28
import org.gvsig.tools.dynobject.DynObject;
29
import org.gvsig.tools.exception.BaseException;
30 29
import org.gvsig.tools.service.spi.ProviderServices;
31 30
import org.gvsig.vectorediting.lib.api.DrawingStatus;
32 31
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
33 32
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
33
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
34
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
35
import org.gvsig.vectorediting.lib.api.exceptions.InvalidOptionException;
36
import org.gvsig.vectorediting.lib.api.exceptions.InvalidValueException;
37
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
34 38
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
35 39
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
36 40
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
......
60 64
    this.featureStore = (FeatureStore) parameters
61 65
        .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
62 66
  }
63
  
67

  
64 68
  /**
65
   * 
66 69
   * @param angle1
67 70
   * @param angle2
68 71
   * @return
......
77 80
  }
78 81

  
79 82
  /**
80
   * 
81 83
   * @param lastPosition
82 84
   * @return
83
   * @throws BaseException
85
   * @throws VectorEditingException
84 86
   */
85 87
  private DrawingStatus calculatePolyline(Point lastPosition)
86
      throws BaseException {
88
      throws VectorEditingException {
87 89
    DrawingStatus geometries = new DefaultDrawingStatus();
88 90
    Double antm = null;
89 91
    Double antb = null;
90 92
    Double m = null;
91 93
    Double b = null;
94
    Point center = null;
95
    double radius = 0.0;
96
    double startAngle = 0.0;
97
    double endAngle = 0.0;
98
    double angleExt = 0.0;
92 99
    boolean right = false;
93 100
    boolean addGeom = false;
94 101

  
102
    Curve lineAntPointToPoint = null;
103

  
95 104
    if (!values.isEmpty()) {
96 105
      for (int i = 0; i < values.size(); i++) {
97 106

  
......
113 122
          return geometries;
114 123
        }
115 124
        if (polyLinePoint.isArcMode()) {
125
          try {
116 126

  
117
          Curve lineAntPointToPoint = editingProviderServices.createLine(
118
              point.getX(), point.getY(), nextPoint.getX(), nextPoint.getY(),
119
              featureStore);
127
            lineAntPointToPoint = editingProviderServices.createLine(
128
                point.getX(), point.getY(), nextPoint.getX(), nextPoint.getY(),
129
                featureStore);
120 130

  
121
          Double[] lineParams = getLineParams(point, nextPoint);
122
          m = lineParams[0];
123
          b = lineParams[1];
131
            Double[] lineParams = getLineParams(point, nextPoint);
132
            m = lineParams[0];
133
            b = lineParams[1];
124 134

  
125
          Point[] pointPerpendicular = getPerpendicular(antm, antb, point,
126
              featureStore);
127
          Line linePointPerpendicular = geomManager
128
              .createLine(editingProviderServices.getSubType(featureStore));
129
          linePointPerpendicular.setPoints(pointPerpendicular[0],
130
              pointPerpendicular[1]);
135
            Point[] pointPerpendicular = getPerpendicular(antm, antb, point,
136
                featureStore);
137
            Line linePointPerpendicular = geomManager
138
                .createLine(editingProviderServices.getSubType(featureStore));
139
            linePointPerpendicular.setPoints(pointPerpendicular[0],
140
                pointPerpendicular[1]);
131 141

  
132
          Point[] bisector = getPerpendicular(m, b,
133
              getMidPoint(point, nextPoint, featureStore), featureStore);
134
          Line lineBisector = geomManager.createLine(editingProviderServices
135
              .getSubType(featureStore));
136
          lineBisector.setPoints(bisector[0], bisector[1]);
142
            Point[] bisector = getPerpendicular(m, b,
143
                getMidPoint(point, nextPoint, featureStore), featureStore);
144
            Line lineBisector = geomManager.createLine(editingProviderServices
145
                .getSubType(featureStore));
146
            lineBisector.setPoints(bisector[0], bisector[1]);
137 147

  
138
          Point center = getIntersection(bisector, pointPerpendicular,
139
              featureStore);
148
            center = getIntersection(bisector, pointPerpendicular, featureStore);
140 149

  
141
          double startAngle = getAngle(center, point);
142
          double endAngle = getAngle(center, nextPoint);
150
            startAngle = getAngle(center, point);
151
            endAngle = getAngle(center, nextPoint);
143 152

  
144
          double radius = center.distance(point);
153
            radius = center.distance(point);
154
          }
155
          catch (Exception e) {
156
            throw new DrawServiceException(e);
157
          }
145 158

  
146
          double angleExt = 0.0;
147

  
148 159
          if (right) {
149 160
            if (nextPoint.getX() >= point.getX()) {
150 161
              if (m >= antm) {
......
190 201
            }
191 202
          }
192 203

  
193
          Arc arco = editingProviderServices.createArc(center, radius,
194
              startAngle, angleExt, featureStore);
204
          Arc arco = null;
205
          try {
206
            arco = editingProviderServices.createArc(center, radius,
207
                startAngle, angleExt, featureStore);
208
          }
209
          catch (Exception e) {
210
            throw new DrawServiceException(e);
211
          }
195 212

  
196 213
          antm = -(nextPoint.getX() - center.getX())
197 214
              / (nextPoint.getY() - center.getY());
......
220 237

  
221 238
        }
222 239
        else {
223
          Curve geometry = editingProviderServices.createLine(point.getX(),
224
              point.getY(), nextPoint.getX(), nextPoint.getY(), featureStore);
225
          geometries.addGeometry(geometry);
226

  
240
          try {
241
            Curve geometry = editingProviderServices.createLine(point.getX(),
242
                point.getY(), nextPoint.getX(), nextPoint.getY(), featureStore);
243
            geometries.addGeometry(geometry);
244
          }
245
          catch (Exception e) {
246
            throw new DrawServiceException(e);
247
          }
227 248
          Double[] antLineParams = getLineParams(point, nextPoint);
228 249
          antm = antLineParams[0];
229 250
          antb = antLineParams[1];
......
232 253
      }
233 254
      return geometries;
234 255
    }
235
    else {
236
      // TODO throw exception to be catch by editingBehavior to and error at
237
      // console.
238
    }
239 256
    return null;
240 257
  }
241
  
258

  
242 259
  /**
243
   * 
244 260
   * @param surface
245 261
   * @return
246 262
   */
......
252 268
    }
253 269
    return surface;
254 270
  }
255
  
256
  public DrawingStatus draw(Point mousePosition) throws BaseException {
257
    return calculatePolyline(mousePosition);
271

  
272
  public DrawingStatus draw(Point mousePosition) throws DrawServiceException {
273
    try {
274
      return calculatePolyline(mousePosition);
275
    }
276
    catch (Exception e) {
277
      throw new DrawServiceException(e);
278
    }
258 279
  }
259
  
260
  public void finish() throws BaseException, DataException {
261
    GeometryType storeGeomType = editingProviderServices
262
        .getGeomType(featureStore);
263
    DrawingStatus finalGeometries = calculatePolyline(null);
264
    if (storeGeomType.isTypeOf(SURFACE)) {
265
      Surface surface = geomManager.createPolygon(storeGeomType.getSubType());
266
      for (Geometry geometry : finalGeometries.getGeometries()) {
267
        surface.addVertex((Point) geometry);
268
      }
269
      surface = closeSurfaceIfNecessary(surface);
270
      editingProviderServices.insertGeometryIntoFeatureStore(surface,
271
          featureStore);
272
    }
273
    else if (storeGeomType.isTypeOf(CURVE)) {
274
      for (Geometry geometry : finalGeometries.getGeometries()) {
275
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
280

  
281
  public void finish() throws FinishServiceException {
282
    try {
283
      GeometryType storeGeomType = editingProviderServices
284
          .getGeomType(featureStore);
285
      DrawingStatus finalGeometries = calculatePolyline(null);
286
      if (storeGeomType.isTypeOf(SURFACE)) {
287
        Surface surface = geomManager.createPolygon(storeGeomType.getSubType());
288
        for (Geometry geometry : finalGeometries.getGeometries()) {
289
          surface.addVertex((Point) geometry);
290
        }
291
        surface = closeSurfaceIfNecessary(surface);
292
        editingProviderServices.insertGeometryIntoFeatureStore(surface,
276 293
            featureStore);
277 294
      }
278
    }
279
    else if (storeGeomType.isTypeOf(MULTISURFACE)) {
280
      MultiSurface multiSurface;
281
      multiSurface = geomManager.createMultiSurface(storeGeomType.getSubType());
282
      Surface surface = geomManager.createPolygon(storeGeomType.getSubType());
283
      for (Geometry geometry : finalGeometries.getGeometries()) {
284
        if(geometry instanceof Curve){
285
          Curve curve = (Curve) geometry;
286
          for(int i=0; i < curve.getNumVertices(); i++){
287
            surface.addVertex((Point) curve.getVertex(i));
295
      else if (storeGeomType.isTypeOf(CURVE)) {
296
        for (Geometry geometry : finalGeometries.getGeometries()) {
297
          editingProviderServices.insertGeometryIntoFeatureStore(geometry,
298
              featureStore);
299
        }
300
      }
301
      else if (storeGeomType.isTypeOf(MULTISURFACE)) {
302
        MultiSurface multiSurface;
303
        multiSurface = geomManager.createMultiSurface(storeGeomType
304
            .getSubType());
305
        Surface surface = geomManager.createPolygon(storeGeomType.getSubType());
306
        for (Geometry geometry : finalGeometries.getGeometries()) {
307
          if (geometry instanceof Curve) {
308
            Curve curve = (Curve) geometry;
309
            for (int i = 0; i < curve.getNumVertices(); i++) {
310
              surface.addVertex((Point) curve.getVertex(i));
311
            }
288 312
          }
289 313
        }
314
        surface = closeSurfaceIfNecessary(surface);
315
        multiSurface.addSurface(surface);
316
        editingProviderServices.insertGeometryIntoFeatureStore(multiSurface,
317
            featureStore);
290 318
      }
291
      surface = closeSurfaceIfNecessary(surface);
292
      multiSurface.addSurface(surface);
293
      editingProviderServices.insertGeometryIntoFeatureStore(multiSurface,
294
          featureStore);
295
    }
296
    else if (storeGeomType.isTypeOf(MULTICURVE)) {
297
      MultiCurve multiCurve;
298
      multiCurve = geomManager.createMultiCurve(storeGeomType.getSubType());
299
      for (Geometry geometry : finalGeometries.getGeometries()) {
300
        multiCurve.addCurve((Curve) geometry);
319
      else if (storeGeomType.isTypeOf(MULTICURVE)) {
320
        MultiCurve multiCurve;
321
        multiCurve = geomManager.createMultiCurve(storeGeomType.getSubType());
322
        for (Geometry geometry : finalGeometries.getGeometries()) {
323
          multiCurve.addCurve((Curve) geometry);
324
        }
325
        editingProviderServices.insertGeometryIntoFeatureStore(multiCurve,
326
            featureStore);
301 327
      }
302
      editingProviderServices.insertGeometryIntoFeatureStore(multiCurve,
303
          featureStore);
304 328
    }
329
    catch (Exception e) {
330
      throw new FinishServiceException(e);
331
    }
305 332
  }
306
  
333

  
307 334
  /**
308
   * 
309 335
   * @param start
310 336
   * @param end
311 337
   * @return
......
328 354
  }
329 355

  
330 356
  /**
331
   * 
332 357
   * @param bisector
333 358
   * @param perpendicular
334 359
   * @param featureStore
......
389 414

  
390 415
    return null;
391 416
  }
392
  
417

  
393 418
  /**
394
   * 
395 419
   * @param point
396 420
   * @param nextPoint
397 421
   * @return
......
421 445
    }
422 446
    return lineParams;
423 447
  }
424
  
448

  
425 449
  /**
426
   * 
427 450
   * @param p1
428 451
   * @param p2
429 452
   * @param featureStore
......
437 460
    double y = (p1.getY() + p2.getY()) / 2;
438 461
    return editingProviderServices.createPoint(x, y, featureStore);
439 462
  }
440
  
463

  
441 464
  public String getName() {
442 465
    return PolylineEditingProviderFactory.PROVIDER_NAME;
443 466
  }
444 467

  
445 468
  /**
446
   * 
447 469
   * @param i
448 470
   * @return
449 471
   */
......
453 475
    }
454 476
    return null;
455 477
  }
456
  
478

  
457 479
  public List<EditingServiceParameter> getParameters() {
458 480
    List<EditingServiceParameter> list = new ArrayList<EditingServiceParameter>();
459 481
    list.add(points);
......
461 483
  }
462 484

  
463 485
  /**
464
   * 
465 486
   * @param m
466 487
   * @param b
467 488
   * @param perp
......
514 535
      return res;
515 536
    }
516 537
  }
517
  
538

  
518 539
  /**
519
   * 
520 540
   * @param surface
521 541
   * @return
522 542
   */
523 543
  private boolean isClose(Surface surface) {
524
    
525
    if(surface != null){
544

  
545
    if (surface != null) {
526 546
      Point firstPoint = surface.getVertex(0);
527
      Point lastPoint = surface.getVertex(surface.getNumVertices()-1);
528
      if(firstPoint.equals(lastPoint)){
547
      Point lastPoint = surface.getVertex(surface.getNumVertices() - 1);
548
      if (firstPoint.equals(lastPoint)) {
529 549
        return true;
530 550
      }
531 551
    }
......
533 553
  }
534 554

  
535 555
  public EditingServiceParameter next() {
536
    if( values.size() >= 2){
537
      if(arcMode){
538
        points.setDescription("Inserts [L] to change to line mode. Double-click to finish. Inserts new point.");
556
    if (values.size() >= 2) {
557
      if (arcMode) {
558
        points
559
            .setDescription("Inserts [L] to change to line mode. Double-click to finish. Inserts new point.");
539 560
      }
540
      else{
541
        points.setDescription("Inserts [A] to change to arc mode. Double-click to finish. Inserts new point.");
561
      else {
562
        points
563
            .setDescription("Inserts [A] to change to arc mode. Double-click to finish. Inserts new point.");
542 564
      }
543 565
    }
544 566
    return points;
......
552 574
    points.setDescription("Inserts a new point.");
553 575
    arcMode = false;
554 576
  }
555
  
577

  
556 578
  /**
557
   * 
558 579
   * @param param
559 580
   * @param value
560 581
   */
561 582
  private void validateAndInsertValue(EditingServiceParameter param,
562
                                      Object value) {
583
                                      Object value) throws InvalidValueException, InvalidOptionException {
563 584
    if (value instanceof String) {
564 585
      if (values.size() >= 2) {
565
        if (((String) value).equalsIgnoreCase("A")) {
566
          arcMode = true;
586
        if(((String) value).equalsIgnoreCase("A") || ((String) value).equalsIgnoreCase("L")){
587
          if (((String) value).equalsIgnoreCase("A")) {
588
            arcMode = true;
589
          }
590
          else if (((String) value).equalsIgnoreCase("L")) {
591
            arcMode = false;
592
          }
593
          if (values.size() > 0) {
594
            values.get(values.size() - 1).setArcMode(arcMode);
595
            return;
596
          }
597
        } else{
598
          throw new InvalidOptionException(null);
567 599
        }
568
        else if (((String) value).equalsIgnoreCase("L")) {
569
          arcMode = false;
570
        }
571
        if (values.size() > 0) {
572
          values.get(values.size() - 1).setArcMode(arcMode);
573
          return;
574
        }
600
        
575 601
      }
576 602
    }
577 603
    else if (param == points && value instanceof Point) {
......
579 605
    }
580 606
  }
581 607

  
582
  public void value(Object value) {
608
  public void value(Object value) throws InvalidValueException, InvalidOptionException{
583 609
    EditingServiceParameter param = next();
584 610
    validateAndInsertValue(param, value);
585 611
  }

Also available in: Unified diff