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     //                  METHODS
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     //                  FLATTEN: PUBLIC INTERFACES
00060     //-----------------------------------------------------------------------
00061     
00062 
00066     public void flatten() {
00067         // save the current root scope
00068         Scope objParentNode = (Scope) treeViewer.getInput();
00069         
00070         // -------------------------------------------------------------------
00071         // copy the "root" of the current input
00072         // -------------------------------------------------------------------
00073         Scope objFlattenedNode = (objParentNode.duplicate());
00074         objFlattenedNode.setExperiment( objParentNode.getExperiment() );
00075         objParentNode.copyMetrics(objFlattenedNode, 0);
00076         
00077         boolean hasKids = false;
00078 
00079         // create the list of flattened node
00080         for (int i=0;i<objParentNode.getChildCount();i++) {
00081             Scope node =  (Scope) objParentNode.getChildAt(i);
00082             if(node.getChildCount()>0) {
00083                 
00084                 // this node has children, add the children
00085                 addChildren(node, objFlattenedNode);
00086                 hasKids = true;
00087             } else {
00088                 // no children: add the node itself !
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                 // we update the data of the table
00100                 this.treeViewer.setInput(objFlattenedNode);
00101                 // refreshing the table to take into account a new data
00102                 this.treeViewer.refresh();
00103                 
00104                 updateAction(FlatAction.Flatten, objFlattenedNode);
00105 
00106                 this.treeViewer.getTree().setRedraw(true);
00107                 checkStates(getSelectedNode());
00108             } else {
00109                 // the original tree has children, but only contains call sites
00110                 // since we do not allow call site as a leaf node, we need to
00111                 // forbid this kind of flatten operation
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      * (non-Javadoc)
00142      * @see edu.rice.cs.hpc.viewer.scope.IToolbarManager#checkStates(edu.rice.cs.hpc.data.experiment.scope.Scope.Node)
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     //                  FLATTEN: PRIVATE METHODS
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      * Once an action is performed, we need to update the buttons and register the action
00187      * 
00188      * @param type
00189      * @param root
00190      */
00191     private void updateAction(IActionType type, Scope root) {
00192         registerAction(type);
00193         
00194         // post processing: inserting the "aggregate metric" into the top row of the table
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             // Laksono 2009.03.04: do not add call site !
00202             Scope nodeKid = ((Scope) node.getChildAt(i));
00203 
00204             if (!(nodeKid instanceof CallSiteScope)) {
00205                 // the kid is a callsite: do nothing
00206                 // otherwise add the kid into the list of scopes to display
00207                 arrNodes.add(nodeKid);
00208             }
00209         }
00210     }
00211 
00212     /*
00213      * (non-Javadoc)
00214      * @see edu.rice.cs.hpc.viewer.scope.ScopeViewActions#registerAction(edu.rice.cs.hpc.viewer.scope.ScopeViewActions.IActionType)
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1