Source code: org/alicebot/gui/SimpleConsole.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.gui;
6
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.io.*;
11 import java.util.Set;
12 import javax.swing.*;
13 import org.alicebot.server.core.*;
14 import org.alicebot.server.core.logging.Log;
15 import org.alicebot.server.core.util.Shell;
16 import org.alicebot.server.core.util.Trace;
17 import org.alicebot.server.net.AliceServer;
18
19 // Referenced classes of package org.alicebot.gui:
20 // ListDialog
21
22 public class SimpleConsole extends JPanel
23 {
24 public class ConsoleInputStream extends InputStream
25 {
26
27 public void receive(String s)
28 {
29 content = (s + '\n').getBytes();
30 mark = 0;
31 }
32
33 public int read(byte abyte0[], int i, int j)
34 throws IOException
35 {
36 while(mark >= content.length)
37 try
38 {
39 Thread.sleep(0L);
40 }
41 catch(InterruptedException interruptedexception)
42 {
43 return -1;
44 }
45 if(abyte0 == null)
46 throw new NullPointerException();
47 if(i < 0 || i > abyte0.length || j < 0 || i + j > abyte0.length || i + j < 0)
48 throw new IndexOutOfBoundsException();
49 if(j == 0)
50 return 0;
51 if(content.length == 0)
52 return -1;
53 int k = 1;
54 abyte0[i] = content[mark++];
55 for(; k < j && k < content.length; k++)
56 abyte0[i + k] = content[mark++];
57
58 return k;
59 }
60
61 public int available()
62 throws IOException
63 {
64 return content.length - mark - 1;
65 }
66
67 public boolean markSupported()
68 {
69 return false;
70 }
71
72 public int read()
73 {
74 while(mark >= content.length)
75 try
76 {
77 Thread.sleep(0L);
78 }
79 catch(InterruptedException interruptedexception)
80 {
81 return -1;
82 }
83 if(mark < content.length)
84 return content[mark++];
85 else
86 return -1;
87 }
88
89 byte content[];
90 private int mark;
91
92 public ConsoleInputStream()
93 {
94 content = new byte[0];
95 mark = 0;
96 }
97 }
98
99 public class ConsolePromptStream extends OutputStream
100 {
101
102 public void write(byte abyte0[], int i, int j)
103 {
104 inputPanel.setPrompt(new String(abyte0, i, j));
105 }
106
107 public void write(int i)
108 {
109 inputPanel.setPrompt(String.valueOf((char)i));
110 }
111
112 public ConsolePromptStream()
113 {
114 }
115 }
116
117 public class ConsoleDisplayStream extends OutputStream
118 {
119
120 public void write(byte abyte0[], int i, int j)
121 {
122 while(paused)
123 try
124 {
125 Thread.sleep(0L);
126 }
127 catch(InterruptedException interruptedexception) { }
128 display.append(new String(abyte0, i, j));
129 display.setCaretPosition(display.getText().length());
130 }
131
132 public void write(int i)
133 {
134 while(paused)
135 try
136 {
137 Thread.sleep(0L);
138 }
139 catch(InterruptedException interruptedexception) { }
140 display.append(String.valueOf((char)i));
141 display.setCaretPosition(display.getText().length());
142 }
143
144 private void togglePause()
145 {
146 paused = !paused;
147 }
148
149 private boolean paused;
150
151
152 public ConsoleDisplayStream()
153 {
154 paused = false;
155 }
156 }
157
158 class InputPanel extends JPanel
159 {
160 private class InputSender
161 implements ActionListener
162 {
163
164 public void actionPerformed(ActionEvent actionevent)
165 {
166 String s = actionevent.getActionCommand();
167 display.append(prompt.getText() + s + SimpleConsole.LINE_SEPARATOR);
168 inStream.receive(s);
169 input.setText(null);
170 }
171
172 private InputSender()
173 {
174 }
175
176 }
177
178
179 public void setPrompt(String s)
180 {
181 prompt.setText(s);
182 prompt.revalidate();
183 input.requestFocus();
184 }
185
186 private JLabel prompt;
187 private JTextField input;
188
189
190
191
192 public InputPanel()
193 {
194 setLayout(new BoxLayout(this, 0));
195 prompt = new JLabel();
196 prompt.setFont(new Font("Courier New", 0, 12));
197 prompt.setForeground(Color.black);
198 prompt.setBackground(Color.white);
199 prompt.setHorizontalAlignment(2);
200 prompt.setAlignmentY(0.5F);
201 input = new JTextField();
202 input.setFont(new Font("Courier New", 0, 12));
203 input.setForeground(Color.black);
204 input.setMinimumSize(new Dimension(50, 20));
205 input.setPreferredSize(new Dimension(200, 20));
206 input.setMaximumSize(new Dimension(32767, 20));
207 input.setHorizontalAlignment(2);
208 input.setAlignmentY(0.5F);
209 input.addActionListener(new InputSender());
210 JButton jbutton = new JButton("Enter");
211 jbutton.setFont(new Font("Fixedsys", 0, 10));
212 jbutton.setForeground(Color.black);
213 jbutton.setMinimumSize(new Dimension(70, 20));
214 jbutton.setPreferredSize(new Dimension(70, 20));
215 jbutton.setMaximumSize(new Dimension(70, 20));
216 jbutton.addActionListener(new InputSender());
217 jbutton.setAlignmentY(0.5F);
218 add(prompt);
219 add(input);
220 add(jbutton);
221 }
222 }
223
224
225 public SimpleConsole()
226 {
227 consoleDisplay = new ConsoleDisplayStream();
228 displayStream = new PrintStream(consoleDisplay);
229 promptStream = new PrintStream(new ConsolePromptStream());
230 inStream = new ConsoleInputStream();
231 setLayout(new BoxLayout(this, 1));
232 display = new JTextArea(40, 90);
233 display.setFont(new Font("Courier New", 0, 12));
234 display.setLineWrap(true);
235 display.setWrapStyleWord(true);
236 display.setTabSize(4);
237 display.setForeground(Color.black);
238 display.setEditable(false);
239 JScrollPane jscrollpane = new JScrollPane(display);
240 jscrollpane.setAlignmentY(0.5F);
241 inputPanel = new InputPanel();
242 add(jscrollpane);
243 setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
244 add(Box.createRigidArea(new Dimension(0, 5)));
245 add(inputPanel);
246 menuBar = new JMenuBar();
247 JMenu jmenu = new JMenu("File");
248 jmenu.setFont(new Font("Fixedsys", 0, 12));
249 jmenu.setMnemonic(70);
250 JMenuItem jmenuitem = new JMenuItem("Load AIML from URL...");
251 jmenuitem.setFont(new Font("Fixedsys", 0, 12));
252 jmenuitem.setMnemonic(85);
253 jmenuitem.addActionListener(new ActionListener() {
254
255 public void actionPerformed(ActionEvent actionevent)
256 {
257 loadAIMLURLBox();
258 }
259
260 });
261 JMenuItem jmenuitem1 = new JMenuItem("Load AIML from file path...");
262 jmenuitem1.setFont(new Font("Fixedsys", 0, 12));
263 jmenuitem1.setMnemonic(80);
264 jmenuitem1.addActionListener(new ActionListener() {
265
266 public void actionPerformed(ActionEvent actionevent)
267 {
268 loadAIMLFilePathChooser();
269 }
270
271 });
272 JMenuItem jmenuitem2 = new JMenuItem("Exit");
273 jmenuitem2.setFont(new Font("Fixedsys", 0, 12));
274 jmenuitem2.setMnemonic(88);
275 jmenuitem2.addActionListener(new ActionListener() {
276
277 public void actionPerformed(ActionEvent actionevent)
278 {
279 shutdown();
280 }
281
282 });
283 jmenu.add(jmenuitem);
284 jmenu.add(jmenuitem1);
285 jmenu.addSeparator();
286 jmenu.add(jmenuitem2);
287 JMenu jmenu1 = new JMenu("Actions");
288 jmenu1.setFont(new Font("Fixedsys", 0, 12));
289 jmenu1.setMnemonic(65);
290 JCheckBoxMenuItem jcheckboxmenuitem = new JCheckBoxMenuItem("Pause Console");
291 jcheckboxmenuitem.setFont(new Font("Fixedsys", 0, 12));
292 jcheckboxmenuitem.setMnemonic(80);
293 jcheckboxmenuitem.addActionListener(new ActionListener() {
294
295 public void actionPerformed(ActionEvent actionevent)
296 {
297 consoleDisplay.togglePause();
298 }
299
300 });
301 JMenuItem jmenuitem3 = new JMenuItem("Talk to bot...");
302 jmenuitem3.setFont(new Font("Fixedsys", 0, 12));
303 jmenuitem3.setMnemonic(66);
304 jmenuitem3.addActionListener(new ActionListener() {
305
306 public void actionPerformed(ActionEvent actionevent)
307 {
308 chooseBot();
309 }
310
311 });
312 JMenuItem jmenuitem4 = new JMenuItem("List bot files");
313 jmenuitem4.setFont(new Font("Fixedsys", 0, 12));
314 jmenuitem4.setMnemonic(70);
315 jmenuitem4.addActionListener(new ActionListener() {
316
317 public void actionPerformed(ActionEvent actionevent)
318 {
319 shell.listBotFiles();
320 }
321
322 });
323 JMenuItem jmenuitem5 = new JMenuItem("List bots");
324 jmenuitem5.setFont(new Font("Fixedsys", 0, 12));
325 jmenuitem5.setMnemonic(76);
326 jmenuitem5.addActionListener(new ActionListener() {
327
328 public void actionPerformed(ActionEvent actionevent)
329 {
330 shell.showBotList();
331 }
332
333 });
334 JMenuItem jmenuitem6 = new JMenuItem("Roll targets");
335 jmenuitem6.setFont(new Font("Fixedsys", 0, 12));
336 jmenuitem6.setMnemonic(84);
337 jmenuitem6.addActionListener(new ActionListener() {
338
339 public void actionPerformed(ActionEvent actionevent)
340 {
341 shell.rollTargets();
342 }
343
344 });
345 jmenu1.add(jcheckboxmenuitem);
346 jmenu1.addSeparator();
347 jmenu1.add(jmenuitem3);
348 jmenu1.add(jmenuitem5);
349 jmenu1.add(jmenuitem4);
350 jmenu1.addSeparator();
351 jmenu1.add(jmenuitem6);
352 JMenu jmenu2 = new JMenu("Help");
353 jmenu2.setFont(new Font("Fixedsys", 0, 12));
354 jmenu2.setMnemonic(72);
355 JMenuItem jmenuitem7 = new JMenuItem("Shell Help...");
356 jmenuitem7.setFont(new Font("Fixedsys", 0, 12));
357 jmenuitem7.setMnemonic(72);
358 jmenuitem7.addActionListener(new ActionListener() {
359
360 public void actionPerformed(ActionEvent actionevent)
361 {
362 shell.help();
363 }
364
365 });
366 JMenuItem jmenuitem8 = new JMenuItem("About Simple Console...");
367 jmenuitem8.setFont(new Font("Fixedsys", 0, 12));
368 jmenuitem8.setMnemonic(65);
369 jmenuitem8.addActionListener(new ActionListener() {
370
371 public void actionPerformed(ActionEvent actionevent)
372 {
373 showAboutBox();
374 }
375
376 });
377 jmenu2.add(jmenuitem8);
378 jmenu2.add(jmenuitem7);
379 menuBar.add(jmenu);
380 menuBar.add(jmenu1);
381 menuBar.add(jmenu2);
382 frame = new JFrame();
383 frame.setTitle("Charliebot Simple Console");
384 frame.getContentPane().add(this);
385 frame.setJMenuBar(menuBar);
386 frame.setDefaultCloseOperation(3);
387 frame.pack();
388 frame.setLocation(50, 50);
389 frame.setVisible(true);
390 }
391
392 public void start(String s)
393 {
394 Globals.load(s);
395 shell = new Shell(inStream, displayStream, promptStream);
396 server = new AliceServer(s, shell);
397 Trace.setOut(displayStream);
398 server.startup();
399 shutdown();
400 }
401
402 private void shutdown()
403 {
404 if(server != null)
405 {
406 SimpleConsole _tmp = this;
407 AliceServer.shutdown();
408 }
409 }
410
411 private void loadAIMLURLBox()
412 {
413 Object obj = JOptionPane.showInputDialog(null, "Enter the URL from which to load.", "Load AIML from URL", -1, null, null, null);
414 if(obj == null)
415 {
416 return;
417 } else
418 {
419 int i = Graphmaster.getTotalCategories();
420 Graphmaster.load((String)obj, shell.getCurrentBotID());
421 Log.userinfo((Graphmaster.getTotalCategories() - i) + " categories loaded from \"" + (String)obj + "\".", Log.LEARN);
422 return;
423 }
424 }
425
426 private void loadAIMLFilePathChooser()
427 {
428 JFileChooser jfilechooser = new JFileChooser();
429 jfilechooser.setDialogTitle("Choose AIML File");
430 int i = jfilechooser.showDialog(this, "Choose");
431 if(i == 0)
432 {
433 File file = jfilechooser.getSelectedFile();
434 String s = null;
435 try
436 {
437 s = file.getCanonicalPath();
438 }
439 catch(IOException ioexception)
440 {
441 Trace.userinfo("I/O error trying to access \"" + s + "\".");
442 return;
443 }
444 int j = Graphmaster.getTotalCategories();
445 Graphmaster.load(s, shell.getCurrentBotID());
446 Log.userinfo((Graphmaster.getTotalCategories() - j) + " categories loaded from \"" + s + "\".", Log.LEARN);
447 }
448 }
449
450 private void chooseBot()
451 {
452 String as[] = (String[])Bots.getIDs().toArray(new String[0]);
453 ListDialog.initialize(frame, as, "Choose a bot", "Choose the bot with whom you want to talk.");
454 String s = ListDialog.showDialog(null, shell.getCurrentBotID());
455 if(s != null)
456 shell.switchToBot(s);
457 }
458
459 private void showAboutBox()
460 {
461 JOptionPane.showMessageDialog(null, ((Object) (HELP_MESSAGE)), "About", 1, aliceLogo);
462 }
463
464 public static void main(String args[])
465 {
466 String s;
467 if(args.length > 0)
468 s = args[0];
469 else
470 s = "server.properties";
471 (new SimpleConsole()).start(s);
472 }
473
474 private AliceServer server;
475 private Shell shell;
476 private JTextArea display;
477 private InputPanel inputPanel;
478 private ConsoleDisplayStream consoleDisplay;
479 private JFrame frame;
480 private PrintStream displayStream;
481 private PrintStream promptStream;
482 private ConsoleInputStream inStream;
483 private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
484 private static final Object HELP_MESSAGE[] = {
485 "Simple Console for", "Charliebot version 4.1.8", "(c) A.L.I.C.E. AI Foundation (http://alicebot.org)"
486 };
487 private static JMenuBar menuBar;
488 private static final ImageIcon aliceLogo = new ImageIcon(ClassLoader.getSystemResource("org/alicebot/icons/aliceLogo.jpg"));
489 private static final ImageIcon aliceIcon = new ImageIcon(ClassLoader.getSystemResource("org/alicebot/icons/aliceIcon.jpg"));
490
491
492
493
494
495
496
497
498
499
500
501
502 }