Added destroy for GUIComponent and GUIWire
[Mograsim.git] / LogicUI / src / era / mi / gui / model / wires / GUIWire.java
1 package era.mi.gui.model.wires;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.graphics.Color;
5
6 import era.mi.gui.model.ViewModel;
7 import era.mi.logic.types.Bit;
8 import era.mi.logic.wires.Wire;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11
12 public class GUIWire
13 {
14         private final ViewModel model;
15         private Pin pin1;
16         private Pin pin2;
17         private double[] path;
18
19         private Wire wire;
20
21         public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path)
22         {
23                 this.model = model;
24                 this.path = new double[path.length * 2 + 4];
25                 for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)
26                 {
27                         this.path[dstI + 0] = path[srcI].x;
28                         this.path[dstI + 1] = path[srcI].y;
29                 }
30                 // TODO support moving pins
31                 Point pos;
32                 pos = pin1.getPos();
33                 this.path[0] = pos.x;
34                 this.path[1] = pos.y;
35                 pos = pin2.getPos();
36                 this.path[this.path.length - 2] = pos.x;
37                 this.path[this.path.length - 1] = pos.y;
38
39                 model.wireCreated(this);
40         }
41
42         public void destroy()
43         {
44                 model.wireDestroyed(this);
45         }
46
47         public void render(GeneralGC gc)
48         {
49                 Color oldFG = gc.getForeground();
50                 gc.setForeground(gc.getDevice().getSystemColor(getSWTColorConstantForWire(wire)));
51                 gc.drawPolyline(path);
52                 gc.setForeground(oldFG);
53         }
54
55         public void setLogicModelWire(Wire wire)
56         {
57                 this.wire = wire;
58         }
59
60         public static int getSWTColorConstantForWire(Wire wire)
61         {
62                 if (wire != null && wire.length == 1)
63                         return getSWTColorConstantForBit(wire.getValue());
64                 else
65                         return SWT.COLOR_BLACK;
66         }
67
68         public static int getSWTColorConstantForBit(Bit bit)
69         {
70                 switch (bit)
71                 {
72                 case ONE:
73                         return SWT.COLOR_GREEN;
74                 case ZERO:
75                         return SWT.COLOR_BLUE;
76                 case Z:
77                         return SWT.COLOR_BLACK;
78                 case U:
79                 case X:
80                         return SWT.COLOR_RED;
81                 default:
82                         throw new IllegalArgumentException("Unknown enum constant: " + bit);
83                 }
84         }
85 }