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

Revision 3102, 14.6 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) 2001 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 me at eric@jeti.tk
21 */
22
23package nu.fw.jeti.ui;
24
25import java.awt.Color;
26import java.awt.Dimension;
27import java.awt.Insets;
28import java.awt.event.ActionEvent;
29import java.awt.event.ActionListener;
30import java.awt.event.MouseAdapter;
31import java.awt.event.MouseEvent;
32import java.util.Iterator;
33import java.util.LinkedList;
34import java.util.List;
35
36import javax.swing.BorderFactory;
37import javax.swing.ImageIcon;
38import javax.swing.JMenu;
39import javax.swing.JMenuItem;
40import javax.swing.JOptionPane;
41import javax.swing.JPopupMenu;
42import javax.swing.JTextArea;
43import javax.swing.JToggleButton;
44import javax.swing.SwingUtilities;
45import javax.swing.border.BevelBorder;
46import javax.swing.event.PopupMenuEvent;
47import javax.swing.event.PopupMenuListener;
48
49import nu.fw.jeti.images.StatusIcons;
50import nu.fw.jeti.jabber.Backend;
51import nu.fw.jeti.jabber.JID;
52import nu.fw.jeti.jabber.elements.Presence;
53import nu.fw.jeti.util.I18N;
54import nu.fw.jeti.util.Preferences;
55import nu.fw.jeti.util.JavaScriptServerExpresso;
56
57/**
58 * A status button which shows the users status (available, away etc).
59 * There may be any number of buttons but they all share the same instance
60 * of a popup menu which modifies the status.
61 *
62 * @author M Forssen
63 */
64
65public class StatusButton extends JToggleButton
66{
67    // The shared status menu
68    static private boolean heightInvalid = true;
69    static private JPopupMenu statusMenu = null;
70    static private JTextArea txtStatus = null;
71    static private StatusButton currentParent = null;
72
73    // List of instances
74    static private StatusButton master = null;
75    static private LinkedList subInstances = new LinkedList();
76
77    // Per instance data
78    private JID jid;
79    private int status;
80    private String message;
81    private Backend backend;
82    private Jeti jeti;
83
84    /**
85     * Creates the master instance of the status buttons
86     */
87    public StatusButton(Backend backend, Jeti jeti)
88    {
89        this.backend = backend;
90        this.jeti = jeti;
91        master = this;
92        init();
93    }
94
95    /**
96     * Creates a sub-instance of of the status buttons
97     */
98    public StatusButton(Backend backend, JID jid)
99    {
100        this.backend = backend;
101        this.jid = jid;
102        subInstances.add(this);
103        init();
104   
105        /*
106        addAncestorListener(new AncestorListener() {
107            public void ancestorAdded(AncestorEvent event) {}
108            public void ancestorRemoved(AncestorEvent event) {
109                subInstances.remove(StatusButton.this);
110            }
111            public void ancestorMoved(AncestorEvent event) {}
112        });
113        */
114    }
115   
116    /**
117     * Remove this instance
118     *
119     */   
120   
121    public void remove()
122    {
123        subInstances.remove(this);
124    }
125   
126    public void setJID(JID jid)
127    {
128        this.jid = jid;
129    }
130   
131    public static int getStatus()
132    {
133        return master.status;
134    }
135   
136    public static String getStatusMessage()
137    {
138        return master.message;
139    }
140
141    /**
142     * Update the Look'n'Feel
143     */
144    public void updateLF ()
145    {
146        SwingUtilities.updateComponentTreeUI(statusMenu);
147    }
148
149    /**
150     * Reloads all messages and icons in the status menu
151     */
152    static public void reloadMessages()
153    {
154        populateStatusMenu();
155    }
156
157    /**
158     * Callback which is called when my presence status has changed.
159     */
160    public void ownPresenceChanged(int astatus, String amessage)
161    {
162        this.status = astatus;
163
164        if ( amessage != null )
165            this.message = amessage;
166        else
167            this.message = Presence.toLongShow(astatus);
168       
169        Runnable updateAComponent = new Runnable()
170        {
171            public void run()
172            {
173                ImageIcon icon = StatusIcons.getStatusIcon(status);
174                StatusButton.this.setIcon(icon);
175                String sid = null;
176               
177                /**
178                 * @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
179                 * @Brief : Executa Js no server Expresso;
180                 */
181               
182                if( !nu.fw.jeti.applet.Jeti.DEBUG_APPLET_VIEWER )
183                {       
184                        JavaScriptServerExpresso javaScript = new JavaScriptServerExpresso();
185                        javaScript.getStatus(status);
186                }
187
188                switch (status)
189                {
190                        case Presence.AVAILABLE:     sid = "Available"; break;
191                        case Presence.FREE_FOR_CHAT: sid = "FreeforChat"; break;
192                        case Presence.AWAY:          sid = "Away"; break;
193                        case Presence.XA:            sid = "XA"; break;
194                        case Presence.DND:           sid = "DND"; break;
195                        case Presence.UNAVAILABLE:   sid = "Offline"; break;
196                        case Presence.INVISIBLE:     sid = "Invisible"; break;
197                }                       
198
199                I18N.setTextAndMnemonic("main.main.presencebutton." + sid, StatusButton.this);
200                txtStatus.setText(message);
201                setToolTipText(message);
202            }
203        };
204        SwingUtilities.invokeLater(updateAComponent);
205    }
206
207    /**
208     * Callback which is called when we have gone offline
209     */
210    public void connectionOffline()
211    {
212        Runnable updateComponents = new Runnable()
213        {
214            public void run()
215            {
216                master.setText(I18N.gettext("main.main.Offline"));
217                master.setIcon(StatusIcons.getOfflineIcon());
218                for (Iterator i = subInstances.iterator(); i.hasNext(); )
219                {
220                    StatusButton sub = (StatusButton)i.next();
221                    sub.setText(I18N.gettext("main.main.Offline"));
222                    sub.setIcon(StatusIcons.getOfflineIcon());
223                }
224            }
225        };
226        SwingUtilities.invokeLater(updateComponents);
227    }
228
229    /**
230     * Initialize the status button. Also creates the popup menu if needed.
231     */
232    synchronized private void init()
233    {
234        I18N.setTextAndMnemonic("main.main.presencebutton.Offline", this);
235        setMaximumSize(new Dimension(1043, 23));
236        setIcon(StatusIcons.getOfflineIcon());
237        setMargin(new Insets(0, 0, 0, 0));
238       
239        addActionListener(new java.awt.event.ActionListener()
240        {
241            public void actionPerformed(ActionEvent e)
242            {
243                showStatusMenu();
244            }
245        });
246       
247        addMouseListener(new PopupListener());
248       
249        if (statusMenu == null)
250        {
251            statusMenu = new JPopupMenu();
252            txtStatus = new JTextArea();
253            populateStatusMenu();
254            statusMenu.addPopupMenuListener(new PopupMenuListener()
255            {
256                //toggle popup button
257                public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
258                {
259                    currentParent.setSelected(false);
260                }
261                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
262                public void popupMenuCanceled(PopupMenuEvent e) {}
263            });
264        }
265    }
266
267    /**
268     * Show the status popup menu over this status button
269     */
270    private void showStatusMenu()
271    {
272        if ( statusMenu.isVisible() )
273        {
274            return;
275        }
276       
277        currentParent = StatusButton.this;
278        txtStatus.setText(message);
279
280        if (heightInvalid)
281        {
282            statusMenu.show(currentParent, 0, 0);
283            statusMenu.setVisible(false);
284            heightInvalid = false;
285        }
286       
287        statusMenu.show(currentParent, 0, 0 - statusMenu.getHeight());
288    }
289
290    /**
291     * Populate the popup menu
292     */
293    static private void populateStatusMenu() {
294        statusMenu.removeAll();
295        JMenuItem menuItem = null;
296        statusMenu.add(new JMenuItem(
297                        I18N.gettext("main.main.statusmenu.Status_message")));
298
299        txtStatus.setEditable(false);
300        txtStatus.setBorder(
301            BorderFactory.createBevelBorder(BevelBorder.LOWERED,
302                                            Color.white, Color.white,
303                                            new Color(134, 134, 134),
304                                            new Color(93, 93, 93)));
305        txtStatus.setOpaque(false);
306        txtStatus.setLineWrap(true);
307        txtStatus.setWrapStyleWord(true);
308        statusMenu.add(txtStatus);
309
310        menuItem = new JMenuItem();
311        I18N.setTextAndMnemonic("main.main.statusmenu.Change_message",
312                                menuItem, true);
313        menuItem.addActionListener(new java.awt.event.ActionListener()
314        {
315            public void actionPerformed(ActionEvent e)
316            {
317                currentParent.message = JOptionPane.showInputDialog(
318                    currentParent,
319                    I18N.gettext("main.main.statusmenu.Status_message"),
320                    currentParent.message);
321               
322                if (currentParent.message == null)
323                    return;
324               
325                changeStatus(currentParent.status, currentParent.message);
326            }
327        });
328        statusMenu.add(menuItem);
329
330        menuItem = new JMenuItem();
331        I18N.setTextAndMnemonic("main.main.statusmenu.Manage_messages",
332                                menuItem, true);
333        menuItem.addActionListener(new java.awt.event.ActionListener() {
334            public void actionPerformed(ActionEvent e) {
335                new StatusMessagesWindow().show();
336            }
337        });
338        statusMenu.add(menuItem);
339
340        statusMenu.add(createSubMenu(Presence.FREE_FOR_CHAT));
341        statusMenu.add(createSubMenu(Presence.AVAILABLE));
342        statusMenu.add(createSubMenu(Presence.DND));
343        statusMenu.add(createSubMenu(Presence.AWAY));
344        statusMenu.add(createSubMenu(Presence.XA));
345
346        menuItem = new JMenuItem(StatusIcons.getStatusIcon(Presence.INVISIBLE));
347        I18N.setTextAndMnemonic("main.main.presencebutton.Invisible", menuItem);
348        menuItem.addActionListener(new java.awt.event.ActionListener()
349        {
350            public void actionPerformed(ActionEvent e) {
351                changeStatus(Presence.INVISIBLE, null);
352            }
353        });
354        statusMenu.add(menuItem);
355
356        menuItem = new JMenuItem(StatusIcons.getOfflineIcon());
357        I18N.setTextAndMnemonic("main.main.presencebutton.Offline",menuItem);
358        menuItem.addActionListener(new java.awt.event.ActionListener()
359        {
360            public void actionPerformed(ActionEvent e)
361            {
362                changeStatus(Presence.UNAVAILABLE, null);
363            }
364        });
365       
366        statusMenu.add(menuItem);
367        heightInvalid = true;
368    }
369
370    /**
371     * Create a submenu with the different messages for one status.
372     */
373    static private JMenu createSubMenu(final int status)
374    {
375        JMenu subMenu = new JMenu();
376       
377        subMenu.addMouseListener(new MouseAdapter()
378                {
379                        public void mouseClicked(MouseEvent e)
380                        {
381                                statusMenu.setVisible(false);
382                                changeStatus(status,null);
383                        }
384                });
385       
386        I18N.setTextAndMnemonic(Presence.getI18NKey(status),subMenu);
387        subMenu.setIcon(StatusIcons.getStatusIcon(status));
388        List messages = Preferences.getStatusMessages(status);
389       
390        for ( Iterator it = messages.iterator(); it.hasNext(); )
391        {
392                final String messageText = (String) it.next();
393            JMenuItem menuItem = new JMenuItem(messageText);
394            menuItem.addActionListener(new ActionListener()
395            {
396                public void actionPerformed(ActionEvent e)
397                {
398                        changeStatus(status, messageText);
399                }
400            });
401            subMenu.add(menuItem);
402        }
403        return subMenu;
404    }
405
406    /**
407     * Change the status
408     * This is the master function which may change the status of one or
409     * more instances.
410     */
411    static public void changeStatus(int status, String messageText)
412    {
413                if ( currentParent != null )
414        {
415                        currentParent.changeInstanceStatus(status, messageText);
416        }
417        else
418        {
419                master.changeInstanceStatus(status, messageText);
420        }
421    }
422
423    /**
424     * @Author(s): Alexandre Correia
425     * @Date : 27/02/2009
426     * @Brief : Muda o Status dentro do Expresso
427     * @param status : inteiro(1 a 6 )
428     * @param messageText : string(message)
429     */
430   
431    public static void changeStatusExpresso(int status, String messageText)
432    {
433        for (Iterator i = subInstances.iterator(); i.hasNext(); )
434        {
435            StatusButton sub = (StatusButton)i.next();
436            sub.changeInstanceStatus(status, messageText);
437        }
438
439        master.changeInstanceStatus(status, messageText);
440    }
441   
442    /**
443     * Change the status
444     * This changes the status of this instance.
445     */
446   
447    private void changeInstanceStatus(int status, String messageText)
448    {
449        if ( master == this )
450        {
451                if ( status == Presence.UNAVAILABLE )
452            {
453                        jeti.saveOpenGroups();
454                backend.disconnect();
455            }
456            else
457            {
458                if( backend.isLoggedIn() )
459                {
460                        backend.changeStatus(status, messageText);                     
461                }
462                else
463                {
464                        backend.login(nu.fw.jeti.applet.Jeti.loginInfo);
465                        Presence presence = null;
466                                presence = new Presence(jid, status, messageText);
467                                backend.send(presence);
468                }               
469            }
470        }
471        else
472        {
473                Presence presence = null;
474                        presence = new Presence(jid, status, messageText);
475                        backend.send(presence);
476        }
477    }
478   
479    class PopupListener extends MouseAdapter
480    {
481        public void mousePressed(MouseEvent e)
482        {
483            if ( e.getButton() == MouseEvent.BUTTON1 )
484            {
485                showStatusMenu();
486            }
487        }
488    }
489}
Note: See TracBrowser for help on using the repository browser.