ViewerWindow.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.window;
00002 
00003 import java.io.File;
00004 import java.util.ArrayList;
00005 import java.util.Map;
00006 import java.util.concurrent.atomic.AtomicInteger;
00007 
00008 import org.eclipse.core.commands.Command;
00009 import org.eclipse.core.commands.State;
00010 import org.eclipse.ui.ISourceProviderListener;
00011 import org.eclipse.ui.IWorkbenchWindow;
00012 import org.eclipse.ui.commands.ICommandService;
00013 import org.eclipse.ui.handlers.RegistryToggleState;
00014 import org.eclipse.ui.services.ISourceProviderService;
00015 
00016 import edu.rice.cs.hpc.data.experiment.Experiment;
00017 import edu.rice.cs.hpc.filter.service.FilterMap;
00018 import edu.rice.cs.hpc.filter.service.FilterStateProvider;
00019 import edu.rice.cs.hpc.viewer.actions.DebugShowCCT;
00020 import edu.rice.cs.hpc.viewer.actions.DebugShowFlatID;
00021 import edu.rice.cs.hpc.viewer.provider.DatabaseState;
00022 
00037 public class ViewerWindow {
00042     public static int maxDbNum = 5;
00046     private IWorkbenchWindow winObj;
00051     ArrayList<Database> dbObj = new ArrayList<Database>(maxDbNum);
00052 
00053     private Command cmdDebugCCT;
00054     private Command cmdDebugFlat;
00055     
00058     private AtomicInteger numAggregateDatabase = new AtomicInteger(1);
00059 
00060     public IWorkbenchWindow getWinObj() {
00061         return winObj;
00062     }
00063     
00064     public void setWinObj(IWorkbenchWindow window) {
00065         winObj = window;
00066         ICommandService commandService = (ICommandService) winObj.getService(ICommandService.class);
00067         cmdDebugCCT = commandService.getCommand( DebugShowCCT.commandId );
00068         cmdDebugFlat = commandService.getCommand( DebugShowFlatID.commandId );
00069         
00070         // listen to filter change of states
00071         final ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class);
00072         final FilterStateProvider service_provider = (FilterStateProvider) service.getSourceProvider(FilterStateProvider.FILTER_REFRESH_PROVIDER);
00073         service_provider.addSourceProviderListener(new ISourceProviderListener() {
00074 
00075             @Override
00076             public void sourceChanged(int sourcePriority, Map sourceValuesByName) {}
00077 
00078             @Override
00079             public void sourceChanged(int sourcePriority, String sourceName,
00080                     Object sourceValue) {
00081                 if (sourceName.equals(FilterStateProvider.FILTER_REFRESH_PROVIDER)) 
00082                 {
00083                     Boolean filter = (Boolean) sourceValue;
00084                     filterAllDatabases(filter);
00085                 }
00086             }           
00087         });
00088     }
00089 
00090     private void filterAllDatabases(boolean filter)
00091     {
00092         // filter the experiment
00093         Experiment []experiments = getExperiments();
00094         if (experiments != null)
00095         {
00096             for (Experiment experiment : experiments)
00097             {
00098                 if (filter) 
00099                 {
00100                     // filtering is needed
00101                     experiment.filter(FilterMap.getInstance());
00102                 } else 
00103                 {
00104                     // filter is disabled, we need to reopen the database
00105                     try {
00106                         experiment.reopen();
00107                     } catch (Exception e) {
00108                         e.printStackTrace();
00109                     }
00110                 }
00111             }
00112         }
00113         final ISourceProviderService service = (ISourceProviderService) winObj.getService(ISourceProviderService.class);
00114         DatabaseState databaseState = (DatabaseState) service.getSourceProvider(DatabaseState.DATABASE_NEED_REFRESH);
00115         databaseState.refreshDatabase(filter);
00116     }
00117     
00122     public int getNextDbNum() {
00123         return dbObj.size();
00124     }
00125 
00126 
00127     public Database getDb(String dbPath) {
00128         for (Database db: dbObj) {
00129             if (db == null) {
00130                 continue;
00131             }
00132             String path = db.getExperiment().getDefaultDirectory().getAbsolutePath(); 
00133             if (dbPath.equals(path)) {
00134                 return db;
00135             }
00136         }
00137         return null;
00138     }
00139 
00140 
00144     public int getOpenDatabases () {
00145         return dbObj.size();
00146     }
00147 
00154     public int addDatabase(Database database) {
00155         dbObj.add(database);
00156         checkService();
00157         return dbObj.size()-1;
00158     }
00159 
00167     public int removeDatabase (String databasePath) {
00168         Database db = getDb(databasePath);
00169         assert(db != null);
00170         
00171         if (dbObj.remove(db)) {
00172             checkService();
00173             db.dispose();
00174             return dbObj.size();
00175         }
00176         return -1;
00177     }
00178 
00179     public int getDbNum(Experiment experiment) {
00180         int i=0;
00181         for (Database db: dbObj) {
00182             if (db == null) {
00183                 continue;
00184             }
00185             Experiment current = db.getExperiment();
00186             if (experiment.equals(current)) {
00187                 break;
00188             }
00189             i++;
00190         }
00191         return i;
00192     }
00193 
00194     
00195     /****
00196      * Remove (nullify) all the allocated resources
00197      */
00198     public void dispose() {
00199         for (Database db : dbObj) {
00200             if (db != null)
00201                 db.dispose();
00202         }
00203         dbObj = null;
00204     }
00205     
00206     public Database[] getDatabases() {
00207         Database []db = new Database[dbObj.size()];
00208         dbObj.toArray(db);
00209         return db;
00210     }
00211     
00212     /*****
00213      * get the opened experiment databases
00214      * @return null if there is no opened database
00215      */
00216     public Experiment[] getExperiments() {
00217         int numDb = getOpenDatabases();
00218         if (numDb == 0)
00219             return null;
00220         Experiment []experiments = new Experiment[numDb];
00221         
00222         int i=0;
00223         for (Database db: dbObj) {
00224             if (db == null)
00225                 continue;
00226             experiments[i] = db.getExperiment();
00227             i++;
00228         }
00229         return experiments;
00230     }
00231     
00237     public String[] getDatabasePaths () {
00238         String[] dbArray = new String[getOpenDatabases()];
00239         
00240         int i=0;
00241         for (Database db : dbObj) {
00242             if (dbObj != null) {
00243                 Experiment experiment = db.getExperiment();
00244                 dbArray[i++] = getDatabasePath(experiment.getXMLExperimentFile());
00245             }
00246         }
00247 
00248         return dbArray;
00249     }
00250     
00251     
00252     public String getDatabasePath(File file) {
00253         
00254         String xmlFileName = file.getPath();
00255         return getDatabasePath(xmlFileName);
00256     }
00257     
00258     
00259     public String getDatabasePath(String filename) {
00260 
00261         int dbDir = filename.lastIndexOf(File.separator);
00262         String dbPath = filename.substring(0,dbDir);
00263         
00264         return dbPath;
00265     }
00266     
00267     /***
00268      * update the service provided by DatabaseState to ensure that the menus's state are refreshed
00269      * 
00270      */
00271     public void checkService()
00272     {
00273         ISourceProviderService sourceProviderService = (ISourceProviderService) winObj.getService(
00274                         ISourceProviderService.class);
00275         // Now get my service
00276         DatabaseState commandStateService = (DatabaseState) sourceProviderService
00277                 .getSourceProvider(DatabaseState.DATABASE_ACTIVE_STATE);
00278         commandStateService.toogleEnabled(winObj);
00279         
00280         commandStateService = (DatabaseState) sourceProviderService
00281                 .getSourceProvider(DatabaseState.DATABASE_MERGE_STATE);
00282         commandStateService.toogleEnabled(winObj);
00283     }
00284     
00285     /****
00286      * get the ID of the database. This ID has to be unique for each window, and always incremented
00287      * 
00288      * Every time we open a database, we only increment by 1
00289      * 
00290      * @return
00291      */
00292     public int reserveDatabaseNumber() {
00293         return numAggregateDatabase.incrementAndGet();
00294     }
00295 
00296     // --------------------------------------------------------------
00297     // Debug mode for this window
00298     // --------------------------------------------------------------
00299     
00304     public boolean showCCTLabel() 
00305     {
00306         return isDebugMode(cmdDebugCCT);
00307     }
00308     
00313     public boolean showFlatLabel() 
00314     {
00315         return isDebugMode(cmdDebugFlat);
00316     }
00317     
00324     private boolean isDebugMode(Command command) 
00325     {
00326         boolean isDebug = false;
00327         final State state = command.getState(RegistryToggleState.STATE_ID);
00328         if (state != null)
00329         {
00330             final Boolean b = (Boolean) state.getValue();
00331             isDebug = b.booleanValue();
00332         }
00333         return isDebug;
00334     }
00335 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1