Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/alicebot/server/core/targeting/gui/TargetPanel.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.server.core.targeting.gui;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   import java.util.Random;
10  import java.util.StringTokenizer;
11  import javax.swing.*;
12  import javax.swing.text.JTextComponent;
13  import org.alicebot.server.core.targeting.Target;
14  import org.alicebot.server.core.targeting.TargetingTool;
15  import org.alicebot.server.core.util.InputNormalizer;
16  import org.alicebot.server.core.util.Toolkit;
17  
18  // Referenced classes of package org.alicebot.server.core.targeting.gui:
19  //            TargetingGUI
20  
21  public class TargetPanel extends JPanel
22  {
23      class TemplateButtons extends JPanel
24      {
25  
26          private JButton random;
27          private JButton sr;
28          private JButton srai;
29          private JButton think;
30          private JButton reduce;
31          private JButton clear;
32  
33  
34          public TemplateButtons()
35          {
36              random = new JButton("<random>");
37              sr = new JButton("<sr/>");
38              srai = new JButton("<srai>");
39              think = new JButton("<think>");
40              reduce = new JButton("Reduce");
41              clear = new JButton("Clear");
42              setLayout(new BoxLayout(this, 1));
43              random.addActionListener(new ActionListener() {
44  
45                  public void actionPerformed(ActionEvent actionevent)
46                  {
47                      templatePane.setText("<random><li>" + templatePane.getText() + "</li><li></random>");
48                  }
49  
50              });
51              think.addActionListener(new ActionListener() {
52  
53                  public void actionPerformed(ActionEvent actionevent)
54                  {
55                      templatePane.setText("<think>" + templatePane.getText() + "</think>");
56                  }
57  
58              });
59              sr.addActionListener(new ActionListener() {
60  
61                  public void actionPerformed(ActionEvent actionevent)
62                  {
63                      templatePane.setText(templatePane.getText() + "<sr/>");
64                  }
65  
66              });
67              reduce.addActionListener(new ActionListener() {
68  
69                  public void actionPerformed(ActionEvent actionevent)
70                  {
71                      String s = targetBar.patternField.getText();
72                      StringTokenizer stringtokenizer = new StringTokenizer(s);
73                      int i = stringtokenizer.countTokens();
74                      String s1 = "";
75                      if(i > 2)
76                      {
77                          for(int j = 0; j < i - 2; j++)
78                              s1 = s1 + stringtokenizer.nextToken() + " ";
79  
80                          s1 = s1 + "<star/>";
81                          s1 = "<srai>" + s1 + "</srai>";
82                      } else
83                      {
84                          s1 = "<sr/>";
85                      }
86                      templatePane.setText(templatePane.getText() + s1);
87                  }
88  
89              });
90              srai.addActionListener(new ActionListener() {
91  
92                  public void actionPerformed(ActionEvent actionevent)
93                  {
94                      templatePane.setText("<srai>" + templatePane.getText() + "</srai>");
95                  }
96  
97              });
98              clear.addActionListener(new ActionListener() {
99  
100                 public void actionPerformed(ActionEvent actionevent)
101                 {
102                     templatePane.setText("");
103                 }
104 
105             });
106             think.setBackground(Color.orange);
107             think.setFont(new Font("Fixedsys", 0, 12));
108             think.setMinimumSize(new Dimension(120, 30));
109             think.setPreferredSize(new Dimension(120, 30));
110             think.setMaximumSize(new Dimension(120, 30));
111             think.setAlignmentY(0.5F);
112             think.setToolTipText("Encloses the current template contents in a <think> tag.");
113             random.setBackground(Color.orange);
114             random.setFont(new Font("Fixedsys", 0, 12));
115             random.setMinimumSize(new Dimension(120, 30));
116             random.setPreferredSize(new Dimension(120, 30));
117             random.setMaximumSize(new Dimension(120, 30));
118             random.setAlignmentY(0.5F);
119             random.setToolTipText("Encloses the current template contents in a <random> tag.");
120             sr.setBackground(Color.orange);
121             sr.setFont(new Font("Fixedsys", 0, 12));
122             sr.setMinimumSize(new Dimension(120, 30));
123             sr.setPreferredSize(new Dimension(120, 30));
124             sr.setMaximumSize(new Dimension(120, 30));
125             sr.setAlignmentY(0.5F);
126             srai.setBackground(Color.orange);
127             srai.setFont(new Font("Fixedsys", 0, 12));
128             srai.setMinimumSize(new Dimension(120, 30));
129             srai.setPreferredSize(new Dimension(120, 30));
130             srai.setMaximumSize(new Dimension(120, 30));
131             srai.setAlignmentY(0.5F);
132             srai.setToolTipText("Encloses the current template contents in a <srai> tag.");
133             reduce.setBackground(Color.orange);
134             reduce.setFont(new Font("Fixedsys", 0, 12));
135             reduce.setMinimumSize(new Dimension(120, 30));
136             reduce.setPreferredSize(new Dimension(120, 30));
137             reduce.setMaximumSize(new Dimension(120, 30));
138             reduce.setAlignmentY(0.5F);
139             clear.setBackground(Color.white);
140             clear.setFont(new Font("Fixedsys", 0, 12));
141             clear.setMinimumSize(new Dimension(120, 30));
142             clear.setPreferredSize(new Dimension(120, 30));
143             clear.setMaximumSize(new Dimension(120, 30));
144             clear.setAlignmentY(0.5F);
145             clear.setToolTipText("Clears the current template contents.");
146             add(think);
147             add(random);
148             add(sr);
149             add(srai);
150             add(reduce);
151             add(clear);
152         }
153     }
154 
155     class SaveTarget
156         implements ActionListener
157     {
158 
159         public void actionPerformed(ActionEvent actionevent)
160         {
161             saveTarget();
162         }
163 
164         SaveTarget()
165         {
166         }
167     }
168 
169     class DiscardAllTargets
170         implements ActionListener
171     {
172 
173         public void actionPerformed(ActionEvent actionevent)
174         {
175             TargetingTool.discardAll();
176             nextTarget();
177         }
178 
179         DiscardAllTargets()
180         {
181         }
182     }
183 
184     class DiscardTarget
185         implements ActionListener
186     {
187 
188         public void actionPerformed(ActionEvent actionevent)
189         {
190             TargetingTool.discard(selectedTarget);
191             nextTarget();
192         }
193 
194         DiscardTarget()
195         {
196         }
197     }
198 
199     class NextInput
200         implements AdjustmentListener
201     {
202 
203         public void adjustmentValueChanged(AdjustmentEvent adjustmentevent)
204         {
205             showInput(inputScroller.getValue());
206         }
207 
208         NextInput()
209         {
210         }
211     }
212 
213     class NextTarget
214         implements ActionListener
215     {
216 
217         public void actionPerformed(ActionEvent actionevent)
218         {
219             nextTarget();
220         }
221 
222         NextTarget()
223         {
224         }
225     }
226 
227     class TargetBar extends CategoryBar
228     {
229 
230         public TargetBar()
231         {
232             super("New category:", "<pattern>", "<that>", "<topic>", 56);
233             patternField.setToolTipText("Suggestion for a new <pattern>");
234             thatField.setToolTipText("Suggestion for a new <that>");
235             topicField.setToolTipText("Suggestion for a new <topic>");
236             setEditable(true);
237         }
238     }
239 
240     class MatchedBar extends CategoryBar
241     {
242 
243         public MatchedBar()
244         {
245             super("Matched:", "<pattern>", "<that>", "<topic>", 56);
246             patternField.setToolTipText("The <pattern> that was matched");
247             thatField.setToolTipText("The <that> that was matched");
248             topicField.setToolTipText("The <topic> that was matched");
249             setEditable(false);
250         }
251     }
252 
253     class InputBar extends CategoryBar
254     {
255 
256         public InputBar()
257         {
258             super("Input:", "text", "<that>", "<topic>", 56);
259             patternField.setToolTipText("What the user said");
260             thatField.setToolTipText("What the bot had said previously");
261             topicField.setToolTipText("The value of <topic> at the time");
262             setEditable(false);
263         }
264     }
265 
266     class CategoryBar extends JPanel
267     {
268         public class PatternFitter
269             implements KeyListener
270         {
271 
272             public void keyTyped(KeyEvent keyevent)
273             {
274                 char c = keyevent.getKeyChar();
275                 if(c == '*' || c == '_')
276                 {
277                     int i = field.getCaretPosition();
278                     if(i > 0)
279                     {
280                         char c1 = field.getText().charAt(i - 1);
281                         if(c1 != ' ')
282                             field.setText(field.getText() + ' ');
283                     }
284                     return;
285                 }
286                 if(!Character.isLetterOrDigit(c) && c != ' ')
287                     keyevent.consume();
288                 if(!Character.isUpperCase(c))
289                     keyevent.setKeyChar(Character.toUpperCase(c));
290                 int j = field.getCaretPosition();
291                 if(j > 0)
292                 {
293                     char c2 = field.getText().charAt(j - 1);
294                     if(c2 == '*' || c2 == '_')
295                         field.setText(field.getText() + ' ');
296                 }
297             }
298 
299             public void keyPressed(KeyEvent keyevent)
300             {
301             }
302 
303             public void keyReleased(KeyEvent keyevent)
304             {
305             }
306 
307             private JTextComponent field;
308 
309             public PatternFitter(JTextComponent jtextcomponent)
310             {
311                 field = jtextcomponent;
312             }
313         }
314 
315 
316         public void setEditable(boolean flag)
317         {
318             patternField.setEditable(flag);
319             thatField.setEditable(flag);
320             topicField.setEditable(flag);
321         }
322 
323         public void setFields(String s, String s1, String s2)
324         {
325             patternField.setText(s);
326             patternField.setCaretPosition(0);
327             thatField.setText(s1);
328             thatField.setCaretPosition(0);
329             topicField.setText(s2);
330             topicField.setCaretPosition(0);
331         }
332 
333         JTextArea patternField;
334         JTextArea thatField;
335         JTextArea topicField;
336 
337         public CategoryBar(String s, String s1, String s2, String s3, int i)
338         {
339             setLayout(new BoxLayout(this, 0));
340             JLabel jlabel = new JLabel(s);
341             jlabel.setMinimumSize(new Dimension(80, i));
342             jlabel.setPreferredSize(new Dimension(80, i));
343             jlabel.setMaximumSize(new Dimension(80, i));
344             jlabel.setHorizontalAlignment(2);
345             jlabel.setFont(new Font("Fixedsys", 0, 12));
346             jlabel.setForeground(Color.black);
347             jlabel.setAlignmentY(0.5F);
348             patternField = new JTextArea();
349             patternField.setLineWrap(true);
350             patternField.setWrapStyleWord(true);
351             patternField.setFont(new Font("Courier New", 0, 12));
352             patternField.addKeyListener(new PatternFitter(patternField));
353             JScrollPane jscrollpane = new JScrollPane(patternField);
354             jscrollpane.setBorder(BorderFactory.createTitledBorder(s1));
355             jscrollpane.setMinimumSize(new Dimension(200, i));
356             jscrollpane.setPreferredSize(new Dimension(200, i));
357             jscrollpane.setMaximumSize(new Dimension(32767, i));
358             jscrollpane.setAlignmentY(0.5F);
359             thatField = new JTextArea();
360             thatField.setFont(new Font("Courier New", 0, 12));
361             thatField.setLineWrap(true);
362             thatField.setWrapStyleWord(true);
363             thatField.addKeyListener(new PatternFitter(thatField));
364             JScrollPane jscrollpane1 = new JScrollPane(thatField);
365             jscrollpane1.setMinimumSize(new Dimension(150, i));
366             jscrollpane1.setPreferredSize(new Dimension(150, i));
367             jscrollpane1.setMaximumSize(new Dimension(32767, i));
368             jscrollpane1.setBorder(BorderFactory.createTitledBorder(s2));
369             jscrollpane1.setAlignmentY(0.5F);
370             topicField = new JTextArea();
371             topicField.setFont(new Font("Courier New", 0, 12));
372             topicField.setLineWrap(true);
373             topicField.setWrapStyleWord(true);
374             topicField.addKeyListener(new PatternFitter(topicField));
375             JScrollPane jscrollpane2 = new JScrollPane(topicField);
376             jscrollpane2.setMinimumSize(new Dimension(150, i));
377             jscrollpane2.setPreferredSize(new Dimension(150, i));
378             jscrollpane2.setMaximumSize(new Dimension(32767, i));
379             jscrollpane2.setBorder(BorderFactory.createTitledBorder(s3));
380             jscrollpane2.setAlignmentY(0.5F);
381             add(jlabel);
382             add(jscrollpane);
383             add(jscrollpane1);
384             add(jscrollpane2);
385         }
386     }
387 
388     class ActionButtonsBar extends JPanel
389     {
390 
391         public ActionButtonsBar()
392         {
393             setLayout(new BoxLayout(this, 0));
394             countField = new JLabel();
395             countField.setFont(new Font("Fixedsys", 0, 12));
396             countField.setForeground(Color.black);
397             countField.setHorizontalAlignment(2);
398             countField.setAlignmentX(1.0F);
399             JButton jbutton = new JButton("Discard Target");
400             jbutton.setFont(new Font("Fixedsys", 0, 12));
401             jbutton.setBackground(Color.red);
402             jbutton.setForeground(Color.white);
403             jbutton.addActionListener(new DiscardTarget());
404             jbutton.setAlignmentX(1.0F);
405             jbutton.setToolTipText("Discards the current target.");
406             JButton jbutton1 = new JButton("Discard All Targets");
407             jbutton1.setFont(new Font("Fixedsys", 1, 12));
408             jbutton1.setBackground(Color.red);
409             jbutton1.setForeground(Color.white);
410             jbutton1.addActionListener(new DiscardAllTargets());
411             jbutton1.setAlignmentX(1.0F);
412             jbutton1.setToolTipText("Discards all targets.");
413             JButton jbutton2 = new JButton("Save Category");
414             jbutton2.setFont(new Font("Fixedsys", 0, 12));
415             jbutton2.setBackground(Color.green);
416             jbutton2.setForeground(Color.black);
417             jbutton2.addActionListener(new SaveTarget());
418             jbutton2.setAlignmentX(1.0F);
419             jbutton2.setToolTipText("Saves a new category using the information you have entered.");
420             JButton jbutton3 = new JButton("Next Target");
421             jbutton3.setFont(new Font("Fixedsys", 0, 12));
422             jbutton3.setBackground(Color.yellow);
423             jbutton3.setForeground(Color.black);
424             jbutton3.addActionListener(new NextTarget());
425             jbutton3.setAlignmentX(1.0F);
426             jbutton2.setToolTipText("Gets the next live target.");
427             Dimension dimension = new Dimension(50, 30);
428             Dimension dimension1 = new Dimension(50, 30);
429             Dimension dimension2 = new Dimension(32767, 30);
430             add(countField);
431             add(new javax.swing.Box.Filler(dimension, dimension1, dimension2));
432             add(jbutton);
433             add(jbutton1);
434             add(jbutton2);
435             add(jbutton3);
436         }
437     }
438 
439     class AIMLTextPane extends JPanel
440     {
441 
442         public void setText(String s)
443         {
444             textArea.setText(Toolkit.formatAIML(s));
445             textArea.setCaretPosition(0);
446         }
447 
448         public String getText()
449         {
450             return textArea.getText();
451         }
452 
453         JTextArea textArea;
454 
455         public AIMLTextPane(String s)
456         {
457             setLayout(new BoxLayout(this, 0));
458             textArea = new JTextArea();
459             textArea.setFont(new Font("Courier New", 0, 12));
460             textArea.setLineWrap(true);
461             textArea.setWrapStyleWord(true);
462             textArea.setTabSize(4);
463             JScrollPane jscrollpane = new JScrollPane(textArea);
464             jscrollpane.setAlignmentY(0.5F);
465             jscrollpane.setBorder(BorderFactory.createTitledBorder(s));
466             add(jscrollpane);
467         }
468     }
469 
470     public class TemplateAndReplyPanel extends JPanel
471     {
472 
473         public TemplateAndReplyPanel()
474         {
475             setLayout(new BoxLayout(this, 0));
476             setPreferredSize(new Dimension(300, 300));
477             TemplateButtons templatebuttons = new TemplateButtons();
478             templatebuttons.setAlignmentY(0.0F);
479             templatePane = new AIMLTextPane("<template>");
480             replyPane = new AIMLTextPane("reply");
481             JTabbedPane jtabbedpane = new JTabbedPane();
482             jtabbedpane.setAlignmentY(0.0F);
483             jtabbedpane.setFont(new Font("Fixedsys", 0, 12));
484             jtabbedpane.setTabPlacement(3);
485             jtabbedpane.add("reply", replyPane);
486             jtabbedpane.add("template", templatePane);
487             add(templatebuttons);
488             add(Box.createRigidArea(new Dimension(10, 0)));
489             add(jtabbedpane);
490         }
491     }
492 
493     public class PatternsPanel extends JPanel
494     {
495 
496         public PatternsPanel()
497         {
498             setLayout(new BoxLayout(this, 1));
499             setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
500             activationsField = new JLabel();
501             activationsField.setFont(new Font("Fixedsys", 0, 12));
502             activationsField.setForeground(Color.black);
503             activationsField.setHorizontalAlignment(2);
504             activationsField.setAlignmentX(0.0F);
505             activationsField.setMinimumSize(new Dimension(200, 20));
506             activationsField.setPreferredSize(new Dimension(200, 20));
507             activationsField.setMaximumSize(new Dimension(200, 20));
508             inputScroller = new JScrollBar(0, 0, 0, 0, 0);
509             inputScroller.setAlignmentX(0.0F);
510             inputScroller.setBackground(Color.gray);
511             inputScroller.addAdjustmentListener(new NextInput());
512             inputBar = new InputBar();
513             inputBar.setAlignmentX(0.0F);
514             matchedBar = new MatchedBar();
515             matchedBar.setAlignmentX(0.0F);
516             targetBar = new TargetBar();
517             targetBar.setAlignmentX(0.0F);
518             add(Box.createRigidArea(new Dimension(0, 5)));
519             add(activationsField);
520             add(inputScroller);
521             add(inputBar);
522             add(Box.createRigidArea(new Dimension(0, 5)));
523             add(matchedBar);
524             add(Box.createRigidArea(new Dimension(0, 5)));
525             add(targetBar);
526         }
527     }
528 
529 
530     public TargetPanel(TargetingGUI targetinggui)
531     {
532         selectedTarget = null;
533         guiparent = targetinggui;
534         setLayout(new BoxLayout(this, 1));
535         setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
536         PatternsPanel patternspanel = new PatternsPanel();
537         patternspanel.setAlignmentX(0.0F);
538         TemplateAndReplyPanel templateandreplypanel = new TemplateAndReplyPanel();
539         templateandreplypanel.setAlignmentX(0.0F);
540         actionButtonsBar = new ActionButtonsBar();
541         actionButtonsBar.setAlignmentX(0.0F);
542         add(patternspanel);
543         add(Box.createRigidArea(new Dimension(0, 10)));
544         add(templateandreplypanel);
545         add(Box.createRigidArea(new Dimension(0, 10)));
546         add(actionButtonsBar);
547     }
548 
549     public void setTarget(Target target)
550     {
551         selectedTarget = target;
552         int i = target.getActivations();
553         activationsField.setText(i + " activations");
554         inputBar.setFields(target.getFirstInputText(), target.getFirstInputThat(), target.getFirstInputTopic());
555         inputScroller.setMinimum(1);
556         inputScroller.setMaximum(i);
557         inputScroller.setValue(1);
558         showInput(1);
559         matchedBar.setFields(target.getMatchPattern(), target.getMatchThat(), target.getMatchTopic());
560         targetBar.setFields(target.getFirstExtensionPattern(), target.getFirstExtensionThat(), target.getFirstExtensionTopic());
561         replyPane.setText(target.getFirstReply());
562         templatePane.setText(target.getMatchTemplate());
563     }
564 
565     public void nextTarget()
566     {
567         Target target = TargetingTool.nextTarget();
568         if(target != null)
569         {
570             setTarget(target);
571             hasTarget = true;
572         } else
573         {
574             inputBar.setFields("", "", "");
575             inputScroller.setMinimum(0);
576             inputScroller.setMaximum(0);
577             inputScroller.setValue(0);
578             matchedBar.setFields("", "", "");
579             targetBar.setFields("", "", "");
580             templatePane.setText("");
581             guiparent.setStatus("No more targets meet your selection criteria.");
582             hasTarget = false;
583         }
584         updateCountDisplay();
585     }
586 
587     private void showInput(int i)
588     {
589         if(i > 0 && selectedTarget != null)
590         {
591             inputBar.setFields(selectedTarget.getNthInputText(i - 1), selectedTarget.getNthInputThat(i - 1), selectedTarget.getNthInputTopic(i - 1));
592             targetBar.setFields(selectedTarget.getNthExtensionPattern(i - 1), selectedTarget.getNthExtensionThat(i - 1), selectedTarget.getNthExtensionTopic(i - 1));
593             replyPane.setText(selectedTarget.getNthReply(i - 1));
594         }
595     }
596 
597     public void scrollToInput(int i)
598     {
599         inputScroller.setValue(i);
600         showInput(i);
601     }
602 
603     public boolean hasTarget()
604     {
605         return hasTarget;
606     }
607 
608     public void updateCountDisplay()
609     {
610         countField.setText(TargetingTool.countLive() + " live, " + TargetingTool.countSaved() + " saved, " + TargetingTool.countDiscarded() + " discarded");
611     }
612 
613     public void saveTarget()
614     {
615         String s = templatePane.getText().trim();
616         if(s.length() == 0)
617         {
618             templatePane.setText("Template is empty!");
619             return;
620         }
621         if(selectedTarget != null)
622         {
623             selectedTarget.setNewPattern(InputNormalizer.patternFit(targetBar.patternField.getText()));
624             selectedTarget.setNewThat(InputNormalizer.patternFit(targetBar.thatField.getText()));
625             selectedTarget.setNewTopic(InputNormalizer.patternFit(targetBar.topicField.getText()));
626             selectedTarget.setNewTemplate(s);
627             TargetingTool.saveCategory(selectedTarget);
628             nextTarget();
629         }
630     }
631 
632     private Target selectedTarget;
633     JLabel activationsField;
634     JLabel countField;
635     public static Random RNG = new Random();
636     TargetingGUI guiparent;
637     InputBar inputBar;
638     JScrollBar inputScroller;
639     MatchedBar matchedBar;
640     TargetBar targetBar;
641     AIMLTextPane templatePane;
642     AIMLTextPane replyPane;
643     ActionButtonsBar actionButtonsBar;
644     private boolean hasTarget;
645 
646 
647 
648 }