001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.sessions;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.ConnectionContext;
008: import com.tc.admin.common.ComponentNode;
009: import com.tc.admin.common.XAbstractAction;
010: import com.tc.admin.dso.ClassesHelper;
011: import com.tc.management.exposed.SessionsProductMBean;
012:
013: import java.awt.event.ActionEvent;
014: import java.awt.event.KeyEvent;
015: import java.awt.event.MouseEvent;
016:
017: import javax.management.MBeanServerNotification;
018: import javax.management.Notification;
019: import javax.management.NotificationListener;
020: import javax.management.ObjectName;
021: import javax.swing.JPopupMenu;
022: import javax.swing.KeyStroke;
023: import javax.swing.tree.DefaultTreeModel;
024:
025: public class SessionsProductNode extends ComponentNode implements
026: NotificationListener {
027: private ConnectionContext m_cc;
028: private ObjectName m_beanName;
029: private JPopupMenu m_popupMenu;
030: private RefreshAction m_refreshAction;
031:
032: private static final String REFRESH_ACTION = "RefreshAction";
033:
034: public SessionsProductNode(ConnectionContext cc,
035: SessionsProductMBean bean, ObjectName beanName) {
036: super ();
037:
038: m_cc = cc;
039: m_beanName = beanName;
040:
041: setLabel(beanName.getKeyProperty("node"));
042: setComponent(new SessionsProductPanel(bean));
043:
044: initMenu();
045: startListening();
046: }
047:
048: private ObjectName getMBeanServerDelegate() {
049: try {
050: return m_cc
051: .queryName("JMImplementation:type=MBeanServerDelegate");
052: } catch (Exception ioe) {
053: return null;
054: }
055: }
056:
057: private void startListening() {
058: ObjectName mbsd = getMBeanServerDelegate();
059:
060: if (mbsd != null) {
061: try {
062: m_cc.addNotificationListener(mbsd, this );
063: } catch (Exception e) {/**/
064: }
065: }
066: }
067:
068: private void stopListening() {
069: ObjectName mbsd = getMBeanServerDelegate();
070:
071: if (mbsd != null) {
072: try {
073: m_cc.removeNotificationListener(mbsd, this );
074: } catch (Exception e) {/**/
075: }
076: }
077: }
078:
079: public void handleNotification(Notification notification,
080: Object handback) {
081: if (notification instanceof MBeanServerNotification) {
082: MBeanServerNotification mbsn = (MBeanServerNotification) notification;
083: String type = notification.getType();
084: ObjectName name = mbsn.getMBeanName();
085:
086: if (type
087: .equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
088: if (name.equals(m_beanName)) {
089: stopListening();
090: ((DefaultTreeModel) getModel())
091: .removeNodeFromParent(this );
092: }
093: }
094: }
095: }
096:
097: private void initMenu() {
098: m_refreshAction = new RefreshAction();
099:
100: m_popupMenu = new JPopupMenu("SessionMonitor Actions");
101: m_popupMenu.add(m_refreshAction);
102:
103: addActionBinding(REFRESH_ACTION, m_refreshAction);
104: }
105:
106: public JPopupMenu getPopupMenu() {
107: return m_popupMenu;
108: }
109:
110: public void refresh() {
111: ((SessionsProductPanel) getComponent()).refresh();
112: }
113:
114: private class RefreshAction extends XAbstractAction {
115: private RefreshAction() {
116: super ();
117:
118: setName(AdminClient.getContext().getMessage("refresh.name"));
119: setSmallIcon(ClassesHelper.getHelper().getRefreshIcon());
120: setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
121: true));
122: }
123:
124: public void actionPerformed(ActionEvent ae) {
125: refresh();
126: }
127: }
128:
129: public void nodeClicked(MouseEvent me) {
130: m_refreshAction.actionPerformed(null);
131: }
132: }
|