Transpiled version (55598L) is out of date.
1 | sclass JG22Network is Swingable {
|
2 | G22Utils g22utils; |
3 | new MainPanel mainPanel; |
4 | new G22Network network; |
5 | Map<G22NetworkElement, JG22NetworkElement> elementToComponent = syncMap(); |
6 | ReliableSingleThread rstUpdate = rstWithPreDelay(0.25, l0 updateImpl); |
7 | JCheckBox cbAutoCalculate; |
8 | |
9 | settable int maxMagneticDistance = 100; |
10 | settable double autoCalcInterval = 0.1; |
11 | |
12 | // example instance of the network so we can test it immediately |
13 | settable volatile G22NetworkInstance networkInstance; |
14 | |
15 | // margin on bottom/right |
16 | transient int margin = 50; |
17 | |
18 | // The MainPanel holds the network elements |
19 | class MainPanel extends JPanel {
|
20 | *() { super(null); } // no layout manager
|
21 | |
22 | // to allow overlapping components |
23 | public bool isOptimizedDrawingEnabled() { false; }
|
24 | |
25 | public Dimension getPreferredSize() {
|
26 | Rect r = combinedChildBounds(this); |
27 | if (r == null) r = rect(0, 0, 0, 0); |
28 | //_print("JG22Network.getPreferredSize = " + r);
|
29 | ret new Dimension(r.x2()+margin, r.y2()+margin); |
30 | } |
31 | |
32 | void revalidateMe() {
|
33 | //_print("JG22Network.revalidateMe");
|
34 | //awtLater(0.5, -> {
|
35 | revalidateIncludingFullCenterContainer(this); |
36 | repaint(); |
37 | //}); |
38 | } |
39 | |
40 | public void paintComponent(Graphics g) {
|
41 | cast g to Graphics2D; |
42 | |
43 | fillRect(g, 0, 0, getWidth(), getHeight(), Color.white); |
44 | for (cable : network.allCables()) {
|
45 | Rect r1 = cable.from.bounds(); |
46 | Rect r2 = cable.to.bounds(); |
47 | drawLine(g, center(r1), center(r2), Color.black); |
48 | } |
49 | } |
50 | } |
51 | |
52 | *(G22Utils *g22utils, G22Network *network) {
|
53 | network.onChangeAndNow(l0 networkChanged); |
54 | for (element : network.elements) |
55 | visualizeElement(element); |
56 | } |
57 | |
58 | void networkChanged { rstUpdate.trigger(); }
|
59 | |
60 | void updateImpl {
|
61 | if (networkInstance == null) |
62 | makeInstance(); |
63 | visualizePorts(); |
64 | } |
65 | |
66 | void visualizePorts { awt {
|
67 | Map<G22NetworkElement.Port, JG22NetworkPort> visualizations = new Map; |
68 | for (port : directChildrenOfType(mainPanel, JG22NetworkPort)) |
69 | visualizations.put(port.port, port); |
70 | |
71 | // Create new ones |
72 | for (e : network.elements) {
|
73 | var c = elementToComponent.get(e); |
74 | if (c == null) |
75 | print("No component for " + e);
|
76 | else |
77 | for (port : e.ports) {
|
78 | //print("Visualizing port " + port);
|
79 | var p = visualizations.get(port); |
80 | if (p != null) {
|
81 | p.update(); |
82 | visualizations.remove(port); |
83 | } else {
|
84 | p = new JG22NetworkPort(g22utils, c, port); |
85 | p.init(); |
86 | mainPanel.add(p); |
87 | } |
88 | } |
89 | } |
90 | |
91 | // Remove old visualizations |
92 | for (port : values(visualizations)) |
93 | removeFromParent(port); |
94 | |
95 | mainPanel.repaint(); |
96 | }} |
97 | |
98 | void makeInstance {
|
99 | var networkInstance = new G22NetworkInstance(network); |
100 | networkInstance.onDirtyStatesChanged(l0 dirtyStatesChanged); |
101 | networkInstance.init(g22utils); |
102 | networkInstance(networkInstance); |
103 | dirtyStatesChanged(); |
104 | print("Network instance made");
|
105 | } |
106 | |
107 | void dirtyStatesChanged {
|
108 | for (component : values(elementToComponent)) |
109 | component.updateDirtyFlag(); |
110 | } |
111 | |
112 | void visualizeElement(G22NetworkElement element) swing {
|
113 | var c = new JG22NetworkElement(g22utils, this, element); |
114 | elementToComponent.put(element, c); |
115 | var component = c.visualize(); |
116 | mainPanel.add(component); |
117 | mainPanel.revalidateMe(); |
118 | componentToFront(component); |
119 | } |
120 | |
121 | void newInstance {
|
122 | networkInstance = null; |
123 | rstUpdate.trigger(); |
124 | } |
125 | |
126 | void newElement {
|
127 | new G22NetworkElement element; |
128 | element.network(network); |
129 | network.elements.add(element); |
130 | network.change(); |
131 | visualizeElement(element); |
132 | } |
133 | |
134 | void deleteElement(JG22NetworkElement element) {
|
135 | network.elements.remove(element.element); |
136 | network.change(); |
137 | mainPanel.remove(element.visualize()); |
138 | mainPanel.repaint(); |
139 | } |
140 | |
141 | void doMagneticConnections {
|
142 | var newCables = network.doMagneticConnections(); |
143 | fOr (cable : newCables) |
144 | networkInstance.invalidateNode(cable.to().element()); |
145 | mainPanel.repaint(); |
146 | } |
147 | |
148 | void configureNodes {
|
149 | for (element : network.elements) |
150 | element.configureBlueprint(g22utils); |
151 | } |
152 | |
153 | void calculationStep {
|
154 | var node = networkInstance?.calculateOneNode(g22utils); |
155 | if (node == null) ret; |
156 | elementToComponent.get(node).visualizeNode(); |
157 | } |
158 | |
159 | cachedVisualize {
|
160 | cbAutoCalculate = jVarCheckBox("Auto-calculate", network.varAutoCalculate());
|
161 | awtCalcEvery(mainPanel, autoCalcInterval, -> {
|
162 | if (isChecked(cbAutoCalculate)) |
163 | calculationStep(); |
164 | }); |
165 | |
166 | var magneticDistanceSlider = jLiveValueSlider_int_bothWays(0, maxMagneticDistance, network.varMagneticDistance()); |
167 | |
168 | ret withRightAlignedButtons(/*jscroll_center_borderless*/jscroll(mainPanel), |
169 | cbAutoCalculate, |
170 | withLabel("Magnetic auto-connect distance:", jMinWidth(maxMagneticDistance, magneticDistanceSlider)),
|
171 | "Do magnetic connections", rThread doMagneticConnections, |
172 | "Configure nodes", rThread configureNodes, |
173 | "New instance", rThread newInstance, |
174 | "New node", rThread newElement); |
175 | } |
176 | } |
Began life as a copy of #1035441
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1035443 |
| Snippet name: | JG22Network - Swing representation of a G22Network |
| Eternal ID of this version: | #1035443/75 |
| Text MD5: | 49431593c63b875397e4e6541bb1b308 |
| Author: | stefan |
| Category: | javax / gazelle 22 |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-06-01 17:00:46 |
| Source code size: | 5382 bytes / 176 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 732 / 1807 |
| Version history: | 74 change(s) |
| Referenced in: | [show references] |