Line.java
01 /*
02  *
03  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
04  *
05  */
06 package demo.sharededitor.models;
07 
08 import java.awt.geom.Ellipse2D;
09 import java.awt.geom.Line2D;
10 import java.awt.Shape;
11 
12 final class Line extends BaseObject {
13    private Line2D.Double shape;
14 
15    protected Shape getShape() {
16       return shape;
17    }
18 
19    private transient Shape[] anchors = null;
20 
21    private Shape[] updateAnchors() {
22       if (anchors == null) {
23          anchors = new Shape[] {
24                new Ellipse2D.Double(shape.x2 - 5, shape.y2 - 51010),
25                new Ellipse2D.Double(shape.x1 - 5, shape.y1 - 51010) };
26          return anchors;
27       }
28 
29       ((Ellipse2D.Doubleanchors[0]).x = shape.x2 - 5;
30       ((Ellipse2D.Doubleanchors[0]).y = shape.y2 - 5;
31       ((Ellipse2D.Doubleanchors[1]).x = shape.x1 - 5;
32       ((Ellipse2D.Doubleanchors[1]).y = shape.y1 - 5;
33       return anchors;
34    }
35 
36    protected Shape[] getAnchors() {
37       return updateAnchors();
38    }
39 
40    public boolean isAt(int x, int y) {
41       return (shape.ptSegDist(x, y<= 5|| super.isAt(x, y);
42    }
43 
44    public void move(int dx, int dy) {
45       synchronized (this) {
46          shape.x1 += dx;
47          shape.y1 += dy;
48          shape.x2 += dx;
49          shape.y2 += dy;
50          updateAnchors();
51       }
52       this.notifyListeners(this);
53    }
54 
55    public void resize(int x, int y) {
56       synchronized (this) {
57          switch (grabbedAnchor()) {
58          case 0:
59             shape.x2 = x;
60             shape.y2 = y;
61             break;
62          case 1:
63             shape.x1 = x;
64             shape.y1 = y;
65             break;
66          }
67          updateAnchors();
68       }
69       this.notifyListeners(this);
70    }
71 
72    public boolean isTransient() {
73       double dx = shape.x1 - shape.x2;
74       double dy = shape.y1 - shape.y2;
75       return Math.sqrt((dx * dx(dy * dy)) 4;
76    }
77 
78    public Line() {
79       shape = new Line2D.Double();
80       shape.x1 = 0;
81       shape.y1 = 0;
82       shape.x2 = 0;
83       shape.y2 = 0;
84    }
85 }