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

134
LINES

< > BotCompany Repo | #1009480 // editJavaXSnippet [Swing] using RSyntaxTextArea

JavaX fragment (include)

static editJavaXSnippet_Editor editJavaXSnippet(fS snippetID) {
  ret (editJavaXSnippet_Editor) swingAndWait(func {
    new editJavaXSnippet_Editor(snippetID)
  });
}

sclass editJavaXSnippet_Editor {
  S snippetID;
  EditorFrame frame;
  RSyntaxTextArea textArea;
  JButton btnSave, btnTranspile, btnMTranspile, btnReload, btnEleuReload;
  L<JButton> buttons;
  int snippetType;

  *(S _snippetID) {
    snippetID = formatSnippetID(_snippetID);
    frame = new EditorFrame;
    frameIcon(frame, "#1004655");
    
    frame.setTitle(getSnippetTitle(snippetID) + " [Editing " + psI(snippetID) + "]");
  
    textArea = frame.textArea;
    
    // Customize text area
    textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
    textArea.setCodeFoldingEnabled(true);
    textArea.setTabSize(2);
    textArea.setTabsEmulated(true); // Spaces are MUCH better!
    
    frame.setStatus("Loading...");
    
    btnSave = new JButton("Save on server");
    btnSave.addActionListener(actionListener { saveSnippet(null); });
    
    btnTranspile = new JButton("Save & transpile");
    btnTranspile.addActionListener(actionListener { transpile(false); });
    
    btnMTranspile = new JButton("S & Multi-transpile");
    btnMTranspile.addActionListener(actionListener { transpile(true); });
    
    btnReload = new JButton("Reload");
    btnReload.addActionListener(actionListener { reload(); });
    
    btnEleuReload = new JButton("Eleu-Reload");
    btnEleuReload.addActionListener(actionListener { eleuReload(); });
    
    buttons = ll(btnReload, btnSave, btnTranspile);
    
    jdisable(buttons);
    // XXX cursor bug? textArea.setEditable(false); // still loading
    
    snippetType = getSnippetType(snippetID);
    JScrollPane bottom = new JScrollPane(
        snippetType != 42
          ? jflowRight(btnSave, btnTranspile, btnMTranspile, btnReload, btnEleuReload)
          : jflowRight(btnSave, btnReload));
    addToWindow(frame, bottom);
  
    onUpdate(textArea, r {
      jenable(buttons);
    });
  
    showFrame(frame);
    reload();    
  }
  
  void saveSnippet(final Runnable next) {
    final S text = textArea.getText();
    jdisable(buttons);
    frame.setStatus("Saving...");
    thread {
      final S url = "http://tinybrain.de:8080/tb-int/update_snippet_text.php";
      S user = loadTextFile(new File(userHome(), ".tinybrain/username").getPath(), null);
      S pass = loadTextFile(new File(userHome(), ".tinybrain/userpass").getPath(), null);
      S query = "id=" + parseSnippetID(snippetID) + "&text=" + urlencode(text) + "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass);
      final S page = doPost(query, url);
      
      awt {
        frame.setStatus("Saved snippet: " + page);
        jenable(buttons);
        callF(next);
      }
    }
  }
  
  void transpile(final bool multi) {
    saveSnippet(r {
      frame.setStatus("Transpiling...");
      jdisable(btnEleuReload);
      
      thread "Transpiling" {
        try {
          final Pair<Bool, S> p = transpileOnServer(snippetID, multi ? "medium" : "quick");
          awt {
            if (p.a)
              frame.setStatus("Transpiled OK!");
            else {
              frame.setStatus("Transpilation error. " + p.b);
              showText("Transpilation Error", p.b);
            }
          }
        } catch e {
          printStackTrace(e);
          frame.setStatus("Transpilation problem. " + e);
        }
        jenable(btnEleuReload);
      }
    });
  }
  
  void reload() {
    thread "Load Snippet" {
      final S s = loadSnippet(snippetID);
      awt {
        frame.setStatus("Loaded " + s.length() + " chars.");
        textArea.setText(s);
        //textArea.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
        textArea.setCaretPosition(0);
        jenable(buttons);
        textArea.setEditable(true);
        textArea.requestFocus();
      }
    }
  }
  
  void eleuReload() {
    //thread "Eleu Reload" {
      S url = "http://ai1.lol/1002076/raw?q=" + urlencode("reload sub bot " + parseSnippetID(snippetID));
      openPlatformBrowser(url);
      /*frame.setStatus(or2(
        loadPage()), "Reload Problem"));*/
    //}
  }
}

Author comment

Began life as a copy of #1004360

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009480
Snippet name: editJavaXSnippet [Swing] using RSyntaxTextArea
Eternal ID of this version: #1009480/1
Text MD5: 4bd5f0bfa3cfdd82df96d78916660c5f
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-04 02:52:28
Source code size: 4311 bytes / 134 lines
Pitched / IR pitched: No / No
Views / Downloads: 441 / 464
Referenced in: [show references]