FlatScopeViewActions.java
Go to the documentation of this file.00001
00004 package edu.rice.cs.hpc.viewer.scope.flat;
00005
00006 import java.util.Stack;
00007
00008 import org.eclipse.swt.widgets.Composite;
00009 import org.eclipse.swt.widgets.Shell;
00010 import org.eclipse.ui.IWorkbenchWindow;
00011 import org.eclipse.swt.widgets.CoolBar;
00012
00013 import edu.rice.cs.hpc.data.experiment.scope.CallSiteScope;
00014 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00015 import edu.rice.cs.hpc.viewer.scope.ScopeViewActions;
00016 import edu.rice.cs.hpc.viewer.scope.ScopeViewActions.ActionType;
00017 import edu.rice.cs.hpc.viewer.scope.ScopeViewActions.IActionType;
00018
00023 public class FlatScopeViewActions extends ScopeViewActions {
00024
00025 private enum FlatAction implements IActionType{ Flatten, Unflatten };
00026
00027 private Stack<Scope> stackFlatNodes;
00028 private Stack<Object[]> stackExpandedNodes;
00029 private Stack<IActionType> stackActions;
00030
00031
00032
00033
00034
00039 public FlatScopeViewActions(Shell shell, IWorkbenchWindow window, Composite parent, CoolBar coolbar) {
00040 super(shell, window, parent, coolbar);
00041
00042 stackActions = new Stack<IActionType>();
00043 stackFlatNodes = new Stack<Scope>();
00044 }
00045
00051 protected Composite createGUI(Composite parent, CoolBar coolbar) {
00052 objActionsGUI = new FlatScopeViewActionsGUI(this.objShell, this.objWindow, parent, this);
00053 objActionsGUI.buildGUI(parent, coolbar);
00054
00055 return parent;
00056 }
00057
00058
00059
00060
00061
00062
00066 public void flatten() {
00067
00068 Scope objParentNode = (Scope) treeViewer.getInput();
00069
00070
00071
00072
00073 Scope objFlattenedNode = (objParentNode.duplicate());
00074 objFlattenedNode.setExperiment( objParentNode.getExperiment() );
00075 objParentNode.copyMetrics(objFlattenedNode, 0);
00076
00077 boolean hasKids = false;
00078
00079
00080 for (int i=0;i<objParentNode.getChildCount();i++) {
00081 Scope node = (Scope) objParentNode.getChildAt(i);
00082 if(node.getChildCount()>0) {
00083
00084
00085 addChildren(node, objFlattenedNode);
00086 hasKids = true;
00087 } else {
00088
00089 objFlattenedNode.add(node);
00090 }
00091 }
00092 if(hasKids) {
00093 if (objFlattenedNode.hasChildren()) {
00094 pushElementStates();
00095
00096 stackFlatNodes.push(objParentNode);
00097
00098 this.treeViewer.getTree().setRedraw(false);
00099
00100 this.treeViewer.setInput(objFlattenedNode);
00101
00102 this.treeViewer.refresh();
00103
00104 updateAction(FlatAction.Flatten, objFlattenedNode);
00105
00106 this.treeViewer.getTree().setRedraw(true);
00107 checkStates(getSelectedNode());
00108 } else {
00109
00110
00111
00112 showErrorMessage("Cannot flatten a tree that has only callsite nodes");
00113 }
00114 }
00115 }
00116
00120 public void unflatten() {
00121 if (stackFlatNodes.isEmpty())
00122 return;
00123
00124 Scope objParentNode = stackFlatNodes.pop();
00125 if(objParentNode != null) {
00126 this.treeViewer.setInput(objParentNode);
00127
00128 updateAction(FlatAction.Unflatten, objParentNode);
00129
00130 popElementStates();
00131
00132 checkStates(getSelectedNode());
00133 }
00134 }
00135
00136 public boolean canUnflatten() {
00137 return (!stackActions.isEmpty() && stackActions.peek()==FlatAction.Flatten);
00138 }
00139
00140
00141
00142
00143
00144 public void checkStates(Scope nodeSelected) {
00145 boolean bCanZoomIn = objZoom.canZoomIn(nodeSelected);
00146 objActionsGUI.enableHotCallPath( bCanZoomIn );
00147 objActionsGUI.enableZoomIn( bCanZoomIn );
00148
00149 ((FlatScopeViewActionsGUI) objActionsGUI).checkFlattenButtons();
00150
00151 checkStates();
00152 }
00153
00154 @Override
00155 public void checkStates() {
00156 boolean bCanZoomOut = objZoom.canZoomOut() &&
00157 (!stackActions.isEmpty() && stackActions.peek()==ActionType.ZoomIn);
00158 objActionsGUI.enableZoomOut( bCanZoomOut );
00159 }
00160
00161
00162
00163
00164
00165
00169 private void pushElementStates() {
00170 Object []arrNodes = this.treeViewer.getExpandedElements();
00171 if (stackExpandedNodes == null)
00172 stackExpandedNodes = new java.util.Stack<Object[]>();
00173
00174 stackExpandedNodes.push(arrNodes);
00175 }
00176
00180 private void popElementStates() {
00181 Object []arrNodes = stackExpandedNodes.pop();
00182 this.treeViewer.setExpandedElements(arrNodes);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 private void updateAction(IActionType type, Scope root) {
00192 registerAction(type);
00193
00194
00195 ((FlatScopeViewActionsGUI) objActionsGUI).updateFlattenView(root);
00196 }
00197
00198 private void addChildren(Scope node, Scope arrNodes) {
00199 int nbChildren = node.getChildCount();
00200 for(int i=0;i<nbChildren;i++) {
00201
00202 Scope nodeKid = ((Scope) node.getChildAt(i));
00203
00204 if (!(nodeKid instanceof CallSiteScope)) {
00205
00206
00207 arrNodes.add(nodeKid);
00208 }
00209 }
00210 }
00211
00212
00213
00214
00215
00216 protected void registerAction(IActionType type) {
00217
00218 if (type == ActionType.ZoomIn || type == FlatAction.Flatten ) {
00219 stackActions.push(type);
00220 } else {
00221 stackActions.pop();
00222 }
00223 }
00224 }