Libraryless. Click here for Pure Java version (2983L/23K/68K).
1 | !747 |
2 | !awt { |
3 | !actionListener { |
4 | |
5 | m { |
6 | static S snippetID; |
7 | static int maxTokens = 100; |
8 | static S fontID = null/*"#1000993"*/; |
9 | |
10 | static S corpusID = "#1001006"; // Snippets DB |
11 | static S predictorID = "#1001011"; |
12 | static boolean allTokens = true; |
13 | |
14 | static JTextArea textArea; |
15 | static JLabel status; |
16 | static JButton btnSave; |
17 | static JTextArea advisorTextPane; |
18 | static JScrollPane textScroller, advisorScroller; |
19 | static volatile Object predictor; |
20 | static Font fancyFont; |
21 | |
22 | static class MyLayoutManager implements LayoutManager { |
23 | public void addLayoutComponent(String s, Component c) {} |
24 | public void removeLayoutComponent(Component c) {} |
25 | |
26 | public Dimension preferredLayoutSize(Container c) { |
27 | return minimumLayoutSize(c); |
28 | } |
29 | |
30 | public Dimension minimumLayoutSize(Container c) { |
31 | return new Dimension(500, 400); |
32 | } |
33 | |
34 | public void layoutContainer(Container c) { |
35 | int w = c.getWidth(), h = c.getHeight(); |
36 | int split = h*2/3; |
37 | textScroller.setBounds(0, 0, w, split); |
38 | advisorScroller.setBounds(0, split, w, h-split); |
39 | } |
40 | } |
41 | |
42 | p { |
43 | if (args.length != 0) snippetID = args[0]; |
44 | thread { ask(); } |
45 | } |
46 | |
47 | static void ask() { |
48 | corpusID = slte("Corpus ID:", corpusID); |
49 | awt { go(); } |
50 | } |
51 | |
52 | static void go() { |
53 | //snippetID = formatSnippetID(snippetID); |
54 | JFrame frame = new JFrame("Predictive Editor v1"); |
55 | JPanel panel2 = new JPanel(new MyLayoutManager()); |
56 | |
57 | advisorTextPane = new JTextArea(); |
58 | advisorTextPane.setEditable(false); |
59 | advisorTextPane.setLineWrap(true); |
60 | advisorTextPane.setWrapStyleWord(true); |
61 | |
62 | // Prevents scrolling down? |
63 | DefaultCaret caret = (DefaultCaret) advisorTextPane.getCaret(); |
64 | caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); |
65 | |
66 | if (fontID != null) try { |
67 | fancyFont = loadFont(fontID, 24); |
68 | advisorTextPane.setFont(fancyFont); |
69 | } catch (Throwable e) { e.printStackTrace(); } |
70 | |
71 | //advisorScroller = new JScrollPane(advisorTextPane); |
72 | advisorScroller = new JScrollPane(makeAnimation()); |
73 | advisorScroller.setMinimumSize(new Dimension(100, 300)); |
74 | |
75 | textArea = new JTextArea(); |
76 | textScroller = new JScrollPane(textArea); |
77 | panel2.add(textScroller); |
78 | panel2.add(advisorScroller); |
79 | |
80 | status = new JLabel(" "); |
81 | btnSave = new JButton("Save " + snippetID); |
82 | btnSave.setEnabled(false); |
83 | |
84 | btnSave.addActionListener(actionListener { |
85 | saveSnippet(); |
86 | }); |
87 | |
88 | JPanel bottom = new JPanel(new BorderLayout); |
89 | bottom.add(BorderLayout.CENTER, status); |
90 | //bottom.add(BorderLayout.EAST, btnSave); |
91 | |
92 | JPanel panel = new JPanel(new BorderLayout); |
93 | panel.add(BorderLayout.CENTER, panel2); |
94 | panel.add(BorderLayout.SOUTH, bottom); |
95 | |
96 | textArea.getDocument().addDocumentListener(new DocumentListener() { |
97 | public void insertUpdate(DocumentEvent e) { |
98 | btnSave.setEnabled(true); |
99 | predict(); |
100 | } |
101 | public void removeUpdate(DocumentEvent e) { |
102 | btnSave.setEnabled(true); |
103 | predict(); |
104 | } |
105 | public void changedUpdate(DocumentEvent e) { |
106 | // apparently it doesn't occur |
107 | } |
108 | }); |
109 | |
110 | textArea.addCaretListener(new CaretListener() { |
111 | public void caretUpdate(CaretEvent e) { |
112 | predict(); |
113 | } |
114 | }); |
115 | |
116 | if (snippetID != null) { |
117 | textArea.setEditable(false); // still loading |
118 | status.setText("Loading..."); |
119 | thread { |
120 | final S s = loadSnippet(snippetID); |
121 | awt { |
122 | status.setText("Loaded " + s.length() + " chars."); |
123 | textArea.setText(s); |
124 | //textArea.scrollRectToVisible(new Rectangle(0, 0, 1, 1)); |
125 | textArea.setCaretPosition(0); |
126 | btnSave.setEnabled(false); |
127 | textArea.setEditable(true); |
128 | textArea.requestFocus(); |
129 | } |
130 | } |
131 | } |
132 | |
133 | frame.addWindowListener(new WindowAdapter() { |
134 | public void windowOpened(WindowEvent e) { |
135 | textArea.requestFocus(); |
136 | } |
137 | }); |
138 | |
139 | frame.add(panel); |
140 | frame.setBounds(200, 150, 500, 420); |
141 | frame.setVisible(true); |
142 | exitOnFrameClose(frame); |
143 | |
144 | thread { |
145 | loadPredictor(); |
146 | } |
147 | } |
148 | |
149 | static void saveSnippet() { |
150 | final S text = textArea.getText(); |
151 | btnSave.setEnabled(false); |
152 | status.setText("Saving..."); |
153 | thread { |
154 | final S url = "http://tinybrain.de:8080/tb-int/update_snippet_text.php"; |
155 | S user = loadTextFile(new File(userHome(), ".tinybrain/username").getPath(), null); |
156 | S pass = loadTextFile(new File(userHome(), ".tinybrain/userpass").getPath(), null); |
157 | S query = "id=" + parseSnippetID(snippetID) + "&text=" + urlencode(text) + "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); |
158 | final S page = doPost(query, url); |
159 | |
160 | awt { |
161 | status.setText("Saved snippet: " + page); |
162 | } |
163 | } |
164 | } |
165 | |
166 | static void loadPredictor() ctex { |
167 | // We have the animation instead. |
168 | //advisorShow("Loading predictor..."); |
169 | |
170 | Class learner = hotwire(predictorID); |
171 | set(learner, "showGUI", false); |
172 | if (corpusID != null) |
173 | set(learner, "corpusID", corpusID); |
174 | callMain(learner); |
175 | predictor = get(get(learner, "collector"), "winner"); |
176 | print("Predictor: " + predictor); |
177 | awt { predict(); } |
178 | } |
179 | |
180 | static void advisorShow(final S text) ctex { |
181 | awt { |
182 | advisorScroller.setViewportView(advisorTextPane); |
183 | advisorTextPane.setText(text); |
184 | } |
185 | } |
186 | |
187 | static void predict() { |
188 | try { |
189 | long startTime = now(); |
190 | Object p = predictor; |
191 | if (p == null) return; |
192 | //p = clone(p); |
193 | p = call(p, "derive"); // properly make a local copy |
194 | S text = textArea.getText(); |
195 | int idx = textArea.getCaretPosition(); |
196 | S previous = safeSubstring(text, 0, idx); |
197 | previous ="\n==\n" + previous; // indicate start of snippet to predictor |
198 | L<S> tok = javaTok(previous); |
199 | if (!allTokens) tok = codeTokensOnly(tok); |
200 | //if (tok.size() != 0) tok.remove(tok.size()-1); |
201 | print(structure(tok)); |
202 | |
203 | // make single prediction |
204 | S prediction = cast call(p, "read", tok); |
205 | new StringBuilder buf; |
206 | |
207 | if (prediction != null) { |
208 | //buf.append(prediction + "\n\n"); |
209 | new L<S> multiPrediction; |
210 | S pred = prediction; |
211 | int i = 0; |
212 | while (true) { |
213 | multiPrediction.add(pred); |
214 | if (++i >= maxTokens) break; |
215 | tok.add(pred); |
216 | pred = (String) call(p, "read", tok); |
217 | if (pred == null) break; |
218 | } |
219 | if (multiPrediction.size() > 1) |
220 | buf.append(join(multiPrediction) + "\n"); |
221 | } |
222 | |
223 | advisorShow(buf.toString()); |
224 | print((now()-startTime) + " ms"); |
225 | } catch (Throwable e) { |
226 | e.printStackTrace(); |
227 | // TODO: use "error" font :)))) |
228 | advisorShow(e.toString()); |
229 | } |
230 | } |
231 | |
232 | static L<S> codeTokensOnly(L<S> tok) { |
233 | new L<S> l; |
234 | for (int i = 1; i < tok.size(); i += 2) |
235 | l.add(tok.get(i)); |
236 | return l; |
237 | } |
238 | |
239 | !include #1001014 // ScalablePane |
240 | |
241 | static JComponent makeAnimation() ctex { |
242 | JLabel label = new JLabel(new ImageIcon(loadLibrary("#1001015").toURI().toURL())); |
243 | label.setText("Loading predictor..."); |
244 | label.setVerticalTextPosition(SwingConstants.BOTTOM); |
245 | label.setHorizontalTextPosition(SwingConstants.CENTER); |
246 | /*label.setBackground(Color.white); |
247 | label.setOpaque(true);*/ |
248 | return label; |
249 | //return new ScalablePane(ImageIO.read(loadLibrary("#1001013"))); |
250 | } |
251 | } |
Began life as a copy of #1000894
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: | #1001004 |
Snippet name: | Swing: Snippet editor with predictor (v1) |
Eternal ID of this version: | #1001004/1 |
Text MD5: | 2077741b735d48c8e4d75717ee17ea1d |
Transpilation MD5: | 94045e862756a9a9a20a8c6b05d141d8 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-16 13:31:59 |
Source code size: | 7704 bytes / 251 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 961 / 979 |
Referenced in: | [show references] |