summaryrefslogtreecommitdiff
path: root/java/EAC/Connector.java
blob: 27c02e820bc4fcb22352c9ba467fe507da2aedf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * Title:        Connector
 * Description:  Connects two primitives in the Event Analysis Configurator
 */
package EAC;
import java.awt.*;

public class Connector extends Primitive {

  // for drawing
  protected RubberbandLine rbLine;

  // Primitives connected by connector
  Primitive startP,
            endP;

  // the second point of the connector
  // (the anchor point is inherited from Primitive)
  protected Point endpoint;

  public Connector(EACPanel p) {
    super(p);
    rbLine = new RubberbandLine(p);
  }

  // inappropriate inherited methods: should not be used
  public void setTop(Point p) {}
  public Point getTop() { return null; }

  public void setEndpoint(Point p) {
    endpoint = p;
  } /* setEndpoint */

  public Point getEndpoint() {
    return endpoint;
  } /* getEndpoint */

  // Anchor the rubberband line AND the connector itself
  public void anchor(Point p) {
    rbLine.anchor(p);
    anchor = p;
  } /* anchor */

  // Stretch the rubberband line
  public void stretch(Point p) {
    rbLine.stretch(p);
  } /* stretch */

  // End the rubberband line AND the connector itself
  public void end(Point p) {
    rbLine.end(p);
    endpoint = p;
  } /* end */

  public void draw() throws BoundsException {
    if (inBounds()) {
      Graphics g = canvas.getGraphics();

      g.drawLine(anchor.x,anchor.y,endpoint.x,endpoint.y);
      drawArrowhead(g);
    } else
      throw new BoundsException("ERROR: Attempted to place Connector endpoint out of bounds");
  } /* draw */

  private void drawArrowhead(Graphics g) {
    final int arrowHeadWidth = 10;
    Point P1 = anchor;
    Point P2 = endpoint;

    // Draw Arrowhead (using java.lang.Math and floating point)
    // source provided by Erik in newsgroup posting
    double dx = P2.x - P1.x;
    double dy = P2.y - P1.y;
    final double ra = java.lang.Math.sqrt(dx*dx + dy*dy);
    final double ri = (double) arrowHeadWidth;
    dx /= ra;
    dy /= ra;
    Point p2 = new Point((int)Math.round(P2.x - dx*ri),
    (int)Math.round(P2.y - dy*ri));
    int[] x = new int[3];
    int[] y = new int[3];
    double r = 0.4 * ri;
    x[0] = (int)Math.round(p2.x + dy * r);
    y[0] = (int)Math.round(p2.y - dx * r);
    x[1] = P2.x;
    y[1] = P2.y;
    x[2] = (int)Math.round(p2.x - dy * r);
    y[2] = (int)Math.round(p2.y + dx * r);
    g.fillPolygon(x, y, x.length);
  } /* drawArrowhead */

  public boolean contains(Point p) {
    return false;
  } /* contains */

  public Point upperLeft() {
    return anchor;
  } /* upperLeft */

  public Point lowerRight() {
    return endpoint;
  } /* lowerRight */

  public Point upperRight() {
    return endpoint;
  } /* upperRight */

  public Point lowerLeft() {
    return anchor;
  } /* lowerLeft */

  public boolean inBounds() {
    if (canvas.contains(endpoint))
      return true;
    else
      return false;
  } /* inBounds */

  public void write(File f) throws java.io.IOException {
    f.writeInt(f.CONNECTOR);
    f.writeInt(anchor.x);
    f.writeInt(anchor.y);
    f.writeInt(endpoint.x);
    f.writeInt(endpoint.y);
  } /* write */

  public void read(File f) throws java.io.IOException {
    anchor = new Point();
    endpoint = new Point();

    anchor.x = f.readInt();
    anchor.y = f.readInt();
    endpoint.x = f.readInt();
    endpoint.y = f.readInt();
  } /* read */

  public int addInput(Connector c) throws ConnectionException {
    throw new ConnectionException("ERROR: Connectors cannot connect each other");
  } /* addInput */

  public int addOutput(Connector c) throws ConnectionException {
    throw new ConnectionException("ERROR: Connectors cannot connect each other");
  } /* addOutput */

  public void setInput(Primitive p) {
    startP = p;
  } /* setInput */

  public void setOutput(Primitive p) {
    endP = p;
  } /* setOutput */

  public Primitive getInput() {
    return startP;
  } /* getInput */

  public Primitive getOutput() {
    return endP;
  } /* getOutput */

  public Connector getInput(int i) throws ConnectionException {
    throw new ConnectionException("Internal error: wrong getInput() called on Connector");
  } /* getInput */

  public Connector getOutput(int i) throws ConnectionException {
    throw new ConnectionException("Internal error: wrong getOutput() called on Connector");
  } /* getOutput */

  public void removeInput(int i) throws ConnectionException {
    //throw new ConnectionException("ERROR: Attempted to remove input from connector");
    startP = null;
  } /* removeInput */

  public void removeOutput(int i) throws ConnectionException {
    //throw new ConnectionException("ERROR: Attempted to remove output from connector");
    endP = null;
  } /* removeOutput */

  public int getInputCount() throws ConnectionException {
    //throw new ConnectionException("ERROR: Attempted to retrieve input count from connector");
    return (startP == null) ? 0 : 1;
  } /* getInputCount */

  public int getOutputCount() throws ConnectionException {
    //throw new ConnectionException("ERROR: Attempted to retrieve output count from connector");
    return (endP == null) ? 0 : 1;
  } /* getOutputCount */

  public void event(Source s) throws ConnectionException {
    //throw new ConnectionException("ERROR: Attempted to push event to connector");
    endP.event(s);
  } /* event */

  public void wakeup(long t) throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to wake up connector");
  } /* wakeup */
}