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

230
LINES

< > BotCompany Repo | #1035168 // G22LAScriptIDE - IDE for a G22LAScript (backup)

JavaX fragment (include)

1  
sclass G22LAScriptIDE<A extends G22LAScript> is Swingable {
2  
  settable G22Utils g22utils;
3  
  settable S noScriptSelectedMsg = "Please select or create a script to edit it";
4  
  
5  
  A script;
6  
  
7  
  transient SingleComponentPanel scp;
8  
  transient JLeftArrowScriptIDE[] ides;
9  
  transient JTabbedPane tabs;
10  
  transient JButton btnSave, btnDiscardChanges, btnClearForAutoRun;
11  
  transient CollapsibleLeftPanel collapsibleResultPanel;
12  
  transient G22ScriptResultPanel resultPanel;
13  
14  
  abstract class Mode {
15  
    G22ScriptMode modeEnum;
16  
    S name;
17  
    gettable bool editable;
18  
    
19  
    *(G22ScriptMode *modeEnum, S *name, bool *editable) {}
20  
    
21  
    simplyCached abstract IVarWithNotify<S> scriptVar();
22  
    
23  
    void addButtons(JPanel panel) {}
24  
    
25  
    toString { ret name; }
26  
    
27  
    S tabName() { ret scriptVar().has() ? name : "Not " + firstToLower(name); }
28  
  }
29  
  
30  
  class ModeClearedForAutoRun > Mode {
31  
    *() { super(G22ScriptMode.autoRunnable, "Cleared for auto-run", false); }
32  
    
33  
    IVarWithNotify<S> scriptVar_load() {
34  
      var var = new VirtualVar<S>(
35  
        -> script.codeForAutoRun(),
36  
        null /*text -> script.setClearedForAutoRun(text == null ?: new ClearForAutoRun(text))*/);
37  
      addWeakChangeListener(script.varClearedForAutoRun(), var);
38  
      ret var;
39  
    }
40  
    
41  
    void addButtons(JPanel panel) {
42  
      panel.add(jbutton("Forget auto-run code", rThread forgetAutoRunCode));
43  
    }
44  
  }
45  
  
46  
  class ModeSaved > Mode {
47  
    *() { super(G22ScriptMode.saved, "Saved", false); }
48  
    
49  
    IVarWithNotify<S> scriptVar_load() {
50  
      ret getterVarOnly(script.varText());
51  
    }
52  
    
53  
    void addButtons(JPanel panel) {
54  
      panel.add(btnClearForAutoRun = jbutton("Clear for auto-run", rThread clearForAutoRun));
55  
      panel.add(jbutton("Forget code", rThread forgetSaved));
56  
    }
57  
  }
58  
  
59  
  class ModeEdit > Mode {
60  
    *() { super(G22ScriptMode.edit, "Edit", true); }
61  
    
62  
    IVarWithNotify<S> scriptVar_load() {
63  
      var var = new VirtualVar<S>(
64  
        -> script.textForEditing(),
65  
        text -> script.receiveEditingText(text)
66  
      );
67  
      addWeakChangeListener(script, var);
68  
      ret var;
69  
    }
70  
    
71  
    void addButtons(JPanel panel) {
72  
      panel.add(btnSave = jbutton("Save", rThread saveEdit));
73  
      panel.add(btnDiscardChanges = jbutton("Discard changes", rThread discardEdit));
74  
    }
75  
  }
76  
  
77  
  transient new ModeEdit modeEdit;
78  
  transient new ModeSaved modeSaved;
79  
  transient new ModeClearedForAutoRun modeClearedForAutoRun;
80  
  transient L<Mode> modes = ll(
81  
    modeEdit,
82  
    modeSaved,
83  
    modeClearedForAutoRun
84  
  );
85  
  
86  
  *(G22Utils *g22utils) {}
87  
  
88  
  cachedVisualize {
89  
    ides = new JLeftArrowScriptIDE[l(modes)];
90  
    if (scp == null) scp = singleComponentPanel();
91  
    loadScript(script);
92  
    ret scp;
93  
  }
94  
  
95  
  void setScript(A script) {
96  
    if (this.script != script)
97  
      if (this.script != null)
98  
        fail("Can't set script after initialisation");
99  
      else
100  
        loadScript(script);
101  
  }
102  
  
103  
  void loadScript(A script) {
104  
    this.script = script;
105  
    if (scp == null) ret;
106  
    if (script == null)
107  
      scp.set(jcenteredlabel(noScriptSelectedMsg()));
108  
    else {
109  
      tabs = jtabs();
110  
      resultPanel = new G22ScriptResultPanel;
111  
      collapsibleResultPanel = new CollapsibleLeftPanel(false, "Output", resultPanel.visualize(), tabs);
112  
      collapsibleResultPanel.sidePanelMargins = c -> withTopAndLeftMargin(c);
113  
      
114  
      // This places tabs vertically at the right hand side...
115  
      // (not what we want)
116  
      //setTabPlacement(JTabbedPane.RIGHT, tabs);
117  
      
118  
      for (int i, Mode mode : unpair iterateWithIndex(modes)) {
119  
        var ide = ides[i] = g22utils.leftArrowIDE();
120  
        ide.withResultPanel(false);
121  
        ide.resultPanel = resultPanel;
122  
        ide.collapsibleResultPanel = collapsibleResultPanel;
123  
        ide.wrapSection = c -> wrapEditorSection(mode, c);
124  
        ide.newCompileResult = -> script.newCompileResult();
125  
        ide.makeParser = -> script.makeParser();
126  
        modifyIDE(ide);
127  
        var varScript = mode.scriptVar();
128  
        ide.lvScript(varWithNotifyToLiveValue(S.class, varScript));
129  
        addTab(tabs, str(mode));
130  
        mode.addButtons(ide.buttons());
131  
        ide.visualize();
132  
        ide.setEditable(mode.editable());
133  
        varScript.onChangeAndNow(text ->
134  
          setTab(tabs, i, text == null ? jcenteredlabel("Empty") : wrapIDE(mode, ide));
135  
        ide.popDownButton.onFillingMenu(menu ->
136  
          addMenuItem(menu, "Show History", rThread showHistory));
137  
      }
138  
      
139  
      script.onChangeAndNow(r {
140  
        for (int i, Mode mode : unpair iterateWithIndex(modes))
141  
          setTabTitle(tabs, i, mode.tabName());
142  
        setEnabled(script.isEditing(), btnSave, btnDiscardChanges);
143  
        setEnabled(btnClearForAutoRun, script.isSavedDistinctFromAutoRunVersion());
144  
      });
145  
      
146  
      setMode(script.isEditing() ? modeEdit : modeSaved);
147  
      scp.set(collapsibleResultPanel);
148  
    }
149  
  }
150  
  
151  
  swappable JComponent wrapEditorSection(Mode mode, JComponent editorSection) {
152  
    if (mode == modeSaved)
153  
      ret withTopMargin(northAndCenterWithMargin(
154  
        withRightMargin(jline(jbutton("Edit script", -> setMode(modeEdit)))),
155  
        editorSection));
156  
    else
157  
      ret editorSection;
158  
  }
159  
  
160  
  swappable JComponent wrapIDE(Mode mode, JLeftArrowScriptIDE ide) {
161  
    ret ide.visualize();
162  
  }
163  
  
164  
  event settingUpIDE(JLeftArrowScriptIDE ide);
165  
  
166  
  swappable void modifyIDE(JLeftArrowScriptIDE ide) {
167  
    ide.showTitle(false);
168  
    settingUpIDE(ide);
169  
  }
170  
  
171  
  void saveEdit {
172  
    script.completeEdit();
173  
    setMode(modeSaved);
174  
  }
175  
  
176  
  JLeftArrowScriptIDE ide(Mode mode) {
177  
    if (ides == null) visualize();
178  
    ret _get(ides, indexOf(modes, mode));
179  
  }
180  
  
181  
  void setMode(Mode mode) {
182  
    selectTab(tabs, indexOf(modes, mode));
183  
  }
184  
  
185  
  // get currently visible mode
186  
  Mode visibleMode() {
187  
    ret _get(modes, indexOfSelectedTab(tabs));
188  
  }
189  
  
190  
  JLeftArrowScriptIDE visibleIDE() {
191  
    ret ide(visibleMode());
192  
  }
193  
  
194  
  void discardEdit {
195  
    setMode(modeSaved);
196  
    script.editingText(null);
197  
  }
198  
  
199  
  void forgetSaved {
200  
    script.setTextWithHistory(null);
201  
  }
202  
  
203  
  void clearForAutoRun {
204  
    script.clearForAutoRun();
205  
    setMode(modeClearedForAutoRun);
206  
  }
207  
  
208  
  void forgetAutoRunCode {
209  
    script.forgetAutoRunCode();
210  
  }
211  
  
212  
  private void selfTest_impl {
213  
    new G22LAScript script;
214  
    setScript((A) script);
215  
    ide(modeEdit).setText("hello");
216  
    assertEqualsVerbose(null, script.text());
217  
    assertEqualsVerbose("hello", script.editedText());
218  
    saveEdit();
219  
    assertEqualsVerbose("hello", script.text());
220  
    assertEqualsVerbose(null, script.editedText());
221  
  }
222  
  
223  
  static void selfTest(G22Utils g22utils) {
224  
    new G22LAScriptIDE(g22utils).selfTest_impl();
225  
  }
226  
  
227  
  void showHistory {
228  
    showText("Edit history of " + script, loadTextFile(script.historyFile()));
229  
  }
230  
}

Author comment

Began life as a copy of #1034345

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035168
Snippet name: G22LAScriptIDE - IDE for a G22LAScript (backup)
Eternal ID of this version: #1035168/1
Text MD5: 5061c254966a0b9dc4a27ec0b5e4deac
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-03 19:45:34
Source code size: 6988 bytes / 230 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 148
Referenced in: [show references]