source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/ui/ChatSplitPane.java @ 3102

Revision 3102, 25.8 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail/IM me at jeti@jabber.org
21 *
22 *      Created on 28-apr-2003
23 */
24
25package nu.fw.jeti.ui;
26
27import java.awt.*;
28import java.awt.event.*;
29import java.text.DateFormat;
30import java.text.MessageFormat;
31import java.util.*;
32import java.util.List;
33
34import javax.swing.Timer;
35import javax.swing.*;
36import javax.swing.event.ChangeEvent;
37import javax.swing.event.ChangeListener;
38import javax.swing.text.*;
39import javax.swing.border.*;
40import javax.swing.ImageIcon;
41
42import nu.fw.jeti.events.ChatEndedListener;
43import nu.fw.jeti.jabber.Backend;
44import nu.fw.jeti.jabber.JID;
45import nu.fw.jeti.jabber.JIDStatus;
46import nu.fw.jeti.jabber.elements.*;
47import nu.fw.jeti.plugins.*;
48import nu.fw.jeti.util.Base64;
49import nu.fw.jeti.util.I18N;
50import nu.fw.jeti.util.Preferences;
51import nu.fw.jeti.util.JavaScriptServerExpresso;
52import nu.fw.jeti.util.ServerExpresso;
53
54/**
55 * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
56 * @Date : 05/11/2008
57 * @Brief : classe focusWindow, faz com que o titulo fique piscando.
58 */
59
60class focusWindow implements ActionListener
61{
62        private boolean flag = false;
63        private JFrame window;
64        private String title;
65        private Timer timer = new Timer(1000, this);
66        private String name;
67       
68        public focusWindow()
69        {
70                timer.start();
71        }
72       
73        public void load(JFrame window, String name)
74        {
75                this.window = window;
76                this.title = window.getTitle();
77                this.name = name;
78               
79        }
80    public void actionPerformed(ActionEvent evt)
81    {
82        if( window.getExtendedState() == window.ICONIFIED )
83        {
84                if(flag)
85                        window.setTitle("## " + this.name + " fala...");
86                else
87                        window.setTitle(this.title);
88                flag = !flag;
89        }
90        else
91        {
92                timer.stop();
93                window.setTitle(this.title);
94        }
95    }
96}
97
98/**
99 * class defining basic feature of a chat window
100 * used by groupchatwindow and chatwindow
101 * @author E.S. de Boer
102 *
103 */
104
105//Do not add text to txtUitvoer outside this class, otherwise the text will
106//not scroll to the end
107
108public class ChatSplitPane extends JSplitPane
109{
110        private JID contactJID;
111        private String me;
112        private Backend backend;
113        private JScrollPane scrlInvoer = new JScrollPane();
114        private JTextPane txtInvoer = new JTextPane();
115        private JScrollPane scrlUitvoer = new JScrollPane();
116        private JTextPane txtUitvoer = new ToolTipTextpane();
117        private String contactName;
118        private String thread;
119        private boolean enterSends = false;
120        private boolean showTimestamp = false;
121        private Emoticons emoticons;
122        private JPanel pnlBottom = new JPanel();
123        private BorderLayout borderLayout1 = new BorderLayout();
124        private JPanel pnlControl = new JPanel();
125        private JPopupMenu popupMenu = new JPopupMenu();
126        private JPopupMenu popupSelectedMenu = new JPopupMenu();
127        private String composingID;
128        private boolean typing = false;
129        private FormattedMessage xhtml;
130        private boolean groupChat;
131        private Spell spell;
132        private Translator translator;
133        private Translator links;
134        private Translator xmppuri;
135        private Translator colormessages;
136        private Notifiers titleTimer;
137        private Notifiers titleFlash;
138        private SimpleAttributeSet colorAttributeSet = new SimpleAttributeSet();
139        private boolean toFrontOnNewMessage = false;
140        private static DateFormat dateFormat = DateFormat.getTimeInstance();
141        DateFormat shortDate= DateFormat.getDateInstance(DateFormat.SHORT);
142        DateFormat shortTime= DateFormat.getTimeInstance(DateFormat.SHORT);
143        private volatile boolean scrolls=true;
144        private volatile boolean systemScroll=true;
145        private Date date = new Date();
146        private ImageIcon avatar;
147
148       
149        class ToolTipTextpane extends JTextPane
150        {
151                //Tooltip on textpane, show time of message
152                public String getToolTipText(MouseEvent e)
153                {
154                        int location = txtUitvoer.viewToModel(e.getPoint());
155                        StyledDocument doc = (StyledDocument) txtUitvoer.getDocument();
156                        AttributeSet set = doc.getCharacterElement(location).getAttributes();
157                        return (String) set.getAttribute("time");       
158                }
159        }
160       
161        /**
162         * Groupchat splitpane constructor
163         * @param backend
164         * @param to
165         * @param toName
166         * @param me
167         * @param thread
168         * @param groupChat
169         * @param type
170         * @param menu
171         * @param frame
172         *
173         */
174        public ChatSplitPane(Backend backend, JID to,String toName,String me,JMenu menu,JFrame frame)
175        {
176                groupChat = true;
177                contactJID = to;
178        contactName = toName;
179                this.backend = backend;
180                this.me = me;
181                toFrontOnNewMessage = Preferences.getBoolean("groupchat", "toFrontOnNewMessage", false);
182                init("groupchat",menu);
183                setParentFrame(frame);
184        }
185       
186        /**
187         * 1-1 chat splitpane constructor
188         * @param backend
189         * @param to
190         * @param me
191         * @param thread
192         * @param menu
193         * @param frame
194         */
195        public ChatSplitPane(Backend backend, JIDStatus jidStatus,String me,String thread,JMenu menu,JFrame frame)
196        {
197                this.groupChat = false;
198                contactJID = jidStatus.getCompleteJID();
199        contactName = jidStatus.getNick();
200                avatar = jidStatus.getAvatar();
201                this.backend = backend;
202                this.me = me;
203                this.thread = thread;
204                toFrontOnNewMessage = Preferences.getBoolean("jeti", "toFrontOnNewMessage", false);
205                init(jidStatus.getType(),menu);
206                setParentFrame(frame);
207        }
208       
209        /**
210         * Add a menu to the menu invoked when the user presses on
211         * the right mousebutton and there is text selected
212         * @param submenu The menu to add
213         */
214        public void addToSelectedTextPopupMenu(JMenu submenu)
215        {
216                popupSelectedMenu.add(submenu);
217        }
218       
219        public String getSelectedText()
220        {
221                String text = txtUitvoer.getSelectedText();
222                if(text!=null) return text;
223                else return txtInvoer.getSelectedText();
224        }
225       
226        public void init(String type,JMenu menu)
227        {
228                JCheckBoxMenuItem chkItem = new JCheckBoxMenuItem(I18N.gettext("main.chat.menu.To_front_on_new_message"));
229                chkItem.setSelected(toFrontOnNewMessage);
230                chkItem.addActionListener(new ActionListener()
231                {
232                        public void actionPerformed(ActionEvent e)
233                        {
234                                toFrontOnNewMessage= ((JCheckBoxMenuItem)e.getSource()).isSelected();
235                        }
236                });
237               
238                menu.add(chkItem);
239               
240                if (PluginsInfo.isPluginLoaded("emoticons"))
241                {
242                        initEmoticons(type,menu);
243                }
244                               
245                Translate translate=null;
246               
247                if (PluginsInfo.isPluginLoaded("translate"))
248                {
249                        translate = (Translate) PluginsInfo.newPluginInstance("translate");
250                        translate.init(this,txtInvoer);
251                        txtUitvoer.addMouseListener(new MouseAdapter()
252                        {
253                                public void mouseClicked(MouseEvent e)
254                                {       
255                                        if(SwingUtilities.isRightMouseButton(e))
256                                        {
257                                                if(txtUitvoer.getSelectedText()!=null)
258                                                {
259                                                        popupSelectedMenu.show(e.getComponent(), e.getX(), e.getY());
260                                                }
261                                        }
262                                }
263                        });
264                }
265               
266                if (PluginsInfo.isPluginLoaded("spell"))
267                {
268                        spell = (Spell) PluginsInfo.newPluginInstance("spell");
269                        spell.addChangeDictoryMenuEntry(menu);
270                }
271               
272                if (PluginsInfo.isPluginLoaded("links"))
273                {
274                        links = (Translator) PluginsInfo.newPluginInstance("links");
275                        links.init(txtUitvoer);
276                }
277                if (PluginsInfo.isPluginLoaded("xmppuri"))
278                {
279                        xmppuri = (Translator) PluginsInfo.newPluginInstance("xmppuri");
280                        xmppuri.init(txtUitvoer);
281                }
282                if (PluginsInfo.isPluginLoaded("colormessages"))
283                {
284                        colormessages = (Translator) PluginsInfo.newPluginInstance("colormessages");
285                }
286                if (PluginsInfo.isPluginLoaded("titlescroller"))
287                {
288                        titleTimer = (Notifiers) PluginsInfo.newPluginInstance("titlescroller");
289                }
290                if (PluginsInfo.isPluginLoaded("titleflash"))
291                {
292                        titleFlash = (Notifiers) PluginsInfo.newPluginInstance("titleflash");
293                }
294                if(PluginsInfo.isPluginLoaded("windowsutils"))
295                {
296                        NativeUtils util =(NativeUtils)PluginsInfo.newPluginInstance("windowsutils");
297                        util.addChatMenus(menu,this);
298                }
299               
300                //translator = PluginsInfo.getTranslator();
301                if(emoticons != null || spell != null || translate!=null) 
302                {       
303                        txtInvoer.addMouseListener(new MouseAdapter()
304                        {
305                                public void mousePressed(MouseEvent e)
306                                {
307                                        if (SwingUtilities.isRightMouseButton(e))
308                                        {
309                                                if(txtInvoer.getSelectedText()!=null)
310                                                {
311                                                        popupSelectedMenu.show(e.getComponent(), e.getX(), e.getY());
312                                                }
313                                                else if(spell != null)
314                                                {
315                                                        if(!spell.rightClick(txtInvoer,e) && emoticons != null) popupMenu.show(e.getComponent(), e.getX(), e.getY());
316                                                }
317                                                else popupMenu.show(e.getComponent(), e.getX(), e.getY());
318                                        }
319                                }
320                        });
321                }
322               
323                ToolTipManager.sharedInstance().registerComponent(txtUitvoer);
324                       
325                enterSends = Preferences.getBoolean("jeti","enterSends",true);
326                showTimestamp = Preferences.getBoolean("jeti","showTimestamp",true);
327               
328                try
329                {
330                        jbInit();
331                }
332                catch (Exception e)
333                {
334                        e.printStackTrace();
335                }
336                if (PluginsInfo.isPluginLoaded("xhtml"))
337                {
338                        //String contactJid = contactJID.toString();
339                        xhtml = (FormattedMessage) PluginsInfo.newPluginInstance("xhtml");
340                        xhtml.initXHTML(this, txtInvoer, pnlControl, contactJID, contactName);
341                }
342               
343                String t = "";
344                //if(!groupChat)       
345                //notify();
346                if (thread == null)t = "no id - ";
347                Document doc = txtUitvoer.getDocument();
348                try
349                {
350                        SimpleAttributeSet sas = new SimpleAttributeSet();
351                        StyleConstants.setForeground(sas, Color.gray);
352                        doc.insertString(doc.getLength(), t + I18N.gettext("main.chat.Chat_started_on") + " " + DateFormat.getDateTimeInstance().format(new Date()), sas);
353                }
354                catch (BadLocationException e)
355                {
356                        e.printStackTrace();
357                }
358               
359                //solves bug iconofied frame then no system message and wrong layout
360                //pack();
361               
362                setBorder(null);               
363                setSize(300, 350);
364        }
365       
366        public JTextPane getTextInput()
367        {
368                return txtInvoer;
369        }
370
371       
372        public void setParentFrame(JFrame frame)
373        {
374                if (titleTimer !=null)
375                {
376                        titleTimer.init(frame,contactName);
377                }
378                if (titleFlash != null)
379                {
380                        titleFlash.init(frame, contactName);
381                }
382        }
383       
384        private void initEmoticons(String type,JMenu menu)
385        {
386                //showEmoticons = true;
387                emoticons = (Emoticons) PluginsInfo.newPluginInstance("emoticons");
388                try
389                {
390                        emoticons.init(txtUitvoer, pnlControl, txtInvoer, popupMenu,type,menu);
391                }
392                catch (IllegalStateException ex)
393                {
394                        ex.printStackTrace();
395                        //showEmoticons = false;
396                        PluginsInfo.unloadPlugin("emoticons");
397                        emoticons = null;
398                        return;
399                }
400        }
401       
402        public void close()
403        {
404                if(groupChat)contactJID = new JID(contactJID.getUser(),contactJID.getDomain(),me);
405                for(Iterator j = backend.getListeners(ChatEndedListener.class);j.hasNext();)
406                {//exit
407                        ((ChatEndedListener)j.next()).chatEnded(contactJID);
408                }
409        }
410       
411//      public boolean compareJID(JID jid)
412//      {
413//              return from.equals(jid);
414//      }
415//     
416//      public String getThread()
417//      {
418//              return thread;
419//      }
420
421        public void appendMessage(Message message,String name)
422        {
423                if (name == null)
424                {
425                        appendSystemMessage(message.getBody());
426                }
427                else if(name.equals(me))
428                {
429                        showMessage(message,new Color(156, 23, 23),Preferences.getString("jeti", "ownName", me));
430                }
431                else
432                {
433                        if(titleTimer!=null )
434                        {
435                titleTimer.start(
436                    MessageFormat.format(
437                        I18N.gettext("main.chat.{0}_says_{1}"),
438                        new Object[] {name,message.getBody()}));
439            }
440                       
441                        if(titleFlash!=null)
442                        {
443                titleFlash.start("");
444            }
445                       
446                        if(!groupChat)contactJID = message.getFrom();
447                       
448                        showMessage(message, new Color(17, 102, 6), name);
449                }
450        }
451       
452        public  void appendSystemMessage(final String message)
453        {
454                SwingUtilities.invokeLater(new Runnable()
455                {
456                        public void run()
457                        {
458                                final Document doc = txtUitvoer.getDocument();
459                                final Point  viewPos = scrlUitvoer.getViewport().getViewPosition();
460                                final boolean scroll = scrolls;//cache scroll value because insertstring will update it
461                                systemScroll=true;
462                                SimpleAttributeSet sas = new SimpleAttributeSet();
463                                StyleConstants.setForeground(sas, Color.gray);
464                                String timeStamp;
465                                if (showTimestamp)      timeStamp = formatTime(new Date()) + " ";
466                                else timeStamp = "";
467                                try
468                                {
469                                        doc.insertString(doc.getLength(), "\n"+ timeStamp + message, sas);
470                                }
471                                catch (BadLocationException e)
472                                {
473                                        e.printStackTrace();
474                                }
475                               
476                                if(scroll)
477                                {
478                                        txtUitvoer.setCaretPosition(doc.getLength());
479                                        scrolls=true;
480                                }
481                                else
482                                {
483                                        scrlUitvoer.getViewport().setViewPosition(viewPos);
484                                }
485                        }
486                });
487        }
488       
489        private void showMessage(final Message message,final Color color,final String name)
490        {//two threads can write to display (typing & receiving)
491                       
492                SwingUtilities.invokeLater(new Runnable()
493                {
494                        public void run()
495                        {
496                                String text = message.getBody();
497                        Date tempdelay =null;
498                                List tempWordList=null;
499                                date.setTime(System.currentTimeMillis());
500                               
501                                // Loop through extensions
502                                if (message.hasExtensions())
503                                {
504                                        for (Iterator i = message.getExtensions(); i.hasNext();)
505                                        {
506                                                Extension extension = (Extension) i.next();
507                                                if (extension instanceof XDelay)
508                                                {
509                                                        tempdelay = ((XDelay) extension).getDate();
510                                                }
511                                                else if(xhtml!=null)
512                                                {
513                                                        if(tempWordList==null) tempWordList = xhtml.getWordList(extension);
514                                                }
515                                        }
516                                }
517                                if (xhtml!=null && !name.equals(me) && !groupChat) {
518                            xhtml.useXHTML(tempWordList!=null, name);
519                        }
520                               
521                                if (tempWordList==null) {
522                                        tempWordList = createWordList(text);
523                        }
524
525                        // Mark eventual weblinks (if links extension is active)
526                                if(links!=null) {
527                            links.translate(tempWordList);
528                        }
529                               
530                                if(xmppuri!=null) {
531                            xmppuri.translate(tempWordList);
532                        }
533                                if(groupChat) {//me replace with nick
534                                        for(Iterator i = tempWordList.iterator();i.hasNext();) {
535                                                Word w = (Word) i.next();
536                                                if(w.word.equals("/me")) {
537                                    w.word = name;
538                                }
539                                        }
540                                }
541
542                                // Handle error messages
543                                if (message.getType().equals("error")) {
544                            insertError(tempWordList, message);
545                                }
546
547                                // Insert eventual emoticons
548                                if (emoticons != null) {
549                            emoticons.insertEmoticons(tempWordList);
550                        }
551
552                                //TODO make a new translator thing for here and below
553                                if (translator != null) translator.translate(tempWordList);
554
555                                final Date delay = tempdelay;
556                                final List wordList = tempWordList;
557                               
558                               
559                                Point  viewPos = scrlUitvoer.getViewport().getViewPosition();
560                                boolean scroll = scrolls;//cache scroll value because insertstring will update it
561                                systemScroll=true;
562                                Document doc = txtUitvoer.getDocument();
563                                try
564                                {
565                                        doc.insertString(doc.getLength(),"\n",null);
566                            if (delay != null)
567                            {
568                                SimpleAttributeSet sas = new SimpleAttributeSet();
569                                StyleConstants.setForeground(sas, Color.darkGray);
570                                doc.insertString(doc.getLength(), formatTime(delay) + " ", sas);
571                            }
572                                        else if (showTimestamp)
573                                        {// Eventual timestamp
574                                SimpleAttributeSet sas = new SimpleAttributeSet();
575                                StyleConstants.setForeground(sas, Color.gray);
576                                doc.insertString(doc.getLength(), formatTime(date) + " ", sas);
577                            }
578                                       
579                            StyleConstants.setForeground(colorAttributeSet, color);
580               
581                            // Print originator if not error
582                            if (!message.getType().equals("error")) {
583                                                colorAttributeSet.addAttribute("time",dateFormat.format(date));
584                                                doc.insertString(doc.getLength(),name+ ": ",colorAttributeSet);
585                                        }
586                                       
587                            // Insert words from wordlist
588                                        for(Iterator i = wordList.iterator();i.hasNext();) {
589                                                Word w = (Word) i.next();
590                                                doc.insertString(doc.getLength(),w.toString(),
591                                                 w.getAttributes());
592                                        }
593               
594                                        if (scroll)     {
595                                txtUitvoer.setCaretPosition(doc.getLength());
596                                scroll=true;
597                            } else {
598                                                scrlUitvoer.getViewport().setViewPosition(viewPos);
599                                        }
600                                }
601                                catch (BadLocationException e)
602                                {
603                                        e.printStackTrace();
604                                }
605
606                                /**
607                                 * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
608                                 * @Date : 04/11/2008
609                                 * @Brief : Trecho de codigo abaixo, faz com que a janela fique piscando caso esta esteja
610                                 * minimizada na barra de tarefas. Criada a classe focusWindow.
611                                 */
612
613                                JFrame frame = (JFrame)getTopLevelAncestor();
614
615                                if(toFrontOnNewMessage)
616                                {
617                                        if(!getTopLevelAncestor().isFocusOwner())
618                                        {
619                                                if(getTopLevelAncestor() instanceof JFrame)
620                                                {
621                                                        if( frame.getExtendedState() == Frame.ICONIFIED )
622                                                        {
623                                                                frame.setState(Frame.NORMAL);
624                                                        }
625                                                        frame.toFront();
626                                                }
627                                                else txtInvoer.requestFocus();
628                                        }
629                                }
630                               
631                                if( frame.getExtendedState() == Frame.ICONIFIED )
632                                {
633                                        frame.setVisible(true);
634                                        focusWindow focusW = new focusWindow();
635                                        focusW.load(frame, name);
636                                }
637                        }
638                });
639        }
640       
641        /**
642     * Format a timestamp for display.
643     */
644    private String formatTime(Date date) {
645                String result = "";
646               
647                if((System.currentTimeMillis() -date.getTime())> 600000)
648                {//message is older then 10 min
649                        Calendar cal = Calendar.getInstance();
650                        cal.set(Calendar.HOUR, 0);
651                    cal.set(Calendar.MINUTE, 0);
652                    cal.set(Calendar.SECOND, 0);
653                    cal.set(Calendar.AM_PM, Calendar.AM);
654                    if(date.before(cal.getTime()))
655                    {//print date if it is the previous day
656                        result += shortDate.format(date)+ " ";
657                    }
658                }
659                result+="["+shortTime.format(date)+"]";
660                return result;
661    }
662
663    // Insert error message into wordList
664    private void insertError(List wordList, Message message) {
665        String error;
666        boolean replace = false;
667
668        switch (message.getErrorCode()) {
669        case 404:
670            error = MessageFormat.format(
671                I18N.gettext("main.error.User_{0}_could_not_be_found"),
672                new Object[] {message.getFrom()});
673            replace = true;
674            break;
675        default:
676            error = I18N.gettext("main.error.Error_in_chat");
677            break;
678        }
679
680        // Create list of error words and make them red
681        List errorWords = createWordList(error);
682        SimpleAttributeSet sas = new SimpleAttributeSet();
683        StyleConstants.setForeground(sas, Color.red);
684        for(Iterator i = errorWords.iterator();i.hasNext();) {
685            ((Word)i.next()).addAttributes(sas);
686        }
687       
688        if (!replace) {
689            errorWords.addAll(wordList);
690        }
691        wordList.clear();
692        wordList.addAll(errorWords);
693        }
694
695    //TODO move to util??
696        public static List createWordList(String text)
697        {
698                List wordList = new ArrayList();
699                StringBuffer temp = new StringBuffer();
700                for(int i = 0;i<text.length();i++)
701                {//split text up in words
702                        char c = text.charAt(i);
703                        switch (c)
704                        {
705                                case ' ':       addWordFromTemp(temp, wordList);
706                                                        wordList.add(new Word(" "));
707                                                        temp = new StringBuffer(); break;
708                                case '\n':      addWordFromTemp(temp, wordList);
709                                                        wordList.add(new Word("\n"));
710                                                        temp = new StringBuffer(); break;
711                                case '\t':      addWordFromTemp(temp, wordList);
712                                                        wordList.add(new Word("\t"));
713                                                        temp = new StringBuffer();break;
714                                default: temp.append(c);
715                        }
716                }
717                addWordFromTemp(temp, wordList);
718                return wordList;
719        }
720       
721        private static void addWordFromTemp(StringBuffer temp, List wordList)
722        {
723                if(temp.length()>0)wordList.add(new Word(temp));
724        }
725
726        private String getPhotoExpresso(String Jid)
727        {
728                ServerExpresso JidPhoto = new ServerExpresso();
729                return JidPhoto.PhotoUid(Jid);
730        }
731       
732        private void jbInit() throws Exception
733        {
734                Color c = UIManager.getColor("TextPane.foreground");
735                txtInvoer.setForeground(c);
736                txtUitvoer.setForeground(c);
737                setResizeWeight(0.9);
738                setOrientation(JSplitPane.VERTICAL_SPLIT);
739                setBottomComponent(pnlBottom);
740                scrlInvoer.setPreferredSize(new Dimension(100, 100));
741
742                txtInvoer.addKeyListener(new java.awt.event.KeyAdapter()
743                {
744                        public void keyPressed(KeyEvent e)
745                        {
746                                txtInvoer_keyPressed(e);
747                        }
748                       
749                        public void keyReleased(KeyEvent e)
750                        {
751                                if(spell!=null)spell.keyReleased(e,txtInvoer);
752                        }
753                });
754                txtUitvoer.setEditable(false);
755                txtUitvoer.getDocument().putProperty( DefaultEditorKit.EndOfLineStringProperty, "\n" );
756                txtInvoer.getDocument().putProperty( DefaultEditorKit.EndOfLineStringProperty, "\n" );
757                addFocusListener(new FocusAdapter()
758                {
759                        public void focusGained(FocusEvent e)
760                        {
761                                txtInvoer.requestFocusInWindow();
762                        }
763                });
764
765                /**
766                 * @Author : Rodrigo Souza
767                 * @Date : 15/10/2008
768                 * @Description : Trecho de codigo abaixo ajusta as dimensoes da foto (avatar)
769                 *                                e demais paineis dentro da janela de conversa.
770                 */
771
772                pnlBottom.setLayout(borderLayout1);
773
774                JPanel text = new JPanel();
775                text.setLayout(new BoxLayout(text,BoxLayout.PAGE_AXIS));
776                text.add(pnlControl);
777                text.add(scrlInvoer);
778                pnlBottom.add(text, BorderLayout.CENTER);
779
780                /**
781                 * @Author : Alexandre Luiz Correia - alexandrecorreia@celepar.pr.gov.br
782                 * @Date : 10/09/2008
783                 * @Description : Avatar - Carrega as Photos do Ldap
784                 */
785
786                // Border
787                Border raisedetched;
788                raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
789
790                // String
791                String userJid = contactJID.toString();
792               
793                /**
794                 *  @Author : Alexandre Correia / Rodrigo Souza
795                 *  @Date : 04/03/2009
796                 *  @Description : Conecta com a classe ServerExpresso(nu.fw.jeti.util.ServerExpresso) no Expresso e passa
797                 *  a variavel userJid(Ex.: teste@nome_maquina), recebendo uma string em base64.
798                 */
799
800                String imgUser = getPhotoExpresso(userJid);
801                byte[] data = Base64.decode(imgUser);
802                ImageIcon icon = new ImageIcon(data);
803                JLabel avatar = new JLabel(icon);
804
805                avatar.setBorder(raisedetched);
806                pnlBottom.add(avatar, BorderLayout.EAST);
807
808                add(pnlBottom, JSplitPane.BOTTOM);
809                add(scrlUitvoer, JSplitPane.TOP);
810
811                scrlUitvoer.getViewport().add(txtUitvoer, null);
812                scrlInvoer.getViewport().add(txtInvoer, null);
813                scrlUitvoer.getVerticalScrollBar().getModel().addChangeListener(new ChangeListener()
814                {
815                        public void stateChanged(ChangeEvent e)
816                        {
817                                BoundedRangeModel model =(BoundedRangeModel)e.getSource();
818                                if(!model.getValueIsAdjusting())
819                                {
820                                        if(!systemScroll)
821                                        {//only update if scroll is performed by the user
822                                                if(model.getMaximum()-model.getExtent()-model.getValue()>10)
823                                                {
824                                                        scrolls=false;
825                                                }
826                                                else scrolls=true;
827                                        }
828                                        systemScroll=false;
829                                }
830                        }
831                });
832
833                setOneTouchExpandable(true);           
834                setDividerLocation(170);
835        }
836
837        public void send()
838        {
839                if (txtInvoer.getText().length()==0)return;
840                boolean sendXHTML = false;
841                XExtension html = null;
842                XExtension ownHtml = null;
843                List wordList=null;
844                if(xhtml != null)wordList = xhtml.makeWordListFromDocument();
845                if(wordList==null)wordList=createWordList(txtInvoer.getText());
846                StringBuffer temp = new StringBuffer();
847                //TODO Translator
848                //if (translator != null) translator.translate(wordList);
849               
850                for(Iterator i = wordList.iterator();i.hasNext();)
851                {
852                        temp.append(i.next());
853                }
854                String text = temp.toString();
855                if(colormessages!=null)
856                {
857                        colormessages.translate(wordList);
858                }
859                if(xhtml != null)
860                {       
861                        html = xhtml.getXHTMLExtension(wordList);
862                        //ownwordlist will be edited so make deep copy
863                        ArrayList tempList = new ArrayList();
864                        try
865                        {
866                                for(Iterator i = wordList.iterator();i.hasNext();)
867                                {
868                                        tempList.add(((Word)i.next()).clone());
869                                }
870                        }
871                        catch (CloneNotSupportedException e)
872                        {
873                                e.printStackTrace();
874                        }
875                        ownHtml = xhtml.getXHTMLExtension(tempList);
876                        sendXHTML = xhtml.sendXML();
877                }
878                Message message = null;
879                if(groupChat)
880                {       
881                        if(html!=null) message = new Message(contactJID,text ,html);
882                        else message = new Message(contactJID,text);
883                }
884                else
885                {
886                        MessageBuilder b = new MessageBuilder();
887                        b.type = "chat";
888                        b.setTo(contactJID);
889                        b.setId(backend.getIdentifier());
890                        b.thread = thread;
891                        b.addXExtension(new XMessageEvent("composing", null));
892                        b.body = text;
893                        if(sendXHTML)b.addXExtension(html);
894
895                        message =((Message) b.build());
896                        showMessage(new Message(text, contactJID, null,thread,ownHtml),new Color(156, 23, 23), "Eu ( " + nu.fw.jeti.applet.Jeti.CNNAME +" ) ");
897                }
898                backend.sendMessage(message);
899               
900                txtInvoer.getHighlighter().removeAllHighlights();
901                txtInvoer.setText("");
902                txtInvoer.requestFocus();
903                typing = false;
904        }
905       
906        public void composingID(String id)
907        {
908                composingID = id;
909        }
910
911        void txtInvoer_keyPressed(KeyEvent e)
912        {
913                /**
914                 * Author(s): Alexandre Correia / Rodrigo Souza
915                 * Date : 02/03/2009
916                 * Brief : Limpa o tempo do Javascript
917                 */
918               
919                JavaScriptServerExpresso cleanSt = new nu.fw.jeti.util.JavaScriptServerExpresso();
920                cleanSt.cleanStatus();
921               
922                if(!groupChat)
923                {
924                        if (txtInvoer.getText().length() < 2 && typing)
925                        {
926                                typing = false;
927                                if (composingID != null)
928                                {
929                                        //send not composing
930                                        backend.send(new Message(null, contactJID, null, new XMessageEvent(null, composingID)));
931                                }
932                        }
933                        else if (txtInvoer.getText().length() > 0 && !typing)
934                        {
935                                typing = true;
936                                if (composingID != null)
937                                {
938                                        //send composing
939                                         backend.send(new Message(null, contactJID, null, new XMessageEvent("composing", composingID)));
940                                }
941                        }
942                }
943                if (e.getKeyCode() == KeyEvent.VK_ENTER)
944                {
945                        if (enterSends)
946                        {
947                                if ((e.getModifiers() == InputEvent.SHIFT_MASK) || (e.getModifiers() == InputEvent.CTRL_MASK))
948                                {
949                                        Document doc = txtInvoer.getDocument();
950                                        try
951                                        {
952                                                doc.insertString(txtInvoer.getCaretPosition(), "\n", null);
953                                        }
954                                        catch (BadLocationException e3)
955                                        {e3.printStackTrace();}
956                                }
957                                else
958                                {
959                                        send();
960                                        e.consume();
961                                }
962                        }
963                        else if ((e.getModifiers() == InputEvent.SHIFT_MASK) || (e.getModifiers() == InputEvent.CTRL_MASK))
964                        {
965                                send();
966                        }
967                }
968        }       
969}
970
971/*
972 * Overrides for emacs
973 * Local variables:
974 * tab-width: 4
975 * End:
976 */
Note: See TracBrowser for help on using the repository browser.