Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

165
LINES

< > BotCompany Repo | #1035449 // JG22NetworkElement - Swing representation of a G22NetworkElement

JavaX fragment (include) [tags: use-pretranspiled]

Uses 2164K of libraries. Click here for Pure Java version (56451L/297K).

1  
srecord noeq JG22NetworkElement(G22Utils g22utils, JG22Network parent, G22NetworkElement element) is Swingable {
2  
  CollapsibleLeftPanel collapsiblePanel;
3  
  JLeftArrowScriptIDE ide;
4  
  SingleComponentPanel scpContent = scp();
5  
  JLabel lblDirty = jlabel();
6  
  transient new BoolVarWithNotify visualizing;
7  
  
8  
  {
9  
    visualizing.onChange(l0 updateDirtyFlag);
10  
  }
11  
12  
  cachedVisualize {
13  
    ide = g22utils.leftArrowIDE();
14  
    ide.makeParser = -> {
15  
      var parser = ide.makeParser_base();
16  
      element.configureParser(parser);
17  
      ret parser;
18  
    };
19  
    ide.withResultPanel(false);
20  
    ide.lvScript(element.varCode());
21  
    ide.runScript = l0 calculate;
22  
23  
    var popDownButton = withRightMargin(jfullcenter(
24  
      jPopDownButton_noText(
25  
        "Calculate" := rThread calculate,
26  
        "Visualize" := rThread visualizeNode,
27  
        "Configure" := rThread configureNode,
28  
        "Create input ports" := rThread autoCreateInputPorts,
29  
        "Create output port" := rThread autoCreateOutputPort,
30  
        "Copy code" := rThread copyCode,
31  
        "Delete node" := rThread deleteElement
32  
      )));
33  
      
34  
    var lblNodeID = toolTip("Click here to drag node around", jlabel("Node:"));
35  
    
36  
    componentPopupMenuItem(lblDirty,
37  
      "Show error", rThread showError);
38  
      
39  
    collapsiblePanel = new CollapsibleLeftPanel(
40  
      false, "Code", jMinWidth(0, ide.visualize()), jMinWidth(0, northAndCenter(
41  
        centerAndEastWithMargin(
42  
          withLabel(lblNodeID, centerTextField(jVarTextField(element.varIdentifier()))),
43  
          jline(
44  
            lblDirty,
45  
            popDownButton)),
46  
        scpContent)));
47  
        
48  
    bindVarToVar(element.varCodeVisible(), collapsiblePanel.varExpanded());
49  
    
50  
    var box = collapsiblePanel.visualize();
51  
52  
    jMinSize(80, 50, box);
53  
    setBorder(box, BorderFactory.createLineBorder(Color.green, 5);
54  
    
55  
    onMouseDown(lblNodeID, event -> {
56  
      new ComponentDragger(box, event).bringToFront(true).start();
57  
    });
58  
    
59  
    new ComponentResizeDragger(box, box);
60  
    var bounds = or(element.bounds, rect(50, 50, 400, 300));
61  
    setBounds(box, bounds);
62  
    
63  
    onBoundsChange(box, r {
64  
      element.bounds(toRect(getBounds(box)));
65  
      element.network.change();
66  
      parent.mainPanel.revalidateMe();
67  
    });
68  
    
69  
    ret box;
70  
  }
71  
  
72  
  void deleteElement {
73  
    parent.deleteElement(this);
74  
  }
75  
  
76  
  void calculate {
77  
    pcall-infobox {
78  
      parent.networkInstance.calculateNode(g22utils, element);
79  
      visualizeNode();
80  
    }
81  
  }
82  
  
83  
  void configureNode {
84  
    pcall-infobox {
85  
      element.configureBlueprint(g22utils);
86  
    }
87  
  }
88  
  
89  
  void visualizeNode {
90  
    pcall-infobox {
91  
      temp tempSetBoolVar(visualizing);
92  
      var parsedScript = element.compile(g22utils)!;
93  
      callVisualizeFunction(parsedScript);
94  
    }
95  
  }
96  
  
97  
  void callVisualizeFunction(GazelleV_LeftArrowScript.Script parsedScript) {
98  
    var visualizeFunction = parsedScript.getFunction("visualize");
99  
    
100  
    O elementInstance = myInstance();
101  
102  
    Component visualized = null;
103  
    if (visualizeFunction != null) {
104  
      var val = myValue();
105  
      O valueHandler = new O {
106  
        void setValue(O o) {
107  
          parent.networkInstance.setObject(element, val, o);
108  
        }
109  
      };
110  
      
111  
      // TODO: timeout?
112  
      RunResultWithTimestamps<O> value = runResultWithTimestamps(-> wrap(
113  
        visualizeFunction.call(
114  
          new FlexibleVarContext, elementInstance, valueHandler, element)));
115  
      ide.showScriptResult(value);
116  
      if (value.isOK())
117  
        visualized = wrap(value!);
118  
      else
119  
        visualized = jErrorView(value.error());
120  
    } else
121  
      visualized = new G22JavaObjectVisualizer(g22utils, elementInstance).visualize();
122  
123  
    print(+visualized);
124  
    scpContent.set(visualized);
125  
  }
126  
  
127  
  void updateDirtyFlag {
128  
    var value = myValue();
129  
    setText(lblDirty, value == null ? " " :
130  
      visualizing! ? "Visualizing"
131  
        : value.calculating() ? "Calculating"
132  
        : value.hasError() ? "Error"
133  
        : value.dirty() ? "Dirty" : "Clean");
134  
  }
135  
  
136  
  O myInstance() {
137  
    O elementInstance = null;
138  
    var ni = parent.networkInstance;
139  
    if (ni != null)
140  
      elementInstance = ni.getObjectForBlueprint(element);
141  
    ret elementInstance;
142  
  }
143  
  
144  
  G22NetworkInstance.Value myValue() {
145  
    O elementInstance = null;
146  
    var ni = parent.networkInstance;
147  
    ret ni?.valuesByBlueprint.get(element);
148  
  }
149  
  
150  
  void copyCode {
151  
    copyTextToClipboardVerbose(element.code());
152  
  }
153  
  
154  
  void showError {
155  
    showErrorFrame(myValue().error());
156  
  }
157  
  
158  
  void autoCreateOutputPort {
159  
    element.autoCreateOutputPort(myValue());
160  
  }
161  
  
162  
  void autoCreateInputPorts {
163  
    element.autoCreateInputPorts(g22utils);
164  
  }
165  
}

Author comment

Began life as a copy of #1035443

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035449
Snippet name: JG22NetworkElement - Swing representation of a G22NetworkElement
Eternal ID of this version: #1035449/82
Text MD5: aef6fa7714f8610167d81f8c88fa5b56
Transpilation MD5: 31de7edf1529612514b6c5d9b483d0d9
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 23:12:07
Source code size: 4804 bytes / 165 lines
Pitched / IR pitched: No / No
Views / Downloads: 192 / 370
Version history: 81 change(s)
Referenced in: [show references]