CloseDatabase.java

Go to the documentation of this file.
00001 
00004 package edu.rice.cs.hpc.viewer.actions;
00005 
00006 import java.io.File;
00007 import java.util.Arrays;
00008 import java.util.List;
00009 
00010 import org.eclipse.core.commands.AbstractHandler;
00011 import org.eclipse.core.commands.ExecutionEvent;
00012 import org.eclipse.core.commands.ExecutionException;
00013 import org.eclipse.jface.dialogs.MessageDialog;
00014 import org.eclipse.jface.viewers.ArrayContentProvider;
00015 import org.eclipse.jface.viewers.LabelProvider;
00016 import org.eclipse.ui.IEditorPart;
00017 import org.eclipse.ui.IEditorReference;
00018 import org.eclipse.ui.IViewPart;
00019 import org.eclipse.ui.IWorkbenchPage;
00020 import org.eclipse.ui.IWorkbenchWindow;
00021 import org.eclipse.ui.WorkbenchException;
00022 import org.eclipse.ui.dialogs.ListSelectionDialog;
00023 import org.eclipse.ui.handlers.HandlerUtil;
00024 
00025 import edu.rice.cs.hpc.data.experiment.Experiment;
00026 import edu.rice.cs.hpc.viewer.editor.IViewerEditor;
00027 import edu.rice.cs.hpc.viewer.framework.ApplicationWorkbenchAdvisor;
00028 import edu.rice.cs.hpc.viewer.scope.AbstractBaseScopeView;
00029 import edu.rice.cs.hpc.viewer.util.WindowTitle;
00030 import edu.rice.cs.hpc.viewer.window.ViewerWindow;
00031 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00032 
00036 public class CloseDatabase extends AbstractHandler {
00037 
00038     /* (non-Javadoc)
00039      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
00040      */
00041     public Object execute(ExecutionEvent event) throws ExecutionException 
00042     {
00043         final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
00044         // get an array of open databases for this window
00045         final ViewerWindow vWin = ViewerWindowManager.getViewerWindow(window);
00046         if ( vWin == null) {
00047             return null;        // get method already issued error dialog
00048         }
00049         
00050         // make sure the viewer perspective is the current active page
00051         String perspectiveId = ApplicationWorkbenchAdvisor.PERSPECTIVE_ID;
00052         try {
00053             window.getWorkbench().showPerspective(perspectiveId, window);
00054         } catch (WorkbenchException e) {
00055             e.printStackTrace();
00056         }
00057         
00058         final IWorkbenchPage curPage = window.getActivePage();
00059         final String[] dbArray = vWin.getDatabasePaths();
00060         Object[] databasesToClose;
00061         
00062         if (dbArray.length == 0) {
00063             MessageDialog.openError(window.getShell(), 
00064                     "Error: No Open Database's Found.", 
00065                     "There are no databases in this window which can be closed.");
00066             return null;        // set method already issued error dialog
00067             
00068         } else if (dbArray.length == 1) {
00069             
00070             // ------------------------------------------------------------
00071             // if only one database is opened, we just close everything
00072             //  no need to ask which database to close !
00073             // ------------------------------------------------------------
00074 
00075             databasesToClose = dbArray;
00076         } else {
00077             
00078             final List<String> dbList = Arrays.asList(dbArray);
00079 
00080             // put up a dialog with the open databases in the current window in a drop down selection box
00081             ListSelectionDialog dlg = new ListSelectionDialog(window.getShell(), dbList, 
00082                 new ArrayContentProvider(), new LabelProvider(), "Select the databases to close:");
00083             dlg.setTitle("Select Databases");
00084             dlg.open();
00085             Object[] selectedDatabases = dlg.getResult();
00086 
00087             if ((selectedDatabases == null) || (selectedDatabases.length <= 0)) {
00088                 return null;
00089             }
00090             databasesToClose = selectedDatabases;
00091         }
00092 
00093         
00094         // -----------------------------------------------------------------------
00095         // close the databases, and all editors and views associated with them
00096         // -----------------------------------------------------------------------
00097         for (Object selectedDatabase: databasesToClose) {
00098 
00099             // close any open editor windows for this database
00100             final org.eclipse.ui.IEditorReference editors[] = curPage.getEditorReferences();
00101             for (IEditorReference editor: editors) {
00102                 IEditorPart edPart = editor.getEditor(false);
00103                 
00104                 // ----------------------------------------------------------
00105                 // if the editor is an instance of hpcviewer's editor, then we close it
00106                 //      if the database associated with it is the same as the database we
00107                 //      want to close
00108                 // ----------------------------------------------------------
00109                 if (edPart instanceof IViewerEditor) {
00110                     final IViewerEditor viewerEditor = (IViewerEditor) edPart;
00111                     final Experiment exp = viewerEditor.getExperiment();
00112                     if (exp != null) {
00113                         final File dir = exp.getDefaultDirectory();
00114                         // ----------------------------------------------------------
00115                         // at the moment we don't have mechanism to compare database
00116                         // thus, we just compare the path 
00117                         // ----------------------------------------------------------
00118                         if (dir.getAbsolutePath().equals(selectedDatabase)) {
00119                             curPage.closeEditor(edPart, false);
00120                         }
00121                     }
00122                 }
00123             }
00124             
00125             // close this databases metrics views
00126             org.eclipse.ui.IViewReference views[] = curPage.getViewReferences();
00127             int nbViews = views.length;
00128             for(int j=0;j<nbViews;j++) {
00129                 IViewPart objPart = views[j].getView(false);
00130                 if (objPart instanceof AbstractBaseScopeView) {
00131                     final AbstractBaseScopeView objView = (AbstractBaseScopeView) objPart;
00132                     final Experiment experiment = objView.getExperiment();
00133                     if (experiment != null) {
00134                         String xmlFileName = experiment.getDefaultDirectory().getAbsolutePath();
00135                         
00136                         if (selectedDatabase.equals(xmlFileName)) {
00137                             curPage.hideView(objView);
00138                         }
00139                     }
00140                 }
00141             }
00142             
00143             // remove the database from our database manager information
00144             int dbNum = vWin.removeDatabase(selectedDatabase.toString());
00145             if (dbNum < 0) {
00146                 // can close views for an entry we could not find
00147                 continue;
00148             }
00149         }
00150         WindowTitle wt = new WindowTitle();
00151         wt.refreshAllTitles();
00152         return null;
00153     }
00154 
00155 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1