001    /*
002     Copyright (C) 2003 Adam Olsen
003    
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008    
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013    
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    package com.valhalla.jbother.groupchat;
019    
020    import java.awt.Component;
021    import java.awt.event.ActionEvent;
022    import java.awt.event.ActionListener;
023    import java.util.Locale;
024    import java.util.ResourceBundle;
025    
026    import javax.swing.JMenu;
027    import javax.swing.JMenuItem;
028    import javax.swing.JOptionPane;
029    import javax.swing.SwingUtilities;
030    
031    import org.jivesoftware.smack.XMPPException;
032    import org.jivesoftware.smackx.packet.MUCUser;
033    
034    import com.valhalla.jbother.JBotherLoader;
035    import com.valhalla.jbother.jabber.BuddyStatus;
036    import com.valhalla.jbother.jabber.MUCBuddyStatus;
037    import com.valhalla.jbother.menus.BuddyListPopupMenu;
038    
039    /**
040     * The menu that pops up if someone right clicks on a user in a groupchat
041     *
042     * @author Adam Olsen
043     * @version 1.0
044     */
045    public class NickListPopupMenu extends BuddyListPopupMenu {
046        private ResourceBundle resources = ResourceBundle.getBundle(
047                "JBotherBundle", Locale.getDefault());
048    
049        private ChatRoomPanel panel;
050    
051        private JMenuItem grantAdmin = new JMenuItem(resources
052                .getString("grantAdmin")), grantMembership = new JMenuItem(
053                resources.getString("grantMembership")),
054                grantModerator = new JMenuItem(resources
055                        .getString("grantModerator")),
056                grantOwnership = new JMenuItem(resources
057                        .getString("grantOwnership")), grantVoice = new JMenuItem(
058                        resources.getString("grantVoice")),
059    
060                revokeAdmin = new JMenuItem(resources.getString("revokeAdmin")),
061                revokeMembership = new JMenuItem(resources
062                        .getString("revokeMembership")),
063                revokeModerator = new JMenuItem(resources
064                        .getString("revokeModerator")),
065                revokeOwnership = new JMenuItem(resources
066                        .getString("revokeOwnership")),
067                revokeVoice = new JMenuItem(resources.getString("revokeVoice")),
068    
069                kick = new JMenuItem(resources.getString("kickParticipant")),
070                ban = new JMenuItem(resources.getString("banParticipant"));
071    
072        private JMenu adminMenu = new JMenu(resources.getString("administration"));
073    
074        /**
075         * Sets up the popup menu
076         */
077        public NickListPopupMenu(ChatRoomPanel panel) {
078            removeAll();
079            MenuActionListener listener = new MenuActionListener();
080            this.panel = panel;
081    
082            grantAdmin.addActionListener(listener);
083            grantMembership.addActionListener(listener);
084            grantModerator.addActionListener(listener);
085            grantOwnership.addActionListener(listener);
086            grantVoice.addActionListener(listener);
087    
088            revokeAdmin.addActionListener(listener);
089            revokeMembership.addActionListener(listener);
090            revokeModerator.addActionListener(listener);
091            revokeOwnership.addActionListener(listener);
092            revokeVoice.addActionListener(listener);
093    
094            kick.addActionListener(listener);
095            ban.addActionListener(listener);
096    
097            add(chatItem);
098            add(messageItem);
099            add(infoItem);
100            add(logItem);
101            addSeparator();
102            add(adminMenu);
103            if (JBotherLoader.isGPGEnabled())
104                addSeparator();
105    
106            grantAdmin.setActionCommand("grantAdmin");
107            grantMembership.setActionCommand("grantMembership");
108            grantModerator.setActionCommand("grantModerator");
109            grantOwnership.setActionCommand("grantOwnership");
110            grantVoice.setActionCommand("grantVoice");
111    
112            revokeAdmin.setActionCommand("revokeAdmin");
113            revokeMembership.setActionCommand("revokeMembership");
114            revokeModerator.setActionCommand("revokeModerator");
115            revokeOwnership.setActionCommand("revokeOwnership");
116            revokeVoice.setActionCommand("revokeVoice");
117    
118            adminMenu.add(grantAdmin);
119            adminMenu.add(grantMembership);
120            adminMenu.add(grantModerator);
121            adminMenu.add(grantOwnership);
122            adminMenu.add(grantVoice);
123            adminMenu.addSeparator();
124    
125            adminMenu.add(revokeAdmin);
126            adminMenu.add(revokeMembership);
127            adminMenu.add(revokeModerator);
128            adminMenu.add(revokeOwnership);
129            adminMenu.add(revokeVoice);
130    
131            adminMenu.addSeparator();
132    
133            adminMenu.add(kick);
134            adminMenu.add(ban);
135        }
136    
137        protected String getFrom() {
138            return panel.getUser();
139        }
140    
141        /**
142         * Listens for a double mouse click, or a right click
143         *
144         * @author Adam Olsen
145         * @version 1.0
146         */
147        class MenuActionListener implements ActionListener {
148            public void actionPerformed(ActionEvent e) {
149                if (e.getSource() == kick) {
150                    String result = (String) JOptionPane.showInputDialog(null,
151                            resources.getString("enterReason"), resources
152                                    .getString("kickParticipant"),
153                            JOptionPane.QUESTION_MESSAGE, null, null, "No reason specified");
154    
155                    if (result == null)
156                        return;
157                    Thread thread = new Thread(new KickThread(result));
158                    thread.start();
159                } else if (e.getSource() == ban) {
160                    String result = (String) JOptionPane.showInputDialog(null,
161                            resources.getString("enterReason"), resources
162                                    .getString("banParticipant"),
163                            JOptionPane.QUESTION_MESSAGE, null, null, "No reason specified");
164    
165                    if (result == null)
166                        return;
167                    Thread thread = new Thread(new BanThread(result));
168                    thread.start();
169                }
170    
171                // MUC stuff
172                else {
173                    JMenuItem item = (JMenuItem) e.getSource();
174                    panel.doAction(item.getActionCommand(), (MUCBuddyStatus) buddy);
175                }
176            }
177        }
178    
179        class KickThread implements Runnable {
180            String reason;
181    
182            public KickThread(String reason) {
183                this.reason = reason;
184            }
185    
186            public void run() {
187                com.valhalla.Logger.debug("kicking " + buddy.getUser());
188    
189                try {
190                    panel.getChat().kickParticipant(buddy.getName(), reason);
191                } catch (final XMPPException ex) {
192                    SwingUtilities.invokeLater(new Runnable() {
193                        public void run() {
194                            String message = "";
195                            if (ex.getXMPPError() != null) {
196                                message = resources.getString("xmppError"
197                                        + ex.getXMPPError().getCode());
198                            } else
199                                message = ex.getMessage();
200                            panel.serverNoticeMessage(message);
201                        }
202                    });
203                }
204            }
205        }
206    
207        class BanThread implements Runnable {
208            String reason;
209    
210            public BanThread(String reason) {
211                this.reason = reason;
212            }
213    
214            public void run() {
215                MUCUser user = ((MUCBuddyStatus) buddy).getMUCUser();
216                if (user == null)
217                    return;
218    
219                MUCUser.Item item = user.getItem();
220                if (item == null)
221                    return;
222    
223                String jid = item.getJid();
224                if (jid == null)
225                    return;
226    
227                try {
228                    panel.getChat().banUser(jid, reason);
229                } catch (final XMPPException ex) {
230                    SwingUtilities.invokeLater(new Runnable() {
231                        public void run() {
232                            String message = "";
233                            if (ex.getXMPPError() != null) {
234                                message = resources.getString("xmppError"
235                                        + ex.getXMPPError().getCode());
236                            } else
237                                message = ex.getMessage();
238                            panel.serverNoticeMessage(message);
239                        }
240                    });
241                }
242            }
243        }
244    
245        /**
246         * Displays the popup menu
247         *
248         * @param comp
249         *            the component to pop the menu up on
250         * @param x
251         *            the x coordinate
252         * @param y
253         *            the y coordinate of the menu
254         * @param buddy
255         *            the BuddyStatus that was clicked on
256         */
257        public void showMenu(Component comp, int x, int y, BuddyStatus buddy) {
258            this.buddy = buddy;
259    
260            if (JBotherLoader.isGPGEnabled()) {
261                remove(unbindPubKeyItem);
262                remove(bindPubKeyItem);
263    
264                if (buddy.getPubKey() != null
265                        && !(this instanceof NickListPopupMenu)) {
266                    add(unbindPubKeyItem);
267                } else {
268                    add(bindPubKeyItem);
269                }
270            }
271    
272            validate();
273            show(comp, x, y);
274        }
275    }