KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > zirc > gui > AbstractChatFrame


1 package zirc.gui ;
2
3 import java.io.* ;
4 import java.net.* ;
5 import java.util.* ;
6
7 import java.awt.* ;
8 import java.awt.event.* ;
9 import javax.swing.* ;
10 import javax.swing.border.* ;
11 import javax.swing.event.* ;
12 import javax.swing.text.html.* ;
13
14 import zirc.base.* ;
15 import zirc.gui.smiliesWindow.* ;
16 import zirc.msg.* ;
17
18 //zIrc, irc client.
19
// Copyright (C) 2004 CoolBytes(Stephane claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com
20
//
21
// This program is free software; you can redistribute it and/or
22
// modify it under the terms of the GNU General Public License
23
// as published by the Free Software Foundation; either version 2
24
// of the License, or (at your option) any later version.
25
//
26
// This program is distributed in the hope that it will be useful,
27
// but WITHOUT ANY WARRANTY; without even the implied warranty of
28
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
// GNU General Public License for more details.
30

31 /**
32  * <p>Title: AbstractChatFrame</p>
33  * <p>Description: classe de base pour toutes les classes de messages</p>
34  * <p>Copyright: Copyright (c) 2004</p>
35  * <p>Company: CoolBytes(Stephane Claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com</p>
36  * @version 1.0
37  */

38
39 public class AbstractChatFrame extends JInternalFrame
40 {
41   //references utiles
42
protected IRCconnexion IRCchat ;
43   protected ToolBarItem toolBarIcone ;
44   private SmiliesCollection smilies ;
45   private String JavaDoc workingDir ;
46
47   //limite de grandeur du texte
48
protected final int MAXLINES = 300 ;
49   private final int MAXDOCLENGTH = 6000 ; //le nombre max de char
50
protected ArrayList pos = new ArrayList() ;
51   private boolean display = true ; //boolean qui sert a afficher ou pas le texte
52

53   protected BorderLayout borderLayout1 = new BorderLayout() ;
54   protected JPanel jPanel1 = new JPanel() ;
55   protected BorderLayout borderLayout2 = new BorderLayout() ;
56   protected JPanel jPanel2 = new JPanel() ;
57   protected JPanel jPanel3 = new JPanel() ;
58   protected JScrollPane jScrollPane1 = new JScrollPane() ;
59   protected BorderLayout borderLayout3 = new BorderLayout() ;
60
61 //les trois �l�ments importants du textpane
62
protected JTextPane jTextPane1 = new JTextPane() ;
63   protected HTMLDocument doc = new HTMLDocument() ;
64   protected HTMLEditorKit htk = new HTMLEditorKit() ;
65   private StringBuffer JavaDoc message ;
66
67   protected BorderLayout borderLayout4 = new BorderLayout() ;
68   protected JTextField jTextField1 = new JTextField() ;
69   Border border1 ;
70   private JButton jButton_Display = new JButton() ;
71
72   public AbstractChatFrame()
73   {
74
75     super() ;
76     try
77     {
78       jbInit() ;
79     }
80     catch (Exception JavaDoc e)
81     {
82       e.printStackTrace() ;
83     }
84     setResizable(true) ;
85   }
86
87   public AbstractChatFrame(IRCconnexion _IRCchat)
88   {
89     IRCchat = _IRCchat ;
90     smilies = IRCchat.getSmiliesCollection() ;
91     pos.ensureCapacity(MAXLINES) ;
92     setSize(IRCchat.getMainDesktopPane().getWidth() - 40, IRCchat.getMainDesktopPane().getHeight() - 40) ;
93     setResizable(true) ;
94
95     //definir le rep de travail
96
File f = new File("") ;
97     workingDir = f.getAbsolutePath() ;
98
99     try
100     {
101       jbInit() ;
102     }
103     catch (Exception JavaDoc e)
104     {
105       e.printStackTrace() ;
106     }
107
108     //ajouter un bouton pour cette fenetre
109
toolBarIcone = new ToolBarItem(IRCchat.GetMainFrm().getFrameToolBar(), this) ;
110   }
111
112   protected void jbInit() throws Exception JavaDoc
113   {
114
115     border1 = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(SystemColor.control, 2),
116                                                  BorderFactory.createEmptyBorder(2, 0, 0, 0)) ;
117     setClosable(true) ;
118     setIcon(false) ;
119     setIconifiable(true) ;
120     setLayer(1) ;
121     setMaximizable(true) ;
122     setResizable(true) ;
123     getContentPane().setBackground(SystemColor.controlText) ;
124     setFont(new java.awt.Font JavaDoc("Arial Narrow", 0, 12)) ;
125     this.setBorder(BorderFactory.createLineBorder(Color.gray)) ;
126     setDoubleBuffered(true) ;
127     addInternalFrameListener(new javax.swing.event.InternalFrameAdapter JavaDoc()
128     {
129       public void internalFrameClosed(InternalFrameEvent e)
130       {
131         this_internalFrameClosed(e) ;
132       }
133
134       public void internalFrameActivated(InternalFrameEvent e)
135       {
136         this_internalFrameActivated(e) ;
137       }
138     }) ;
139     //getContentPane().setLayout(borderLayout1) ;
140
jPanel1.setLayout(borderLayout2) ;
141     jPanel3.setLayout(borderLayout3) ;
142     jPanel2.setLayout(borderLayout4) ;
143     //jTextPane1.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION);
144
jTextPane1.setBorder(BorderFactory.createLineBorder(Color.gray)) ;
145     jTextPane1.setMaximumSize(new Dimension(2147483647, 2147483647)) ;
146     jTextPane1.setPreferredSize(new Dimension(6, 6)) ;
147     jTextPane1.setCaretColor(Color.black) ;
148     jTextPane1.setCaretPosition(0) ;
149     jTextPane1.setEditable(false) ;
150     jTextPane1.setText("") ;
151     jTextPane1.setContentType("text/plain") ;
152
153     jTextField1.addActionListener(new java.awt.event.ActionListener JavaDoc()
154     {
155       public void actionPerformed(ActionEvent e)
156       {
157         jTextField1_actionPerformed(e) ;
158       }
159     }) ;
160     jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
161                                               HORIZONTAL_SCROLLBAR_NEVER) ;
162     jScrollPane1.setBorder(null) ;
163     jScrollPane1.setPreferredSize(new Dimension(0, 0)) ;
164
165     jPanel1.setBackground(Color.white) ;
166     jPanel1.setBorder(null) ;
167     jTextField1.setBorder(BorderFactory.createLineBorder(Color.gray)) ;
168     jTextField1.setMinimumSize(new Dimension(4, 26)) ;
169     jTextField1.setPreferredSize(new Dimension(4, 26)) ;
170     jTextField1.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
171     {
172       public void keyTyped(KeyEvent e)
173       {
174         jTextField1_keyTyped(e) ;
175       }
176     }) ;
177     jPanel3.setBorder(null) ;
178     jButton_Display.setBorder(BorderFactory.createLineBorder(Color.gray)) ;
179     jButton_Display.setMinimumSize(new Dimension(40, 17)) ;
180     jButton_Display.setPreferredSize(new Dimension(40, 17)) ;
181     jButton_Display.setActionCommand("jButton_Run") ;
182     jButton_Display.setFocusPainted(false) ;
183     jButton_Display.setText("Stop") ;
184     jButton_Display.addActionListener(new java.awt.event.ActionListener JavaDoc()
185     {
186       public void actionPerformed(ActionEvent e)
187       {
188         jButton_Display_actionPerformed(e) ;
189       }
190     }) ;
191     getContentPane().add(jPanel1, BorderLayout.CENTER) ;
192     jPanel1.add(jPanel2, BorderLayout.SOUTH) ;
193     jPanel2.add(jTextField1, BorderLayout.CENTER) ;
194     jPanel2.add(jButton_Display, BorderLayout.EAST) ;
195     jPanel1.add(jPanel3, BorderLayout.CENTER) ;
196     jPanel3.add(jScrollPane1, BorderLayout.CENTER) ;
197     jScrollPane1.getViewport().add(jTextPane1, null) ;
198
199     //pr�parer la feuille de style par d�faut
200
StyleSheet ss = new StyleSheet() ;
201     ss.addRule("body {font-size:15pt;}") ;
202     ss.addRule("body {text-align:left;}") ;
203     //ss.addRule("body {line-height:45pt;}");
204
ss.addRule("body {font-family:" + IRCconnexion.FIXEDSYS.getFontName() + ";}") ;
205
206     //la soumettre au document
207
doc = new HTMLDocument(ss) ;
208
209     //placer les outils editeurs dans le textpane
210
jTextPane1.setEditorKit(htk) ;
211     jTextPane1.setDocument(doc) ;
212
213     //FONT du jtextfield
214
jTextField1.setFont(new Font(IRCconnexion.FIXEDSYS.getName(), 0, 15)) ;
215
216     //listener pour le texpane
217
jTextPane1.addHyperlinkListener(new HyperlinkListener()
218     {
219       /**
220        * hyperlinkUpdate
221        *
222        * @param e HyperlinkEvent
223        */

224       public void hyperlinkUpdate(HyperlinkEvent e)
225       {
226         hyperlink_Activated(e) ;
227       }
228
229     }) ;
230
231   }
232
233   void this_internalFrameClosed(InternalFrameEvent e)
234   {
235     //enlever l'item de la toolbar.
236
toolBarIcone.destroy() ;
237   }
238
239 //m�thode pour ecrire dans le JtxtPane
240
public void displayTxt(StringBuffer JavaDoc _message, int initalLength, boolean _info) throws Exception JavaDoc
241   {
242     if (display) //si true on affiche...
243
{
244       message = _message ;
245
246       //ajouter la longeur de la ligne au tableau d integer
247
pos.add(new Integer JavaDoc(initalLength)) ;
248
249       //si devient trop grand, virer la plus ancienne ligne
250
//en se r�f�rant a la longeur enregistr�e ci dessus.
251
if (pos.size() > MAXLINES && doc.getLength() > MAXDOCLENGTH)
252       {
253         doc.remove(0, ((Integer JavaDoc)(pos.get(0))).intValue()) ;
254         pos.remove(0) ;
255       }
256
257       //
258
//METTRE CETTE PARTIE DANS ABSTRACTMESSAGE
259
//
260
//ici on cherche si il y a des liens html, un vrai merdier...
261
//
262
int index = 0 ;
263       int pos = 0 ;
264       URL url ;
265       String JavaDoc tag1 = "<a HREF=\"" ;
266       String JavaDoc tag2 = "\">" ;
267       String JavaDoc tag3 = "</a>" ;
268
269       while ((index = message.indexOf("http://", pos)) >= 0)
270       {
271         if (message.indexOf("<", index + 1) > 0)
272         {
273           url = new URL(message.substring(index, message.indexOf("<", index + 1))) ; //ok
274
message.delete(index, message.indexOf("<", index + 1)) ;
275         }
276         else
277         {
278           if (message.indexOf(" ", index + 1) > 0)
279           {
280             url = new URL(message.substring(index, message.indexOf(" ", index + 1))) ; //ok
281
message.delete(index, message.indexOf(" ", index + 1)) ;
282           }
283           else
284           {
285             message.insert(message.length(), " ") ;
286             url = new URL(message.substring(index, message.indexOf(" ", index + 1)).trim()) ; //ok
287
message.delete(index, message.indexOf(" ", index + 1)) ;
288           }
289         }
290         message.insert(index, " " + tag1 + url + tag2 + url + tag3) ;
291         pos += index + tag1.length() + url.toString().length() + tag2.length() + url.toString().length() + tag3.length() ;
292       }
293
294       pos = 0 ;
295       while ((index = message.indexOf("www.", pos)) >= 0)
296       {
297         //si on a un http://www. on passe pas
298
if (!message.substring(index - 1, index).equals("/"))
299         {
300           if (message.indexOf("<", index + 2) > 0)
301           {
302             url = new URL("http://www." + message.substring(index + 4, message.indexOf("<", index + 2))) ; //ok
303
message.delete(index, message.indexOf("<", index + 2)) ;
304           }
305           else
306           {
307             if (message.indexOf(" ", index + 2) > 0)
308             {
309               url = new URL("http://www." + message.substring(index + 4, message.indexOf(" ", index + 2))) ; //ok
310
message.delete(index, message.indexOf(" ", index + 2)) ;
311             }
312             else
313             {
314               message.insert(message.length(), " ") ;
315               url = new URL("http://www." + message.substring(index + 4, message.indexOf(" ", index + 2))) ; //ok
316
message.delete(index, message.indexOf(" ", index + 2)) ;
317             }
318           }
319           message.insert(index, tag1 + url.toString() + tag2 + url.toString() + tag3) ;
320           pos += index + tag1.length() + url.toString().length() + tag2.length() + url.toString().length() + tag3.length() ;
321         }
322         else
323         {
324           pos += index + 1 ;
325         }
326       }
327
328       //
329
//ici on insere le html pour afficher les smilies
330
//
331
tag1 = "<img SRC=\"file:///" + workingDir + "/" ;
332       tag2 = "\"></img>" ;
333       for (int i = 0 ; i < smilies.getRowsCount() ; i++)
334       {
335         for (int j = 0 ; j < smilies.getColumnsCount() ; j++)
336         { //tag1 + smilies.getValueAt(i, j, 0).toString() + tag2
337
String JavaDoc tmp = smilies.getValueAt(i, j, 1).toString() ;
338           while ((index = message.indexOf(tmp)) >= 0)
339           {
340             message.insert(index + tmp.length(), (tag1 + smilies.getValueAt(i, j, 0).toString() + tag2)) ;
341             for (int k = 0 ; k < tmp.length() ; k++) //delete par char sinon ca plante
342
{
343               message.deleteCharAt(index) ;
344             }
345           }
346         }
347       }
348
349       htk.insertHTML(doc, doc.getLength(), message.toString(), 0, 0, null) ;
350       //faire descendre l ascenseur de la longeur de ce nouveau message
351
//si on est pas en trai de regarder un truc bien plus haut
352
if (jTextPane1.getCaretPosition() > doc.getLength() - 300)
353       {
354         jTextPane1.setCaretPosition(doc.getLength() - 1) ; //important le-1!
355
}
356
357       //si info vaut vrai, faire devenir rouge le bouton de la fenetre pour que
358
//machin sache qu un message l'attend.
359
if (_info)
360       {
361         toolBarIcone.setRouge() ;
362       }
363     }
364   }
365
366 //Getter pour le textAREA
367
public JTextPane GetTextPane()
368   {
369     return this.jTextPane1 ;
370   }
371
372   //getter pour le textbox
373
public JTextField getTextBox()
374   {
375     return this.jTextField1 ;
376   }
377
378   void jTextField1_actionPerformed(ActionEvent e)
379   {
380     String JavaDoc txt = jTextField1.getText() ;
381
382     if (txt.length() > 0)
383     {
384       if (txt.charAt(0) == '/')
385       {
386         if (txt.length() > 3 && txt.substring(0, 4).equalsIgnoreCase("/msg"))
387         {
388           IRCchat.sendCommand("PRIV" + txt.substring(1)) ;
389         }
390         else
391         {
392           if (txt.length() > 3 && txt.substring(0, 5).equalsIgnoreCase("/ctcp"))
393           {
394             IRCchat.sendCommand("PRIVMSG " + txt.substring(5)) ;
395           }
396           else
397           {
398             IRCchat.sendCommand(txt.substring(1)) ;
399           }
400         }
401
402       }
403       else
404       {
405         //non...
406
MSGinfoPlus msg = new MSGinfoPlus(IRCchat, this, "Vous n'etes pas sur un channel") ;
407         msg.reagit() ;
408         msg.affiche() ;
409       }
410       jTextField1.setText("") ;
411     }
412   }
413
414 //settitle red�fini pour mettre a jour l icone de la barre aussi
415
public void setTitle(String JavaDoc title)
416   {
417     super.setTitle(title) ;
418     toolBarIcone.setLibelle(this.getTitle()) ;
419   }
420
421   void this_internalFrameActivated(InternalFrameEvent e)
422   {
423     toolBarIcone.setPressed() ;
424     IRCchat.setChatFrameWithFocus(this) ;
425   }
426
427   void jTextField1_keyTyped(KeyEvent e)
428   {
429     /*
430          //code des balises Mirc
431        public static char mircColorCode = '' ; ctrl+k
432        public static char mircBoldCode = '' ; ctrl+b
433        public static char mircUnderlineCode = '' ; ctrl+u
434        public static char mircEraseCode = '' ; ctrl+r
435      */

436
437     if (e.isControlDown())
438     {
439       if (e.getKeyChar() == '\u000B') //ctrl+k
440
{ //"\u00035"
441
jTextField1.setText(jTextField1.getText() + '\u0003') ;
442       }
443       if (e.getKeyChar() == '\u0002') //ctrl+b
444
{
445         jTextField1.setText(jTextField1.getText() + '\u0002') ;
446       }
447       if (e.getKeyChar() == '\u0015') //ctrl+u
448
{
449         jTextField1.setText(jTextField1.getText() + '\u001F') ;
450       }
451       if (e.getKeyChar() == '\u0012') //ctrl+r
452
{
453         jTextField1.setText(jTextField1.getText() + '\u000F') ;
454       }
455     }
456   }
457
458   //on lance le browser
459
void hyperlink_Activated(HyperlinkEvent e)
460   {
461     if (e.getEventType() == e.getEventType().ACTIVATED)
462     {
463       BrowserLauncher browser = new BrowserLauncher() ;
464       browser.launchBrowserForInternet(e.getURL().toString()) ;
465     }
466   }
467
468   void jButton_Display_actionPerformed(ActionEvent e)
469   {
470     if (jButton_Display.getText().equalsIgnoreCase("stop"))
471     {
472       jButton_Display.setText("Go") ;
473       display = false ;
474     }
475     else
476     {
477       jButton_Display.setText("Stop") ;
478       display = true ;
479     }
480   }
481 }
482
Popular Tags