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

250
LINES

< > BotCompany Repo | #1001105 // Java programming android (developing)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1154L/9K/30K).

!747
!awt {

m {
  static new L<Task> tasks;
  static new AIEngine engine;
  
  !include #1001107 // AIEngine
  
  static class Task {
    S text;
    Task forTask;
    S state = "new";
    Throwable error;
    
    *(S *text) {}
    
    boolean runnable() { return false; }
    void run() {}
    
    boolean ok() { return state.equals("done"); }
  }
  
  static class Show extends Task {
    Find find;
    
    *() { super("show java class"); }
    
    boolean runnable() { return find.result != null; }
    
    void run() {
      //showText("A Java class", find.result);
      JTextArea textArea = new JTextArea(find.result);
      makeFrame("A Java class", new JScrollPane(textArea));
      engine.remember(engine.addObject(find.result) + " is visualized in " + engine.addObject(textArea));
    }
  }
  
  static class Find extends Task {
    S result;
    
    *() { super("find a java class"); }
    
    boolean runnable() { return true; }
    
    void run() ctex {
      result = loadSnippet("#1001027"); // DerivedHashMap
      S classID = engine.addObject(result);
      engine.remember("the java class is " + classID);
    }
  }
  
  static class FindText extends Task {
    JTextArea textArea;
    
    *() { super("find text"); }
    
    boolean runnable() { return true; }
    
    void run() {
      textArea = engine.findObjectOfType(JTextArea.class);
      if (textArea == null)
        fail("no text area found");
    }
    
    S getText() {
      return textArea.getText();
    }
    
    void updateText(final S text) {
      awt {
        textArea.setText(text);
      }
    }
  }
    
  static class DeleteFirstLine extends Task {
    new FindText find;
    
    *() {
      super("delete first line");
      tasks.add(find);
    }
    
    boolean runnable() { return find.ok(); }
    
    void run() {
      S text = find.getText();
      int i = text.indexOf('\n');
      text = i >= 0 ? text.substring(i+1) : "";
      find.updateText(text);
    }
  }
  
  static class DeleteDoubleVariables extends Task {
    new FindText find;
    
    *() {
      super("delete double variables");
      tasks.add(find);
    }
    
    boolean runnable() { return find.ok(); }
    
    void run() {
      S text = find.getText();
      text = applyTranslator("#1001045", text);
      find.updateText(text);
    }
  }
  
  static class ReplaceTwoDigitNumbersWithNothing extends Task {
    new FindText find;
    
    *() {
      super("replace bla");
      tasks.add(find);
    }
    
    boolean runnable() { return find.ok(); }
    
    void run() {
      S text = find.getText();
      text = text.replaceAll("\\d\\d", "");
      find.updateText(text);
    }
  }
  
  static class CountLines extends Task {
    new FindText find;
    int result;
    
    *() {
      super("count lines");
      tasks.add(find);
    }
    
    boolean runnable() { return find.ok(); }
    
    void run() {
      S text = find.getText();
      result = toLines(text).size();
    }
  }
  
  static S taskResult(Task t) {
    return t.state.equals("new") ? "hm" :
      t.state.equals("done") ? "ok" :
      t.state.equals("error") ? "oops: " + t.error :
      "hmhm? " + t.state; // impossible state
  }
  
  p {
    makeAndroid("This is the Java programming android.", stringfunc {
      fallback(s, answer(s))
    });
  }
  
  static S fallback(S s, S answer) {
    if (answer != null) return answer;
    return "?";
  }
  
  static synchronized S answer(S s) {
    if (match3("give me a java class", s) || match3("show me a java class", s)) {
      new Show show;
      new Find find;
      show.find = find;
      tasks.add(show);
      tasks.add(find);
      doStuff();
      return taskResult(show);
    }
    
    if (match3("delete first line", s) || match3("delete the first line", s)) {
      new DeleteFirstLine t;
      process(t);
      return taskResult(t);
    }
    
    if (match3("count lines", s)) {
      new CountLines t;
      process(t);
      return t.ok() ? t.result + " line(s)" : taskResult(t);
    }
    
    if (match3("delete double variables", s)) {
      new DeleteDoubleVariables t;
      process(t);
      return taskResult(t);
    }
    
    if (match3("replace two digit numbers with nothing", s)) {
      new ReplaceTwoDigitNumbersWithNothing t;
      process(t);
      return taskResult(t);
    }
    
    return null;
  }
  
  static void process(Task t) {
    tasks.add(t);
    doStuff();
  }
  
  static void showTasks() {
    for (Task t: tasks)
      print(t.state + ": " + t.text);
  }
  
  static void doStuff() {
    while (doStuff1()) {}
    showTasks();
    engine.show();
  }
  
  static boolean doStuff1() {
    boolean didAnything = false;
    for (int i = 0; i < tasks.size(); i++) {
      Task t = tasks.get(i);
      if (!t.state.equals("new")) continue;
      if (t.runnable()) {
        didAnything = true;
        try {
          t.run();
          t.state = "done";
        } catch (Throwable e) {
          e.printStackTrace();
          t.error = e;
          t.state = "error";
        }
      }
    }
    return didAnything;
  }
  
  // called by match3
  static L<S> parse(S s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }
  
  static S applyTranslator(S translatorID, S text) {
    Class javax = getJavaX();
    File X = cast call(javax, "TempDirMaker_make");
    saveTextFile(new File(X, "main.java"), text);
    X = (File) call(javax, "applyTranslator", X, translatorID, "", new ArrayList<File>());
    return readTextFile(new File(X, "main.java"));
  }
}

Author comment

Began life as a copy of #1001102

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001105
Snippet name: Java programming android (developing)
Eternal ID of this version: #1001105/1
Text MD5: 4100432220e23c005c93fc465c3119a4
Transpilation MD5: a500ad514ac935a51c760472afa04ca3
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-23 15:44:51
Source code size: 5785 bytes / 250 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 696 / 732
Referenced in: [show references]