⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buddylistpopupmenu.java

📁 网站即时通讯系统
💻 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.smack.filter.PacketFilter;import org.jivesoftware.smack.filter.OrFilter;import org.jivesoftware.smack.filter.PacketTypeFilter;import org.jivesoftware.smack.packet.*;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 * @version 1.1*/public class BuddyListPopupMenu extends JPopupMenu{	protected ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );	protected JMenuItem removeBuddyItem = new JMenuItem( resources.getString( "removeFromRoster" ) );	protected JMenuItem modifyBuddyItem = new JMenuItem( resources.getString( "modify" ) );	protected JMenu     resourceMenu = new JMenu( resources.getString( "resources" ) );	protected JMenu authMenu = new JMenu( resources.getString( "authorization" ) );    protected JMenu infoMenu = new JMenu( resources.getString( "informationMenu" ) );    protected JMenu editMenu = new JMenu( resources.getString( "editMenu" ) );	protected JMenuItem requestSubscription = new JMenuItem( resources.getString( "requestNotification" ) );	protected JMenuItem resendSubscription = new JMenuItem( resources.getString( "resendNotification" ) );	protected JMenuItem removeSubscription = new JMenuItem( resources.getString( "removeNotification" ) );	protected JMenuItem chatItem = new JMenuItem( resources.getString( "openChatDialog" ) );	protected JMenuItem messageItem = new JMenuItem( resources.getString( "messageWindow" ) );	protected JMenuItem logItem = new JMenuItem( resources.getString( "viewLog" ) );	protected JMenuItem infoItem = new JMenuItem( resources.getString( "getInfo" ) );	protected JMenuItem blockItem = new JMenuItem( resources.getString( "blockUser" ) );	protected JMenuItem bindPubKeyItem = new JMenuItem( resources.getString( "gnupgBindPublicKey" ) );	protected JMenuItem unbindPubKeyItem = new JMenuItem( resources.getString( "gnupgUnbindPublicKey" ) );	protected BuddyStatus buddy;	private JTree tree;	protected JMenuItem sendFileItem = new JMenuItem ( resources.getString( "sendFile" ));	/**	 * Creates the menu	*/	public BuddyListPopupMenu()	{		MenuActionListener listener = new MenuActionListener();		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 );		authMenu.add( requestSubscription );		authMenu.add( resendSubscription );		authMenu.add( removeSubscription );		authMenu.add( blockItem );        infoMenu.add(infoItem);        infoMenu.add(logItem);        editMenu.add(modifyBuddyItem);        editMenu.add(removeBuddyItem);		add( chatItem );		add( messageItem );		addSeparator();        add(infoMenu);        add(editMenu);        addSeparator();		add( resourceMenu );		add( authMenu );        addSeparator();		add( sendFileItem );		//sendFileItem.setEnabled( false );	}	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	 * @version 1.0	*/	class MenuActionListener implements ActionListener	{		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.getUser() + "/" + buddy.getHighestResource() );				}				else {					new InformationViewerDialog( buddy.getUser() );				}			}			else if( e.getSource() == logItem )			{				new LogViewerDialog( null, 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();			}		}	}	/**	 * 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" ) ); return; }		BuddyList.getInstance().getBuddyListTree().removeBuddy( buddy, buddy.getGroup(), true );		Standard.noticeMessage( null, resources.getString( "blockUser" ), resources.getString( "userHasBeenBlocked" ) );	}	/**	 * Opens the AddBuddyDialog to modify a buddy	*/	protected void modifyBuddyHandler()	{		AddBuddyDialog buddyDialog = new AddBuddyDialog();		buddyDialog.setBuddy( buddy.getRosterEntry() );		buddyDialog.setVisible(true);	}	/**	 * Confirms buddy removal, and then removes the buddy	*/	protected void removeBuddyHandler()	{		if( !BuddyList.getInstance().checkConnection() )		{			BuddyList.getInstance().connectionError();			return;		}		int result = 1;		result = JOptionPane.showConfirmDialog( null, resources.getString( "sureRemoveContact" ), resources.getString( "removeFromRoster" ),			JOptionPane.YES_NO_OPTION );		if( result != 0 ) return;		RosterEntry entry = buddy.getRosterEntry();		buddy.setRemoved( true );		String gp = buddy.getGroup();		if( entry != null )		{			com.valhalla.Logger.debug( "Remove entry user: " + entry.getUser() );			BuddyList.getInstance().getBuddyListTree().removeBuddy( buddy, gp, true );			Iterator iterator = entry.getGroups();			while( iterator.hasNext() )			{				RosterGroup group = (RosterGroup)iterator.next();				group.removeEntry( entry );			}			RosterPacket packet = new RosterPacket();			packet.setType( IQ.Type.SET );			RosterPacket.Item item = new RosterPacket.Item( entry.getUser(), entry.getName() );			item.setItemType( RosterPacket.ItemType.REMOVE );			packet.addRosterItem( item );			BuddyList.getInstance().getConnection().sendPacket( packet );		}		else BuddyList.getInstance().getBuddyListTree().removeBuddy( buddy, gp, true );	}	/**	 * Updates the resources menu	*/	public void updateResourceMenu()	{		ResourceActionListener listener = new ResourceActionListener( buddy );		resourceMenu.removeAll();		Set keys = buddy.keySet();		Iterator i = keys.iterator();		while( i.hasNext() )		{			boolean na = false;			String key = (String)i.next();			if( key.equals( "N/A" ) )			{				na = true;				key = "None";			}			JMenuItem item = new JMenuItem( key );			if( !na )			{				Presence.Mode mode;				if( buddy.size() == 0 ) {					mode = null;					item.setForeground( Color.GRAY );				} else {					mode = buddy.getPresence( key );				}				ImageIcon icon = StatusIconCache.getStatusIcon(mode);				if (icon != null) item.setIcon(icon);				item.addActionListener( listener );			}			else item.setEnabled( false );			resourceMenu.add( item );		}	}  /**   * lets user choose the file to send and send it   */  private void sendFileHandler()  {    StreamInitiation si = new StreamInitiation();    // @todo: what if user has 2 clients and the one with highest priority does not handle <si>?    si.setTo(buddy.getUser() + "/" + buddy.getHighestResource());    si.setFrom(getFrom());    FileSendDialog sendfiledlg = new FileSendDialog(si);    PacketFilter filter =      new OrFilter( new PacketTypeFilter( StreamInitiation.class ) ,      new PacketTypeFilter( Streamhost.class));    PacketFilter orfilter =      new OrFilter( filter, new PacketTypeFilter (StreamhostUsed.class) );    BuddyList.getInstance().getConnection().addPacketListener(sendfiledlg, orfilter);    if(sendfiledlg.getFileDetails() != null) {      sendfiledlg.setVisible(true);    }  }	/**	 * binds Public Key to buddy	*/	private void bindPubKeyHandler()	{		KeySelectDialog dialog = new KeySelectDialog( "pub" );			dialog.showDialog();			if (dialog.getID() != null)			{				buddy.setPubKey( dialog.getID() );		}	}	/**	 * unbinds Public Key from buddy	*/	private void unbindPubKeyHandler()	{			buddy.setPubKey(null);	}	/**	 * Shows the popup menu	 * @param tree the tree to show the menu on	 * @param x the x coordinate	 * @param y the y coordinate	 * @param buddy the buddy that this menu should show up on	*/	public void showMenu( Component tree, int x, int y, BuddyStatus buddy )	{		this.buddy = buddy;		this.tree = (JTree)tree;		updateResourceMenu();		validate();		if( JBotherLoader.isGPGEnabled() )		{			remove (unbindPubKeyItem);			remove (bindPubKeyItem);			if (buddy.getPubKey() != null )			{				add( unbindPubKeyItem );			}			else			{				add( bindPubKeyItem );			}		}		show( tree, x, y );	}	/**	 * Listens for a resource to get clicked in the ResourceMenu	 */	class ResourceActionListener implements ActionListener	{		private BuddyStatus buddy;		public ResourceActionListener( BuddyStatus buddy )		{			this.buddy = buddy;		}		public void actionPerformed( ActionEvent e )		{			JMenuItem item = (JMenuItem)e.getSource();			BuddyList.getInstance().getBuddyListTree().initiateConversation( buddy );			ChatPanel window = (ChatPanel)buddy.getConversation();			window.getResourceBox().setSelectedItem( item.getText() );			window.getResourceBox().validate();		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -