Download Jar. Uses 3874K of libraries. Click here for Pure Java version (9239L/65K).
1 | !7 |
2 | |
3 | concepts. |
4 | |
5 | !include #1005347 // Classes for Smart Console |
6 | |
7 | static JTextField tf; |
8 | static SingleComponentPanel scp; |
9 | static UserLine userLine; |
10 | static SystemAnswer systemAnswer; |
11 | static JTable table; |
12 | static L<Map> tableData; |
13 | |
14 | p-awt { |
15 | //mistAqua(); // GREAT, with rounded buttons/scrollbars |
16 | //emeraldDusk(); // wellll... |
17 | magellan(); // funny, but not that readable |
18 | |
19 | loadAndAutoSaveConcepts(); |
20 | showFrameHidingConsole(jtabs( |
21 | "Console", |
22 | northAndCenter( |
23 | centerAndEast(tf = jCenteredTextField(initialLine()), |
24 | jbutton("Answer...", "answerQ")), |
25 | scp = new SingleComponentPanel(jFullCenter(centeredHtmlLabel("Yo!")))), |
26 | "Questions & Answers", |
27 | table = sexyTable())); |
28 | tf.selectAll(); |
29 | tf.requestFocus(); |
30 | setFrameIconLater(tf, "#1005372"); |
31 | |
32 | onEnter(tf, r { |
33 | S s = tf.getText().trim(); |
34 | userLine = new UserLine(s); |
35 | processUserLine(); |
36 | }); |
37 | |
38 | tablePopupMenu(table, voidfunc(JPopupMenu menu, final int row) { |
39 | final S q = first(getTableLine(table, row)); |
40 | Map map = _get(tableData, row); |
41 | final UserAnswer ua = cast _get(map, "[hidden] ua"); |
42 | |
43 | menu.add(jmenuItem("Answer...", r { |
44 | userLine = uniq(UserLine, text := q); |
45 | answerQ2(); |
46 | })); |
47 | |
48 | menu.add(jmenuItem("Mark solvable", r { |
49 | cset(ua, "solvable", true); |
50 | })); |
51 | |
52 | menu.add(jmenuItem("Un-mark solvable", r { |
53 | cset(ua, "solvable", false); |
54 | })); |
55 | }); |
56 | |
57 | /*registerCtrlKey(table, KeyEvent.VK_PAGE_UP, "Tab Left", r { |
58 | print("lalala"); |
59 | });*/ |
60 | |
61 | awtOnConceptChanges(table, 500, "updateTable"); |
62 | |
63 | processUserLine(); |
64 | } |
65 | |
66 | svoid answerQ { |
67 | final S s = tf.getText().trim(); |
68 | if (userLine == null || neq(userLine.text, s)) |
69 | userLine = new UserLine(s); |
70 | answerQ2(); |
71 | } |
72 | |
73 | svoid answerQ2 { |
74 | final UserLine _userLine = userLine; |
75 | final new JTextField tfA; |
76 | final JCheckBox cbSolvable = jCheckBox(uniq(SCDefaults).solvable); |
77 | showTitledForm("Answer question", |
78 | "Question", _userLine.text, |
79 | "Answer", tfA, |
80 | "Solvable", cbSolvable, |
81 | r { |
82 | S a = tfA.getText().trim(); |
83 | cnew(UserAnswer, userLine := _userLine, text := a, |
84 | solvable := (uniq(SCDefaults).solvable = isChecked(cbSolvable))); |
85 | print("Answer recorded! " + _userLine.text + " => " + a); |
86 | processUserLine(); |
87 | }); |
88 | } |
89 | |
90 | svoid processUserLine { |
91 | if (userLine == null) ret; |
92 | try { |
93 | scp.setComponent(makeComponent1()); |
94 | } catch e { |
95 | scp.setComponent(typeWriterTextArea(getStackTrace(e))); |
96 | } |
97 | } |
98 | |
99 | static Component makeComponent1() { |
100 | makeSystemAnswer(); |
101 | final S s = systemAnswer.text; |
102 | JComponent c = jcenteredLabel(s); |
103 | if (startsWith(s, "!j ")) |
104 | c = centerAndSouth(withBottomMargin(c), jcenteredLine(jbutton("Run", r { |
105 | thread "Running" { |
106 | print(evalJava(dropPrefixTrim("!j ", s))); |
107 | } |
108 | }))); |
109 | ret c; |
110 | } |
111 | |
112 | static void makeSystemAnswer() { |
113 | S ua = findUserAnswer(userLine.text); |
114 | S answer; |
115 | if (nempty(ua)) answer = ua; |
116 | else answer = "Yeah. " + userLine.text; |
117 | systemAnswer = cnew(SystemAnswer, +userLine, "text", answer); |
118 | } |
119 | |
120 | svoid updateTable() { |
121 | //print("Updating table (c=" + changes + ")"); |
122 | new L<Map> l; |
123 | for (UserLine ul) { |
124 | L<SystemAnswer> sa = findBackRefs(ul, SystemAnswer); |
125 | L<UserAnswer> ua = findBackRefs(ul, UserAnswer); |
126 | int n = max(l(sa), l(ua), 1); |
127 | new L<Map> l2; |
128 | for i to n: { |
129 | UserAnswer _ua = _get(ua, i); |
130 | l2.add(litorderedmap( |
131 | "Question" := (_ua != null && _ua.solvable ? "! " : "") + ul.text, |
132 | "User's Answer" := i < l(ua) ? ua.get(i).text : "", |
133 | "System's Answer" := i < l(sa) ? sa.get(i).text : "", |
134 | "[hidden] ua" := _ua)); |
135 | } |
136 | l.addAll(reversedList(l2)); |
137 | } |
138 | dataToTable_uneditable(table, tableData = reversedList(l)); |
139 | } |
140 | |
141 | static S findUserAnswer(S q) { |
142 | for (UserLine ul) |
143 | if (eqic(ul.text, q)) { |
144 | UserAnswer ua = findBackRef(ul, UserAnswer); |
145 | if (ua != null) |
146 | ret ua.text; |
147 | } |
148 | null; |
149 | } |
150 | |
151 | static S initialLine() { |
152 | userLine = last(list(UserLine)); |
153 | ret userLine != null ? userLine.text : "What is love?"; |
154 | } |
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt
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: | 921 / 2737 |
Version history: | 6 change(s) |
Referenced in: | [show references] |