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.dso;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.AdminClientContext;
008: import com.tc.admin.ConnectionContext;
009: import com.tc.admin.common.ComponentNode;
010: import com.tc.admin.common.XAbstractAction;
011: import com.tc.admin.common.XTreeModel;
012: import com.tc.stats.DSOMBean;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.KeyEvent;
016: import java.awt.event.MouseEvent;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019:
020: import javax.management.Notification;
021: import javax.management.NotificationListener;
022: import javax.management.ObjectName;
023: import javax.swing.Icon;
024: import javax.swing.JPopupMenu;
025: import javax.swing.KeyStroke;
026: import javax.swing.SwingUtilities;
027:
028: public class RootsNode extends ComponentNode implements
029: NotificationListener {
030: private ConnectionContext m_cc;
031: private DSORoot[] m_roots;
032: private JPopupMenu m_popupMenu;
033: private RefreshAction m_refreshAction;
034:
035: private static final String REFRESH_ACTION = "RefreshAction";
036:
037: public RootsNode(ConnectionContext cc) throws Exception {
038: super ();
039:
040: m_cc = cc;
041: m_roots = getRoots();
042:
043: initMenu();
044:
045: AdminClientContext acc = AdminClient.getContext();
046: String label = acc.getMessage("dso.roots");
047: RootsPanel panel = new RootsPanel(cc, m_roots);
048:
049: panel.setNode(this );
050: setLabel(label);
051: setComponent(panel);
052:
053: for (int i = 0; i < m_roots.length; i++) {
054: insert(new RootNode(cc, m_roots[i]), i);
055: }
056:
057: ObjectName dso = DSOHelper.getHelper().getDSOMBean(m_cc);
058: m_cc.addNotificationListener(dso, this );
059: }
060:
061: public DSORoot[] getRoots() throws Exception {
062: return RootsHelper.getHelper().getRoots(m_cc);
063: }
064:
065: public DSORoot getRoot(int index) {
066: return m_roots != null ? m_roots[index] : null;
067: }
068:
069: public int getRootCount() {
070: return m_roots != null ? m_roots.length : 0;
071: }
072:
073: private void initMenu() {
074: m_refreshAction = new RefreshAction();
075:
076: m_popupMenu = new JPopupMenu("Roots Actions");
077: m_popupMenu.add(m_refreshAction);
078:
079: addActionBinding(REFRESH_ACTION, m_refreshAction);
080: }
081:
082: public JPopupMenu getPopupMenu() {
083: return m_popupMenu;
084: }
085:
086: public Icon getIcon() {
087: return RootsHelper.getHelper().getRootsIcon();
088: }
089:
090: public void refresh() throws Exception {
091: AdminClientContext acc = AdminClient.getContext();
092: boolean expanded = acc.controller.isExpanded(this );
093:
094: tearDownChildren();
095:
096: m_roots = getRoots();
097: for (int i = 0; i < m_roots.length; i++) {
098: m_roots[i].refresh();
099: insert(new RootNode(m_cc, m_roots[i]), i);
100: }
101: ((RootsPanel) getComponent()).setRoots(m_roots);
102:
103: getModel().nodeStructureChanged(this );
104: if (expanded) {
105: acc.controller.expand(this );
106: }
107: }
108:
109: private class RefreshAction extends XAbstractAction {
110: private RefreshAction() {
111: super ();
112:
113: setName(AdminClient.getContext().getMessage("refresh.name"));
114: setSmallIcon(RootsHelper.getHelper().getRefreshIcon());
115: setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
116: true));
117: }
118:
119: public void actionPerformed(ActionEvent ae) {
120: AdminClientContext acc = AdminClient.getContext();
121:
122: acc.controller.setStatus(acc
123: .getMessage("dso.roots.refreshing"));
124: acc.controller.block();
125:
126: try {
127: refresh();
128: } catch (Exception e) {
129: AdminClient.getContext().log(e);
130: }
131:
132: acc.controller.unblock();
133: acc.controller.clearStatus();
134: }
135: }
136:
137: public void nodeClicked(MouseEvent me) {
138: m_refreshAction.actionPerformed(null);
139: }
140:
141: private boolean haveRoot(ObjectName objectName) {
142: if (m_roots == null)
143: return false;
144: for (DSORoot root : m_roots) {
145: if (root.getObjectName().equals(objectName)) {
146: return true;
147: }
148: }
149: return false;
150: }
151:
152: public void handleNotification(final Notification notice,
153: Object handback) {
154: String type = notice.getType();
155:
156: if (DSOMBean.ROOT_ADDED.equals(type)) {
157: final ObjectName rootObjectName = (ObjectName) notice
158: .getSource();
159: if (haveRoot(rootObjectName))
160: return;
161:
162: SwingUtilities.invokeLater(new Runnable() {
163: public void run() {
164: AdminClientContext acc = AdminClient.getContext();
165:
166: acc
167: .setStatus(acc
168: .getMessage("dso.root.retrieving"));
169:
170: DSORoot root = new DSORoot(m_cc, rootObjectName);
171: ArrayList<DSORoot> list = new ArrayList<DSORoot>(
172: Arrays.asList(m_roots));
173:
174: list.add(root);
175: m_roots = list.toArray(new DSORoot[] {});
176:
177: RootNode rn = new RootNode(m_cc, root);
178: XTreeModel model = getModel();
179: if (model != null) {
180: model.insertNodeInto(rn, RootsNode.this ,
181: getChildCount());
182: } else {
183: RootsNode.this .add(rn);
184: }
185:
186: ((RootsPanel) getComponent()).add(root);
187:
188: acc
189: .setStatus(acc.getMessage("dso.root.new")
190: + root);
191: }
192: });
193: }
194: }
195:
196: public void tearDown() {
197: try {
198: ObjectName dso = DSOHelper.getHelper().getDSOMBean(m_cc);
199: if (dso != null) {
200: m_cc.removeNotificationListener(dso, this );
201: }
202: } catch (Exception e) {/**/
203: }
204:
205: m_cc = null;
206: m_roots = null;
207: m_popupMenu = null;
208: m_refreshAction = null;
209:
210: super.tearDown();
211: }
212: }
|