/* * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or mail/IM me at jeti@jabber.org */ package nu.fw.jeti.ui; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.text.MessageFormat; import javax.swing.*; import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreePath; import nu.fw.jeti.backend.roster.JIDStatusTree; import nu.fw.jeti.backend.roster.NormalJIDStatus; import nu.fw.jeti.backend.roster.PrimaryJIDStatus; import nu.fw.jeti.events.JavaErrorListener; import nu.fw.jeti.events.RegisterListener; import nu.fw.jeti.events.ServerListener; import nu.fw.jeti.images.StatusIcons; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JID; import nu.fw.jeti.jabber.JIDStatus; import nu.fw.jeti.jabber.elements.*; import nu.fw.jeti.ui.models.RosterTreeModel; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Preferences; /** * @author E.S. de Boer */ public class ServerTree extends JTree implements JavaErrorListener, RegisterListener { private Backend backend; private JPopupMenu popupMenu; private JIDStatus currentJIDStatus; private JID registerJID; private Jeti main; public ServerTree(Backend backend,Jeti main) { super(new RosterTreeModel()); this.backend = backend; backend.addListener(ServerListener.class,(RosterTreeModel)getModel()); backend.addListener(JavaErrorListener.class,this); this.main = main; ToolTipManager.sharedInstance().registerComponent(this); setRootVisible(false); putClientProperty("JTree.lineStyle", "None"); setToggleClickCount(1); javax.swing.plaf.basic.BasicTreeUI basicTreeUI = (javax.swing.plaf.basic.BasicTreeUI) getUI(); basicTreeUI.setRightChildIndent(1); basicTreeUI.setLeftChildIndent(1); basicTreeUI.setExpandedIcon(null); basicTreeUI.setCollapsedIcon(null); createPopupMenu(); setCellRenderer(new MyRenderer()); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { TreePath selPath = getPathForLocation(e.getX(), e.getY()); if(selPath != null) { setSelectionPath(selPath); Object o = selPath.getLastPathComponent(); if(o instanceof PrimaryJIDStatus) { maybeShowPopup(e,((PrimaryJIDStatus)o).getJIDPrimaryStatus());//cde if (SwingUtilities.isLeftMouseButton(e)) { if(e.getClickCount() >1) sendChat(((PrimaryJIDStatus)o).getJIDPrimaryStatus()); } } } } public void mouseReleased(MouseEvent e) { TreePath selPath = getPathForLocation(e.getX(), e.getY()); if(selPath != null) { Object o = selPath.getLastPathComponent(); if(o instanceof PrimaryJIDStatus) { maybeShowPopup(e,((PrimaryJIDStatus)o).getJIDPrimaryStatus()); } } } public void mouseExited(MouseEvent e) { clearSelection();//weg als multi select? } } ); addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { TreePath selPath = getPathForLocation(e.getX(), e.getY()); if(selPath != null) { Object o = selPath.getLastPathComponent(); if(o instanceof PrimaryJIDStatus) setSelectionPath(selPath); } } }); } private void createPopupMenu() { popupMenu = new JPopupMenu(); JMenuItem menuItem =null; menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Message",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { sendMessage(currentJIDStatus); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Chat",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { sendChat(currentJIDStatus); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Log_On",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { ((NormalJIDStatus)currentJIDStatus).setForcedOffline(false); backend.send(new Presence(currentJIDStatus.getJID(),"available")); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Log_Off",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { ((NormalJIDStatus)currentJIDStatus).setForcedOffline(true); backend.send(new Presence(currentJIDStatus.getJID(),"unavailable")); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Remove") +"..."); I18N.setMnemonic("main.main.rostermenu.Remove",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { JID to = currentJIDStatus.getJID(); 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) { IQXRoster roster = new IQXRoster(new RosterItem(to,null,"remove",null,null)); backend.send(new InfoQuery("set",roster)); } } }); popupMenu.add(menuItem); menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Rename")+"..."); I18N.setMnemonic("main.main.rostermenu.Rename",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { String nick = JOptionPane.showInputDialog(main,MessageFormat.format(I18N.gettext("main.popup.Rename_{0}_to"),new Object[] { currentJIDStatus.getNick() })); if (nick == null) return; IQXRoster roster = new IQXRoster(new RosterItem(currentJIDStatus.getJID(),nick,null,null,currentJIDStatus.getGroupsCopy())); backend.send(new InfoQuery("set",roster)); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Time",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQTime())); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Last_Seen",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQLast())); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Version",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQVersion())); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Invisible",menuItem); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new Presence(currentJIDStatus.getJID(), "invisible")); } }); popupMenu.add(menuItem); menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Edit_Registration")); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.addListener(RegisterListener.class,ServerTree.this); registerJID = currentJIDStatus.getJID(); backend.send(new InfoQuery(registerJID,"get",backend.getIdentifier(),new IQRegister())); } }); popupMenu.add(menuItem); } public void updateLF() { SwingUtilities.updateComponentTreeUI(popupMenu); } public void clearError() { ((RosterTreeModel)getModel()).remove(); } public void error() { if(Preferences.getBoolean("jeti","jetimenuShowLog",true) && Preferences.getBoolean("jeti","jetiShowError",true)) { ((RosterTreeModel)getModel()).add(); } } public void openGroups() { SwingUtilities.invokeLater(new Runnable() { public void run() { JIDStatusTree tree = ((JIDStatusTree)getModel().getRoot()); TreePath path = new TreePath(new Object[] {tree,tree.getGroup(I18N.gettext("main.main.roster.Servers"))}); expandPath(path); } }); } private void sendMessage(JIDStatus jidStatus) { new SendMessage(backend,jidStatus.getJID(),jidStatus.getNick()).setVisible(true); } private void sendChat(JIDStatus jidStatus) { main.chat(jidStatus); } private void maybeShowPopup(MouseEvent e,JIDStatus jidStatus) { if(e.isPopupTrigger()) { currentJIDStatus = jidStatus; popupMenu.show(e.getComponent(),e.getX(),e.getY()); } } public String getToolTipText(MouseEvent ev) { if(ev == null) return null; TreePath path = getPathForLocation(ev.getX(),ev.getY()); if (path != null) { Object o = path.getLastPathComponent(); if(o instanceof PrimaryJIDStatus) { JIDStatus jidStatus = ((PrimaryJIDStatus)o).getJIDPrimaryStatus(); return "
" + I18N.gettext("main.main.roster.Status") + " " + Presence.toLongShow(jidStatus.getShow())+"
" + I18N.gettext("main.main.statusmenu.Status_message") + " " + jidStatus.getStatus() + "
JID: " + jidStatus.getJID() + "
" + I18N.gettext("main.main.roster.Subscription") + " " + jidStatus.getSubscription() + "
" + I18N.gettext("main.main.roster.Waiting_Status") + " " + jidStatus.getWaiting() + "
"; } } return null; } public void register(IQRegister register,String id) { backend.removeListener(RegisterListener.class,this); new RegisterWindow(backend,register,registerJID,id); } static class MyRenderer implements TreeCellRenderer { private JLabel renderer; public MyRenderer() { renderer = new JLabel(); renderer.setOpaque(Preferences.getBoolean("jeti","bmw",true)); renderer.setBackground(UIManager.getColor("Tree.selectionBackground")); renderer.setForeground(UIManager.getColor("Tree.textForeground")); renderer.setFont(UIManager.getFont("Tree.font")); } public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded, boolean leaf,int row,boolean hasFocus) { renderer.setForeground(UIManager.getColor("Tree.textForeground")); if(sel) { renderer.setOpaque(true); } else { renderer.setOpaque(false); } renderer.setText(value.toString()); if (leaf) { return(makeComponent((JIDStatus)value)); } if (value instanceof PrimaryJIDStatus) return makeComponent(((PrimaryJIDStatus)value).getJIDPrimaryStatus()); else if (!leaf && expanded) renderer.setIcon(StatusIcons.getImageIcon("arrowDown")); else if (!leaf && !expanded) renderer.setIcon(StatusIcons.getImageIcon("arrowUp")); return renderer; } private Component makeComponent(JIDStatus jidStatus) { renderer.setIcon(StatusIcons.getStatusIcon(jidStatus)); return renderer; } } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */