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

281
LINES

< > BotCompany Repo | #1034345 // G22LAScriptIDE - IDE for a G22LAScript (with the 3 modes, LIVE)

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

Transpiled version (69830L) is out of date.

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

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034345
Snippet name: G22LAScriptIDE - IDE for a G22LAScript (with the 3 modes, LIVE)
Eternal ID of this version: #1034345/120
Text MD5: 46e31e0ea6f40bf408741f937b4a2700
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-07-30 19:32:40
Source code size: 8661 bytes / 281 lines
Pitched / IR pitched: No / No
Views / Downloads: 374 / 1127
Version history: 119 change(s)
Referenced in: [show references]