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

Revision 3102, 12.0 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/IM me at jeti@jabber.org
21 */
22
23package nu.fw.jeti.ui;
24
25import java.awt.Component;
26import java.awt.event.ActionEvent;
27import java.awt.event.MouseAdapter;
28import java.awt.event.MouseEvent;
29import java.awt.event.MouseMotionAdapter;
30import java.text.MessageFormat;
31
32import javax.swing.*;
33import javax.swing.tree.TreeCellRenderer;
34import javax.swing.tree.TreePath;
35
36import nu.fw.jeti.backend.roster.JIDStatusTree;
37import nu.fw.jeti.backend.roster.NormalJIDStatus;
38import nu.fw.jeti.backend.roster.PrimaryJIDStatus;
39import nu.fw.jeti.events.JavaErrorListener;
40import nu.fw.jeti.events.RegisterListener;
41import nu.fw.jeti.events.ServerListener;
42import nu.fw.jeti.images.StatusIcons;
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.ui.models.RosterTreeModel;
48import nu.fw.jeti.util.I18N;
49import nu.fw.jeti.util.Preferences;
50
51
52/**
53 * @author E.S. de Boer
54 */
55
56public class ServerTree extends JTree implements JavaErrorListener, RegisterListener
57{
58        private Backend backend;
59        private JPopupMenu popupMenu;
60        private JIDStatus currentJIDStatus;
61        private JID registerJID;
62        private Jeti main;
63
64        public ServerTree(Backend backend,Jeti main)
65        {
66                super(new RosterTreeModel());
67                this.backend = backend;
68                backend.addListener(ServerListener.class,(RosterTreeModel)getModel());
69                backend.addListener(JavaErrorListener.class,this);
70                this.main = main;
71                ToolTipManager.sharedInstance().registerComponent(this);
72                setRootVisible(false);
73                putClientProperty("JTree.lineStyle", "None");
74                setToggleClickCount(1);
75                javax.swing.plaf.basic.BasicTreeUI basicTreeUI = (javax.swing.plaf.basic.BasicTreeUI) getUI();
76                basicTreeUI.setRightChildIndent(1);
77                basicTreeUI.setLeftChildIndent(1);
78                basicTreeUI.setExpandedIcon(null);
79                basicTreeUI.setCollapsedIcon(null);
80                createPopupMenu();
81                setCellRenderer(new MyRenderer());
82
83                addMouseListener(new MouseAdapter()
84                {
85                                public void mousePressed(MouseEvent e)
86                                {
87                                         TreePath selPath = getPathForLocation(e.getX(), e.getY());
88                                         if(selPath != null)
89                                         {
90                                                setSelectionPath(selPath);
91                                                Object o = selPath.getLastPathComponent();
92                                                if(o instanceof PrimaryJIDStatus)
93                                                {
94                                                        maybeShowPopup(e,((PrimaryJIDStatus)o).getJIDPrimaryStatus());//cde
95                                                        if (SwingUtilities.isLeftMouseButton(e))
96                                                        {
97                                                                if(e.getClickCount() >1) sendChat(((PrimaryJIDStatus)o).getJIDPrimaryStatus());
98                                                        }
99                                                }
100                                         }
101                                }
102                                public void mouseReleased(MouseEvent e)
103                                {
104                                        TreePath selPath = getPathForLocation(e.getX(), e.getY());
105                                        if(selPath != null)
106                                        {
107                                                Object o = selPath.getLastPathComponent();
108                                                if(o instanceof PrimaryJIDStatus)
109                                                {
110                                                        maybeShowPopup(e,((PrimaryJIDStatus)o).getJIDPrimaryStatus());
111                                                }
112                                        }
113                                }
114                                public void mouseExited(MouseEvent e)
115                                {
116                                        clearSelection();//weg als multi select?
117                                }
118                        }
119                );
120
121                addMouseMotionListener(new MouseMotionAdapter()
122                {
123                        public void mouseMoved(MouseEvent e)
124                        {
125                                TreePath selPath = getPathForLocation(e.getX(), e.getY());
126                                if(selPath != null)
127                                {
128                                        Object o = selPath.getLastPathComponent();
129                                        if(o instanceof PrimaryJIDStatus)       setSelectionPath(selPath);
130                                }
131                        }
132                });
133
134        }
135
136
137        private void createPopupMenu()
138        {
139                popupMenu = new JPopupMenu();
140                JMenuItem menuItem =null;
141                menuItem = new JMenuItem();
142                I18N.setTextAndMnemonic("main.main.rostermenu.Message",menuItem);
143                menuItem.addActionListener(new java.awt.event.ActionListener()
144                {
145                        public void actionPerformed(ActionEvent e)
146                        {
147                                sendMessage(currentJIDStatus);
148                        }
149                });
150                popupMenu.add(menuItem);
151                menuItem = new JMenuItem();
152                I18N.setTextAndMnemonic("main.main.rostermenu.Chat",menuItem);
153                menuItem.addActionListener(new java.awt.event.ActionListener()
154                {
155                        public void actionPerformed(ActionEvent e)
156                        {
157
158                                sendChat(currentJIDStatus);
159                        }
160                });
161                popupMenu.add(menuItem);
162               
163                menuItem = new JMenuItem();
164                I18N.setTextAndMnemonic("main.main.rostermenu.Log_On",menuItem);
165                menuItem.addActionListener(new java.awt.event.ActionListener()
166                {
167                        public void actionPerformed(ActionEvent e)
168                        {
169                                ((NormalJIDStatus)currentJIDStatus).setForcedOffline(false);
170                                backend.send(new Presence(currentJIDStatus.getJID(),"available"));
171                        }
172                });
173                popupMenu.add(menuItem);
174                menuItem = new JMenuItem();
175                I18N.setTextAndMnemonic("main.main.rostermenu.Log_Off",menuItem);
176                menuItem.addActionListener(new java.awt.event.ActionListener()
177                {
178                        public void actionPerformed(ActionEvent e)
179                        {
180                                ((NormalJIDStatus)currentJIDStatus).setForcedOffline(true);
181                                backend.send(new Presence(currentJIDStatus.getJID(),"unavailable"));
182                        }
183                });
184                popupMenu.add(menuItem);
185                menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Remove") +"...");
186                I18N.setMnemonic("main.main.rostermenu.Remove",menuItem);
187                menuItem.addActionListener(new java.awt.event.ActionListener()
188                {
189                        public void actionPerformed(ActionEvent e)
190                        {
191                                JID to = currentJIDStatus.getJID();
192                                if (JOptionPane.showConfirmDialog(main,MessageFormat.format(I18N.gettext("main.popup.Really_remove_{0}_from_all_groups_?"),new Object[] { to.toString() }),I18N.gettext("Remove"),JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
193                                {
194                                        IQXRoster roster = new IQXRoster(new RosterItem(to,null,"remove",null,null));
195                                        backend.send(new InfoQuery("set",roster));
196                                }
197                        }
198                });
199                popupMenu.add(menuItem);
200                menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Rename")+"...");
201                I18N.setMnemonic("main.main.rostermenu.Rename",menuItem);
202                menuItem.addActionListener(new java.awt.event.ActionListener()
203                {
204                        public void actionPerformed(ActionEvent e)
205                        {
206                                String nick = JOptionPane.showInputDialog(main,MessageFormat.format(I18N.gettext("main.popup.Rename_{0}_to"),new Object[] { currentJIDStatus.getNick() }));
207                                if (nick == null) return;
208                                IQXRoster roster = new IQXRoster(new RosterItem(currentJIDStatus.getJID(),nick,null,null,currentJIDStatus.getGroupsCopy()));
209                                backend.send(new InfoQuery("set",roster));
210                        }
211                });
212                popupMenu.add(menuItem);
213                menuItem = new JMenuItem();
214                I18N.setTextAndMnemonic("main.main.rostermenu.Local_Time",menuItem);
215                menuItem.addActionListener(new java.awt.event.ActionListener()
216                {
217                        public void actionPerformed(ActionEvent e)
218                        {
219                                backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQTime()));
220                        }
221                });
222                popupMenu.add(menuItem);
223                menuItem = new JMenuItem();
224                I18N.setTextAndMnemonic("main.main.rostermenu.Last_Seen",menuItem);
225                menuItem.addActionListener(new java.awt.event.ActionListener()
226                {
227                        public void actionPerformed(ActionEvent e)
228                        {
229                                backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQLast()));
230                        }
231                });
232                popupMenu.add(menuItem);
233                menuItem = new JMenuItem();
234                I18N.setTextAndMnemonic("main.main.rostermenu.Local_Version",menuItem);
235                menuItem.addActionListener(new java.awt.event.ActionListener()
236                {
237                        public void actionPerformed(ActionEvent e)
238                        {
239                                backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQVersion()));
240                        }
241                });
242                popupMenu.add(menuItem);
243                menuItem = new JMenuItem();
244                I18N.setTextAndMnemonic("main.main.rostermenu.Invisible",menuItem);
245                menuItem.addActionListener(new java.awt.event.ActionListener()
246                {
247                        public void actionPerformed(ActionEvent e)
248                        {
249                                backend.send(new Presence(currentJIDStatus.getJID(), "invisible"));
250                        }
251                });
252                popupMenu.add(menuItem);
253                menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Edit_Registration"));
254                menuItem.addActionListener(new java.awt.event.ActionListener()
255                {
256                        public void actionPerformed(ActionEvent e)
257                        {
258                                backend.addListener(RegisterListener.class,ServerTree.this);
259                                registerJID = currentJIDStatus.getJID();
260                                backend.send(new InfoQuery(registerJID,"get",backend.getIdentifier(),new IQRegister()));
261                        }
262                });
263                popupMenu.add(menuItem);
264        }
265       
266        public void updateLF()
267        {
268                SwingUtilities.updateComponentTreeUI(popupMenu);
269        }
270       
271        public void clearError()
272        {
273                ((RosterTreeModel)getModel()).remove();
274        }
275       
276        public void error()
277        {
278                if(Preferences.getBoolean("jeti","jetimenuShowLog",true)
279                                && Preferences.getBoolean("jeti","jetiShowError",true))
280                {
281                        ((RosterTreeModel)getModel()).add();
282                }
283        }
284       
285        public void openGroups()
286        {
287                SwingUtilities.invokeLater(new Runnable()
288                {
289                        public void run()
290                        {
291                                JIDStatusTree tree = ((JIDStatusTree)getModel().getRoot());
292                                TreePath path = new TreePath(new Object[] {tree,tree.getGroup(I18N.gettext("main.main.roster.Servers"))});
293                                expandPath(path);
294                        }
295                });
296                                                       
297        }
298       
299        private void sendMessage(JIDStatus jidStatus)
300        {
301                new SendMessage(backend,jidStatus.getJID(),jidStatus.getNick()).setVisible(true);
302        }
303
304        private void sendChat(JIDStatus jidStatus)
305        {
306                main.chat(jidStatus);
307        }
308
309        private void maybeShowPopup(MouseEvent e,JIDStatus jidStatus)
310        {
311                if(e.isPopupTrigger())
312                {
313                        currentJIDStatus = jidStatus;
314                        popupMenu.show(e.getComponent(),e.getX(),e.getY());
315                }
316        }
317
318
319        public String getToolTipText(MouseEvent ev)
320        {
321                if(ev == null) return null;
322                TreePath path = getPathForLocation(ev.getX(),ev.getY());
323                if (path != null)
324                {
325                        Object o = path.getLastPathComponent();
326                        if(o instanceof PrimaryJIDStatus)
327                        {
328                                JIDStatus jidStatus = ((PrimaryJIDStatus)o).getJIDPrimaryStatus();
329                                return "<HTML><P>" + I18N.gettext("main.main.roster.Status")
330                                                + " " + Presence.toLongShow(jidStatus.getShow())+"</p><p>" +
331                                                I18N.gettext("main.main.statusmenu.Status_message")
332                                                + " " + jidStatus.getStatus()
333                                                + "</p><p> JID: " + jidStatus.getJID() + "</p><p>"
334                                                + I18N.gettext("main.main.roster.Subscription")
335                                                + " " + jidStatus.getSubscription()     + "</p><p>"
336                                                + I18N.gettext("main.main.roster.Waiting_Status")
337                                                + " " + jidStatus.getWaiting() + "</p></HTML>";
338                               
339                        }
340                 }
341                return null;
342        }
343
344         public void register(IQRegister register,String id)
345         {
346                        backend.removeListener(RegisterListener.class,this);
347                        new RegisterWindow(backend,register,registerJID,id);
348         }
349
350        static class MyRenderer implements TreeCellRenderer
351        {
352                private JLabel renderer;
353                                       
354                public MyRenderer() {
355                        renderer = new JLabel();
356                        renderer.setOpaque(Preferences.getBoolean("jeti","bmw",true));
357                        renderer.setBackground(UIManager.getColor("Tree.selectionBackground"));
358                        renderer.setForeground(UIManager.getColor("Tree.textForeground"));
359                        renderer.setFont(UIManager.getFont("Tree.font"));
360          }
361
362                public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,     boolean leaf,int row,boolean hasFocus)
363                {
364                        renderer.setForeground(UIManager.getColor("Tree.textForeground"));
365                        if(sel)
366                        {
367                                renderer.setOpaque(true);
368                        }
369                        else
370                        {
371                                renderer.setOpaque(false); 
372                        }
373                        renderer.setText(value.toString());
374                        if (leaf)
375                        {
376                                return(makeComponent((JIDStatus)value));
377                        }
378                        if (value instanceof PrimaryJIDStatus) return makeComponent(((PrimaryJIDStatus)value).getJIDPrimaryStatus());
379                        else if (!leaf && expanded) renderer.setIcon(StatusIcons.getImageIcon("arrowDown"));
380                        else if (!leaf && !expanded) renderer.setIcon(StatusIcons.getImageIcon("arrowUp"));
381                        return renderer;
382                }
383
384                private Component makeComponent(JIDStatus jidStatus)
385                {
386                        renderer.setIcon(StatusIcons.getStatusIcon(jidStatus));
387                        return renderer;
388                }
389        }
390}
391/*
392 * Overrides for emacs
393 * Local variables:
394 * tab-width: 4
395 * End:
396 */
Note: See TracBrowser for help on using the repository browser.