KickJava   Java API By Example, From Geeks To Geeks.

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


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.XAbstractAction;
11 import com.tc.stats.DSOMBean;
12
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.KeyEvent JavaDoc;
15 import java.awt.event.MouseEvent JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18
19 import javax.management.Notification JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.swing.Icon JavaDoc;
23 import javax.swing.JPopupMenu JavaDoc;
24 import javax.swing.KeyStroke JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26
27 public class RootsNode extends ComponentNode
28   implements NotificationListener JavaDoc
29 {
30   private ConnectionContext m_cc;
31   private DSORoot[] m_roots;
32   private JPopupMenu JavaDoc m_popupMenu;
33   private RefreshAction m_refreshAction;
34
35   private static final String JavaDoc REFRESH_ACTION = "RefreshAction";
36
37   public RootsNode(ConnectionContext cc) {
38     super();
39
40     m_cc = cc;
41     m_roots = getRoots();
42
43     initMenu();
44
45     AdminClientContext acc = AdminClient.getContext();
46     String JavaDoc label = acc.getMessage("dso.roots");
47     RootsPanel panel = new RootsPanel(cc, m_roots);
48
49     panel.setNode(this);
50     setLabel(label);
51     setComponent(panel);
52
53     for(int i = 0; i < m_roots.length; i++) {
54       insert(new RootNode(cc, m_roots[i]), i);
55     }
56
57     try {
58       ObjectName JavaDoc dso = DSOHelper.getHelper().getDSOMBean(m_cc);
59       m_cc.addNotificationListener(dso, this);
60     }
61     catch(Exception JavaDoc e) {
62       acc.log(e);
63     }
64   }
65
66   public DSORoot[] getRoots() {
67     DSORoot[] roots;
68
69     try {
70       roots = RootsHelper.getHelper().getRoots(m_cc);
71     }
72     catch(Exception JavaDoc e) {
73       AdminClient.getContext().log(e);
74       roots = new DSORoot[]{};
75     }
76
77     return roots;
78   }
79
80   public DSORoot getRoot(int index) {
81     return m_roots != null ? m_roots[index] : null;
82   }
83
84   public int getRootCount() {
85     return m_roots != null ? m_roots.length : 0;
86   }
87
88   private void initMenu() {
89     m_refreshAction = new RefreshAction();
90
91     m_popupMenu = new JPopupMenu JavaDoc("Roots Actions");
92     m_popupMenu.add(m_refreshAction);
93
94     addActionBinding(REFRESH_ACTION, m_refreshAction);
95   }
96
97   public JPopupMenu JavaDoc getPopupMenu() {
98     return m_popupMenu;
99   }
100
101   public Icon JavaDoc getIcon() {
102     return RootsHelper.getHelper().getRootsIcon();
103   }
104
105   public void refresh() {
106     AdminClientContext acc = AdminClient.getContext();
107     boolean expanded = acc.controller.isExpanded(this);
108
109     tearDownChildren();
110
111     m_roots = getRoots();
112     for(int i = 0; i < m_roots.length; i++) {
113       m_roots[i].refresh();
114       insert(new RootNode(m_cc, m_roots[i]), i);
115     }
116     ((RootsPanel)getComponent()).setRoots(m_roots);
117
118     getModel().nodeStructureChanged(this);
119     if(expanded) {
120       acc.controller.expand(this);
121     }
122   }
123
124   private class RefreshAction extends XAbstractAction {
125     private RefreshAction() {
126       super();
127
128       setName(AdminClient.getContext().getMessage("refresh.name"));
129       setSmallIcon(RootsHelper.getHelper().getRefreshIcon());
130       setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0, true));
131     }
132
133     public void actionPerformed(ActionEvent JavaDoc ae) {
134       AdminClientContext acc = AdminClient.getContext();
135
136       acc.controller.setStatus(acc.getMessage("dso.roots.refreshing"));
137       acc.controller.block();
138
139       try {
140         refresh();
141       }
142       catch(Throwable JavaDoc t) {
143         t.printStackTrace();
144       }
145
146       acc.controller.unblock();
147       acc.controller.clearStatus();
148     }
149   }
150
151   public void nodeClicked(MouseEvent JavaDoc me) {
152     m_refreshAction.actionPerformed(null);
153   }
154
155   public void handleNotification(final Notification JavaDoc notice, Object JavaDoc handback) {
156     String JavaDoc type = notice.getType();
157
158     if(DSOMBean.ROOT_ADDED.equals(type)) {
159       SwingUtilities.invokeLater(new Runnable JavaDoc() {
160         public void run() {
161           AdminClientContext acc = AdminClient.getContext();
162
163           acc.setStatus(acc.getMessage("dso.root.retrieving"));
164
165           ObjectName JavaDoc rootObjectName = (ObjectName JavaDoc)notice.getSource();
166           DSORoot root = new DSORoot(m_cc, rootObjectName);
167           ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(m_roots));
168
169           list.add(root);
170           m_roots = (DSORoot[])list.toArray(new DSORoot[]{});
171
172           RootNode rn = new RootNode(m_cc, root);
173           getModel().insertNodeInto(rn, RootsNode.this, getChildCount());
174
175           ((RootsPanel)getComponent()).add(root);
176
177           acc.setStatus(acc.getMessage("dso.root.new") + root);
178         }
179       });
180     }
181   }
182 }
183
Popular Tags