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

154
LINES

< > BotCompany Repo | #1005320 - Smart Console

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

Uses 3874K of libraries. Click here for Pure Java version (9239L/65K).

!7

concepts.

!include #1005347 // Classes for Smart Console

static JTextField tf;
static SingleComponentPanel scp;
static UserLine userLine;
static SystemAnswer systemAnswer;
static JTable table;
static L<Map> tableData;

p-awt {
  //mistAqua(); // GREAT, with rounded buttons/scrollbars
  //emeraldDusk(); // wellll...
  magellan(); // funny, but not that readable
  
  loadAndAutoSaveConcepts();
  showFrameHidingConsole(jtabs(
    "Console",
      northAndCenter(
        centerAndEast(tf = jCenteredTextField(initialLine()),
          jbutton("Answer...", "answerQ")),
        scp = new SingleComponentPanel(jFullCenter(centeredHtmlLabel("Yo!")))),
    "Questions & Answers",
      table = sexyTable()));
  tf.selectAll();
  tf.requestFocus();
  setFrameIconLater(tf, "#1005372");
    
  onEnter(tf, r {
    S s = tf.getText().trim();
    userLine = new UserLine(s);
    processUserLine();
  });
  
  tablePopupMenu(table, voidfunc(JPopupMenu menu, final int row) {
    final S q = first(getTableLine(table, row));
    Map map = _get(tableData, row);
    final UserAnswer ua = cast _get(map, "[hidden] ua");

    menu.add(jmenuItem("Answer...", r {
      userLine = uniq(UserLine, text := q);
      answerQ2();
    }));
    
    menu.add(jmenuItem("Mark solvable", r {
      cset(ua, "solvable", true);
    }));
    
    menu.add(jmenuItem("Un-mark solvable", r {
      cset(ua, "solvable", false);
    }));
  });
  
  /*registerCtrlKey(table, KeyEvent.VK_PAGE_UP, "Tab Left", r {
    print("lalala");
  });*/

  awtOnConceptChanges(table, 500, "updateTable");
  
  processUserLine();
}

svoid answerQ {
  final S s = tf.getText().trim();
  if (userLine == null || neq(userLine.text, s))
    userLine = new UserLine(s);
  answerQ2();
}

svoid answerQ2 {
  final UserLine _userLine = userLine;
  final new JTextField tfA;
  final JCheckBox cbSolvable = jCheckBox(uniq(SCDefaults).solvable);
  showTitledForm("Answer question",
    "Question", _userLine.text,
    "Answer", tfA,
    "Solvable", cbSolvable,
    r {
      S a = tfA.getText().trim();
      cnew(UserAnswer, userLine := _userLine, text := a,
        solvable := (uniq(SCDefaults).solvable = isChecked(cbSolvable)));
      print("Answer recorded! " + _userLine.text + " => " + a);
      processUserLine();
    });
}

svoid processUserLine {
  if (userLine == null) ret;
  try {
    scp.setComponent(makeComponent1());
  } catch e {
    scp.setComponent(typeWriterTextArea(getStackTrace(e)));
  }
}

static Component makeComponent1() {
  makeSystemAnswer();
  final S s = systemAnswer.text;
  JComponent c = jcenteredLabel(s);
  if (startsWith(s, "!j "))
    c = centerAndSouth(withBottomMargin(c), jcenteredLine(jbutton("Run", r {
      thread "Running" {
        print(evalJava(dropPrefixTrim("!j ", s)));
      }
    })));
  ret c;
}

static void makeSystemAnswer() {
  S ua = findUserAnswer(userLine.text);
  S answer;
  if (nempty(ua)) answer = ua;
  else answer = "Yeah. " + userLine.text;
  systemAnswer = cnew(SystemAnswer, +userLine, "text", answer);
}

svoid updateTable() {
  //print("Updating table (c=" + changes + ")");
  new L<Map> l;
  for (UserLine ul) {
    L<SystemAnswer> sa = findBackRefs(ul, SystemAnswer);
    L<UserAnswer> ua = findBackRefs(ul, UserAnswer);
    int n = max(l(sa), l(ua), 1);
    new L<Map> l2;
    for i to n: {
      UserAnswer _ua = _get(ua, i);
      l2.add(litorderedmap(
        "Question" := (_ua != null && _ua.solvable ? "! " : "") + ul.text,
        "User's Answer" := i < l(ua) ? ua.get(i).text : "",
        "System's Answer" := i < l(sa) ? sa.get(i).text : "",
        "[hidden] ua" := _ua));
    }
    l.addAll(reversedList(l2));
  }
  dataToTable_uneditable(table, tableData = reversedList(l));
}

static S findUserAnswer(S q) {
  for (UserLine ul)
    if (eqic(ul.text, q)) {
      UserAnswer ua = findBackRef(ul, UserAnswer);
      if (ua != null)
        ret ua.text;
    }
  null;
}

static S initialLine() {
  userLine = last(list(UserLine));
  ret userLine != null ? userLine.text : "What is love?";
}

download  show line numbers  debug dex   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, sawdedvomwva, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1005320
Snippet name: Smart Console
Eternal ID of this version: #1005320/7
Text MD5: 69e14eed5775b72091bbbb73b1a205d2
Transpilation MD5: 19bebadb998cebbea8cad2ca0ede18c1
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-21 12:20:28
Source code size: 4182 bytes / 154 lines
Pitched / IR pitched: No / No
Views / Downloads: 597 / 1500
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1005320 & http://1005320.tinybrain.de