KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > dso > ClientsNode


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.dso;
5
6 import com.tc.admin.AdminClient;
7 import com.tc.admin.AdminClientContext;
8 import com.tc.admin.ConnectionContext;
9 import com.tc.admin.common.ComponentNode;
10 import com.tc.admin.common.XTreeNode;
11 import com.tc.stats.DSOMBean;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15
16 import javax.management.Notification JavaDoc;
17 import javax.management.NotificationListener JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.swing.SwingUtilities JavaDoc;
20
21 public class ClientsNode extends ComponentNode
22   implements NotificationListener JavaDoc
23 {
24   private ConnectionContext m_cc;
25   private DSOClient[] m_clients;
26
27   public ClientsNode(ConnectionContext cc) {
28     super();
29
30     m_cc = cc;
31
32     try {
33       ObjectName JavaDoc dso = DSOHelper.getHelper().getDSOMBean(m_cc);
34       m_cc.addNotificationListener(dso, this);
35       m_clients = ClientsHelper.getHelper().getClients(m_cc);
36     }
37     catch(Exception JavaDoc e) {
38       AdminClient.getContext().log(e);
39       m_clients = new DSOClient[]{};
40     }
41
42     for(int i = 0; i < m_clients.length; i++) {
43       add(new ClientTreeNode(m_cc, m_clients[i]));
44     }
45
46     ClientsPanel panel = new ClientsPanel(m_clients);
47
48     panel.setNode(this);
49     setLabel(AdminClient.getContext().getMessage("clients"));
50     setComponent(panel);
51   }
52
53   public DSOClient[] getClients() {
54     return m_clients;
55   }
56
57   public void handleNotification(final Notification JavaDoc notice, final Object JavaDoc handback) {
58     SwingUtilities.invokeLater(new Runnable JavaDoc() {
59       public void run() {
60         internalHandleNotification(notice, handback);
61       }
62     });
63   }
64   
65   private void internalHandleNotification(Notification JavaDoc notice, Object JavaDoc handback) {
66     String JavaDoc type = notice.getType();
67
68     if(DSOMBean.CLIENT_ATTACHED.equals(type)) {
69       AdminClientContext acc = AdminClient.getContext();
70
71       acc.setStatus(acc.getMessage("dso.client.retrieving"));
72
73       ObjectName JavaDoc clientObjectName = (ObjectName JavaDoc)notice.getSource();
74       DSOClient client = new DSOClient(m_cc, clientObjectName);
75       ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(m_clients));
76
77       list.add(client);
78       m_clients = (DSOClient[])list.toArray(new DSOClient[]{});
79
80       ClientTreeNode ctn = new ClientTreeNode(m_cc, client);
81       getModel().insertNodeInto(ctn, this, getChildCount());
82
83       ((ClientsPanel)getComponent()).add(client);
84
85       acc.setStatus(acc.getMessage("dso.client.new") + client);
86     }
87     else if(DSOMBean.CLIENT_DETACHED.equals(type)) {
88       AdminClientContext acc = AdminClient.getContext();
89
90       acc.setStatus(acc.getMessage("dso.client.detaching"));
91
92       int nodeIndex = -1;
93       ObjectName JavaDoc clientObjectName = (ObjectName JavaDoc)notice.getSource();
94       ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(m_clients));
95       DSOClient client = null;
96
97       for(int i = 0; i < list.size(); i++) {
98         client = (DSOClient)list.get(i);
99
100         if(client.getObjectName().equals(clientObjectName)) {
101           list.remove(client);
102           m_clients = (DSOClient[])list.toArray(new DSOClient[]{});
103           nodeIndex = i;
104           break;
105         }
106       }
107
108       if(nodeIndex != -1) {
109         acc.controller.remove((XTreeNode)getChildAt(nodeIndex));
110         ((ClientsPanel)getComponent()).remove(client);
111       }
112
113       acc.setStatus(acc.getMessage("dso.client.detached") + client);
114     }
115   }
116 }
117
Popular Tags