MetricPropertyDialog.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.metric;
00002 
00003 import java.util.ArrayList;
00004 
00005 import org.eclipse.core.runtime.Assert;
00006 import org.eclipse.jface.dialogs.Dialog;
00007 import org.eclipse.jface.dialogs.IDialogConstants;
00008 import org.eclipse.jface.dialogs.InputDialog;
00009 import org.eclipse.jface.dialogs.TitleAreaDialog;
00010 import org.eclipse.jface.layout.GridDataFactory;
00011 import org.eclipse.jface.layout.GridLayoutFactory;
00012 import org.eclipse.jface.viewers.ArrayContentProvider;
00013 import org.eclipse.jface.viewers.CellLabelProvider;
00014 import org.eclipse.jface.viewers.DoubleClickEvent;
00015 import org.eclipse.jface.viewers.IDoubleClickListener;
00016 import org.eclipse.jface.viewers.ISelection;
00017 import org.eclipse.jface.viewers.ISelectionChangedListener;
00018 import org.eclipse.jface.viewers.SelectionChangedEvent;
00019 import org.eclipse.jface.viewers.StructuredSelection;
00020 import org.eclipse.jface.viewers.TableViewer;
00021 import org.eclipse.jface.viewers.TableViewerColumn;
00022 import org.eclipse.jface.viewers.ViewerCell;
00023 
00024 import org.eclipse.swt.SWT;
00025 import org.eclipse.swt.events.SelectionEvent;
00026 import org.eclipse.swt.events.SelectionListener;
00027 import org.eclipse.swt.layout.FillLayout;
00028 import org.eclipse.swt.widgets.Button;
00029 import org.eclipse.swt.widgets.Composite;
00030 import org.eclipse.swt.widgets.Control;
00031 import org.eclipse.swt.widgets.Display;
00032 import org.eclipse.swt.widgets.Group;
00033 import org.eclipse.swt.widgets.List;
00034 import org.eclipse.swt.widgets.Shell;
00035 import org.eclipse.swt.widgets.Table;
00036 import org.eclipse.swt.widgets.TableColumn;
00037 import org.eclipse.ui.IWorkbenchWindow;
00038 
00039 import edu.rice.cs.hpc.common.ui.Util;
00040 import edu.rice.cs.hpc.data.experiment.Experiment;
00041 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00042 import edu.rice.cs.hpc.data.experiment.metric.DerivedMetric;
00043 import edu.rice.cs.hpc.data.experiment.metric.Metric;
00044 import edu.rice.cs.hpc.data.experiment.metric.MetricType;
00045 import edu.rice.cs.hpc.viewer.util.Utilities;
00046 import edu.rice.cs.hpc.viewer.window.ViewerWindow;
00047 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00048 
00049 
00050 /*****
00051  * 
00052  * dialog window class to show list of metrics 
00053  *
00054  */
00055 public class MetricPropertyDialog extends TitleAreaDialog 
00056 {
00057     private TableViewer viewer;
00058     private Button btnEdit;
00059 
00060     private Experiment experiment;
00061     final private IWorkbenchWindow window;
00062 
00063     /***
00064      * Default constructor: 
00065      *  <p/>
00066      *  There is no return value of this window. Each caller is
00067      *  responsible to check the metrics if they are modified or not
00068      * 
00069      * @param parentShell : the parent shell of this dialog
00070      * @param window : the window where the database is stored.
00071      *  in hpcviewer, list of databases is managed based on window
00072      *  if the value of window is null, then users have to use the
00073      *  method setELements() to setup the list of metrics to modify
00074      */
00075     public MetricPropertyDialog(Shell parentShell, IWorkbenchWindow window) {
00076         super(parentShell);
00077         this.window = window;
00078     }
00079 
00080     
00081     /*
00082      * (non-Javadoc)
00083      * @see org.eclipse.jface.dialogs.TitleAreaDialog#createContents(org.eclipse.swt.widgets.Composite)
00084      */
00085     protected Control createContents(Composite parent) {
00086         
00087         Control contents = super.createContents(parent);
00088         
00089         final String TITLE = "Metric property";
00090         
00091         setTitle(TITLE);
00092         getShell().setText(TITLE);
00093         
00094         setMessage("Double-click the cell or select a metric and click edit button to modify the metric");
00095         
00096         return contents;
00097     }
00098     
00099     /*
00100      * (non-Javadoc)
00101      * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
00102      */
00103     protected Control createDialogArea(Composite aParent) {
00104         
00105         // initialize table viewer in the derived class
00106         initTableViewer(aParent);
00107         
00108         return aParent;
00109     }
00110 
00111 
00112     /*
00113      * (non-Javadoc)
00114      * @see org.eclipse.jface.dialogs.TrayDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
00115      */
00116     protected Control createButtonBar(Composite parent) {
00117         
00118         Control ctrl = super.createButtonBar(parent);
00119 
00120         final Button btnOk = getButton(IDialogConstants.OK_ID);
00121         btnOk.setText("Quit");
00122         
00123         final Button btnCancel = getButton(IDialogConstants.CANCEL_ID);
00124         btnCancel.setVisible(false);
00125         
00126         // -----------------
00127         // edit button: use the default "details" button ID
00128         // -----------------
00129         
00130         btnEdit = createButton((Composite) ctrl, IDialogConstants.DETAILS_ID, "Edit", true);
00131         btnEdit.setEnabled(false);      
00132         btnEdit.addSelectionListener(new SelectionListener() {
00133 
00134             @Override
00135             public void widgetSelected(SelectionEvent e) {              
00136                 doAction();
00137             }
00138 
00139             @Override
00140             public void widgetDefaultSelected(SelectionEvent e) {}
00141             
00142         });
00143 
00144         return ctrl;
00145     }
00146 
00147     //--------------------------------------------------
00148     //  PRIVATE
00149     //--------------------------------------------------
00150 
00151     /***
00152      * initialize the table
00153      * 
00154      * @param composite
00155      */
00156     private void initTableViewer(Composite composite) {
00157         
00158         boolean singleExperiment = true;
00159         
00160         // -----------------
00161         // database table
00162         // -----------------
00163         
00164         if (window != null) {
00165             // variable window is null only when the class is in unit test mode
00166             // in app mode, the value of window will never be null
00167             
00168             final ViewerWindow vw = ViewerWindowManager.getViewerWindow(window);
00169             Assert.isNotNull(vw, "Error: No window is detected !");
00170             
00171             final int numDB = vw.getOpenDatabases();
00172             singleExperiment = (numDB == 1);
00173             
00174             if (singleExperiment)  {
00175                 // -------------------------------------
00176                 // case of having only 1 database
00177                 // -------------------------------------
00178                 experiment = vw.getExperiments()[0];
00179                 
00180             } else {
00181                 // -------------------------------------
00182                 // case of having more than 1 databases, show the list of databases
00183                 // -------------------------------------
00184                 updateContent(composite, vw) ;
00185             }
00186         }
00187         
00188         // -----------------
00189         // metrics table 
00190         // -----------------
00191         
00192         Composite metricArea = new Composite(composite, SWT.BORDER);
00193         Table table = new Table(metricArea, SWT.BORDER | SWT.V_SCROLL);
00194 
00195         table.setHeaderVisible(true);
00196 
00197         viewer = new TableViewer(table);
00198         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
00199             
00200             @Override
00201             public void selectionChanged(SelectionChangedEvent event) {
00202                 
00203                 boolean isEnabled = (getSelectElement() != null);
00204 
00205                 // Eclipse bug (or feature?): case of no metric is selected
00206                 // On Mac, a SelectionChangedEvent is triggered when we refresh the input
00207                 //  in this case, no item has been selected since the content of the table is new
00208                 btnEdit.setEnabled(isEnabled);
00209             }
00210         });
00211         
00212         // set double click listener
00213         viewer.addDoubleClickListener(new IDoubleClickListener() {
00214             
00215             @Override
00216             public void doubleClick(DoubleClickEvent event) {
00217                 doAction();
00218             }
00219         });
00220         
00221         // set the provider to handle the table content
00222         viewer.setContentProvider( new ArrayContentProvider() );
00223         
00224         // first column: metric name
00225         final TableViewerColumn columnName = new TableViewerColumn(viewer, SWT.NONE);
00226         final TableColumn colName = columnName.getColumn();
00227         colName.setText("Metric");
00228         colName.setWidth(200);
00229         columnName.setLabelProvider(new CellLabelProvider() {
00230             
00231             @Override
00232             public void update(ViewerCell cell) {
00233                 cell.setText( ((PropertiesModel)cell.getElement()).sTitle );
00234             }
00235         });
00236         
00237         // second column: description
00238         final TableViewerColumn columnDesc = new TableViewerColumn(viewer, SWT.NONE);
00239         final TableColumn colDesc = columnDesc.getColumn();
00240         colDesc.setText("Description");
00241         colDesc.setWidth(100);
00242         columnDesc.setLabelProvider(new CellLabelProvider() {
00243             
00244             @Override
00245             public void update(ViewerCell cell) {
00246                 final PropertiesModel obj = (PropertiesModel) cell.getElement();
00247                 
00248                 if (obj.metric instanceof DerivedMetric)
00249                     cell.setText( "Derived metric" );
00250             }
00251         });
00252         
00253         GridDataFactory.defaultsFor(table).hint(600, 300).grab(true, true).applyTo(table);
00254         
00255         GridDataFactory.swtDefaults().align(SWT.CENTER, SWT.CENTER).
00256             grab(true, true).applyTo(metricArea);
00257         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(metricArea);
00258         
00259         // -------------------------------------
00260         // initialize metric table if necessary
00261         // -------------------------------------
00262         if (singleExperiment)
00263         {
00264             setElements(experiment);
00265         }
00266     }
00267 
00268 
00274     private void updateContent(Composite component, ViewerWindow vw) {
00275         
00276         // -------------------------------------
00277         // case of having more than 1 databases: create a list of databases to select
00278         // -------------------------------------
00279 
00280         Group group = new Group(component, SWT.SHADOW_IN);
00281         group.setText("Select a database");
00282         
00283         GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).applyTo(group);
00284         GridLayoutFactory.swtDefaults().numColumns(1).applyTo(group);
00285         
00286         final List list = new List(group, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);        
00287         final Experiment []experiments = vw.getExperiments();
00288         
00289         for (Experiment exp: experiments) {
00290             // add the database path to the list
00291             list.add( exp.getDefaultDirectory().getAbsolutePath() );
00292         }
00293         GridDataFactory.swtDefaults().hint(600, 100).align(SWT.CENTER, SWT.CENTER).
00294             grab(true, true).applyTo(list);
00295         
00296         list.addSelectionListener( new SelectionListener() {
00297             
00298             @Override
00299             public void widgetSelected(SelectionEvent e) {
00300 
00301                 final Experiment exp = experiments[ list.getSelectionIndex() ];
00302                 setElements( exp );
00303             }
00304             
00305             @Override
00306             public void widgetDefaultSelected(SelectionEvent e) {}
00307         } );
00308     }
00309 
00310     
00311     /***********
00312      * create an array for the input of the table
00313      * 
00314      * @param exp : experiment database 
00315      */
00316     private ArrayList<PropertiesModel> createInput(Experiment exp) {
00317         int nbColumns = exp.getMetricCount();
00318         BaseMetric []metrics = exp.getMetrics();
00319         
00320         final ArrayList<PropertiesModel> arrElements = new ArrayList<PropertiesModel>(nbColumns);
00321         
00322         for(int i=0;i<nbColumns;i++) {
00323             if (metrics[i] != null) {
00324 
00325                 String sTitle = metrics[i].getDisplayName();
00326                 
00327                 PropertiesModel model = new PropertiesModel(sTitle, i, metrics[i]);
00328                 arrElements.add( model );
00329             }
00330         }
00331         return arrElements;
00332     }
00333     
00334     /***
00335      * set the value for arrElements (used by table) based on the specified experiment
00336      * 
00337      * @param exp
00338      */
00339     private void setElements(Experiment exp) {
00340         final ArrayList<PropertiesModel> arrElements = createInput(exp);
00341         viewer.setInput(arrElements);
00342         viewer.refresh();
00343     }
00344     
00345     private void setExperiment(Experiment exp) {
00346         this.experiment = exp;
00347     }
00348     
00349     /***
00350      * retrieve the selected object in the table
00351      * 
00352      * @return The selected PropertiesModel element
00353      */
00354     private PropertiesModel getSelectElement() {
00355         
00356         ISelection selection = viewer.getSelection();
00357         PropertiesModel obj = (PropertiesModel) ((StructuredSelection) selection).getFirstElement();
00358         
00359         return obj;
00360     }
00361     
00362     /****
00363      * show the dialog window
00364      */
00365     private void doAction() {
00366         PropertiesModel obj = getSelectElement();
00367         BaseMetric metric = obj.metric;
00368         
00369         if (metric == null)
00370             return;
00371         
00372         if (metric instanceof DerivedMetric) {
00373 
00374             Experiment experiment = Utilities.getActiveExperiment( Util.getActiveWindow() );
00375             ExtDerivedMetricDlg dialog = new ExtDerivedMetricDlg( getShell(), experiment, 
00376                     experiment.getRootScope().getSubscope(0) );
00377             
00378             DerivedMetric dm = (DerivedMetric) metric;
00379             dialog.setMetric(dm);
00380             
00381             if (dialog.open() == Dialog.OK) {
00382                 
00383                 dm = dialog.getMetric();
00384                 
00385                 updateMetricName(dm, dm.getDisplayName() );             
00386             }
00387             
00388         } else {
00389             InputDialog inDlg = new InputDialog(getShell(), "Edit metric displayed name", 
00390                     "Enter the new display name metric", metric.getDisplayName(), null);
00391             if (inDlg.open() == Dialog.OK) {
00392                 String name = inDlg.getValue();
00393                 updateMetricName(metric, name);
00394             }
00395         }
00396     }
00397     
00398     /***
00399      * make change the metric
00400      * 
00401      * @param metric
00402      * @param sNewName
00403      */
00404     private void updateMetricName(BaseMetric metric, String sNewName) {
00405         
00406         PropertiesModel obj = getSelectElement();
00407         obj.sTitle = sNewName;
00408         metric.setDisplayName(sNewName);
00409         viewer.update(obj, null);
00410     }
00411 
00412     
00413     /* (non-Javadoc)
00414     * @see org.eclipse.jface.window.Window#setShellStyle(int)
00415     */
00416     protected void setShellStyle(int newShellStyle) {
00417 
00418         super.setShellStyle(newShellStyle | SWT.RESIZE | SWT.MAX);
00419     }
00420     
00421     //--------------------------------------------------
00422     //  CLASSES
00423     //--------------------------------------------------
00424 
00430     protected class PropertiesModel {
00431 
00432         public String sTitle;
00433         public int iIndex;
00434         public BaseMetric metric;
00435 
00436         public PropertiesModel(String s, int i, BaseMetric metric) {
00437             this.sTitle = s;
00438             this.iIndex = i;
00439             this.metric = metric;
00440         }
00441     }
00442 
00443     
00444     //--------------------------------------------------
00445     //  Unit test
00446     //--------------------------------------------------
00447 
00451     public static void main(String[] args) {
00452         Display display = new Display();
00453 
00454         Shell shell = new Shell(display);
00455         shell.setLayout(new FillLayout());
00456         
00457         // first step: create the dialog, and implement all the abstract interfaces
00458         MetricPropertyDialog dialog = new MetricPropertyDialog(shell, null);
00459         
00460         // second step: initialize the column, make sure they have all the data to 
00461         //  distinguish with user custom column
00462         Experiment exp = new Experiment();
00463         java.util.List<BaseMetric> list = new java.util.ArrayList<BaseMetric>(10);
00464         
00465         for (int i=0; i<10; i++) {
00466             final String id = String.valueOf(4 * i + 10);
00467             list.add( new Metric(id, id, "M" + id, true, null, null, null, i, MetricType.INCLUSIVE, i) );
00468         }
00469         exp.setMetrics(list);
00470         dialog.setExperiment(exp);
00471         
00472         dialog.open();
00473     }
00474 
00475 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1