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

213
LINES

< > BotCompany Repo | #1034760 // G22LAScriptIDE - IDE for a G22LAScript (with the 3 modes, backup with old layout)

JavaX fragment (include)

sclass G22LAScriptIDE<A extends G22LAScript> is Swingable {
  settable G22Utils g22utils;
  settable S noScriptSelectedMsg = "Please select or create a script to edit it";
  
  A script;
  
  transient SingleComponentPanel scp;
  transient JLeftArrowScriptIDE[] ides;
  transient JTabbedPane tabs;
  transient JButton btnSave, btnDiscardChanges, btnClearForAutoRun;
  
  abstract class Mode {
    G22ScriptMode modeEnum;
    S name;
    gettable bool editable;
    
    *(G22ScriptMode *modeEnum, S *name, bool *editable) {}
    
    simplyCached abstract IVarWithNotify<S> scriptVar();
    
    void addButtons(JPanel panel) {}
    
    toString { ret name; }
    
    S tabName() { ret scriptVar().has() ? name : "Not " + firstToLower(name); }
  }
  
  class ModeClearedForAutoRun > Mode {
    *() { super(G22ScriptMode.autoRunnable, "Cleared for auto-run", false); }
    
    IVarWithNotify<S> scriptVar_load() {
      var var = new VirtualVar<S>(
        -> script.codeForAutoRun(),
        null /*text -> script.setClearedForAutoRun(text == null ?: new ClearForAutoRun(text))*/);
      addWeakChangeListener(script.varClearedForAutoRun(), var);
      ret var;
    }
    
    void addButtons(JPanel panel) {
      panel.add(jbutton("Forget auto-run code", rThread forgetAutoRunCode));
    }
  }
  
  class ModeSaved > Mode {
    *() { super(G22ScriptMode.saved, "Saved", false); }
    
    IVarWithNotify<S> scriptVar_load() {
      ret getterVarOnly(script.varText());
    }
    
    void addButtons(JPanel panel) {
      panel.add(btnClearForAutoRun = jbutton("Clear for auto-run", rThread clearForAutoRun));
      panel.add(jbutton("Forget code", rThread forgetSaved));
    }
  }
  
  class ModeEdit > Mode {
    *() { super(G22ScriptMode.edit, "Edit", true); }
    
    IVarWithNotify<S> scriptVar_load() {
      var var = new VirtualVar<S>(
        -> script.textForEditing(),
        text -> script.receiveEditingText(text)
      );
      addWeakChangeListener(script, var);
      ret var;
    }
    
    void addButtons(JPanel panel) {
      panel.add(btnSave = jbutton("Save", rThread saveEdit));
      panel.add(btnDiscardChanges = jbutton("Discard changes", rThread discardEdit));
    }
  }
  
  transient new ModeEdit modeEdit;
  transient new ModeSaved modeSaved;
  transient new ModeClearedForAutoRun modeClearedForAutoRun;
  transient L<Mode> modes = ll(
    modeEdit,
    modeSaved,
    modeClearedForAutoRun
  );
  
  *(G22Utils *g22utils) {}
  
  cachedVisualize {
    ides = new JLeftArrowScriptIDE[l(modes)];
    if (scp == null) scp = singleComponentPanel();
    loadScript(script);
    ret scp;
  }
  
  void setScript(A script) {
    if (this.script != script)
      if (this.script != null)
        fail("Can't set script after initialisation");
      else
        loadScript(script);
  }
  
  void loadScript(A script) {
    this.script = script;
    if (scp == null) ret;
    if (script == null)
      scp.set(jcenteredlabel(noScriptSelectedMsg()));
    else {
      tabs = jtabs();
      
      // This places tabs vertically at the right hand side...
      // (not what we want)
      //setTabPlacement(JTabbedPane.RIGHT, tabs);
      
      for (int i, Mode mode : unpair iterateWithIndex(modes)) {
        var ide = ides[i] = g22utils.leftArrowIDE();
        ide.wrapSection = c -> wrapEditorSection(mode, c);
        ide.newCompileResult = -> script.newCompileResult();
        ide.makeParser = -> script.makeParser();
        modifyIDE(ide);
        var varScript = mode.scriptVar();
        ide.lvScript(varWithNotifyToLiveValue(S.class, varScript));
        addTab(tabs, str(mode));
        mode.addButtons(ide.buttons());
        ide.visualize();
        ide.setEditable(mode.editable());
        varScript.onChangeAndNow(text ->
          setTab(tabs, i, text == null ? jcenteredlabel("Empty") : wrapIDE(mode, ide));
        ide.popDownButton.onFillingMenu(menu ->
          addMenuItem(menu, "Show History", rThread showHistory));
      }
      
      script.onChangeAndNow(r {
        for (int i, Mode mode : unpair iterateWithIndex(modes))
          setTabTitle(tabs, i, mode.tabName());
        setEnabled(script.isEditing(), btnSave, btnDiscardChanges);
        setEnabled(btnClearForAutoRun, script.isSavedDistinctFromAutoRunVersion());
      });
      
      setMode(script.isEditing() ? modeEdit : modeSaved);
      scp.set(tabs);
    }
  }
  
  swappable JComponent wrapEditorSection(Mode mode, JComponent editorSection) {
    if (mode == modeSaved)
      ret withTopMargin(northAndCenterWithMargin(
        withRightMargin(jline(jbutton("Edit script", -> setMode(modeEdit)))),
        editorSection));
    else
      ret editorSection;
  }
  
  swappable JComponent wrapIDE(Mode mode, JLeftArrowScriptIDE ide) {
    ret ide.visualize();
  }
  
  event settingUpIDE(JLeftArrowScriptIDE ide);
  
  swappable void modifyIDE(JLeftArrowScriptIDE ide) {
    ide.showTitle(false);
    settingUpIDE(ide);
  }
  
  void saveEdit {
    script.completeEdit();
    setMode(modeSaved);
  }
  
  JLeftArrowScriptIDE ide(Mode mode) {
    if (ides == null) visualize();
    ret _get(ides, indexOf(modes, mode));
  }
  
  void setMode(Mode mode) {
    selectTab(tabs, indexOf(modes, mode));
  }
  
  void discardEdit {
    setMode(modeSaved);
    script.editingText(null);
  }
  
  void forgetSaved {
    script.setTextWithHistory(null);
  }
  
  void clearForAutoRun {
    script.clearForAutoRun();
    setMode(modeClearedForAutoRun);
  }
  
  void forgetAutoRunCode {
    script.forgetAutoRunCode();
  }
  
  private void selfTest_impl {
    new G22LAScript script;
    setScript((A) script);
    ide(modeEdit).setText("hello");
    assertEqualsVerbose(null, script.text());
    assertEqualsVerbose("hello", script.editedText());
    saveEdit();
    assertEqualsVerbose("hello", script.text());
    assertEqualsVerbose(null, script.editedText());
  }
  
  static void selfTest(G22Utils g22utils) {
    new G22LAScriptIDE(g22utils).selfTest_impl();
  }
  
  void showHistory {
    showText("Edit history of " + script, loadTextFile(script.historyFile()));
  }
}

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: #1034760
Snippet name: G22LAScriptIDE - IDE for a G22LAScript (with the 3 modes, backup with old layout)
Eternal ID of this version: #1034760/1
Text MD5: f7d050d5ffaf7e2432b6abcd5c7726a3
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-08 05:34:08
Source code size: 6306 bytes / 213 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 155
Referenced in: [show references]