summaryrefslogtreecommitdiff
path: root/java/EAC/EACLabel.java
blob: e841c4e4c402415f114e88c1deb31932f4d52c0b (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
/**
 * Title:        EACLabel
 * Description:  Text labels for Event Analysis Configurator graphical
 *               primitives
 */
package EAC;
import java.awt.*;

public class EACLabel extends Primitive {

  // Font for labels
  protected Font label_font = new EACFont().getFont();

  // Font Metrics for sizing and bounds checking labels
  protected FontMetrics fm = canvas.getFontMetrics(label_font);

  // Label text
  protected char[] text;

  // associated Primitive
  protected Primitive labelee;

  public EACLabel(EACPanel p, int n) {
    super(p);
    text = new char[n];
  } /* constructor */

  public void setLabelee(Primitive p) {
    labelee = p;
  } /* setLabelee */

  public Primitive getLabelee() {
    return labelee;
  } /* getLabelee */

  public void setText(String s) {
    text = s.toCharArray();
  } /* setText */

  public String getText() {
    return java.lang.String.valueOf(text);
  } /* getText */

  public void setTop(Point p) {
    anchor = new Point(p.x - (3 * text.length),
                       p.y + 9);
  } /* setTop */

  public Point getTop() {
    return new Point(anchor.x + (3 * text.length),
                     anchor.y - 9);
  } /* getTop */

  public void draw() throws BoundsException {
    if (inBounds()) {
      Graphics g = canvas.getGraphics();
      g.setFont(label_font);
      g.drawChars(text,0,text.length,anchor.x,anchor.y);
    } else
      throw new BoundsException("Attempted to place Label partially out of bounds");
  } /* draw */

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

    g.setXORMode(canvas.getBackground());
    g.drawRect(upperLeft().x, upperLeft().y,
               upperRight().x - upperLeft().x, // WIDTH
               lowerRight().y - upperRight().y); // HEIGHT
  } /* specialDraw */

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

    g.setColor(canvas.getBackground());
    g.setXORMode(canvas.getForeground());
    g.drawRect(upperLeft().x, upperLeft().y,
               upperRight().x - upperLeft().x, // WIDTH
               lowerRight().y - upperRight().y); // HEIGHT
  } /* specialUndraw */

  public boolean contains(Point p) {
    return ((p.x >= upperLeft().x) &&
            (p.x <= upperRight().x) &&
            (p.y <= lowerLeft().y) &&
            (p.y >= upperLeft().y));
  } /* contains */

  public boolean inBounds() {
    // don't know why we need this, but fm.stringWidth seems to return a
    // value that's bigger than the actual width of the text
    //final int xTweak = 20;

    //if (canvas.contains(anchor.x + fm.stringWidth(text.toString()) - xTweak,
    //                    anchor.y - fm.getHeight()))
    if ((canvas.contains(upperLeft())) &&
        (canvas.contains(upperRight())) &&
        (canvas.contains(lowerLeft())) &&
        (canvas.contains(lowerRight())))
      return true;
    else
      return false;
  } /* inBounds */

  public Point upperLeft() {
    return new Point(anchor.x,anchor.y - 9);
  } /* upperLeft */

  public Point upperRight() {
    return new Point(anchor.x + (6 * text.length),anchor.y - 9);
  } /* upperRight */

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

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

  public void write(File f) throws java.io.IOException {
    int i;

    // if there's an associated primitive, that primitive
    // is responsible for writing out the label info too,
    // so only write out the info if there is no labelee
    if (labelee == null) {
      f.writeInt(f.LABEL);
      f.writeInt(text.length);
      f.writeInt(anchor.x);
      f.writeInt(anchor.y);
      for (i = 0; i < text.length; i++)
        f.writeChar(text[i]);
    }
  } /* write */

  public void read(File f) throws java.io.IOException {
    int i;

    anchor = new Point();
    anchor.x = f.readInt();
    anchor.y = f.readInt();

    for (i = 0; i < text.length; i++)
      text[i] = f.readChar();
  } /* read */

  public int addInput(Connector c) throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to add input to label");
  } /* addInput */

  public int addOutput(Connector c) throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to add output to label");
  } /* addOutput */

  public Connector getInput(int i) throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to retrieve input from label");
  } /* getInput */

  public Connector getOutput(int i) throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to retrieve output from label");
  } /* getOutput */

  public int getInputCount() throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to retrieve input count from label");
  } /* getInputCount */

  public int getOutputCount() throws ConnectionException {
    throw new ConnectionException("ERROR: Attempted to retrieve output count from label");
  } /* getOutputCount */

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

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

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

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