001    /*
002     author: Lukasz Wiechec
003     */
004    package com.valhalla.jbother.menus;
005    
006    import java.awt.Component;
007    import java.awt.event.ActionEvent;
008    import java.awt.event.ActionListener;
009    import java.util.Locale;
010    import java.util.ResourceBundle;
011    
012    import javax.swing.JMenuItem;
013    import javax.swing.JOptionPane;
014    import javax.swing.JPopupMenu;
015    import javax.swing.JTree;
016    import javax.swing.SwingUtilities;
017    
018    import org.jivesoftware.smack.RosterGroup;
019    
020    import com.valhalla.gui.Standard;
021    import com.valhalla.jbother.*;
022    import com.valhalla.jbother.jabber.BuddyGroup;
023    
024    /**
025     * The menu that pops up when you right click on a group in roster
026     *
027     * @author Lukasz Wiechec
028     * @created March 2, 2005
029     * @version 1.0
030     */
031    public class GroupPopupMenu extends JPopupMenu {
032        private ResourceBundle resources = ResourceBundle.getBundle(
033                "JBotherBundle", Locale.getDefault());
034    
035        private JMenuItem sendMessageToGroupItem = new JMenuItem(resources
036                .getString("sendMessageToGroup"));
037    
038        private JMenuItem rename = new JMenuItem(resources.getString("renameGroup"));
039    
040        private String userList;
041    
042        private JTree tree;
043    
044        private BuddyGroup group;
045    
046        /**
047         * Creates the menu
048         */
049        public GroupPopupMenu() {
050            MenuActionListener listener = new MenuActionListener();
051            sendMessageToGroupItem.addActionListener(listener);
052            rename.addActionListener(listener);
053    
054            add(sendMessageToGroupItem);
055            add(rename);
056        }
057    
058        /**
059         * Requests sending message to a group
060         */
061        private void sendMessageToGroupHandler() {
062            // question: how to handle users that not belong to any group?
063            // they are displayed in BuddyListTree in group "Contacts"
064            // 1) get it from the tree!
065            com.valhalla.Logger.debug("message to group handler called!");
066    
067            MessagePanel groupMessageWindow = new MessagePanel();
068            groupMessageWindow.setTo(userList);
069            // trim the last "; "
070            groupMessageWindow.setVisible(true);
071            MessageDelegator.getInstance().showPanel(groupMessageWindow);
072            MessageDelegator.getInstance().frontFrame(groupMessageWindow);
073            groupMessageWindow.getSubjectField().grabFocus();
074    
075        }
076    
077        private void renameHandler() {
078            RosterGroup g = ConnectorThread.getInstance().getRoster()
079                    .getGroup(group.getGroupName());
080            if (g == null) {
081                Standard.warningMessage(
082                        BuddyList.getInstance().getContainerFrame(), resources
083                                .getString("renameGroup"), resources
084                                .getString("cannotRenameGroup"));
085                return;
086            }
087    
088            final String result = (String) JOptionPane.showInputDialog(BuddyList
089                    .getInstance().getContainerFrame(), resources
090                    .getString("newGroupName"), resources.getString("renameGroup"),
091                    JOptionPane.QUESTION_MESSAGE, null, null, "");
092    
093            if (result == null || result.equals(""))
094                return;
095    
096            new Thread(new RenameThread(result)).start();
097        }
098    
099        public String getUserList()
100        {
101            return userList;
102        }
103    
104        class RenameThread implements Runnable {
105            private String newName;
106    
107            public RenameThread(String name) {
108                this.newName = name;
109            }
110    
111            public void run() {
112                if (!BuddyList.getInstance().checkConnection())
113                    return;
114                RosterGroup g = ConnectorThread.getInstance().getRoster()
115                        .getGroup(group.getGroupName());
116                if (g == null)
117                    return;
118    
119                String error = null;
120                try {
121                    g.setName(newName);
122                } catch (Exception ex) {
123                    error = ex.getMessage();
124                }
125    
126                final String er = error;
127    
128                try {
129                    Thread.sleep(1000);
130                } catch (Exception yo) {
131                }
132    
133                SwingUtilities.invokeLater(new Runnable() {
134                    public void run() {
135                        if (er != null) {
136                            Standard.warningMessage(BuddyList.getInstance()
137                                    .getContainerFrame(), resources
138                                    .getString("renameGroup"), er);
139                        }
140    
141                        BuddyList.getInstance().getBuddyListTree().reloadBuddies();
142                    }
143                });
144            }
145        }
146    
147        /**
148         * Listens for different items to get clicked on in the popup menu
149         *
150         * @author Adam Olsen
151         * @created March 2, 2005
152         * @version 1.0
153         */
154        class MenuActionListener implements ActionListener {
155            /**
156             * Description of the Method
157             *
158             * @param e
159             *            Description of the Parameter
160             */
161            public void actionPerformed(ActionEvent e) {
162                if (e.getSource() == sendMessageToGroupItem) {
163                    sendMessageToGroupHandler();
164                } else if (e.getSource() == rename) {
165                    renameHandler();
166                }
167            }
168        }
169    
170        /**
171         * Shows the popup menu
172         *
173         * @param tree
174         *            the tree to show the menu on
175         * @param x
176         *            the x coordinate
177         * @param y
178         *            the y coordinate
179         * @param aUserList
180         *            the list of users (separated by '; ') to send the message to
181         */
182        public void showMenu(Component tree, int x, int y, String aUserList,
183                BuddyGroup group) {
184            this.userList = aUserList;
185            this.tree = (JTree) tree;
186            this.group = group;
187    
188            validate();
189    
190            show(tree, x, y);
191        }
192    }
193