summaryrefslogtreecommitdiff
path: root/java/EAC/Operator.java
blob: 724c9583b2090b66b8afa4c559404893aff7cb9c (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
198
199
200
201
202
203
204
205
206
/**
 *  Title:        Operator
 *  Description:  An abstract binary operator class for the Event Analysis
 *                Configurator
 */
package EAC;
import java.awt.*;

public abstract class Operator extends Primitive {

  // Radius of the circle
  protected final int DIAMETER = 40;

  // inputs
  protected Connector left_input,
                      right_input;

  // outputs
  protected Connector output[] = new Connector[MAX_OUTPUTS];
  protected int output_count = 0;

  // helps with dragging the operator
  protected boolean selected;
  Point old_anchor;

  public Operator(EACPanel p) {
    super(p);
  } /* constructor */

  public void setTop(Point p) {
    anchor = new Point(p.x - (int) (DIAMETER / 2), p.y);
  } /* setTop */

  public Point getTop() {
    return new Point(anchor.x + (int) (DIAMETER / 2), anchor.y);
  } /* getTop */

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

    if (selected)
      selected = false;
    else if (inBounds())
      g.drawOval(anchor.x, anchor.y, DIAMETER, DIAMETER);
    else
      throw new BoundsException("ERROR: Attempted to place Operator partially out of bounds");
  } /* draw */

  public void specialDraw() {
    Graphics g = canvas.getGraphics();

    g.setXORMode(canvas.getBackground());
    g.drawOval(anchor.x,anchor.y,DIAMETER,DIAMETER);
  } /* specialDraw */

  public void specialUndraw() {
    Graphics g = canvas.getGraphics();

    g.setColor(canvas.getBackground());
    g.setXORMode(canvas.getForeground());
    g.drawOval(anchor.x,anchor.y,DIAMETER,DIAMETER);
  } /* specialUndraw */

  public void selectedDraw() {
    old_anchor = anchor;
    selected = true;
    super.selectedDraw();
  } /* selectedDraw */

  public void reconnect() {
    int i;

    // move output anchors
    for (i = 0; i < output_count; i++)
      output[i].setAnchor(new Point(output[i].getAnchor().x + anchor.x - old_anchor.x,
                                    output[i].getAnchor().y + anchor.y - old_anchor.y));

    // move input endpoints
    if (left_input != null)
      left_input.setEndpoint(new Point(left_input.upperRight().x + anchor.x - old_anchor.x,
                                       left_input.upperRight().y + anchor.y - old_anchor.y));
    if (right_input != null)
      right_input.setEndpoint(new Point(right_input.upperRight().x + anchor.x - old_anchor.x,
                                        right_input.upperRight().y + anchor.y - old_anchor.y));

  } /* reconnect */

  public boolean contains(Point p) {
    if ((p.x >= anchor.x) &&
        (p.x <= anchor.x + DIAMETER) &&
        (p.y >= anchor.y) &&
        (p.y <= anchor.y + DIAMETER))
      return true;
    else
      return false;
  } /* contains */

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

  public Point lowerRight() {
    return new Point(anchor.x + DIAMETER, anchor.y + DIAMETER);
  } /* lowerRight */

  public Point upperRight() {
    return new Point(anchor.x + DIAMETER, anchor.y);
  } /* upperRight */

  public Point lowerLeft() {
    return new Point(anchor.x, anchor.y + DIAMETER);
  } /* lowerLeft */

  public int addInput(Connector c) throws ConnectionException {
    if (left_input == null) {
      left_input = c;
      return 0;
    } else if (right_input == null) {
      right_input = c;
      return 1;
    } else
      throw new ConnectionException("ERROR: Operator already has two inputs");
  } /* addInput */

  public int addOutput(Connector c) throws ConnectionException {
    if (output_count < MAX_OUTPUTS)
      output[output_count++] = c;
    else
      throw new ConnectionException("ERROR: Maximum outputs established for this operator");

    return output_count;
  } /* addOutput */

  public Connector getInput(int i) throws ConnectionException {
    if ((i < 0) || (i > 1))
      throw new ConnectionException("ERROR: Bad input index for operator");
    else if (i == 0) {
      if (left_input == null)
        throw new ConnectionException("ERROR: No left input for operator");
      else
        return left_input;
    } else // i == 1
      if (right_input == null)
        throw new ConnectionException("ERROR: No right input for operator");

    return right_input;
  } /* getInput */

  public Connector getOutput(int i) throws ConnectionException {
    if ((i >= 0) && (i < output_count))
      return output[i];
    else
      throw new ConnectionException("ERROR: Bad output index for operator");
  } /* getOutput */

  public void removeInput(int i) throws ConnectionException {
    if ((i < 0) || (i > 1))
      throw new ConnectionException("ERROR: Bad input index for operator");
    else if (i == 0) {
      if (left_input == null)
        throw new ConnectionException("ERROR: No left input for operator");
      else
        left_input = null;
    } else if (i == 1) {
      if (right_input == null)
        throw new ConnectionException("ERROR: No right input for operator");
      else
        right_input = null;
    }
  } /* removeInput */

  public void removeOutput(int i) throws ConnectionException {
    if ((i < 0) || (i > output_count))
      throw new ConnectionException("ERROR: Bad output index for operator");
    else {
      output[i] = output[output_count-1];
      --output_count;
    }
  } /* removeOutput */

  public int getOutputCount() throws ConnectionException {
    return output_count;
  } /* getOutputCount */

  public int getInputCount() throws ConnectionException {
    if (left_input == null)
      return 0;
    else if (right_input == null)
      return 1;
    else
      return 2;
  } /* getInputCount */

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

  public boolean inBounds() {
    if (canvas.contains(anchor.x,anchor.y) &&
        canvas.contains(anchor.x + DIAMETER, anchor.y + DIAMETER))
      return true;
    else
      return false;
  } /* inBounds */

}