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
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
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 public MetricPropertyDialog(Shell parentShell, IWorkbenchWindow window) {
00076 super(parentShell);
00077 this.window = window;
00078 }
00079
00080
00081
00082
00083
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
00101
00102
00103 protected Control createDialogArea(Composite aParent) {
00104
00105
00106 initTableViewer(aParent);
00107
00108 return aParent;
00109 }
00110
00111
00112
00113
00114
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
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
00149
00150
00151
00152
00153
00154
00155
00156 private void initTableViewer(Composite composite) {
00157
00158 boolean singleExperiment = true;
00159
00160
00161
00162
00163
00164 if (window != null) {
00165
00166
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
00177
00178 experiment = vw.getExperiments()[0];
00179
00180 } else {
00181
00182
00183
00184 updateContent(composite, vw) ;
00185 }
00186 }
00187
00188
00189
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
00206
00207
00208 btnEdit.setEnabled(isEnabled);
00209 }
00210 });
00211
00212
00213 viewer.addDoubleClickListener(new IDoubleClickListener() {
00214
00215 @Override
00216 public void doubleClick(DoubleClickEvent event) {
00217 doAction();
00218 }
00219 });
00220
00221
00222 viewer.setContentProvider( new ArrayContentProvider() );
00223
00224
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
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
00261
00262 if (singleExperiment)
00263 {
00264 setElements(experiment);
00265 }
00266 }
00267
00268
00274 private void updateContent(Composite component, ViewerWindow vw) {
00275
00276
00277
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
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
00313
00314
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
00336
00337
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
00351
00352
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
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
00400
00401
00402
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
00414
00415
00416 protected void setShellStyle(int newShellStyle) {
00417
00418 super.setShellStyle(newShellStyle | SWT.RESIZE | SWT.MAX);
00419 }
00420
00421
00422
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
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
00458 MetricPropertyDialog dialog = new MetricPropertyDialog(shell, null);
00459
00460
00461
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 }