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 org.dijon.Component;
007:
008: import com.tc.admin.AdminClient;
009: import com.tc.admin.AdminClientContext;
010: import com.tc.admin.ConnectionContext;
011: import com.tc.admin.common.ComponentNode;
012: import com.tc.admin.common.XAbstractAction;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.KeyEvent;
016: import java.awt.event.MouseEvent;
017:
018: import javax.swing.Icon;
019: import javax.swing.JPopupMenu;
020: import javax.swing.KeyStroke;
021:
022: public class RootNode extends ComponentNode {
023: private ConnectionContext m_cc;
024: private DSORoot m_root;
025: private RootsPanel m_rootsPanel;
026: private MoreAction m_moreAction;
027: private LessAction m_lessAction;
028: private JPopupMenu m_popupMenu;
029: private int m_batchSize;
030: private RefreshAction m_refreshAction;
031:
032: private static final String REFRESH_ACTION = "RefreshAction";
033:
034: public RootNode(ConnectionContext cc, DSORoot root) {
035: super ();
036:
037: m_cc = cc;
038: m_root = root;
039: m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
040:
041: initMenu();
042:
043: setLabel(root.toString());
044: }
045:
046: public Component getComponent() {
047: if (m_rootsPanel == null) {
048: m_rootsPanel = new RootsPanel(m_cc,
049: new DSORoot[] { m_root });
050: m_rootsPanel.setNode(this );
051: }
052:
053: return m_rootsPanel;
054: }
055:
056: private void initMenu() {
057: m_refreshAction = new RefreshAction();
058:
059: m_popupMenu = new JPopupMenu("Root Actions");
060: m_popupMenu.add(m_refreshAction);
061:
062: if (m_root.isArray() || m_root.isCollection()) {
063: m_popupMenu.add(m_moreAction = new MoreAction());
064: m_popupMenu.add(m_lessAction = new LessAction());
065: }
066:
067: addActionBinding(REFRESH_ACTION, m_refreshAction);
068: }
069:
070: public JPopupMenu getPopupMenu() {
071: return m_popupMenu;
072: }
073:
074: public Icon getIcon() {
075: return RootsHelper.getHelper().getRootIcon();
076: }
077:
078: private void refresh() {
079: ((RootsPanel) getComponent()).refresh();
080: setLabel(m_root.toString());
081: nodeChanged();
082: }
083:
084: private class RefreshAction extends XAbstractAction {
085: private RefreshAction() {
086: super ("Refresh", RootsHelper.getHelper().getRefreshIcon());
087: setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
088: true));
089: }
090:
091: public void actionPerformed(ActionEvent ae) {
092: AdminClientContext acc = AdminClient.getContext();
093: String name = m_root.getName();
094:
095: acc.controller.setStatus("Refreshing root " + name + "...");
096: acc.controller.block();
097:
098: refresh();
099:
100: acc.controller.clearStatus();
101: acc.controller.unblock();
102: }
103: }
104:
105: public void nodeClicked(MouseEvent me) {
106: m_refreshAction.actionPerformed(null);
107: }
108:
109: private class MoreAction extends XAbstractAction {
110: private MoreAction() {
111: super ("More");
112: }
113:
114: public void actionPerformed(ActionEvent ae) {
115: AdminClientContext acc = AdminClient.getContext();
116: String name = m_root.getName();
117:
118: if (incrementDSOBatchSize() == ConnectionContext.DSO_MAX_BATCH_SIZE) {
119: setEnabled(false);
120: }
121: m_lessAction.setEnabled(true);
122: m_root.setBatchSize(m_batchSize);
123:
124: acc.controller.setStatus("Refreshing root " + name + "...");
125: acc.controller.block();
126:
127: refresh();
128:
129: acc.controller.clearStatus();
130: acc.controller.unblock();
131: }
132: }
133:
134: private class LessAction extends XAbstractAction {
135: private LessAction() {
136: super ("Less");
137: setEnabled(false);
138: }
139:
140: public void actionPerformed(ActionEvent ae) {
141: AdminClientContext acc = AdminClient.getContext();
142: String name = m_root.getName();
143:
144: if (decrementDSOBatchSize() == ConnectionContext.DSO_SMALL_BATCH_SIZE) {
145: setEnabled(false);
146: }
147: m_moreAction.setEnabled(true);
148: m_root.setBatchSize(m_batchSize);
149:
150: acc.controller.setStatus("Refreshing root " + name + "...");
151: acc.controller.block();
152:
153: refresh();
154:
155: acc.controller.clearStatus();
156: acc.controller.unblock();
157: }
158: }
159:
160: int incrementDSOBatchSize() {
161: switch (m_batchSize) {
162: case ConnectionContext.DSO_SMALL_BATCH_SIZE:
163: m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
164: break;
165: case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
166: m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
167: break;
168: case ConnectionContext.DSO_LARGE_BATCH_SIZE:
169: m_batchSize = ConnectionContext.DSO_MAX_BATCH_SIZE;
170: break;
171: }
172:
173: return m_batchSize;
174: }
175:
176: int decrementDSOBatchSize() {
177: switch (m_batchSize) {
178: case ConnectionContext.DSO_MEDIUM_BATCH_SIZE:
179: m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
180: break;
181: case ConnectionContext.DSO_LARGE_BATCH_SIZE:
182: m_batchSize = ConnectionContext.DSO_MEDIUM_BATCH_SIZE;
183: break;
184: case ConnectionContext.DSO_MAX_BATCH_SIZE:
185: m_batchSize = ConnectionContext.DSO_LARGE_BATCH_SIZE;
186: break;
187: }
188:
189: return m_batchSize;
190: }
191:
192: public int resetDSOBatchSize() {
193: return m_batchSize = ConnectionContext.DSO_SMALL_BATCH_SIZE;
194: }
195:
196: public void tearDown() {
197: super.tearDown();
198:
199: m_popupMenu = null;
200: m_moreAction = null;
201: m_lessAction = null;
202: }
203: }
|