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.ObjectNameTreeNode;
010: import com.tc.admin.common.XAbstractAction;
011: import com.tc.admin.common.XTreeModel;
012: import com.tc.admin.common.XTreeNode;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.KeyEvent;
016: import java.awt.event.MouseEvent;
017: import java.util.Vector;
018:
019: import javax.swing.Icon;
020: import javax.swing.JPopupMenu;
021: import javax.swing.KeyStroke;
022: import javax.swing.SwingUtilities;
023: import javax.swing.tree.TreeNode;
024:
025: public class RootTreeNode extends ObjectNameTreeNode implements
026: DSOObjectTreeNode {
027: private ConnectionContext m_cc;
028: private DSORoot m_root;
029: private JPopupMenu m_popupMenu;
030: private MoreAction m_moreAction;
031: private LessAction m_lessAction;
032: private int m_batchSize;
033: private RefreshAction m_refreshAction;
034:
035: private static final String REFRESH_ACTION = "RefreshAction";
036:
037: public RootTreeNode(ConnectionContext cc, DSORoot root) {
038: super (root.getObjectName());
039:
040: m_cc = cc;
041: m_root = root;
042: m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
043:
044: initMenu();
045: init();
046: }
047:
048: public DSOObject getDSOObject() {
049: return getDSORoot();
050: }
051:
052: public DSORoot getDSORoot() {
053: return m_root;
054: }
055:
056: private void init() {
057: int count = m_root.getFieldCount();
058:
059: if (count > 0) {
060: if (children == null) {
061: children = new Vector();
062: }
063: children.setSize(count);
064: }
065: }
066:
067: private void initMenu() {
068: m_refreshAction = new RefreshAction();
069:
070: m_popupMenu = new JPopupMenu("Root Actions");
071: m_popupMenu.add(m_refreshAction);
072:
073: if (m_root.isArray() || m_root.isCollection()) {
074: m_popupMenu.add(m_moreAction = new MoreAction());
075: m_popupMenu.add(m_lessAction = new LessAction());
076: }
077:
078: addActionBinding(REFRESH_ACTION, m_refreshAction);
079: }
080:
081: public JPopupMenu getPopupMenu() {
082: return m_popupMenu;
083: }
084:
085: private void fillInChildren() {
086: int childCount = getChildCount();
087: boolean nso = false;
088:
089: for (int i = 0; i < childCount; i++) {
090: DSOObject field = m_root.getField(i);
091: XTreeNode child = createFieldNode(field);
092:
093: children.setElementAt(child, i);
094: child.setParent(this );
095:
096: if (field == null) {
097: nso = true;
098: }
099: }
100:
101: if (nso) {
102: SwingUtilities.invokeLater(new ChildReaper());
103: }
104: }
105:
106: class ChildReaper implements Runnable {
107: public void run() {
108: refresh();
109: }
110: }
111:
112: public TreeNode getChildAt(int index) {
113: if (children != null && children.elementAt(index) == null) {
114: AdminClientContext acc = AdminClient.getContext();
115:
116: acc.controller.block();
117: fillInChildren();
118: acc.controller.unblock();
119: }
120:
121: return super .getChildAt(index);
122: }
123:
124: private XTreeNode createFieldNode(DSOObject field) {
125: return RootsHelper.getHelper().createFieldNode(m_cc, field);
126: }
127:
128: public int getChildCount() {
129: return m_root != null ? m_root.getFieldCount() : 0;
130: }
131:
132: public String toString() {
133: return m_root.toString();
134: }
135:
136: public Icon getIcon() {
137: return RootsHelper.getHelper().getRootIcon();
138: }
139:
140: public void refresh() {
141: AdminClientContext acc = AdminClient.getContext();
142: boolean expanded = acc.controller.isExpanded(this );
143: XTreeModel model = getModel();
144: XTreeNode node;
145:
146: for (int i = getChildCount() - 1; i >= 0; i--) {
147: node = (XTreeNode) getChildAt(i);
148: node.tearDown();
149: model.removeNodeFromParent(node);
150: }
151:
152: m_root.refresh();
153:
154: init();
155:
156: model.nodeStructureChanged(RootTreeNode.this );
157: if (expanded) {
158: acc.controller.expand(this );
159: }
160: }
161:
162: private class RefreshAction extends XAbstractAction {
163: private RefreshAction() {
164: super ("Refresh", RootsHelper.getHelper().getRefreshIcon());
165: setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
166: true));
167: }
168:
169: public void actionPerformed(ActionEvent ae) {
170: AdminClientContext acc = AdminClient.getContext();
171: String name = m_root.getName();
172:
173: acc.controller.setStatus("Refreshing root " + name + "...");
174: acc.controller.block();
175:
176: refresh();
177:
178: acc.controller.clearStatus();
179: acc.controller.unblock();
180: }
181: }
182:
183: public void nodeClicked(MouseEvent me) {
184: m_refreshAction.actionPerformed(null);
185: }
186:
187: private class MoreAction extends XAbstractAction {
188: private MoreAction() {
189: super ("More");
190: }
191:
192: public void actionPerformed(ActionEvent ae) {
193: AdminClientContext acc = AdminClient.getContext();
194: String name = m_root.getName();
195:
196: if (incrementDSOBatchSize() == ConnectionContext.DSO_MAX_BATCH_SIZE) {
197: setEnabled(false);
198: }
199: m_lessAction.setEnabled(true);
200: m_root.setBatchSize(m_batchSize);
201:
202: acc.controller.setStatus("Refreshing root " + name + "...");
203: acc.controller.block();
204:
205: refresh();
206:
207: acc.controller.clearStatus();
208: acc.controller.unblock();
209: }
210: }
211:
212: private class LessAction extends XAbstractAction {
213: private LessAction() {
214: super ("Less");
215: setEnabled(false);
216: }
217:
218: public void actionPerformed(ActionEvent ae) {
219: AdminClientContext acc = AdminClient.getContext();
220: String name = m_root.getName();
221:
222: if (decrementDSOBatchSize() == ConnectionContext.DSO_SMALL_BATCH_SIZE) {
223: setEnabled(false);
224: }
225: m_moreAction.setEnabled(true);
226: m_root.setBatchSize(m_batchSize);
227:
228: acc.controller.setStatus("Refreshing root " + name + "...");
229: acc.controller.block();
230:
231: refresh();
232:
233: acc.controller.clearStatus();
234: acc.controller.unblock();
235: }
236: }
237:
238: int incrementDSOBatchSize() {
239: switch (m_batchSize) {
240: case ConnectionContext.DSO_SMALL_BATCH_SIZE:
241: m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
242: break;
243: case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
244: m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
245: break;
246: case ConnectionContext.DSO_LARGE_BATCH_SIZE:
247: m_batchSize = ConnectionContext.DSO_MAX_BATCH_SIZE;
248: break;
249: }
250:
251: return m_batchSize;
252: }
253:
254: int decrementDSOBatchSize() {
255: switch (m_batchSize) {
256: case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
257: m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
258: break;
259: case ConnectionContext.DSO_LARGE_BATCH_SIZE:
260: m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
261: break;
262: case ConnectionContext.DSO_MAX_BATCH_SIZE:
263: m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
264: break;
265: }
266:
267: return m_batchSize;
268: }
269:
270: public int resetDSOBatchSize() {
271: return m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
272: }
273:
274: public void tearDown() {
275: super.tearDown();
276:
277: m_cc = null;
278: m_root = null;
279: m_popupMenu = null;
280: }
281: }
|