ExperimentManager.java

Go to the documentation of this file.
00001 
00004 package edu.rice.cs.hpc.viewer.experiment;
00005 
00006 import org.eclipse.ui.IWorkbenchWindow;
00007 import org.eclipse.jface.dialogs.MessageDialog;
00008 import org.eclipse.swt.widgets.DirectoryDialog;
00009 import org.eclipse.ui.IWorkbenchPage;
00010 import org.eclipse.ui.preferences.ScopedPreferenceStore;
00011 import org.eclipse.swt.widgets.Shell;
00012 
00013 import java.io.File;
00014 
00015 import edu.rice.cs.hpc.data.util.Constants;
00016 import edu.rice.cs.hpc.data.util.Util.FileXMLFilter;
00017 
00018 import edu.rice.cs.hpc.viewer.framework.Activator;
00019 import edu.rice.cs.hpc.viewer.util.PreferenceConstants;
00020 import edu.rice.cs.hpc.viewer.window.Database;
00021 import edu.rice.cs.hpc.viewer.window.ViewerWindow;
00022 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00023 
00032 public class ExperimentManager {
00033     final static public int FLAG_DEFAULT = 0;
00034     final static public int FLAG_WITH_CALLER_VIEW = 1;
00035     final static public int FLAG_WITHOUT_CALLER_VIEW = 2;
00036 
00040     static public String sLastPath=null;
00044     private IWorkbenchWindow window;
00048     private boolean flagCallerView = true;
00049     
00054     public ExperimentManager(IWorkbenchWindow win) {
00055         this.window = win;
00056         ScopedPreferenceStore objPref = (ScopedPreferenceStore)Activator.getDefault().getPreferenceStore();
00057         if(ExperimentManager.sLastPath == null)
00058             ExperimentManager.sLastPath = objPref.getString(PreferenceConstants.P_PATH);
00059     }
00060     
00061 
00062     
00071     private File[] getDatabaseFileList(Shell shell, String sTitle) {
00072         // preparing the dialog for selecting a directory
00073         Shell objShell = shell;
00074         DirectoryDialog dirDlg = new DirectoryDialog(objShell);
00075         dirDlg.setText("hpcviewer");
00076         dirDlg.setFilterPath(ExperimentManager.sLastPath);      // recover the last opened path
00077         dirDlg.setMessage(sTitle);
00078         String sDir = dirDlg.open();    // ask the user to select a directory
00079         if(sDir != null){
00080             return this.getListOfXMLFiles(sDir);
00081         }
00082         
00083         return null;
00084     }
00085     
00093     public boolean openDatabaseFromDirectory(String sPath, int flag) {
00094         File []fileXML = this.getListOfXMLFiles(sPath);
00095         if(fileXML != null)
00096             return this.openFileExperimentFromFiles(fileXML, flag);
00097         return false;
00098     }
00104     public boolean openFileExperiment(int flag) {
00105         File []fileXML = this.getDatabaseFileList(this.window.getShell(), 
00106                 "Select a directory containing a profiling database.");
00107         if(fileXML != null)
00108             return this.openFileExperimentFromFiles(fileXML, flag);
00109         return false;
00110     }
00111 
00112     //==================================================================
00113     // ---------- PRIVATE PART-----------------------------------------
00114     //==================================================================
00120     private boolean openFileExperimentFromFiles(File []filesXML, int flag) {
00121         if((filesXML != null) && (filesXML.length>0)) {
00122             boolean bContinue = true;
00123             // let's make it complicated: assuming there are more than 1 XML file in this directory,
00124             // we need to test one by one if it is a valid database file.
00125             // Problem: if in the directory it has two XML files, then the second one will NEVER be opened !
00126             for(int i=0;i<(filesXML.length) && (bContinue);i++) 
00127             {
00128                 File objFile = filesXML[i];
00129                 String sFile=objFile.getAbsolutePath();
00130 
00131                 // Since rel 5.x, the name of database is experiment.xml
00132                 // there is no need to maintain compatibility with hpctoolkit prior 5.x 
00133                 //  where the name of database is config.xml
00134                 if(objFile.getName().startsWith(Constants.DATABASE_FILENAME))  
00135                 {
00136                     // ------------------------------------------------------------------
00137                     // we will continue to verify the content of the list of XML files
00138                     // until we fine the good one.
00139                     // ------------------------------------------------------------------
00140                     
00141                     // check if the database has been opened in this window
00142                     ViewerWindow vw = ViewerWindowManager.getViewerWindow(window);
00143                     Database db = vw.getDb(vw.getDatabasePath(sFile));
00144                     
00145                     if (db == null) {                       
00146                         // check if we can open the database successfully
00147                         bContinue = (this.setExperiment(sFile, flag) == false);
00148                     } else {
00149                         MessageDialog.openError(window.getShell(), "Database is already opened", 
00150                                 "The database is already opened in this window.\n" +
00151                                 "There is no need to open the same database twice in the same window.");
00152                         bContinue = false;
00153                     }
00154                 }
00155             }
00156             if(bContinue) {
00157             } else
00158                 return true;
00159         }
00160         MessageDialog.openError(window.getShell(), "Failed to open a database", 
00161             "Either the selected directory is not a database or the max number of databases allowed per window are already opened.\n"+
00162             "A database directory must contain at least one XML file which contains profiling information.");
00163         return false;
00164     }
00165     
00171     private boolean setExperiment(String sFilename, int flag) {
00172         IWorkbenchPage objPage = this.window.getActivePage();
00173         ExperimentView expViewer = new ExperimentView(objPage);
00174 
00175         // data looks OK
00176         boolean bResult;
00177         if (flag == FLAG_WITHOUT_CALLER_VIEW)  {
00178             flagCallerView = false;
00179             bResult = expViewer.loadExperimentAndProcess(sFilename, flagCallerView);
00180         } else if (flag == FLAG_DEFAULT && !flagCallerView )
00181             // use the initial flag (set by command line or preference page)
00182             bResult = expViewer.loadExperimentAndProcess(sFilename, flagCallerView );
00183         else
00184             bResult = expViewer.loadExperimentAndProcess(sFilename);
00185 
00186         return bResult; 
00187     }
00188     
00194     private File[] getListOfXMLFiles(String sPath) {
00195         // find XML files in this directory
00196         File files = new File(sPath);
00197         // for debugging purpose, let have separate variable
00198         File filesXML[] = files.listFiles(new FileXMLFilter());
00199         // store it in the class variable for further usage
00200         ExperimentManager.sLastPath = sPath;
00201         // store the current path in the preference
00202         ScopedPreferenceStore objPref = (ScopedPreferenceStore)Activator.getDefault().getPreferenceStore();
00203         objPref.setValue(PreferenceConstants.P_PATH, sPath);
00204         return filesXML;
00205     }
00206     
00207 
00208 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1