1 | static JTable table; |
2 | static JTextArea chat; |
3 | static JTextField input; |
4 | static new L<S> log; |
5 | static L<S> recommendations; |
6 | |
7 | static int listDelay = 2000; |
8 | |
9 | static Bool thinking; |
10 | static new Thinker thinker; |
11 | |
12 | static S systemPrefix = "[system]"; |
13 | |
14 | static S dialog = "new"; |
15 | |
16 | svoid randomMain {
|
17 | //substanceLAF("EmeraldDusk"); // Too dark!
|
18 | substanceLAF("ChallengerDeep");
|
19 | loadLog(); |
20 | |
21 | logEvent("Starting");
|
22 | |
23 | thinker.startUp(log); |
24 | |
25 | table = tableWithTooltips(); |
26 | chat = autoScroll(wordWrapTextArea()); |
27 | chat.setText(joinLines(log)); |
28 | input = new JTextField; |
29 | JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), table)); |
30 | //setFrameIconLater(frame, "#1003593"); |
31 | |
32 | onEnter(input, r {
|
33 | post(); |
34 | }); |
35 | |
36 | onDoubleClick(table, voidfunc(int row) {
|
37 | chooseSuggestion(row); |
38 | }); |
39 | |
40 | for (int i = 1; i <= 12; i++) {
|
41 | final int _i = i; |
42 | registerFunctionKey(frame, i, r {
|
43 | chooseSuggestion(_i-1); |
44 | }); |
45 | } |
46 | |
47 | onUpdate(input, r { updateOnce(); });
|
48 | |
49 | updateOnce(); |
50 | |
51 | input.requestFocus(); |
52 | |
53 | if (isAction(last(log)) && confirmYesNo(input, "Run action? " + last(log))) |
54 | action(last(log)); |
55 | } |
56 | |
57 | static S getInput() {
|
58 | ret joinLines(" # ", input.getText().trim());
|
59 | } |
60 | |
61 | static void post() {
|
62 | S i = getInput(); |
63 | if (inputAllowedByUser(i)) |
64 | post(i); |
65 | } |
66 | |
67 | svoid chooseSuggestion(int row) {
|
68 | L<S> line = getTableLine(table, row); |
69 | if (line == null) ret; |
70 | Map<S, S> map = getTableLineAsMap(table, row); |
71 | S s = line.get(0); |
72 | if (empty(s)) ret; |
73 | |
74 | new L topSuggesters; |
75 | for (int i = 0; i < row; i++) |
76 | topSuggesters.add(getTableLineAsMap(table, i)); |
77 | if (empty(topSuggesters)) topSuggesters = null; |
78 | |
79 | logEvent("Suggestion chosen", mapPlus(map, "Row", row+1, "Input", getInput(), "Top Suggesters", topSuggesters));
|
80 | input.setText(s); |
81 | post(); |
82 | } |
83 | |
84 | svoid logEvent(S type) {
|
85 | logEvent(type, litmap()); |
86 | } |
87 | |
88 | svoid logEvent(S type, Map map) {
|
89 | logStructure(new File(dialogDir(), "event.log"), |
90 | ll(type, chatTime(), map)); |
91 | } |
92 | |
93 | static bool inputAllowedByUser(S i) {
|
94 | ret !swic(i, systemPrefix); |
95 | } |
96 | |
97 | // may be called from other thread |
98 | static void postSystemMessage(final S msg) {
|
99 | if (nempty(msg)) awtIfNecessary {
|
100 | post(systemPrefix + " " + msg); |
101 | } |
102 | } |
103 | |
104 | static void post(S i) {
|
105 | S s = i + "\n"; |
106 | chat.append(s); |
107 | appendToFile(logFile(), "[" + chatTime() + "] " + s); |
108 | logEvent("Posting", litmap("Text", s));
|
109 | synchronized(mc()) {
|
110 | log.add(i); |
111 | } |
112 | input.selectAll(); |
113 | updateOnce(); |
114 | try {
|
115 | action(i); |
116 | } catch e {
|
117 | printStackTrace(e); |
118 | postSystemMessage(exceptionToStringShort(e)); |
119 | } |
120 | } |
121 | |
122 | static bool isAction(S s) {
|
123 | if (s == null) false; |
124 | s = dropBracketPrefix(s); // e.g. "[bot]" |
125 | ret s.startsWith("!");
|
126 | } |
127 | |
128 | static void action(S s) {
|
129 | s = dropBracketPrefix(s); // e.g. "[bot]" |
130 | if (!s.startsWith("!")) ret;
|
131 | s = dropPrefix("!", s);
|
132 | |
133 | final S _s = s; |
134 | thread "Action" {
|
135 | loading {
|
136 | try {
|
137 | randomCmds(_s); |
138 | actionImpl(_s); // user must provide this |
139 | } catch e {
|
140 | printStackTrace(e); |
141 | postSystemMessage("Error - " + exceptionToStringShort(e));
|
142 | } |
143 | } |
144 | } |
145 | } |
146 | |
147 | static void fillList(bool force) {
|
148 | bool t = force || shouldUpdateList(); |
149 | if (neq(t, thinking)) {
|
150 | thinking = t; |
151 | setFrameIcon(table, t ? "#1003603" : "#1003593"); |
152 | } |
153 | |
154 | if (!t) |
155 | againl8r(); |
156 | else thread "Fill List" {
|
157 | final new L<S> data; |
158 | thinker.makeListData(cloneList(log), getInput(), data); |
159 | |
160 | dataToTable_uneditable(table, data); |
161 | againl8r(); |
162 | } |
163 | } |
164 | |
165 | static void updateOnce() { fillList(true); }
|
166 | |
167 | static void againl8r() {
|
168 | swingAfter(table, listDelay, r { fillList(false); });
|
169 | } |
170 | |
171 | static bool shouldUpdateList() {
|
172 | ret getFrame(table).isFocused() && !mouseInComponent(table); |
173 | } |
174 | |
175 | // also called from outside |
176 | static L<S> loadLog() {
|
177 | log.clear(); |
178 | for (S s : toLines(loadTextFile(logFile()))) pcall {
|
179 | log.add(substring(s, s.indexOf(']')+1).trim());
|
180 | } |
181 | ret log; |
182 | } |
183 | |
184 | synchronized static L<S> getLastFromLog(int n) {
|
185 | ret cloneList(getLast(log, n)); |
186 | } |
187 | |
188 | static File dialogDir() {
|
189 | ret prepareProgramFile(dialog); |
190 | } |
191 | |
192 | static File logFile() {
|
193 | ret new File(dialogDir(), "log.txt"); |
194 | } |
195 | |
196 | static void switchDialog(final S name) {
|
197 | swingAndWait(r {
|
198 | dialog = name; |
199 | loadLog(); |
200 | thinker = new Thinker; |
201 | thinker.startUp(log); |
202 | chat.setText(joinLines(log)); |
203 | }); |
204 | } |
205 | |
206 | static void randomCmds(S s) {
|
207 | new Matches m; |
208 | if "dialog *" {
|
209 | switchDialog(m.unq(0)); |
210 | postSystemMessage("OK, dialog switched to " + quote(dialog));
|
211 | } |
212 | } |
Began life as a copy of #1003695
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1003813 |
| Snippet name: | Random Main v7 (include) |
| Eternal ID of this version: | #1003813/1 |
| Text MD5: | 08784d65196411302e1bc77102b5d2a9 |
| Author: | stefan |
| Category: | javax / talking robots |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2016-07-30 17:56:44 |
| Source code size: | 4665 bytes / 212 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 803 / 1054 |
| Referenced in: | [show references] |