GraphMenu.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.graph;
00002 
00003 import org.eclipse.jface.action.Action;
00004 import org.eclipse.jface.action.IMenuManager;
00005 import org.eclipse.jface.action.MenuManager;
00006 import org.eclipse.ui.IEditorInput;
00007 import org.eclipse.ui.IEditorPart;
00008 import org.eclipse.ui.IEditorReference;
00009 import org.eclipse.ui.IWorkbenchPage;
00010 import org.eclipse.ui.IWorkbenchWindow;
00011 import org.eclipse.ui.PartInitException;
00012 
00013 import edu.rice.cs.hpc.data.experiment.Experiment;
00014 import edu.rice.cs.hpc.data.experiment.metric.MetricRaw;
00015 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00016 import edu.rice.cs.hpc.viewer.editor.BaseEditorManager;
00017 import edu.rice.cs.hpc.viewer.metric.ThreadLevelDataManager;
00018 import edu.rice.cs.hpc.viewer.window.Database;
00019 
00020 /****
00021  * 
00022  * Class to handle metric graph menus (plot, sorted and histo)
00023  *
00024  */
00025 public class GraphMenu 
00026 {
00027     final private IWorkbenchWindow window;
00028     private Database database;
00029     
00030     public GraphMenu(IWorkbenchWindow window) {
00031         this.window = window;
00032     }
00033     
00034     public void createAdditionalContextMenu(IMenuManager mgr, Database database, Scope scope) {
00035         if (scope != null) {
00036             this.database = database;
00037             
00038             ThreadLevelDataManager objDataManager = database.getThreadLevelDataManager();
00039             if (objDataManager == null)
00040                 return;
00041 
00042             // return immediately if the experiment doesn't contain thread level data
00043             if (!objDataManager.isDataAvailable())
00044                 return;
00045 
00046             final MetricRaw []metrics = database.getExperiment().getMetricRaw();
00047             if (metrics == null)
00048                 return;
00049             
00050             final int num_metrics = metrics.length;
00051 
00052             for (int i=0; i<num_metrics; i++) {
00053                 MenuManager subMenu = new MenuManager("Graph "+ metrics[i].getDisplayName() );
00054                 this.createGraphMenus(subMenu, scope, metrics[i]);
00055                 mgr.add(subMenu);
00056 
00057             }
00058         }       
00059     } 
00060 
00061     /***
00062      * Create 3 submenus for plotting graph: plot, sorted and histo
00063      * @param menu
00064      * @param scope
00065      * @param m
00066      * @param index
00067      */
00068     private void createGraphMenus(IMenuManager menu, Scope scope, MetricRaw m) {
00069         menu.add( createGraphMenu(scope, m, GraphType.PlotType.PLOT) );
00070         menu.add( createGraphMenu(scope, m, GraphType.PlotType.SORTED) );
00071         menu.add( createGraphMenu(scope, m, GraphType.PlotType.HISTO) );
00072     }
00073     
00074     /***
00075      * Create a menu action for graph
00076      * @param scope
00077      * @param m
00078      * @param index
00079      * @param t
00080      * @return
00081      */
00082     private ScopeGraphAction createGraphMenu( Scope scope, MetricRaw m, GraphType.PlotType t) {
00083         final String sTitle = GraphType.toString(t);
00084         return new ScopeGraphAction( sTitle, scope, m, t);
00085     }
00086     
00087 
00088     /********************************************************************************
00089      * class to initialize an action for displaying a graph
00090      ********************************************************************************/
00091     private class ScopeGraphAction extends Action {
00092         final private GraphType.PlotType graph_type;
00093         final private MetricRaw metric; 
00094         final private Scope scope;
00095         
00096         public ScopeGraphAction(String sTitle, Scope scopeCurrent, MetricRaw m, GraphType.PlotType type) {
00097             
00098             super(sTitle);
00099             this.metric = m;
00100             this.graph_type = type;
00101             scope = scopeCurrent;
00102         }
00103         
00104         public void run() {
00105             IWorkbenchPage objPage = window.getActivePage();
00106             
00107             try {
00108                 final Experiment experiment = (Experiment) this.scope.getExperiment();
00109                 
00110                 // prepare to split the editor pane
00111                 boolean needNewPartition = BaseEditorManager.splitBegin(objPage, experiment);
00112                 
00113                 String id = GraphEditorInput.getID(scope, metric, graph_type, database);
00114                 GraphEditorInput objInput = getGraphEditorInput(id);
00115                 
00116                 if (objInput == null) {
00117                     objInput = new GraphEditorInput(database, scope, metric, graph_type, window);
00118                 }
00119                 IEditorPart editor = null;
00120                 switch (graph_type) {
00121                 case PLOT:
00122                     editor = objPage.openEditor(objInput, GraphEditorPlot.ID);
00123                     break;
00124                 case SORTED:
00125                     editor = objPage.openEditor(objInput, GraphEditorPlotSort.ID);
00126                     break;
00127                 case HISTO:
00128                     editor = objPage.openEditor(objInput, GraphEditorHisto.ID);
00129                     break;
00130                 }
00131                 
00132                 if (editor instanceof GraphEditorBase) {
00133                     ((GraphEditorBase)editor).finalize();
00134                 }
00135                 
00136                 // finalize the pane splitting if needed
00137                 BaseEditorManager.splitEnd(needNewPartition, editor);
00138                 
00139             } catch (PartInitException e) {
00140                 e.printStackTrace();
00141             }
00142         }
00143     }
00144 
00145     /****
00146      * If the editor has been displayed, we need to activate it
00147      * If not, return null and let the caller to create a new editor
00148      * @param id
00149      * @return
00150      */
00151     private GraphEditorInput getGraphEditorInput(String id) {
00152         IEditorReference editors[] = window.getActivePage().getEditorReferences();
00153         if (editors == null)
00154             return null;
00155         
00156         //-------------------------------------------------------------------
00157         // look at all active editors if our editor has been there or not
00158         //-------------------------------------------------------------------
00159         for (int i = 0; i<editors.length; i++) {
00160             String name = editors[i].getName();
00161             
00162             // check if it is a graph editor (started with [....])
00163             if (name != null && name.charAt(0)=='[') {
00164                 try {
00165                     IEditorInput input = editors[i].getEditorInput();
00166                     if (input instanceof GraphEditorInput) {
00167                         String editor_id = ((GraphEditorInput)input).getID();
00168                         if (editor_id.equals(id)) {
00169                             // we found out editor !
00170                             return (GraphEditorInput) input;
00171                         }
00172                     }
00173                 } catch (PartInitException e) {
00174                     // TODO Auto-generated catch block
00175                     e.printStackTrace();
00176                 }
00177             }
00178         }
00179         
00180         return null;
00181     }
00182     
00183 
00184 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1