Source code: com/memoire/bu/BuAssistant.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:24 $
3 * @statut unstable
4 * @file BuAssistant.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.bu;
13
14 import com.memoire.bu.*;
15 import com.memoire.fu.*;
16
17 import javax.swing.*;
18 import javax.swing.event.*;
19 import javax.swing.border.*;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import java.lang.reflect.*;
25 import java.io.*;
26 import java.util.*;
27
28 /**
29 * An always-happy assistant.
30 */
31
32 public class BuAssistant
33 extends JComponent
34 implements ActionListener, FocusListener,
35 InternalFrameListener, MouseListener
36 // , KeyListener
37 {
38 public static final int PAROLE =0;
39 public static final int ATTENTE =1;
40 public static final int PEUR =2;
41 public static final int COLERE =3;
42 public static final int ABSENT =4;
43 public static final int MAX_ATTITUDES=5;
44
45 private static final int DELAI =100;
46 private static final int AINC = 20;
47 private static final int NBCOL = 6;
48
49 private Hashtable table_=new Hashtable();
50
51 protected void init()
52 {
53 // temporaire
54 loadTips("assistance.txt");
55
56 String l=Locale.getDefault().getLanguage();
57 if(!"fr".equals(l)) loadTips("assistance_"+l+".txt");
58 }
59
60 protected void loadTips(String _fichier)
61 {
62 loadTips(getClass(),_fichier);
63 }
64
65 private void loadTips(Class _classe, String _fichier)
66 {
67 if((_classe!=null)&&(_classe!=BuAssistant.class))
68 loadTips(_classe.getSuperclass(),_fichier);
69
70 try
71 {
72 LineNumberReader rin=
73 new LineNumberReader
74 (new InputStreamReader
75 (new BufferedInputStream
76 (_classe.getResourceAsStream(_fichier))));
77
78 // System.err.println("$$$ "+getClass()+"."+_fichier);
79
80 String k,l,v;
81
82 k=null;
83 v=null;
84
85 while(rin.ready())
86 {
87 l=rin.readLine();
88 if(l==null) break;
89
90 // System.err.println(">>> "+l);
91 if(l.equals("")&&(k!=null)&&(v!=null))
92 {
93 // System.err.println(k+"\n"+v);
94 table_.put(k,v);
95 k=null;
96 v=null;
97 }
98 else if(l.startsWith("#")) ;
99 else if(l.equals("")) ;
100 else if(k==null) k=l;
101 else if(v==null) v=l;
102 else v+="\n"+l;
103 }
104
105 if((k!=null)&&(v!=null))
106 {
107 // System.err.println(k+"\n"+v);
108 table_.put(k,v);
109 }
110 }
111 catch(Exception ex)
112 {
113 //System.err.println(_fichier+" not found for "+_classe.getName());
114 }
115
116 /*
117 table_.put("MESSAGE",
118 "Cette fenêtre contient\n"
119 +"tous les messages\n"
120 +"renvoyés par les analyseurs\n"
121 +"et les générateurs.");
122 table_.put("CONFIGURER",
123 "Cette fenêtre vous permet\n"
124 +"de configurer les options\n"
125 +"d'analyse et de génération.");
126 */
127 }
128
129 protected int aval_;
130 protected Blinker blinker_;
131 protected Thread festival_;
132
133 class Point
134 {
135 int x_,y_,r_,v_,b_;
136
137 Point(int _x, int _y, Color _c)
138 {
139 x_=_x;
140 y_=_y;
141 r_=_c.getRed();
142 v_=_c.getGreen();
143 b_=_c.getBlue();
144 }
145
146 Point(int _x, int _y, int _r, int _v, int _b)
147 {
148 x_=_x;
149 y_=_y;
150 r_=_r;
151 v_=_v;
152 b_=_b;
153 }
154
155 Point(int _a, Point _p1, Point _p2)
156 {
157 x_=_p1.x_+(_p2.x_-_p1.x_)*_a/100;
158 y_=_p1.y_+(_p2.y_-_p1.y_)*_a/100;
159 r_=_p1.r_+(_p2.r_-_p1.r_)*_a/100;
160 v_=_p1.v_+(_p2.v_-_p1.v_)*_a/100;
161 b_=_p1.b_+(_p2.b_-_p1.b_)*_a/100;
162 }
163 }
164
165 class Triangle
166 {
167 Point[] p_;
168
169 Triangle(Point _p0, Point _p1, Point _p2)
170 {
171 p_=new Point[3];
172 p_[0]=_p0;
173 p_[1]=_p1;
174 p_[2]=_p2;
175 }
176
177 Triangle(int _a, Triangle _t1, Triangle _t2)
178 {
179 p_=new Point[3];
180
181 for(int i=0;i<3;i++)
182 p_[i]=new Point(_a,_t1.p_[i],_t2.p_[i]);
183 }
184
185 void draw(Graphics _g)
186 {
187 int[] x=new int[] { p_[0].x_, p_[1].x_, p_[2].x_ };
188 int[] y=new int[] { p_[0].y_, p_[1].y_, p_[2].y_ };
189 int r=(p_[0].r_+p_[1].r_+p_[2].r_)/3;
190 int v=(p_[0].v_+p_[1].v_+p_[2].v_)/3;
191 int b=(p_[0].b_+p_[1].b_+p_[2].b_)/3;
192
193
194 BuLib.setColor(_g,new Color(r,v,b));
195 _g.fillPolygon(x,y,3);
196 }
197 }
198
199 class Disque
200 {
201 Point p_;
202 int r_;
203
204 Disque(Point _p, int _r)
205 {
206 p_=_p;
207 r_=_r;
208 }
209
210 Disque(int _a, Disque _d1, Disque _d2)
211 {
212 p_=new Point(_a,_d1.p_,_d2.p_);
213 r_=_d1.r_+(_d2.r_-_d1.r_)*_a/100;
214 }
215
216 void draw(Graphics _g)
217 {
218 int r,v,b;
219
220 for(int i=NBCOL;i>0;i--)
221 {
222 r=p_.r_-64+64*i/NBCOL;
223 v=p_.v_-64+64*i/NBCOL;
224 b=p_.b_-64+64*i/NBCOL;
225
226 if(r<0) r=0; if(r>255) r=255;
227 if(v<0) v=0; if(v>255) v=255;
228 if(b<0) b=0; if(b>255) b=255;
229
230 BuLib.setColor(_g,new Color(r,v,b));
231 _g.fillOval(p_.x_-r_*i/NBCOL,p_.y_-r_*i/NBCOL,2*r_*i/NBCOL,2*r_*i/NBCOL);
232 }
233 }
234 }
235
236 class Face
237 {
238 Disque tete_;
239 Triangle bouche_;
240 Disque oeilGauche_, oeilDroit_;
241 Disque pupilleGauche_, pupilleDroit_;
242 Triangle sourcilGauche_, sourcilDroit_;
243
244 Face()
245 {
246 }
247
248 Face(int _a, Face _f1, Face _f2)
249 {
250 tete_ =new Disque (_a,_f1.tete_,_f2.tete_);
251 bouche_ =new Triangle(_a,_f1.bouche_,_f2.bouche_);
252 oeilDroit_ =new Disque (_a,_f1.oeilDroit_,_f2.oeilDroit_);
253 oeilGauche_ =new Disque (_a,_f1.oeilGauche_,_f2.oeilGauche_);
254 pupilleDroit_ =new Disque (_a,_f1.pupilleDroit_,_f2.pupilleDroit_);
255 pupilleGauche_=new Disque (_a,_f1.pupilleGauche_,_f2.pupilleGauche_);
256 sourcilDroit_ =new Triangle(_a,_f1.sourcilDroit_,_f2.sourcilDroit_);
257 sourcilGauche_=new Triangle(_a,_f1.sourcilGauche_,_f2.sourcilGauche_);
258 }
259
260 void draw(Graphics _g)
261 {
262 tete_.draw(_g);
263 bouche_.draw(_g);
264 oeilGauche_.draw(_g);
265 oeilDroit_.draw(_g);
266 pupilleGauche_.draw(_g);
267 pupilleDroit_.draw(_g);
268 sourcilGauche_.draw(_g);
269 sourcilDroit_.draw(_g);
270 }
271 }
272
273 Face faceActuelle_,faceAvant_,faceApres_;
274 Face[] faces_=new Face[MAX_ATTITUDES];
275
276 public BuAssistant()
277 {
278 super();
279 init();
280
281 // F0
282
283 {
284 faces_[0]=new Face();
285
286 Point p00=new Point( 50, 50,255,192,192);
287 faces_[0].tete_ =new Disque(p00,45);
288
289 Point p01=new Point( 25, 65,255,120,120);
290 Point p02=new Point( 75, 60,255,120,120);
291 Point p03=new Point( 52, 80,192, 30, 30);
292 faces_[0].bouche_ =new Triangle(p01,p02,p03);
293
294 Point p04=new Point( 15, 25,128,128,128);
295 Point p05=new Point( 40, 15, 80, 80, 80);
296 Point p06=new Point( 45, 20,100,100,100);
297 faces_[0].sourcilGauche_=new Triangle(p04,p05,p06);
298
299 Point p07=new Point( 85, 25,128,128,128);
300 Point p08=new Point( 60, 15, 80, 80, 80);
301 Point p09=new Point( 55, 20,100,100,100);
302 faces_[0].sourcilDroit_ =new Triangle(p07,p08,p09);
303
304 Point p10=new Point( 35, 40,255,255,255);
305 faces_[0].oeilGauche_ =new Disque(p10,12);
306
307 Point p11=new Point( 65, 40,255,255,255);
308 faces_[0].oeilDroit_ =new Disque(p11,12);
309
310 Point p12=new Point( 35, 35,128, 64, 0);
311 faces_[0].pupilleGauche_=new Disque(p12,7);
312
313 Point p13=new Point( 65, 35,128, 64, 0);
314 faces_[0].pupilleDroit_ =new Disque(p13,7);
315 }
316
317 // F1
318
319 {
320 faces_[1]=new Face();
321
322 Point p00=new Point( 50, 50,255,198,198);
323 faces_[1].tete_ =new Disque(p00,42);
324
325 Point p01=new Point( 25, 65,255,120,120);
326 Point p02=new Point( 75, 65,255,120,120);
327 Point p03=new Point( 50, 75,192, 30, 30);
328 faces_[1].bouche_ =new Triangle(p01,p02,p03);
329
330 Point p04=new Point( 15, 20,128,128,128);
331 Point p05=new Point( 35, 10, 80, 80, 80);
332 Point p06=new Point( 40, 15,100,100,100);
333 faces_[1].sourcilGauche_=new Triangle(p04,p05,p06);
334
335 Point p07=new Point( 85, 20,128,128,128);
336 Point p08=new Point( 65, 10, 80, 80, 80);
337 Point p09=new Point( 60, 15,100,100,100);
338 faces_[1].sourcilDroit_ =new Triangle(p07,p08,p09);
339
340 Point p10=new Point( 36, 41,255,255,255);
341 faces_[1].oeilGauche_ =new Disque(p10,11);
342
343 Point p11=new Point( 66, 41,255,255,255);
344 faces_[1].oeilDroit_ =new Disque(p11,11);
345
346 Point p12=new Point( 37, 40,128, 64, 0);
347 faces_[1].pupilleGauche_=new Disque(p12,7);
348
349 Point p13=new Point( 67, 40,128, 64, 0);
350 faces_[1].pupilleDroit_ =new Disque(p13,7);
351 }
352
353 // F2
354
355 {
356 faces_[2]=new Face();
357
358 Point p00=new Point( 50, 50,255, 64, 64);
359 faces_[2].tete_ =new Disque(p00,34);
360
361 Point p01=new Point( 35, 60,128, 80, 80);
362 Point p02=new Point( 65, 60,128, 80, 80);
363 Point p03=new Point( 57, 65, 64, 50, 50);
364 faces_[2].bouche_ =new Triangle(p01,p02,p03);
365
366 Point p04=new Point( 25, 20,128,128,128);
367 Point p05=new Point( 40, 25, 80, 80, 80);
368 Point p06=new Point( 45, 35,100,100,100);
369 faces_[2].sourcilGauche_=new Triangle(p04,p05,p06);
370
371 Point p07=new Point( 75, 20,128,128,128);
372 Point p08=new Point( 60, 25, 80, 80, 80);
373 Point p09=new Point( 55, 35,100,100,100);
374 faces_[2].sourcilDroit_ =new Triangle(p07,p08,p09);
375
376 Point p10=new Point( 45, 44,255,255,255);
377 faces_[2].oeilGauche_ =new Disque(p10,11);
378
379 Point p11=new Point( 55, 44,255,255,255);
380 faces_[2].oeilDroit_ =new Disque(p11,11);
381
382 Point p12=new Point( 45, 45,128, 64, 0);
383 faces_[2].pupilleGauche_=new Disque(p12,5);
384
385 Point p13=new Point( 55, 45,128, 64, 0);
386 faces_[2].pupilleDroit_ =new Disque(p13,5);
387 }
388
389 // F3
390
391 {
392 faces_[3]=new Face();
393
394 Point p00=new Point( 50, 50,128,192, 64);
395 faces_[3].tete_ =new Disque(p00,54);
396
397 Point p01=new Point( 35, 70,128, 80, 80);
398 Point p02=new Point( 65, 70,128, 80, 80);
399 Point p03=new Point( 50, 90, 64, 50, 50);
400 faces_[3].bouche_ =new Triangle(p01,p02,p03);
401
402 Point p04=new Point( 25, 20,128,128,128);
403 Point p05=new Point( 40, 20, 80, 80, 80);
404 Point p06=new Point( 45, 25,100,100,100);
405 faces_[3].sourcilGauche_=new Triangle(p04,p05,p06);
406
407 Point p07=new Point( 75, 20,128,128,128);
408 Point p08=new Point( 60, 20, 80, 80, 80);
409 Point p09=new Point( 55, 25,100,100,100);
410 faces_[3].sourcilDroit_ =new Triangle(p07,p08,p09);
411
412 Point p10=new Point( 37, 44,255,255,255);
413 faces_[3].oeilGauche_ =new Disque(p10,16);
414
415 Point p11=new Point( 62, 44,255,255,255);
416 faces_[3].oeilDroit_ =new Disque(p11,16);
417
418 Point p12=new Point( 37, 45,128, 64, 0);
419 faces_[3].pupilleGauche_=new Disque(p12,12);
420
421 Point p13=new Point( 62, 45,128, 64, 0);
422 faces_[3].pupilleDroit_ =new Disque(p13,12);
423 }
424
425 // F4
426
427 {
428 faces_[4]=new Face();
429
430 Point p00=new Point( 0, 0,255,224,224);
431 faces_[4].tete_ =new Disque(p00,0);
432
433 Point p01=new Point( 0, 0,255,152,152);
434 Point p02=new Point( 0, 0,255,152,152);
435 Point p03=new Point( 0, 0,224, 62, 62);
436 faces_[4].bouche_ =new Triangle(p01,p02,p03);
437
438 Point p04=new Point( 0, 0,160,160,160);
439 Point p05=new Point( 0, 0,112,112,112);
440 Point p06=new Point( 0, 0,132,132,132);
441 faces_[4].sourcilGauche_=new Triangle(p04,p05,p06);
442
443 Point p07=new Point( 0, 0,160,160,160);
444 Point p08=new Point( 0, 0,112,112,112);
445 Point p09=new Point( 0, 0,132,132,132);
446 faces_[4].sourcilDroit_ =new Triangle(p07,p08,p09);
447
448 Point p10=new Point( 0, 0,255,255,255);
449 faces_[4].oeilGauche_ =new Disque(p10,0);
450
451 Point p11=new Point( 0, 0,255,255,255);
452 faces_[4].oeilDroit_ =new Disque(p11,0);
453
454 Point p12=new Point( 0, 0,160, 96, 0);
455 faces_[4].pupilleGauche_=new Disque(p12,0);
456
457 Point p13=new Point( 0, 0,160, 96, 0);
458 faces_[4].pupilleDroit_ =new Disque(p13,0);
459 }
460
461 face_ =PAROLE;
462 message_=new String[]
463 { BuResource.BU.getString("Bonjour")+" !" };
464
465 aval_=100;
466 blinker_=null;
467
468 faceActuelle_=faces_[face_];
469 faceAvant_ =faces_[face_];
470 faceApres_ =faces_[face_];
471
472 setName("buASSISTANT");
473 updateUI();
474 //setFont(BuLib.deriveFont("Label",Font.PLAIN,-2));
475 setOpaque(false);
476 //setDoubleBuffered(false);
477
478 Dimension ps=new Dimension(150,120);
479 if(BuPreferences.BU.getBooleanProperty("assistant.speech",false))
480 { ps.width=100; ps.height=100; }
481 setPreferredSize(ps);
482 setSize(ps);
483
484 addMouseListener(this);
485 }
486
487 public void updateUI()
488 {
489 super.updateUI();
490
491 Font ft=BuLib.deriveFont("Label",Font.PLAIN,-2);
492 if("dialog.bold".equals(ft.getFamily()))
493 ft=new Font("SansSerif",Font.PLAIN,ft.getSize());
494 setFont(ft);
495 }
496
497 public void mouseClicked(MouseEvent _evt)
498 {
499 }
500
501 public void mouseEntered(MouseEvent _evt)
502 {
503 JComponent source=(JComponent)_evt.getSource();
504
505 if(source==this)
506 {
507 changeAttitude
508 (PAROLE,BuResource.BU.getString("Que puis-je pour vous ?"));
509 }
510 else
511 {
512 String info=source.getName();
513 if(info==null) info="";
514 if(!"".equals(info))
515 commentUser(info+"_ENTER",getDefaultText(source));
516 }
517 }
518
519 public void mouseExited(MouseEvent _evt)
520 {
521 JComponent source=(JComponent)_evt.getSource();
522
523 if(source==this)
524 changeAttitude(ATTENTE,"");
525 }
526
527 public void mousePressed(MouseEvent _evt)
528 {
529 JComponent source=(JComponent)_evt.getSource();
530
531 if(source==this)
532 changeAttitude(COLERE,"Aie !");
533 }
534
535 public void mouseReleased(MouseEvent _evt)
536 {
537 JComponent source=(JComponent)_evt.getSource();
538
539 if(source==this)
540 changeAttitude(ATTENTE,"");
541 }
542
543 // Anti-aliasing
544
545 public void paint(Graphics _g)
546 {
547 BuLib.setAntialiasing(_g);
548 super.paint(_g);
549 }
550
551 // Paint
552
553 public synchronized void paintComponent(Graphics _g)
554 {
555 // catch temporaire.
556 try { super.paintComponent(_g); }
557 catch(NullPointerException ex) { }
558
559 Dimension d =getSize();
560 Insets insets=getInsets();
561 Rectangle clip =_g.getClipBounds();
562
563 /**** BG
564 {
565 int x,xmax,lmin;
566
567 xmax=d.width+d.height;
568 lmin=(int)System.currentTimeMillis()/250%128;
569 for(x=0;x<xmax;x++)
570 {
571 int l=lmin+255*x/xmax;
572 if(l>255) l=255;
573 g.setColor(new Color(l,l,l));
574 g.drawLine(x,0,x-d.height,d.height);
575 }
576 }
577 ****/
578
579 faceActuelle_.draw(_g);
580
581 /*
582 Container papy=getTopLevelAncestor();
583 if((papy!=null)&&(papy instanceof JFrame))
584 {
585 JRootPane rootpane=((JFrame)papy).getRootPane();
586 // System.err.println(""+rootpane);
587 if(rootpane!=null)
588 {
589 Component glasspane=rootpane.getGlassPane();
590
591 g=glasspane.getGraphics();
592 g.translate(100,50);
593 }
594 }
595 */
596
597 if(!BuPreferences.BU.getBooleanProperty("assistant.speech",false))
598 {
599 if(message_!=null)
600 {
601 Font font=getFont();
602 FontMetrics fm =_g.getFontMetrics(font);
603 int ws =0;
604 int hs =0;
605 int hs1 =fm.getAscent()+fm.getDescent();
606 Color fg =getForeground();
607 Color bg =getBackground().brighter();
608
609 int i,l;
610 l=message_.length;
611 hs=hs1*l;
612 for(i=0;i<l;i++)
613 ws=Math.max(ws,fm.stringWidth(message_[i]));
614
615 int x=d.width-insets.right-ws-4;
616 int y=d.height-insets.bottom-hs-4;
617
618 if( !getUseCommentWindow()
619 ||((x>hs1+insets.left)&&(y>insets.top)))
620 {
621 if(getUseCommentWindow()) getCommentWindow().setVisible(false);
622 BuLib.setColor(_g,bg);
623 _g.fillOval(x-hs1,y,2*hs1,2*hs1);
624 BuLib.setColor(_g,fg);
625 _g.drawOval(x-hs1,y,2*hs1,2*hs1);
626 BuLib.setColor(_g,bg);
627 _g.fillRect(x,y,d.width-x,d.height-y);
628 _g.fillRect(x-hs1,y+hs1,d.width-x+hs1,d.height-y-hs1);
629 BuLib.setColor(_g,fg);
630 _g.drawLine(x,y,d.width-1,y);
631 _g.drawLine(x-hs1,y+hs1,x-hs1,d.height-1);
632 _g.drawLine(d.width-1,y,d.width-1,d.height-1);
633 _g.drawLine(x-hs1,d.height-1,d.width-1,d.height-1);
634 }
635 else
636 {
637 JWindow win=getCommentWindow();
638
639 // if(!win.isVisible()||(message_!=window_message_))
640 {
641 win.setBackground(bg);
642 win.setForeground(fg);
643 java.awt.Point p=getLocationOnScreen();
644 p.x+=d.width-ws-8;
645 p.y+=d.height-hs-6;
646 if(p.x<0) p.x=0;
647 if(p.y<0) p.y=0;
648 d.width=ws+8;
649 d.height=hs+6;
650 win.setSize(d);
651 win.setLocation(p);
652 }
653
654 win.setVisible(true);
655
656 _g=win.getGraphics();
657 BuLib.setColor(_g,bg);
658 _g.fillRect(0,0,d.width,d.height);
659 BuLib.setColor(_g,fg);
660 _g.drawRect(0,0,d.width-1,d.height-1);
661
662 window_message_=message_;
663 x=4;
664 y=2;
665 }
666
667 _g.setFont(font);
668 for(i=0;i<l;i++)
669 {
670 y+=hs1;
671 _g.drawString(message_[i],x,y);
672 }
673 }
674 else
675 if(getUseCommentWindow())
676 getCommentWindow().setVisible(false);
677 }
678
679 // catch temporaire.
680 // try { super.paintComponent(_g); }
681 // catch(NullPointerException ex) { }
682 }
683
684 // Evenements
685
686 private static String logfileName()
687 {
688 String f="assistance.log";
689 if(BuLib.isUnix())
690 f=FuLib.getUserHome()+
691 File.separator+
692 f;
693 return f;
694 }
695
696 private PrintWriter log_=null;
697
698 public void actionPerformed(ActionEvent _evt)
699 {
700 Object source=_evt.getSource();
701 String info =null;
702
703 if(source instanceof JComponent)
704 info=((JComponent)source).getName();
705
706 if(info==null) info="";
707
708 if(log_==null)
709 {
710 try
711 {
712 String f=logfileName();
713 // System.err.println("log: opening "+f);
714 log_=new PrintWriter(new FileWriter(f,true),true);
715 }
716 catch(Throwable th) { } //IOException ex) { }
717 }
718
719 if(log_!=null)
720 {
721 String t=_evt.getActionCommand();
722 if(source instanceof JCheckBox)
723 t+="("+(((JCheckBox)source).isSelected() ? "VRAI" : "FAUX")+")";
724 if(source instanceof JComboBox)
725 t+="("+((JComboBox)source).getSelectedItem()+")";
726 if(source instanceof JList)
727 t+="("+((JList)source).getSelectedValue()+")";
728 log_.println(t);
729 // try { throw new RuntimeException("hello"); }
730 // catch(Exception ex) { ex.printStackTrace(log_); }
731 }
732
733 try
734 {
735 if(source instanceof JCheckBox)
736 info+="_"+(((JCheckBox)source).isSelected() ? "VRAI" : "FAUX");
737 if(source instanceof JComboBox)
738 info+="_"+((JComboBox)source).getSelectedItem()
739 .toString().toUpperCase();
740 if(source instanceof JList)
741 info+="_"+((JList)source).getSelectedValue()
742 .toString().toUpperCase();
743 }
744 catch(Exception ex) { }
745
746 // !!! System.err.println("BuAssistant: info="+info);
747
748 if(info==null) info="";
749 if(!info.equals(""))
750 commentUser(info+"_ACTION",getDefaultText(source));
751 }
752
753 public void focusGained(FocusEvent _evt)
754 {
755 JComponent source=(JComponent)_evt.getSource();
756 String info ="";
757 int essai =0;
758
759 while(essai<3)
760 {
761 info=source.getName();
762 // System.err.println("!!! Source "+source);
763
764 if(info==null) info="";
765 if(info.equals(""))
766 {
767 essai++;
768 source=(JComponent)source.getParent();
769 if(source==null) break;
770 }
771 else break;
772 }
773
774 if(!"".equals(info))
775 commentUser(info+"_FOCUS",getDefaultText(source));
776 }
777
778 public void focusLost(FocusEvent _evt)
779 {
780 }
781
782 // InternalFrame
783
784 private void internalFrameAction(InternalFrameEvent _evt)
785 {
786 int id=_evt.getID();
787 String action=null;
788 switch(id)
789 {
790 case InternalFrameEvent.INTERNAL_FRAME_ACTIVATED: action="FILLE_ACTIVER"; break;
791 case InternalFrameEvent.INTERNAL_FRAME_CLOSED: action="FILLE_FERMER"; break;
792 case InternalFrameEvent.INTERNAL_FRAME_ICONIFIED: action="FILLE_ICONIFIER"; break;
793 case InternalFrameEvent.INTERNAL_FRAME_DEICONIFIED: action="FILLE_DEICONIFIER"; break;
794 }
795 String frame=((JInternalFrame)_evt.getSource()).getName();
796 // System.err.println("IFE: "+action+"("+frame+")");
797 if((action!=null)&&(frame!=null))
798 {
799 if(frame.startsWith("if")) frame=frame.substring(2);
800 actionPerformed(new ActionEvent
801 (this,ActionEvent.ACTION_PERFORMED,
802 action+"("+frame+")"));
803 }
804 }
805
806 public void internalFrameActivated(InternalFrameEvent _evt)
807 { internalFrameAction(_evt); }
808 public void internalFrameDeactivated(InternalFrameEvent _evt)
809 { internalFrameAction(_evt); }
810 public void internalFrameClosed(InternalFrameEvent _evt)
811 { internalFrameAction(_evt); }
812 public void internalFrameClosing(InternalFrameEvent _evt)
813 { internalFrameAction(_evt); }
814 public void internalFrameDeiconified(InternalFrameEvent _evt)
815 { internalFrameAction(_evt); }
816 public void internalFrameIconified(InternalFrameEvent _evt)
817 { internalFrameAction(_evt); }
818 public void internalFrameOpened(InternalFrameEvent _evt)
819 { internalFrameAction(_evt); }
820
821
822 /*
823 public void keyTyped(KeyEvent _evt)
824 {
825 changeAttitude(COLERE,"Key="+_evt.getKeyChar());
826 if(_evt.getKeyCode()!=KeyEvent.VK_F1) return;
827
828 JComponent source=(JComponent)_evt.getSource();
829 String info ="";
830 int essai =0;
831
832 while(essai<3)
833 {
834 info=source.getName();
835 System.err.println("!!! Source "+source);
836
837 if(info==null) info="";
838 if(info.equals(""))
839 {
840 essai++;
841 source=(JComponent)source.getParent();
842 if(source==null) break;
843 }
844 else break;
845 }
846
847 changeAttitude(PEUR,"F1: "+info);
848 }
849
850 public void keyPressed(KeyEvent _evt)
851 {
852 }
853
854 public void keyReleased(KeyEvent _evt)
855 {
856 }
857 */
858
859 protected String getDefaultText(Object _o)
860 {
861 String r=null;
862 if(_o instanceof JComponent)
863 r=((JComponent)_o).getToolTipText();
864 return r;
865 }
866
867 public void commentUser(String _info)
868 {
869 commentUser(_info,null);
870 }
871
872 public void commentUser(String _info, String _default)
873 {
874 /*
875 String info=_info;
876 int i;
877 // System.err.println("!!! Real "+info);
878 i=0;
879 while(i<info.length())
880 {
881 int c=info.charAt(i);
882 if((c<'A')||(c>'Z')) i++;
883 else break;
884 }
885 info=info.substring(i);
886 */
887
888 String info;
889 Object o;
890 int i;
891
892 info=BuLib.simplifyComponentName(_info);
893 o=table_.get(info);
894
895 if(o==null)
896 do
897 {
898 i=info.lastIndexOf('_');
899 if(i<0) break;
900
901 info=info.substring(0,i);
902 o=table_.get(info);
903 }
904 while((i>=0)&&(o==null));
905
906 // System.err.println("!!! Comment "+info);
907
908 if(o==null) o=_default;
909
910 if(o!=null)
911 changeAttitude(PAROLE,o.toString());
912
913 // else
914 // if(!info.equals(""))
915 // changeAttitude(PAROLE,"Demandée:\n"+info);
916 }
917
918 // Thread
919
920 public void start()
921 {
922 if(blinker_!=null) blinker_.exit();
923
924 blinker_=new Blinker();
925 blinker_.start();
926 }
927
928 public void stop()
929 {
930 if(blinker_!=null)
931 {
932 blinker_.exit();
933 blinker_=null;
934 }
935 }
936
937 private final class Blinker extends Thread
938 {
939 private boolean exited_;
940
941 public Blinker()
942 {
943 super("Bu assistant");
944 setPriority(Thread.MIN_PRIORITY);
945 exited_=false;
946 }
947
948 public void exit()
949 {
950 exited_=true;
951 }
952
953 public void run()
954 {
955 while(!exited_)
956 {
957 aval_=0;
958 while(aval_<=100)
959 {
960 long avant=System.currentTimeMillis();
961
962 if(exited_) break;
963 faceActuelle_=new Face(aval_%101,faceAvant_,faceApres_);
964 BuUpdateGUI.repaintNow(BuAssistant.this);
965 long apres=System.currentTimeMillis();
966
967 long d=avant+DELAI-apres;
968 if(d>40)
969 {
970 try { Thread.sleep(d); }
971 catch (InterruptedException ex) { }
972 }
973
974 aval_+=AINC;
975 }
976 if(exited_) break;
977
978 faceActuelle_=faceApres_;
979 faceAvant_ =faceApres_;
980
981 if(face_!=ATTENTE)//&&(face_!=ABSENT))
982 {
983 try { Thread.sleep(5000); }
984 catch (InterruptedException ex) { }
985 setMessage("");
986 setFace(ATTENTE);
987 }
988 else
989 if("".equals(getMessage())&&(Math.random()<0.20))
990 {
991 try { Thread.sleep(5000); }
992 catch (InterruptedException ex) { }
993 setMessage("Zzz...");
994 BuUpdateGUI.repaintNow(BuAssistant.this);
995 try { Thread.sleep(2000); }
996 catch (InterruptedException ex) { }
997 setMessage("");
998 BuUpdateGUI.repaintNow(BuAssistant.this);
999 exited_=true;
1000 }
1001 else
1002 exited_=true;
1003 }
1004 }
1005 }
1006
1007 // Proprietes
1008
1009 private String[] message_;
1010 public synchronized String getMessage()
1011 {
1012 String r="";
1013 if(message_!=null)
1014 {
1015 for(int i=0;i<message_.length;i++)
1016 {
1017 if(i>0) r+='\n';
1018 r+=message_[i];
1019 }
1020 }
1021 return r;
1022 }
1023 public synchronized void setMessage(String _v)
1024 {
1025 if(_v==null) return;
1026
1027 String vp=getMessage();
1028
1029 if(!vp.equals(_v))
1030 {
1031 if(_v.equals(""))
1032 {
1033 message_=null;
1034 }
1035 else
1036 {
1037 int i,j,m,n;
1038
1039 n=1;
1040 for(i=0;i<_v.length();i++)
1041 if(_v.charAt(i)=='\n') n++;
1042
1043 message_=new String[n];
1044 m=0;
1045 j=0;
1046 for(i=0;i<_v.length();i++)
1047 if(_v.charAt(i)=='\n')
1048 {
1049 message_[m]=_v.substring(j,i);
1050 m++;
1051 j=i+1;
1052 }
1053 message_[m]=_v.substring(j);
1054 }
1055
1056 firePropertyChange("message",_v,vp);
1057
1058 if( BuPreferences.BU.getBooleanProperty("assistant.speech",false)
1059 &&(festival_==null))
1060 {
1061 //System.err.println("SPEAK: "+_v.replace('\n',' '));
1062 try
1063 {
1064 PrintWriter wout=new PrintWriter
1065 (new FileWriter("/tmp/assistant.speech"));
1066 wout.print(_v);
1067 wout.close();
1068 String[] cmd=new String[]
1069 {
1070 "/usr/bin/festival",
1071 "--tts",
1072 "/tmp/assistant.speech"
1073 };
1074 final Process p=Runtime.getRuntime().exec(cmd);
1075
1076 festival_=new Thread
1077 (new Runnable()
1078 {
1079 public void run()
1080 {
1081 try { p.waitFor(); }
1082 catch(InterruptedException ex) { }
1083 festival_=null;
1084 }
1085 }
1086 );
1087 festival_.start();
1088 }
1089 catch(Exception ex) { ex.printStackTrace(); }
1090 }
1091 }
1092 }
1093
1094 private String[] window_message_;
1095
1096 private boolean use_=false; // TMP @GDX true
1097
1098 public synchronized boolean getUseCommentWindow()
1099 { return use_; }
1100 public synchronized void setUseCommentWindow(boolean _use)
1101 { use_=_use; }
1102
1103 private JWindow window_;
1104 public synchronized JWindow getCommentWindow()
1105 {
1106 if(window_==null) window_=new JWindow();
1107 return window_;
1108 }
1109 public synchronized void setCommentWindow(JWindow _window)
1110 {
1111 window_=_window;
1112 }
1113
1114 private int face_;
1115 public synchronized int getFace() { return face_; }
1116 public synchronized void setFace(int _v)
1117 {
1118 _v%=faces_.length;
1119
1120 if(face_!=_v)
1121 {
1122 int vp=face_;
1123 face_=_v;
1124 faceApres_=faces_[_v];
1125 faceAvant_=faceActuelle_;
1126 firePropertyChange("face",_v,vp);
1127 }
1128 }
1129
1130 public void setVisible(boolean _v)
1131 {
1132 if(_v!=isVisible())
1133 {
1134 super.setVisible(_v);
1135 if(_v) changeAttitude(ATTENTE,"Me revoilà !");
1136 else changeAttitude(ABSENT, "Au revoir !");
1137 }
1138 }
1139
1140 public void changeAttitude(int _f, String _m)
1141 {
1142 if(_f>=0) setFace(_f);
1143 if(_m!=null) setMessage(_m);
1144 if(isShowing()) start();
1145 }
1146
1147 // Emitters
1148
1149 public void addEmitters(Container _parent)
1150 {
1151 addSpecificEmitters(_parent,"Action");
1152 addSpecificEmitters(_parent,"Focus");
1153 //addSpecificEmitters(_parent,"Mouse");
1154 addSpecificEmitters(_parent,"InternalFrame");
1155
1156 // pas utiliser pour l'instant
1157 // addSpecificEmitters(_parent,"Key");
1158 }
1159
1160 public void addSpecificEmitters(Container _parent, String _type)
1161 {
1162 for(Enumeration e=BuLib.getAllSubComponents(_parent).elements();
1163 e.hasMoreElements(); )
1164 {
1165 Component o=(Component)e.nextElement();
1166 boolean b=(o.getName()!=null);
1167
1168 if(o instanceof BuMenuBar) b=false;
1169 //if(o instanceof BuToolBar) b=false;
1170
1171 if(b&&_type.equals("Focus"))
1172 {
1173 b=(o.isFocusTraversable());
1174 if(b&&(o instanceof JComponent))
1175 b=((JComponent)o).isRequestFocusEnabled();
1176 }
1177
1178 if(b)
1179 {
1180 // try to remove and add after, ensuring that there is only one.
1181
1182 try
1183 {
1184 Class[] c=new Class[] { Class.forName("java.awt.event."+_type+"Listener") };
1185 Method m=o.getClass().getMethod("remove"+_type+"Listener",c);
1186 // System.err.println("---> "+m);
1187
1188 Object[] a=new Object[] { this };
1189 m.invoke(o,a);
1190 // System.err.println(_type+" ---> "+o.getName());
1191 }
1192 catch(Exception ex) { }
1193
1194 try
1195 {
1196 Class[] c=new Class[] { Class.forName("java.awt.event."+_type+"Listener") };
1197 Method m=o.getClass().getMethod("add"+_type+"Listener",c);
1198 // System.err.println("---> "+m);
1199
1200 Object[] a=new Object[] { this };
1201 m.invoke(o,a);
1202 // System.err.println(_type+" ---> "+o.getName());
1203 }
1204 catch(Exception ex) { }
1205
1206 try
1207 {
1208 Class[] c=new Class[] { Class.forName("javax.swing.event."+_type+"Listener") };
1209 Method m=o.getClass().getMethod("remove"+_type+"Listener",c);
1210 // System.err.println("---> "+m);
1211
1212 Object[] a=new Object[] { this };
1213 m.invoke(o,a);
1214 // System.err.println(_type+" ---> "+o.getName());
1215 }
1216 catch(Exception ex) { }
1217
1218 try
1219 {
1220 Class[] c=new Class[] { Class.forName("javax.swing.event."+_type+"Listener") };
1221 Method m=o.getClass().getMethod("add"+_type+"Listener",c);
1222 // System.err.println("---> "+m);
1223
1224 Object[] a=new Object[] { this };
1225 m.invoke(o,a);
1226 // System.err.println(_type+" ---> "+o.getName());
1227 }
1228 catch(Exception ex) { }
1229 }
1230 }
1231 }
1232
1233 // Run Acto Script
1234
1235 public void runActoScript(ActionListener _l)
1236 {
1237 String f=logfileName();
1238 System.err.println("log: opening "+f);
1239
1240 try
1241 {
1242 LineNumberReader log_=new LineNumberReader(new FileReader(f));
1243 while(true)
1244 {
1245 String l=log_.readLine();
1246 if(l==null) break;
1247 setMessage(l);
1248 try
1249 {
1250 _l.actionPerformed(new ActionEvent
1251 (this,ActionEvent.ACTION_PERFORMED,l));
1252 }
1253 catch(Exception ex) { }
1254 }
1255 }
1256 catch(IOException ex) { }
1257 }
1258
1259 // Analyse de assistant.log
1260
1261 public static void main(String[] _args)
1262 {
1263 String f=logfileName();
1264 System.err.println("Lecture: "+f);
1265
1266 try
1267 {
1268 File file =new File(f);
1269 int taille=11+(int)file.length()/7;
1270
1271 Hashtable mots =new Hashtable(taille);
1272 Hashtable rmot =new Hashtable(taille);
1273 short nbmots=0;
1274 short[] ind =new short[taille];
1275 short nbind =0;
1276
1277 LineNumberReader log=new LineNumberReader(new FileReader(file));
1278
1279 String m="-----";
1280 while(true)
1281 {
1282 String l=log.readLine();
1283 if(l==null) break;
1284 // System.out.println(""+log.getLineNumber()+": "+l);
1285 if(!l.startsWith(m))
1286 {
1287 if(mots.get(l)==null)
1288 {
1289 mots.put(l,new Short(nbmots));
1290 rmot.put(new Short(nbmots),l);
1291 nbmots++;
1292 }
1293 ind[nbind]=((Short)mots.get(l)).shortValue();
1294 nbind++;
1295 if(!"".equals(l))
1296 {
1297 m=l;
1298 int i=m.indexOf('(');
1299 if(i>=0) m=m.substring(0,i);
1300 }
1301 }
1302 }
1303
1304 System.out.println("Mots : "+nbmots);
1305 System.out.println("Indices: "+nbind);
1306
1307 Hashtable seqs=new Hashtable(taille);
1308 short vmax=0;
1309 for(int i=0;i<nbind;i++)
1310 {
1311 if( (ind[i ]!=ind[i+2])
1312 &&(ind[i ]!=ind[i+3])
1313 &&(ind[i+1]!=ind[i+2])
1314 &&(ind[i+1]!=ind[i+3]))
1315 {
1316 Long l=new Long(((long)ind[i ])*16777216l+((long)ind[i+1])*65536l+
1317 ((long)ind[i+2])*256l +((long)ind[i+3]));
1318 Short s=(Short)seqs.get(l);
1319 short v=1;
1320 if(s!=null) v=(short)(s.shortValue()+1);
1321 seqs.put(l,new Short(v));
1322 if(v>vmax) vmax=v;
1323 // System.out.println(""+l+": "+s);
1324 }
1325 }
1326
1327 boolean suggestion=false;
1328 for(Enumeration e=seqs.keys(); e.hasMoreElements(); )
1329 {
1330 Object o=e.nextElement();
1331 Short s=(Short)seqs.get(o);
1332 int v=s.intValue();
1333 if((v>3)&&(v>=vmax/2))
1334 {
1335 long l=((Long)o).longValue();
1336 short i1=(short)(l%256l);
1337 short i2=(short)((l/256l)%256l);
1338 short i3=(short)((l/65536l)%256l);
1339 short i4=(short)((l/16777216l)%256l);
1340
1341 String a="\n - "+rmot.get(new Short(i1))+
1342 "\n - "+rmot.get(new Short(i2))+
1343 "\n - "+rmot.get(new Short(i3))+
1344 "\n - "+rmot.get(new Short(i4));
1345 if(a.indexOf("DEBUTER")<0)
1346 {
1347 System.out.println("Suggestion ("+v+")"+a);
1348 suggestion=true;
1349 }
1350 }
1351 }
1352 if(!suggestion)
1353 System.out.println("Pas de suggestion ("+vmax+")");
1354 }
1355 catch(Exception ex) { }
1356 }
1357}