/* * 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 me at jeti@jabberstudio.org * created 2001 */ package nu.fw.jeti.ui; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.event.*; import java.text.MessageFormat; import java.util.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; import nu.fw.jeti.backend.roster.JIDStatusGroup; import nu.fw.jeti.backend.roster.JIDStatusTree; import nu.fw.jeti.backend.roster.PrimaryJIDStatus; 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.plugins.PluginsInfo; import nu.fw.jeti.plugins.RosterMenuListener; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Preferences; import nu.fw.jeti.util.TreeExpander; /** * @author E.S. de Boer */ public class RosterTree extends JTree { private Backend backend; private JPopupMenu popupMenu; private JPopupMenu groupPopupMenu; private JIDStatus currentJIDStatus; private JIDStatusGroup currentJIDStatusGroup2; private JIDStatusGroup currentJIDStatusGroup; private boolean onlineTree; private Jeti main; private String currentGroup; private Map menuItems; //added menus private TreeExpander treeExpander; /** * @param type True for an online tree */ public RosterTree( Backend backend, Jeti main, boolean onlineTree, TreeModel model ) { super(model); this.onlineTree = onlineTree; this.backend = backend; this.main = main; ToolTipManager.sharedInstance().registerComponent(this); setRootVisible(false); setToggleClickCount(0);//set expanding on mouseclicks of because // detection needed for single or double click javax.swing.plaf.basic.BasicTreeUI basicTreeUI = (javax.swing.plaf.basic.BasicTreeUI) getUI(); basicTreeUI.setRightChildIndent(1); basicTreeUI.setLeftChildIndent(1); basicTreeUI.setExpandedIcon(null); basicTreeUI.setCollapsedIcon(null); putClientProperty("JTree.lineStyle", "None"); createPopupMenu(); createGroupPopupMenu(); if (UIManager.getLookAndFeel().isNativeLookAndFeel()) { setCellRenderer(new MacRenderer()); if (onlineTree) { treeExpander = new TreeExpander(this, model); } } else setCellRenderer(new MyRenderer()); addMouseListener(new MouseAdapter() { //save tree path for single click event TreePath lastTreePath; //timer needed to check if double or single mouseclick Timer timer = new Timer(300, new ActionListener() { public void actionPerformed(ActionEvent e) { if (isExpanded(lastTreePath)) collapsePath(lastTreePath); else expandPath(lastTreePath); timer.stop(); } }); public void mousePressed(MouseEvent e) { TreePath selPath = getPathForLocation(e.getX(), e.getY()); if (selPath != null) { setSelectionPath(selPath); Object o = selPath.getLastPathComponent(); if (o instanceof JIDStatusGroup) {//group maybeShowGroupPopup(e, (JIDStatusGroup) o); if (SwingUtilities.isLeftMouseButton(e)) { if (isExpanded(selPath)) collapsePath(selPath); else expandPath(selPath); } } else { JIDStatusGroup group = null; JIDStatus jidStatus = null; if (o instanceof PrimaryJIDStatus) { group = ((JIDStatusGroup) selPath.getPathComponent(selPath.getPathCount() - 2)); jidStatus = ((PrimaryJIDStatus) o).getJIDPrimaryStatus(); } else if (o instanceof JIDStatus) { group = ((JIDStatusGroup) selPath.getPathComponent(selPath.getPathCount() - 3)); jidStatus = (JIDStatus)o; } else return; maybeShowPopup(e, jidStatus, group);//cde if (SwingUtilities.isLeftMouseButton(e)) { lastTreePath = selPath; //check if double or single mouseclick, needed // because double click= 2 single clicks if (timer.isRunning()) { timer.stop(); sendChat(jidStatus); } else { timer.restart(); } } } } } public void mouseReleased(MouseEvent e) { TreePath selPath = getPathForLocation(e.getX(), e.getY()); if (selPath != null) { Object o = selPath.getLastPathComponent(); if (o instanceof JIDStatusGroup) {//group maybeShowGroupPopup(e, (JIDStatusGroup) o); } if (o instanceof PrimaryJIDStatus) { JIDStatusGroup group = ((JIDStatusGroup) selPath.getPathComponent(selPath.getPathCount() - 2)); maybeShowPopup(e, ((PrimaryJIDStatus) o).getJIDPrimaryStatus(), group); } else if (o instanceof JIDStatus) { JIDStatusGroup group = ((JIDStatusGroup) selPath.getPathComponent(selPath.getPathCount() - 3)); maybeShowPopup(e, (JIDStatus) o, group); } } } 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 || o instanceof JIDStatus) setSelectionPath(selPath); } } }); } public void updateLF() { SwingUtilities.updateComponentTreeUI(popupMenu); SwingUtilities.updateComponentTreeUI(groupPopupMenu); } private void createGroupPopupMenu() { groupPopupMenu = new JPopupMenu(); JMenuItem menuItem = null; menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Remove",menuItem,true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(main, MessageFormat.format(I18N.gettext("main.popup.Really_remove_{0}?_All_JIDs_in_this_group_will_be_removed!"), new Object[] {currentGroup}), I18N.gettext("main.popup.Remove_Group"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { for (Iterator i = currentJIDStatusGroup.iterator(); i.hasNext();) { PrimaryJIDStatus primary = (PrimaryJIDStatus) i.next(); if (primary.hasMultiple()) { for (Iterator j = primary.getOtherJidStatussen(); j.hasNext();) { groupRemove((JIDStatus) j.next()); } } groupRemove(primary.getJIDPrimaryStatus()); } } } private void groupRemove(JIDStatus jidStatus) { JID to = jidStatus.getJID(); if (jidStatus.groupCount() == 1) { if (JOptionPane.showConfirmDialog(main, MessageFormat.format(I18N.gettext( "main.popup.{0}_is_in_the_last_group,_remove_completely?") , new Object[] {to.toString()}) , I18N.gettext("main.main.rostermenu.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)); } } else { List groups = jidStatus.getGroupsCopy(); groups.remove(currentGroup); IQXRoster roster = new IQXRoster(new RosterItem(to, jidStatus.getNick(), null, null, groups)); backend.send(new InfoQuery("set", roster)); } } }); groupPopupMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Rename",menuItem,true); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String groupName = JOptionPane.showInputDialog(main, MessageFormat.format(I18N.gettext("main.popup.Rename_{0}_to"),new Object[] {currentGroup})); if (groupName == null) return; for (Iterator i = currentJIDStatusGroup.iterator(); i.hasNext();) { PrimaryJIDStatus primary = (PrimaryJIDStatus) i.next(); if (primary.hasMultiple()) { for (Iterator j = primary.getOtherJidStatussen(); j.hasNext();) { renameGroup(groupName, (JIDStatus) j.next()); } } renameGroup(groupName, primary.getJIDPrimaryStatus()); } } private void renameGroup(String groupName, JIDStatus jidStatus) { List groups = jidStatus.getGroupsCopy(); groups.remove(currentGroup); groups.add(groupName); IQXRoster roster = new IQXRoster(new RosterItem(jidStatus.getJID(), jidStatus.getNick(), null, null, groups)); backend.send(new InfoQuery("set", roster)); } }); groupPopupMenu.add(menuItem); } public void createPopupMenu() { popupMenu = new JPopupMenu(); JMenuItem menuItem = null; JMenu subMenu = null; menuItem = new JMenuItem(); if(Preferences.getBoolean("jeti","rostermenuShowMessage",true)) { I18N.setTextAndMnemonic("main.main.rostermenu.Message",menuItem,true); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendMessage(currentJIDStatus); } }); popupMenu.add(menuItem); } if(Preferences.getBoolean("jeti","rostermenuShowChat",true)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Chat",menuItem,true); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendChat(currentJIDStatus); } }); popupMenu.add(menuItem); } if(Preferences.getBoolean("jeti","rostermenuSubscriptions",true)) { subMenu = new JMenu(I18N.gettext("main.main.rostermenu.Subscriptions")); menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Subscribe_from")); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Subscribe_from",menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new Presence(currentJIDStatus.getJID(), "subscribe")); } }); subMenu.add(menuItem); menuItem = new JMenuItem(I18N.gettext("main.main.rostermenu.Unsubscribe_from")); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Unsubscribe_from",menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new Presence(currentJIDStatus.getJID(), "unsubscribe")); } }); subMenu.add(menuItem); popupMenu.add(subMenu); } if(Preferences.getBoolean("jeti","rostermenuRemove",true)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Remove",menuItem,true); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /** * @Author - Alexandre Correia - alexandrecorreia@celepar.pr.gov.br * @Date - 14/10/2008 * Criada a variavel String nickName para mostrar o nickname do usuario ao inves do UID * */ JID to = currentJIDStatus.getJID(); String nickName = currentJIDStatus.getNick(); if (JOptionPane.showConfirmDialog(main, MessageFormat.format(I18N.gettext("main.popup.Really_remove_{0}_from_all_groups_?"), new Object[] { nickName }),I18N.gettext("main.main.rostermenu.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); } if(Preferences.getBoolean("jeti","rostermenuRename",true)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Rename",menuItem,true); menuItem.addActionListener(new 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); } if(Preferences.getBoolean("jeti","rostermenuGroup",true)) { subMenu = new JMenu(); I18N.setTextAndMnemonic("main.main.rostermenu.Group",subMenu); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Change_Group", menuItem,true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { new GroupDialog(currentGroup, currentJIDStatus, backend).setVisible(true); } }); subMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Add_to_Group", menuItem, true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { new GroupDialog(currentJIDStatus, backend).setVisible(true); } }); subMenu.add(menuItem); menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Remove_from_group", menuItem,true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { JID to = currentJIDStatus.getJID(); if (currentJIDStatus.groupCount() == 1) { if (JOptionPane.showConfirmDialog(main , MessageFormat.format( I18N.gettext("main.popup.{0}_is_in_the_last_group,_remove_completely?") ,new Object[]{to.toString() }) , I18N.gettext("main.main.rostermenu.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)); } } else { List groups = currentJIDStatus.getGroupsCopy(); groups.remove(currentGroup); IQXRoster roster = new IQXRoster(new RosterItem(to, currentJIDStatus.getNick(), null, null, groups)); backend.send(new InfoQuery("set", roster)); } } }); subMenu.add(menuItem); popupMenu.add(subMenu); } if (onlineTree) { if(Preferences.getBoolean("jeti","rostermenuLocalTime",true) && !Preferences.getBoolean("jeti", "privacy", false)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Time", menuItem, true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQTime())); } }); popupMenu.add(menuItem); } if(Preferences.getBoolean("jeti","rostermenuLocalVersion",true) && !Preferences.getBoolean("jeti", "privacy", false)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Local_Version", menuItem, true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getCompleteJID(), "get", new IQVersion())); } }); popupMenu.add(menuItem); } } else { if(Preferences.getBoolean("jeti","rostermenuLastSeen",true)) { menuItem = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Last_Seen",menuItem,true); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { backend.send(new InfoQuery(currentJIDStatus.getJID(), "get", new IQLast())); } }); popupMenu.add(menuItem); } } if(Preferences.getBoolean("jeti","rostermenuInvisible",true)) { final JMenuItem menuItem3 = new JMenuItem(); I18N.setTextAndMnemonic("main.main.rostermenu.Set_Invisible",menuItem3); menuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if(menuItem3.getText().equals(I18N.gettext("main.main.rostermenu.Set_Invisible"))) { I18N.setTextAndMnemonic("main.main.rostermenu.Set_Visible",menuItem3); backend.send(new Presence(currentJIDStatus.getJID(), "invisible")); } else { I18N.setTextAndMnemonic("main.main.rostermenu.Set_Invisible",menuItem3); backend.send(new Presence(currentJIDStatus.getJID(),"available")); } } }); popupMenu.add(menuItem3); } if (menuItems != null) { for (Iterator i = menuItems.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); JMenuItem item = new JMenuItem((String) entry.getKey()); final RosterMenuListener listener = (RosterMenuListener) entry.getValue(); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { listener.actionPerformed(currentJIDStatus, currentJIDStatusGroup2); } }); popupMenu.add(item); } } } public void addToMenu(String name, RosterMenuListener listener) { if (menuItems == null) menuItems = new HashMap(10); menuItems.put(name, listener); if (popupMenu != null) createPopupMenu(); } public void removeFromMenu(String name) { if (menuItems == null) return; menuItems.remove(name); if (menuItems.isEmpty()) menuItems = null; if (popupMenu != null) createPopupMenu(); } public List getOpenGroups() { List tempList = new LinkedList(); JIDStatusTree tree = ((JIDStatusTree) getModel().getRoot()); if (tree == null) return null; for (int i = 0; i < tree.getSize(); i++) { TreePath path = new TreePath(new Object[] { tree, tree.get(i)}); if (isExpanded(path)) { tempList.add(tree.get(i).toString()); } } return tempList; } public void openGroups(JetiPrivateRosterExtension extension) { String[] groups = extension.getOpenGroups(); JIDStatusTree tree = ((JIDStatusTree) getModel().getRoot()); if (groups == null) return; for (int i = 0; i < groups.length; i++) { if(tree.existGroup(groups[i])) { TreePath path = new TreePath(new Object[] { tree, tree.getGroup(groups[i])}); if (treeExpander != null) { treeExpander.expand(path); } else { 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, JIDStatusGroup group) { if (e.isPopupTrigger()) { currentJIDStatus = jidStatus; currentGroup = group.getName(); currentJIDStatusGroup2 = group; popupMenu.show(e.getComponent(), e.getX(), e.getY()); } } private void maybeShowGroupPopup(MouseEvent e, JIDStatusGroup jidStatusGroup) { if (e.isPopupTrigger()) { currentJIDStatusGroup = jidStatusGroup; currentGroup = jidStatusGroup.getName(); groupPopupMenu.show(e.getComponent(), e.getX(), e.getY()); } } JIDStatus tooltipjids; public String getToolTipText(MouseEvent ev) { if ( ev == null) return null; TreePath path = getPathForLocation(ev.getX(), ev.getY()); if ( path != null ) { JIDStatus jidStatus = null; Object o = path.getLastPathComponent(); if (o instanceof PrimaryJIDStatus) { jidStatus = ((PrimaryJIDStatus) o).getJIDPrimaryStatus(); } else if (o instanceof JIDStatus) { jidStatus = ((JIDStatus) o); } else { return null; } String statusMsg; if (jidStatus.getStatus() != null) { statusMsg = jidStatus.getStatus(); } else { statusMsg = ""; } String waitingStatus = jidStatus.getWaiting(); if ( jidStatus.getWaiting() != null ) { waitingStatus = I18N.gettext("main.main.roster.Waiting_Status") + " " + jidStatus.getWaiting(); } else { waitingStatus = ""; } String htmlReturn = ""; htmlReturn = ""; htmlReturn += "
";
htmlReturn += "Nick: " + jidStatus.getNick() + " "; htmlReturn += "" + I18N.gettext("main.main.roster.Status") + " " + Presence.toLongShow(jidStatus.getShow()) + " "; htmlReturn += "" + I18N.gettext("main.main.statusmenu.Status_message") + " " + statusMsg + " "; htmlReturn += "" + I18N.gettext("main.main.roster.Subscription") + " " + jidStatus.getSubscription() + " "; htmlReturn += "" + waitingStatus + ""; htmlReturn += " | ";
htmlReturn += "
" + nu.fw.jeti.applet.Jeti.NAMECOMPANY + " | "; htmlReturn += "