📄 buddylistpopupmenu.java
字号:
/* * Copyright (C) 2003 Adam Olsen * 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother.menus;import java.awt.*;import java.awt.event.*;import java.net.URL;import java.io.*;import java.util.*;import javax.swing.*;import org.jivesoftware.smack.*;import org.jivesoftware.smackx.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smack.filter.PacketFilter;import org.jivesoftware.smack.filter.OrFilter;import org.jivesoftware.smack.filter.PacketTypeFilter;import org.jivesoftware.smack.packet.*;import net.infonode.tabbedpanel.*;import com.valhalla.jbother.groupchat.*;import com.valhalla.gui.*;import com.valhalla.jbother.*;import com.valhalla.jbother.BuddyList;import com.valhalla.jbother.jabber.*;import com.valhalla.jbother.jabber.smack.*;/** * The menu that pops up when you right click on a user in your roster * *@author Adam Olsen *@author Andrey Zakirov *@created June 23, 2005 *@version 1.1 */public class BuddyListPopupMenu extends JPopupMenu implements UserChooserListener { /** * Description of the Field */ protected ResourceBundle resources = ResourceBundle.getBundle("JBotherBundle", Locale.getDefault()); /** * Description of the Field */ protected JMenuItem removeBuddyItem = new JMenuItem(resources.getString("removeFromRoster")); /** * Description of the Field */ protected JMenuItem modifyBuddyItem = new JMenuItem(resources.getString("modify")); /** * Description of the Field */ protected JMenu resourceMenu = new JMenu(resources.getString("resources")); /** * Description of the Field */ protected JMenu authMenu = new JMenu(resources.getString("authorization")); /** * Description of the Field */ protected JMenu infoMenu = new JMenu(resources.getString("informationMenu")); /** * Description of the Field */ protected JMenu editMenu = new JMenu(resources.getString("editMenu")); /** * Description of the Field */ protected JMenuItem requestSubscription = new JMenuItem(resources.getString("requestNotification")); /** * Description of the Field */ protected JMenuItem resendSubscription = new JMenuItem(resources.getString("resendNotification")); /** * Description of the Field */ protected JMenuItem removeSubscription = new JMenuItem(resources.getString("removeNotification")); /** * Description of the Field */ protected JMenuItem chatItem = new JMenuItem(resources.getString("openChatDialog")); /** * Description of the Field */ protected JMenuItem messageItem = new JMenuItem(resources.getString("messageWindow")); /** * Description of the Field */ protected JMenuItem logItem = new JMenuItem(resources.getString("viewLog")); /** * Description of the Field */ protected JMenuItem infoItem = new JMenuItem(resources.getString("getInfo")); /** * Description of the Field */ protected JMenuItem blockItem = new JMenuItem(resources.getString("blockUser")); /** * Description of the Field */ protected JMenuItem bindPubKeyItem = new JMenuItem(resources.getString("gnupgBindPublicKey")); /** * Description of the Field */ protected JMenuItem unbindPubKeyItem = new JMenuItem(resources.getString("gnupgUnbindPublicKey")); protected JMenu misc = new JMenu(resources.getString("misc")); protected JMenu invite = new JMenu(resources.getString("inviteToMuc")); protected JMenuItem noItem = new JMenuItem(resources.getString("noMucs")); protected JMenu rosterExchange = new JMenu(resources.getString("sendRosterItems")); protected JMenuItem individualItems = new JMenuItem(resources.getString("individualItems")); protected JMenuItem entireRoster = new JMenuItem(resources.getString("entireRoster")); /** * Description of the Field */ protected BuddyStatus buddy; private JTree tree; /** * Description of the Field */ protected JMenuItem sendFileItem = new JMenuItem(resources.getString("sendFile")); protected JMenu sendFileMenu = new JMenu(resources.getString("sendFile")); /** * Creates the menu */ public BuddyListPopupMenu() { MenuActionListener listener = new MenuActionListener(); noItem.setEnabled(false); infoItem.addActionListener(listener); removeBuddyItem.addActionListener(listener); chatItem.addActionListener(listener); messageItem.addActionListener(listener); modifyBuddyItem.addActionListener(listener); requestSubscription.addActionListener(listener); logItem.addActionListener(listener); resendSubscription.addActionListener(listener); removeSubscription.addActionListener(listener); blockItem.addActionListener(listener); sendFileItem.addActionListener(listener); bindPubKeyItem.addActionListener(listener); unbindPubKeyItem.addActionListener(listener); entireRoster.addActionListener(listener); individualItems.addActionListener(listener); authMenu.add(requestSubscription); authMenu.add(resendSubscription); authMenu.add(removeSubscription); authMenu.add(blockItem); infoMenu.add(infoItem); infoMenu.add(logItem); editMenu.add(modifyBuddyItem); editMenu.add(removeBuddyItem); misc.add(invite); misc.add(rosterExchange); add(chatItem); add(messageItem); addSeparator(); add(infoMenu); add(editMenu); addSeparator(); add(resourceMenu); add(authMenu); add(misc); addSeparator(); add(sendFileItem); } /** * Gets the from attribute of the BuddyListPopupMenu object * *@return The from value */ protected String getFrom() { if (BuddyList.getInstance().checkConnection()) { return BuddyList.getInstance().getConnection().getUser(); } return ""; } /** * Requests subscription to a user * *@param type the type of subscription to send */ private void subscriptionHandler(Presence.Type type) { Presence presence = new Presence(type); presence.setTo(buddy.getUser()); if (BuddyList.getInstance().checkConnection()) { BuddyList.getInstance().getConnection().sendPacket(presence); } else { BuddyList.getInstance().connectionError(); } } /** * Listens for different items to get clicked on in the popup menu * *@author Adam Olsen *@created June 23, 2005 *@version 1.0 */ class MenuActionListener implements ActionListener { /** * Description of the Method * *@param e Description of the Parameter */ public void actionPerformed(ActionEvent e) { if (e.getSource() == removeBuddyItem) { removeBuddyHandler(); } else if (e.getSource() == chatItem) { BuddyList.getInstance().getBuddyListTree().initiateConversation(buddy); } else if (e.getSource() == messageItem) { MessagePanel panel = new MessagePanel(); panel.setBuddy(buddy); if (!(buddy instanceof MUCBuddyStatus)) { String to = buddy.getUser(); if (buddy.size() > 0) { to += "/" + buddy.getHighestResource(); } panel.setTo(to); } else { panel.setTo(buddy.getUser()); } panel.setFrom(getFrom()); MessageDelegator.getInstance().showPanel(panel); MessageDelegator.getInstance().frontFrame(panel); panel.getSubjectField().grabFocus(); } else if (e.getSource() == modifyBuddyItem) { modifyBuddyHandler(); } else if (e.getSource() == requestSubscription) { subscriptionHandler(Presence.Type.SUBSCRIBE); } else if (e.getSource() == removeSubscription) { subscriptionHandler(Presence.Type.UNSUBSCRIBED); } else if (e.getSource() == resendSubscription) { subscriptionHandler(Presence.Type.SUBSCRIBED); } else if (e.getSource() == infoItem) { if (!(buddy instanceof MUCBuddyStatus)) { new InformationViewerDialog(buddy.getAddress()); } else { new InformationViewerDialog(buddy.getUser()); } } else if (e.getSource() == logItem) { new LogViewerDialog(buddy.getConversation(), buddy.getUser()); } else if (e.getSource() == blockItem) { blockHandler(); } else if (e.getSource() == sendFileItem) { sendFileHandler(); } else if (e.getSource() == bindPubKeyItem) { bindPubKeyHandler(); } else if (e.getSource() == unbindPubKeyItem) { unbindPubKeyHandler(); } else if (e.getSource() == individualItems) { UserChooser chooser = new UserChooser(BuddyList.getInstance().getContainerFrame(), resources.getString("sendRosterItems"), resources.getString("selectSendItems"), true); chooser.addListener(BuddyListPopupMenu.this); chooser.setVisible(true); } else if(e.getSource() == entireRoster) { int result = JOptionPane.showConfirmDialog(BuddyList.getInstance().getContainerFrame(), resources.getString("sureSendEntireRoster"), resources.getString("sendRosterItems"), JOptionPane.YES_NO_OPTION); if(result != JOptionPane.YES_OPTION) return; RosterExchangeManager manager = ConnectorThread.getInstance().getExchangeManager(); Roster roster = BuddyList.getInstance().getConnection().getRoster(); manager.send(roster, buddy.getUser()); } } } public void usersChosen(UserChooser.Item[] items) { Message message = new Message(buddy.getUser()); RosterExchange r = new RosterExchange(); message.addExtension(r); Roster roster = BuddyList.getInstance().getConnection().getRoster(); for(int i = 0; i < items.length; i++) { RosterEntry entry = roster.getEntry(items[i].getJID()); r.addRosterEntry(entry); } BuddyList.getInstance().getConnection().sendPacket(message); } /** * Blocks the user */ protected void blockHandler() { // just adds the user to the blocked list in buddy list File blockedFile = new File(JBother.profileDir + File.separator + "blocked"); BuddyList.getInstance().getBlockedUsers().put(buddy.getUser(), "blocked"); // and then writes all of them to the blocked users file try { FileWriter fw = new FileWriter(blockedFile); PrintWriter out = new PrintWriter(fw); Iterator i = BuddyList.getInstance().getBlockedUsers().keySet().iterator(); while (i.hasNext()) { out.println((String) i.next()); } fw.close(); } catch (IOException ex) { Standard.warningMessage(null, resources.getString("blockUser"), resources.getString("problemWritingBlockedFile"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -