📄 bsrostertreeview.java
字号:
package edu.ou.kmi.buddyspace.gui;
/*
* BSRosterTreeView.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 22 July 2002, 12:14
*/
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;
import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.util.*;
/**
* <code>BSRosterTreeView</code> is graphical display of roster using tree.
* It relies on <code>BSRosterBean</code>, <code>BSPresenceBean</code> and
* <code>BSMessengerBean</code>.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSRosterTreeView implements RosterListener, BSPresenceListener,
BSMessageListener, MouseListener,
ActionListener/*, TreeWillExpandListener*/ {
private TreeMap groups = null;
private Hashtable items = null;
private Hashtable relations = null;
private BSRosterBean rosterBean = null;
private BSPresenceBean presenceBean = null;
private BSMessengerBean messengerBean = null;
private BSRosterRestorableTree tree = null;
private JScrollPane scrollPane = null;
private DefaultMutableTreeNode root = null;
private DefaultTreeModel model = null;
private JMenuItem refreshMenuItem = null;
private JMenuItem addContactMenuItem = null;
private JMenuItem msgMenuItem = null;
private JMenuItem msgGroupOnlineMenuItem = null;
private JMenuItem msgWholeGroupMenuItem = null;
private JMenuItem chatMenuItem = null;
private JMenuItem sendFileMenuItem = null;
private JMenuItem deleteMenuItem = null;
private JMenuItem subscribeMenuItem = null;
private JMenuItem changeMenuItem = null;
private JMenuItem getPlanMenuItem = null;
private JMenu presenceMenu;
private JCheckBoxMenuItem onlinePresenceMenuItem;
private JCheckBoxMenuItem chatPresenceMenuItem;
private JCheckBoxMenuItem elsePresenceMenuItem;
private JCheckBoxMenuItem busyPresenceMenuItem;
private JCheckBoxMenuItem awayPresenceMenuItem;
private JCheckBoxMenuItem xaPresenceMenuItem;
private JCheckBoxMenuItem dndPresenceMenuItem;
private JCheckBoxMenuItem invisiblePresenceMenuItem;
private JMenuItem clearPresenceMenuItem;
private JMenuItem clearAllPresenceMenuItem;
private JCheckBoxMenuItem policyPresenceMenuItem;
private JMenuItem loginMenuItem = null;
private JMenuItem logoutMenuItem = null;
private JPopupMenu groupContextMenu = null;
private JPopupMenu buddyContextMenu = null;
private JPopupMenu resourceContextMenu = null;
private JPopupMenu componentContextMenu = null;
// IX panels stuff
JMenu relationsMenu = null;
JMenuItem relSupMenuItem = null;
JMenuItem relSubMenuItem = null;
JMenuItem relPeerMenuItem = null;
JMenuItem relContactMenuItem = null;
JMenuItem relDeleteMenuItem = null;
// end of IX panels stuff
private boolean onlyOnline = true;
final private String myResGroupName = "My resources";
private boolean expand = true;
private BSCore core = null;
private JID myJID = null;
private BSMainFrame mainFrame = null;
/** Constructor */
public BSRosterTreeView (BSMainFrame mainFrame, BSCore core) {
this.core = core;
this.mainFrame = mainFrame;
groups = new TreeMap();
items = new Hashtable();
if (core != null) {
setRosterBean(core.getRosterBean());
setPresenceBean(core.getPresenceBean());
setMessengerBean(core.getMessengerBean());
}
initComponents();
}
/** Sets rosterBean */
public void setRosterBean(BSRosterBean rosterBean) {
if (this.rosterBean != null)
this.rosterBean.removeRosterListener(this);
this.rosterBean = rosterBean;
if (rosterBean != null)
rosterBean.addRosterListener(this);
}
/** Sets presenceBean */
public void setPresenceBean(BSPresenceBean presenceBean) {
if (this.presenceBean != null)
this.presenceBean.removePresenceListener(this);
this.presenceBean = presenceBean;
if (presenceBean != null)
presenceBean.addPresenceListener(this);
}
/** Sets messengerBean */
public void setMessengerBean(BSMessengerBean messengerBean) {
if (this.messengerBean != null)
this.messengerBean.removeMessageListener(this);
this.messengerBean = messengerBean;
if (messengerBean != null)
messengerBean.addMessageListener(this);
}
/**
* Sets information about current user.
* That is used for displaying myResources...
*/
public void setUserInfo(String username, String resource, String server) {
myJID = new JID(username, server, resource);
}
/** Inits GUI components */
protected void initComponents() {
root = new DefaultMutableTreeNode("Roster");
model = new DefaultTreeModel(root);
//tree = new JTree(model);
tree = new BSRosterRestorableTree(model);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setCellRenderer(new BSRosterRenderer());
tree.setRootVisible(false);
//tree.setEditable(true);
tree.setToggleClickCount(1);
tree.addMouseListener(this);
//tree.addTreeExpansionListener(this);
ToolTipManager.sharedInstance().registerComponent(tree);
scrollPane = new JScrollPane(tree);
scrollPane.setBorder(new javax.swing.border.TitledBorder("Roster"));
scrollPane.setAutoscrolls(true);
scrollPane.setPreferredSize(new Dimension(100, 400));
// context menu items
refreshMenuItem = new JMenuItem("Refresh roster");
refreshMenuItem.addActionListener(this);
addContactMenuItem = new JMenuItem("Add contact");
addContactMenuItem.addActionListener(this);
changeMenuItem = new JMenuItem("Change contact");
changeMenuItem.addActionListener(this);
getPlanMenuItem = new JMenuItem("Get plan");
getPlanMenuItem.addActionListener(this);
subscribeMenuItem = new JMenuItem("Subscribe to presence");
subscribeMenuItem.addActionListener(this);
msgMenuItem = new JMenuItem("Send message");
msgMenuItem.addActionListener(this);
msgGroupOnlineMenuItem = new JMenuItem("Message to those online");
msgGroupOnlineMenuItem.addActionListener(this);
msgWholeGroupMenuItem = new JMenuItem("Message to whole group");
msgWholeGroupMenuItem.addActionListener(this);
chatMenuItem = new JMenuItem("Chat");
chatMenuItem.addActionListener(this);
deleteMenuItem = new JMenuItem("Delete contact");
deleteMenuItem.addActionListener(this);
sendFileMenuItem = new JMenuItem("Send file");
sendFileMenuItem.addActionListener(this);
// presence menu
presenceMenu = new JMenu("Set individual presence");
onlinePresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[0]);
onlinePresenceMenuItem.addActionListener(this);
presenceMenu.add(onlinePresenceMenuItem);
chatPresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[1]);
chatPresenceMenuItem.addActionListener(this);
presenceMenu.add(chatPresenceMenuItem);
busyPresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[2]);
busyPresenceMenuItem.addActionListener(this);
presenceMenu.add(busyPresenceMenuItem);
elsePresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[3]);
elsePresenceMenuItem.addActionListener(this);
presenceMenu.add(elsePresenceMenuItem);
awayPresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[4]);
awayPresenceMenuItem.addActionListener(this);
presenceMenu.add(awayPresenceMenuItem);
xaPresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[5]);
xaPresenceMenuItem.addActionListener(this);
presenceMenu.add(xaPresenceMenuItem);
dndPresenceMenuItem = new JCheckBoxMenuItem(BSMainFrame.presenceStrs[6]);
dndPresenceMenuItem.addActionListener(this);
presenceMenu.add(dndPresenceMenuItem);
invisiblePresenceMenuItem = new JCheckBoxMenuItem("Invisible");
invisiblePresenceMenuItem.addActionListener(this);
presenceMenu.add(invisiblePresenceMenuItem);
presenceMenu.add(new JSeparator());
clearPresenceMenuItem = new JMenuItem("Clear");
clearPresenceMenuItem.addActionListener(this);
presenceMenu.add(clearPresenceMenuItem);
clearAllPresenceMenuItem = new JMenuItem("Clear all");
clearAllPresenceMenuItem.addActionListener(this);
presenceMenu.add(clearAllPresenceMenuItem);
presenceMenu.add(new JSeparator());
policyPresenceMenuItem = new JCheckBoxMenuItem("Offline > away > online");
policyPresenceMenuItem.addActionListener(this);
presenceMenu.add(policyPresenceMenuItem);
// component menu items
loginMenuItem = new JMenuItem("Log-in to this service");
loginMenuItem.addActionListener(this);
logoutMenuItem = new JMenuItem("Log-off of this service");
logoutMenuItem.addActionListener(this);
// IX panels stuff
relationsMenu = new JMenu("Set relation");
relSupMenuItem = new JMenuItem("Superior");
relSupMenuItem.addActionListener(this);
relSubMenuItem = new JMenuItem("Subordinate");
relSubMenuItem.addActionListener(this);
relPeerMenuItem = new JMenuItem("Peer");
relPeerMenuItem.addActionListener(this);
relContactMenuItem = new JMenuItem("Contact");
relContactMenuItem.addActionListener(this);
relDeleteMenuItem = new JMenuItem("Delete");
relDeleteMenuItem.addActionListener(this);
// end of IX panels stuff
// group context menu
groupContextMenu = new JPopupMenu();
//groupContextMenu.add(addContactMenuItem);
//groupContextMenu.add(refreshMenuItem);
//groupContextMenu.add(msgMenuItem);
// buddy context menu
buddyContextMenu = new JPopupMenu();
//buddyContextMenu.add(msgMenuItem);
//buddyContextMenu.add(chatMenuItem);
//buddyContextMenu.add(deleteMenuItem);
//buddyContextMenu.add(changeMenuItem);
//buddyContextMenu.add(subscribeMenuItem);
//buddyContextMenu.addSeparator();
//buddyContextMenu.add(addContactMenuItem);
//buddyContextMenu.add(refreshMenuItem);
// resource context menu
resourceContextMenu = new JPopupMenu();
//resourceContextMenu.add(chatMenuItem);
//resourceContextMenu.add(msgMenuItem);
//resourceContextMenu.add(sendFileMenuItem);
//resourceContextMenu.addSeparator();
//resourceContextMenu.add(addContactMenuItem);
//resourceContextMenu.add(refreshMenuItem);
// component context menu
componentContextMenu = new JPopupMenu();
//componentContextMenu.add(loginMenuItem);
//componentContextMenu.add(logoutMenuItem);
}
/** Returns scrollPane - the top GUI component of rosterTree */
public JScrollPane getScrollPane() {
return scrollPane;
}
/** Sets the tree to display only online users */
public void setOnlyOnline(boolean onlyOnline) {
this.onlyOnline = onlyOnline;
updateRoster();
}
/** <code>BSPresenceListener</code> function - updates roster. */
public void presenceChanged(BSPresenceInfo pi) {
updateRoster();
}
/** <code>BSPresenceListener</code> function - updates roster. */
public void presencesCleared() {
updateRoster();
}
/** <code>BSPresenceListener</code> function - empty implementation. */
public void subscriptionRequested(JID jid) { }
/** <code>BSPresenceListener</code> function - empty implementation. */
public void subscriptionApproved(JID jid) { }
/** <code>RosterListener</code> function - updates roster. */
public void changedRoster(Roster r) {
updateRoster();
}
/** <code>RosterListener</code> function - updates roster. */
public void replacedRoster(Roster r) {
updateRoster();
}
/**
* Completely updates roster taking information from underlying
* roster, presence and messenger beans.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -