ApplicationWorkbenchWindowAdvisor.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.framework;
00002 
00003 import java.io.File;
00004 import java.net.URI;
00005 import java.net.URISyntaxException;
00006 
00007 import org.eclipse.swt.widgets.Display;
00008 import org.eclipse.ui.application.ActionBarAdvisor;
00009 import org.eclipse.ui.application.IActionBarConfigurer;
00010 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
00011 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
00012 import org.eclipse.ui.IWorkbench;
00013 import org.eclipse.ui.IWorkbenchPage;
00014 import org.eclipse.ui.IWorkbenchWindow;
00015 import org.eclipse.ui.IWorkbenchListener;
00016 
00017 import org.eclipse.core.filesystem.EFS;
00018 import org.eclipse.core.filesystem.IFileInfo;
00019 import org.eclipse.core.filesystem.IFileStore;
00020 import org.eclipse.core.runtime.Path;
00021 import org.eclipse.jface.action.IContributionItem;
00022 import org.eclipse.jface.action.IMenuManager;
00023 import org.eclipse.jface.action.MenuManager;
00024 import org.eclipse.jface.dialogs.MessageDialog;
00025 
00026 import edu.rice.cs.hpc.common.ui.Util;
00027 import edu.rice.cs.hpc.common.util.SleakManager;
00028 import edu.rice.cs.hpc.viewer.experiment.ExperimentManager;
00029 import edu.rice.cs.hpc.viewer.experiment.ExperimentView;
00030 import edu.rice.cs.hpc.viewer.util.Utilities;
00031 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00032 
00033 public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor 
00034 {
00035     private String[] sArgs = null; // command line arguments
00036     final private IWorkbench workbench;
00037     final private IWorkbenchWindow window;
00038     
00046     public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer, String []args) {
00047         super(configurer);
00048         window = configurer.getWindow();
00049         workbench = window.getWorkbench();
00050         this.sArgs = args;
00051     }
00052 
00057     public ActionBarAdvisor createActionBarAdvisor(
00058             IActionBarConfigurer configurer) {
00059         return new ApplicationActionBarAdvisor(configurer);
00060     }
00061 
00065     public void preWindowOpen() {
00066         IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
00067         
00068         final IWorkbenchWindow window = configurer.getWindow();
00069         if (!Util.checkJavaVendor(window.getShell()))
00070             window.close();
00071 
00072         configurer.setShowCoolBar(false);   // remove toolbar/coolbar
00073         configurer.setShowStatusLine(true); // show status bar
00074         configurer.setShowProgressIndicator(true);
00075 
00076         // tell the viewer window manager we are creating a new window
00077         ViewerWindowManager vwm = new ViewerWindowManager();
00078         vwm.addNewWindow(window);
00079     }
00080 
00084     public void postWindowOpen() {
00085         boolean withCallerView = true;
00086 
00087         // -------------------
00088         // set the status bar
00089         // -------------------
00090         org.eclipse.jface.action.IStatusLineManager statusline = getWindowConfigurer()
00091         .getActionBarConfigurer().getStatusLineManager();
00092 
00093         // -------------------
00094         // close existing views and editors in this window
00095         // -------------------
00096         removeViews();
00097         closeAllEditors();
00098 
00099         // -------------------
00100         // set the default metric
00101         // -------------------
00102         Utilities.setFontMetric(window.getShell().getDisplay());
00103 
00104         this.shutdownEvent(this.workbench, window.getActivePage());
00105 
00106         // -------------------
00107         // memleak is on if needed
00108         // -------------------
00109         final Display display = window.getShell().getDisplay();
00110         SleakManager.init(display);
00111 
00112         // -------------------
00113         // see if the argument provides the database to load
00114         // -------------------
00115         if(sArgs != null && (sArgs.length > 0) &&
00116                 // only the first window open the database specified in the command line 
00117                 (ViewerWindowManager.getWindowNumber(window)==0) ) {
00118             
00119             // possibly we have express the experiment file in the command line
00120             IWorkbenchPage pageCurrent = window.getActivePage();
00121             assert (pageCurrent != null);
00122             
00123             ExperimentView expViewer = new ExperimentView(pageCurrent);
00124             assert (expViewer != null); 
00125             
00126             String sPath = null;
00127             // find the file in the list of arguments
00128             for(int i=0;i<sArgs.length;i++) {
00129 
00130                 if(sArgs[i].charAt(0) == '-') {
00131                     String sOption = sArgs[i].substring(1);
00132                     if (sArgs[i].charAt(1) == 'n' || 
00133                             (sArgs.length == 14 && sArgs[i].charAt(2)=='-' && sOption.equalsIgnoreCase("nocallerview")) ){
00134                         // user has set the option not to display caller view
00135                         withCallerView = false;
00136                     }
00137                 } else {
00138                     sPath = sArgs[i];
00139                 }
00140             }
00141             
00142             // -------------------
00143             // if a filename exist, try to open it
00144             // -------------------
00145             if(sPath != null) {
00146                 IFileStore fileStore;
00147                 boolean doPrint = false;
00148                 try {
00149                     fileStore = EFS.getLocalFileSystem().getStore(new URI(sPath));
00150                 } catch (URISyntaxException e) {
00151                     // somehow, URI may throw an exception for certain schemes. 
00152                     // in this case, let's do it traditional way
00153                     fileStore = EFS.getLocalFileSystem().getStore(new Path(sPath));
00154                     e.printStackTrace();
00155                 }
00156                 IFileInfo objFileInfo = fileStore.fetchInfo();
00157                 
00158                 if( objFileInfo.exists() ) {
00159                     if ( objFileInfo.isDirectory() ) {
00160                         if (doPrint) System.out.println("Opening database: " + fileStore.fetchInfo().getName() + " " + withCallerView);
00161 
00162                         final ExperimentManager objManager = new ExperimentManager(window); 
00163                         objManager.openDatabaseFromDirectory(sPath, this.getFlag(withCallerView));
00164                     } else {
00165                         File objFile = new File(sPath);
00166                         EFS.getLocalFileSystem().fromLocalFile(new File(sPath));
00167                         if (doPrint) System.out.println("Opening file: " + objFile.getAbsolutePath() + " " +  fileStore.fetchInfo().getName());
00168                         expViewer.loadExperimentAndProcess( objFile.getAbsolutePath(), withCallerView);
00169                     }
00170                 } else {
00171                     final String sMsg = "File doesn't exist: " + fileStore.getName();
00172                     System.err.println(sMsg );
00173                     MessageDialog.openError(window.getShell(), "Fail to open a database", sMsg);
00174                     this.removeViews();
00175                 }
00176 
00177                 return;
00178             } 
00179         }
00180         
00181         // there is no information about the database
00182         statusline.setMessage(null, "Load a database to start.");
00183         // we need load the file ASAP
00184         this.openDatabase(withCallerView);
00185     }
00186     
00191     private void openDatabase( boolean withCallerView ) {
00192 
00193         final ExperimentManager expFile = new ExperimentManager(window); 
00194         boolean has_database = expFile.openFileExperiment( this.getFlag(withCallerView));
00195 
00196         if (!has_database) {
00197             this.removeViews();
00198         }
00199     }
00200 
00201 
00202     /****
00203      * when there is no database opened, we have to hide views to avoid users to click buttons
00204      * since most action buttons assume the database is already opened, then an action of this
00205      * button while there is no database will cause chaos
00206      */
00207     private void removeViews() {
00208         IWorkbenchPage page = this.window.getActivePage();
00209         removeViews( page );
00210     }
00211     
00212     private void removeViews( IWorkbenchPage page )
00213     {
00214         org.eclipse.ui.IViewReference views[] = page.getViewReferences();
00215         int nbViews = views.length;
00216         
00217         for(int i=0;i<nbViews;i++)
00218             page.hideView(views[i]);
00219     }
00220     
00224     private void closeAllEditors() {
00225         IWorkbenchPage page = this.window.getActivePage();
00226         page.closeAllEditors(false);
00227     }
00228     
00234     private int getFlag (boolean withCallerView) {
00235         if (withCallerView) return ExperimentManager.FLAG_DEFAULT;
00236         else return ExperimentManager.FLAG_WITHOUT_CALLER_VIEW;
00237     }
00241     public boolean preWindowShellClose() {
00242         
00243         // ----------------------------------------------------------------------------------------------
00244         // get "my" window from the configurer instead of from the active window
00245         // in some platforms such as Mac OS, in order to close window, we don't need to activate it first.
00246         // ----------------------------------------------------------------------------------------------
00247         closeAllEditors();
00248         removeViews();
00249         
00250         ViewerWindowManager.removeWindow(window);
00251 
00252         return true; // we allow app to shut down
00253     }
00254     
00258     private void shutdownEvent(IWorkbench workbench, final IWorkbenchPage pageCurrent) {
00259         // attach a new listener to the workbench
00260         workbench.addWorkbenchListener(new IWorkbenchListener(){
00261             public boolean preShutdown(IWorkbench workbench,
00262                     boolean forced) {
00263                 // bug on Mac OS: Mac will allow user to close via menu system while hpcviewer is still displaying 
00264                 //  a modal dialog box (such as  open file dialog). This will create infinite loop in the SWT events
00265                 //  and has to be killed 
00266                 if (pageCurrent != null){
00267                     // somehow, closeEditors method works better than closeAllEditors.
00268                     pageCurrent.closeEditors(pageCurrent.getEditorReferences(), false);
00269                     removeViews( pageCurrent );
00270                 }
00271                 
00272                 return true;
00273             }
00274             public void postShutdown(IWorkbench workbench) {
00275                 
00276                 //---------------------------------------------------------------------------
00277                 // we need to explicitly remove all allocated native resources since Eclipse
00278                 //  will not do this for us (due to resources that are platform dependent)
00279                 //---------------------------------------------------------------------------
00280                 Utilities.dispose();
00281             }
00282         });
00283     }
00284     
00288     public void postWindowCreate()
00289     {
00290       IContributionItem[] mItems, mSubItems;
00291       IMenuManager mm = getWindowConfigurer ().getActionBarConfigurer ().getMenuManager ();
00292       mItems = mm.getItems ();
00293       for (int i = 0; i < mItems.length; i++)
00294       {
00295         if (mItems[i] instanceof MenuManager)
00296         {
00297           mSubItems = ((MenuManager) mItems[i]).getItems ();
00298           for (int j = 0; j < mSubItems.length; j++)
00299           {
00300               if (mItems[i].getId ().equals ("help"))
00301             {
00302                   // John doesn't like some of the key assist tooltips. Let's remove all for once
00303               ((MenuManager) mItems[i]).remove ("org.eclipse.ui.actions.showKeyAssistHandler");
00304             }
00305           }
00306         }
00307       }
00308     }
00309 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1