KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > sessions > SessionsProductNode


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.sessions;
5
6 import com.tc.admin.AdminClient;
7 import com.tc.admin.ConnectionContext;
8 import com.tc.admin.common.ComponentNode;
9 import com.tc.admin.common.XAbstractAction;
10 import com.tc.admin.dso.ClassesHelper;
11 import com.tc.management.exposed.SessionsProductMBean;
12
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.KeyEvent JavaDoc;
15 import java.awt.event.MouseEvent JavaDoc;
16
17 import javax.management.MBeanServerNotification JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationListener JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.swing.JPopupMenu JavaDoc;
22 import javax.swing.KeyStroke JavaDoc;
23 import javax.swing.tree.DefaultTreeModel JavaDoc;
24
25 public class SessionsProductNode extends ComponentNode implements NotificationListener JavaDoc {
26   private ConnectionContext m_cc;
27   private ObjectName JavaDoc m_beanName;
28   private JPopupMenu JavaDoc m_popupMenu;
29   private RefreshAction m_refreshAction;
30
31   private static final String JavaDoc REFRESH_ACTION = "RefreshAction";
32   
33   public SessionsProductNode(ConnectionContext cc, SessionsProductMBean bean, ObjectName JavaDoc beanName) {
34     super();
35     
36     m_cc = cc;
37     m_beanName = beanName;
38         
39     setLabel(beanName.getKeyProperty("node"));
40     setComponent(new SessionsProductPanel(bean));
41
42     initMenu();
43     startListening();
44   }
45
46   private ObjectName JavaDoc getMBeanServerDelegate() {
47     try {
48       return m_cc.queryName("JMImplementation:type=MBeanServerDelegate");
49     } catch(Exception JavaDoc ioe) {
50       return null;
51     }
52   }
53   
54   private void startListening() {
55     ObjectName JavaDoc mbsd = getMBeanServerDelegate();
56     
57     if(mbsd != null) {
58       try {
59         m_cc.addNotificationListener(mbsd, this);
60       } catch(Exception JavaDoc e) {/**/}
61     }
62   }
63   
64   private void stopListening() {
65     ObjectName JavaDoc mbsd = getMBeanServerDelegate();
66       
67     if(mbsd != null) {
68       try {
69         m_cc.removeNotificationListener(mbsd, this);
70       } catch(Exception JavaDoc e) {/**/}
71     }
72   }
73   
74   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
75     if(notification instanceof MBeanServerNotification JavaDoc) {
76       MBeanServerNotification JavaDoc mbsn = (MBeanServerNotification JavaDoc)notification;
77       String JavaDoc type = notification.getType();
78       ObjectName JavaDoc name = mbsn.getMBeanName();
79       
80       if(type.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
81         if(name.equals(m_beanName)) {
82           stopListening();
83           ((DefaultTreeModel JavaDoc)getModel()).removeNodeFromParent(this);
84         }
85       }
86     }
87   }
88   
89   private void initMenu() {
90     m_refreshAction = new RefreshAction();
91
92     m_popupMenu = new JPopupMenu JavaDoc("SessionMonitor Actions");
93     m_popupMenu.add(m_refreshAction);
94
95     addActionBinding(REFRESH_ACTION, m_refreshAction);
96   }
97
98   public JPopupMenu JavaDoc getPopupMenu() {
99     return m_popupMenu;
100   }
101
102   public void refresh() {
103     ((SessionsProductPanel)getComponent()).refresh();
104   }
105
106   private class RefreshAction extends XAbstractAction {
107     private RefreshAction() {
108       super();
109
110       setName(AdminClient.getContext().getMessage("refresh.name"));
111       setSmallIcon(ClassesHelper.getHelper().getRefreshIcon());
112       setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0, true));
113     }
114
115     public void actionPerformed(ActionEvent JavaDoc ae) {
116       refresh();
117     }
118   }
119
120   public void nodeClicked(MouseEvent JavaDoc me) {
121     m_refreshAction.actionPerformed(null);
122   }
123 }
124
Popular Tags