ShowView.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.actions;
00002 
00003 import org.eclipse.core.commands.AbstractHandler;
00004 import org.eclipse.core.commands.ExecutionEvent;
00005 import org.eclipse.core.commands.ExecutionException;
00006 import org.eclipse.jface.dialogs.Dialog;
00007 import org.eclipse.jface.viewers.BaseLabelProvider;
00008 import org.eclipse.jface.viewers.ILabelProvider;
00009 import org.eclipse.jface.viewers.ITreeContentProvider;
00010 import org.eclipse.jface.viewers.TreeNode;
00011 import org.eclipse.jface.viewers.TreeNodeContentProvider;
00012 import org.eclipse.jface.viewers.TreeViewer;
00013 import org.eclipse.swt.graphics.Image;
00014 import org.eclipse.swt.widgets.Composite;
00015 import org.eclipse.swt.widgets.Control;
00016 import org.eclipse.swt.widgets.Shell;
00017 import org.eclipse.ui.IViewSite;
00018 import org.eclipse.ui.IWorkbenchPage;
00019 import org.eclipse.ui.IWorkbenchWindow;
00020 import org.eclipse.ui.PartInitException;
00021 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
00022 import org.eclipse.ui.handlers.HandlerUtil;
00023 
00024 import edu.rice.cs.hpc.data.experiment.Experiment;
00025 import edu.rice.cs.hpc.viewer.experiment.ExperimentView;
00026 import edu.rice.cs.hpc.viewer.scope.BaseScopeView;
00027 import edu.rice.cs.hpc.viewer.util.WindowTitle;
00028 import edu.rice.cs.hpc.viewer.window.Database;
00029 import edu.rice.cs.hpc.viewer.window.ViewerWindow;
00030 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00031 
00032 
00033 /****
00034  * 
00035  * Command action to activate a (hidden) view
00036  *
00037  */
00038 public class ShowView extends AbstractHandler {
00039     
00040     private IWorkbenchWindow window;
00041     //@Override
00042     public Object execute(ExecutionEvent event) throws ExecutionException {
00043         
00044         window= HandlerUtil.getActiveWorkbenchWindow(event);
00045         ViewerWindow vWin = ViewerWindowManager.getViewerWindow(window);
00046         
00047         // ------------------------------------------------------------
00048         // gather data: databases and its views
00049         // ------------------------------------------------------------
00050         Database []databases = vWin.getDatabases();
00051         TreeNode []dbNode = new TreeNode[ databases.length ];
00052         
00053         for(int i=0; i<databases.length; i++) {
00054             
00055             Database db = databases[i];
00056             Experiment exp = db.getExperiment();
00057             
00058             // set the name of the root nodes
00059             dbNode[i] = new TreeNode(exp.getName() + " (" + 
00060                     exp.getXMLExperimentFile().getParent() + ") ");
00061             
00062             final ExperimentView ev = db.getExperimentView();
00063             BaseScopeView []views = ev.getViews();
00064             
00065             TreeNode []viewNode = new TreeNode[views.length];
00066             
00067             // gather all the views of this database
00068             for(int j=0; j<views.length; j++) {
00069                 TreeItemNode item = new TreeItemNode(ev, j, views[j]);
00070                 viewNode[j] = new TreeNode(item);
00071                 viewNode[j].setParent(dbNode[i]);
00072             }
00073             dbNode[i].setChildren(viewNode);
00074         }
00075         
00076         // ------------------------------------------------------------
00077         // show dialog box so that users can choose which view to show
00078         // ------------------------------------------------------------
00079         final DatabaseLabelProvider dbLabelProvider = new DatabaseLabelProvider();
00080         
00081         ViewTreeDialog dlg = new ViewTreeDialog(window.getShell(),
00082                 dbLabelProvider, new TreeNodeContentProvider());
00083         
00084         dlg.setInput(dbNode);
00085         
00086         if ( dlg.open() == Dialog.OK ) {
00087             Object []results = dlg.getResult();
00088             if (results != null && results.length>0) {
00089                 for (Object obj: results) {
00090                     Object item = ((TreeNode) obj).getValue();
00091                     
00092                     if (item instanceof TreeItemNode) {
00093                         TreeItemNode itemNode = (TreeItemNode) item;
00094                         BaseScopeView view = itemNode.view;
00095                         try {
00096                             IViewSite site = (IViewSite) view.getSite();
00097                             IWorkbenchPage page = window.getActivePage();
00098 
00099                             // ------------------------------------------------------------
00100                             // Activate the view
00101                             // ------------------------------------------------------------
00102                             BaseScopeView newView = ExperimentView.openView(page, 
00103                                     view.getRootScope(), site.getSecondaryId(), 
00104                                     view.getDatabase(), IWorkbenchPage.VIEW_ACTIVATE);
00105                             
00106                             BaseScopeView []views = itemNode.ev.getViews();
00107                             views[itemNode.index] = newView;
00108                             itemNode.ev.setViews(views);
00109                             
00110                             // when a view is closed, we lose the information of hide/show columns
00111                             // at the moment, the only thing to fix this, is to reset the column status
00112                             
00113                             newView.setInput(view.getDatabase(), view.getRootScope(), false);
00114        
00115                         } catch (PartInitException e) {
00116                             // TODO Auto-generated catch block
00117                             e.printStackTrace();
00118                         }
00119                     }
00120                 }
00121             }
00122         }
00123         // hack: force all parts to refresh the title
00124         // we should instead refresh only activated parts
00125         dbLabelProvider.wt.refreshViewTitles(window);
00126         
00127         dispose(dbNode);
00128         
00129         return null;
00130     }
00131     
00132     /***
00133      * 
00134      * Label provider for the tree node item
00135      *
00136      */
00137     private class DatabaseLabelProvider 
00138         extends BaseLabelProvider implements ILabelProvider
00139     {
00140         final private WindowTitle wt = new WindowTitle();
00141         
00142         //@Override
00143         public Image getImage(Object element) {
00144             Object o = ((TreeNode)element).getValue();
00145             if (o instanceof TreeItemNode) {
00146                 BaseScopeView view = (BaseScopeView) ((TreeItemNode)o).view;
00147                 return view.getTitleImage();
00148             }
00149             return null;
00150         }
00151 
00152         //@Override
00153         public String getText(Object element) {
00154             
00155             TreeNode node = (TreeNode)element; 
00156             Object o = node.getValue();
00157             
00158             if (o instanceof TreeItemNode) {
00159                 BaseScopeView view = (BaseScopeView) ((TreeItemNode)o).view;
00160                 String title = wt.setTitle(window, view);
00161 
00162                 if (view.getTreeViewer().getTree().isDisposed()) {
00163                     title += " *closed*"; 
00164                 }
00165                 return title;
00166             }
00167             return (String)o;
00168         }
00169     }
00170     
00171     /***
00172      * 
00173      * class to store the information of views
00174      *
00175      */
00176     private class TreeItemNode {
00177         private ExperimentView ev;
00178         private int index;
00179         private BaseScopeView view;
00180         
00181         public TreeItemNode(ExperimentView ev, int index, BaseScopeView view) {
00182             this.ev = ev;
00183             this.index = index;
00184             this.view = view;
00185         }
00186     }
00187     
00188     /****
00189      * remove the tree to allow garbage collector to retain the memory
00190      * 
00191      * @param root
00192      */
00193     private void dispose(TreeNode []root) {
00194         
00195         for(TreeNode node: root ) {
00196             
00197             for(TreeNode viewNode: node.getChildren()) {
00198                 viewNode.setParent(null);
00199             }
00200             node.setChildren(null);
00201             node.setParent(null);
00202         }
00203     }
00204     
00205     /**************************
00206      * 
00207      * private class to show all "created "views, whether it's opened or not
00208      *
00209      */
00210     private class ViewTreeDialog extends ElementTreeSelectionDialog
00211     {
00212         /***
00213          * create a view tree dialog
00214          * @param parent
00215          * @param labelProvider
00216          * @param contentProvider
00217          */
00218         public ViewTreeDialog(Shell parent, ILabelProvider labelProvider,
00219                 ITreeContentProvider contentProvider) {
00220             
00221             super(parent, labelProvider, contentProvider);
00222             setTitle("Show a view");
00223         }
00224         
00225         /*
00226          * (non-Javadoc)
00227          * @see org.eclipse.ui.dialogs.ElementTreeSelectionDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
00228          */
00229         protected Control createDialogArea(Composite parent) {
00230 
00231             setMessage("Please select a view to activate");
00232 
00233             Composite composite = (Composite) super.createDialogArea(parent);
00234 
00235             // force to expand all trees
00236             final TreeViewer tree = getTreeViewer();
00237             tree.expandAll();
00238             
00239             return composite;           
00240         }
00241         
00242            /*
00243          *  (non-Javadoc)
00244          * @see org.eclipse.jface.window.Window#open()
00245          */
00246         public int open() {
00247             
00248             super.open();
00249             return getReturnCode();
00250         }
00251     }
00252 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1