MetricLabelProvider.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.metric;
00002 
00003 import org.eclipse.core.runtime.CoreException;
00004 import org.eclipse.core.runtime.IConfigurationElement;
00005 import org.eclipse.core.runtime.ISafeRunnable;
00006 import org.eclipse.core.runtime.Platform;
00007 import org.eclipse.core.runtime.SafeRunner;
00008 import org.eclipse.swt.graphics.Color;
00009 import org.eclipse.swt.graphics.Font;
00010 
00011 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00012 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00013 import edu.rice.cs.hpc.viewer.metric.IMetricLabelProvider;
00014 
00020 public class MetricLabelProvider extends BaseMetricLabelProvider {
00021     static private enum MethodFlag { TEXT, FONT, FOREGROUND, BACKGROUND };
00022 
00023     // This is the ID of our extension point
00024     private static final String METRIC_LABEL_PROVIDER_ID = "edu.rice.cs.hpc.viewer.metric.metricLabelProvider";
00025     
00026     private IMetricLabelProvider extLabelProvider[] = null;
00027     private ExtensionSafeRunnable runnable = null;
00028 
00029     
00030     /***
00031      * 
00032      * @param metricNew
00033      */
00034     public MetricLabelProvider(BaseMetric metricNew) {
00035         super(metricNew);
00036         IConfigurationElement[] configs = Platform.getExtensionRegistry().getConfigurationElementsFor(METRIC_LABEL_PROVIDER_ID);
00037         
00038         if (configs != null && configs.length>0) {
00039             
00040             extLabelProvider = new IMetricLabelProvider[configs.length];
00041             int i = 0;
00042             
00043             for (IConfigurationElement e: configs)
00044             {
00045                 try {
00046                     final Object o = e.createExecutableExtension("class");
00047                     if (o instanceof IMetricLabelProvider) {
00048                         ((IMetricLabelProvider)o).setMetric(metricNew);
00049                         extLabelProvider[i] = ((IMetricLabelProvider)o);
00050                         i++;
00051                         
00052                         if (runnable == null)
00053                             runnable = new ExtensionSafeRunnable();
00054                     }
00055                 } catch (CoreException e1) {
00056                     // TODO Auto-generated catch block
00057                     e1.printStackTrace();
00058                 }
00059             }
00060         }       
00061     }
00062     
00063     /*
00064      * (non-Javadoc)
00065      * @see edu.rice.cs.hpc.viewer.metric.BaseMetricLabelProvider#getFont(java.lang.Object)
00066      */
00067     public Font getFont(Object element) {
00068 
00069         if (!(element instanceof Scope)) {
00070                 return null;
00071         }
00072 
00073         if (runnable != null) {
00074                 if ( this.runExtension(runnable, element, MethodFlag.FONT) )
00075                         return (Font) runnable.getResult();
00076         }
00077 
00078         return super.getFont(element);
00079     }
00080 
00081     /*
00082      * (non-Javadoc)
00083      * @see edu.rice.cs.hpc.viewer.metric.BaseMetricLabelProvider#getText(java.lang.Object)
00084      */
00085     public String getText(Object element) {
00086 
00087         if (!(element instanceof Scope)) {
00088             return null;
00089         }
00090         
00091         if (runnable != null) {
00092             if ( this.runExtension(runnable, element, MethodFlag.TEXT) )
00093                 return (String) runnable.getResult();
00094         }
00095         
00096         return super.getText(element);
00097     }
00098 
00108     public Color getBackground(final Object element) {
00109 
00110         if (!(element instanceof Scope)) {
00111             return null;
00112         }
00113         
00114         if (runnable != null) {
00115             if ( this.runExtension(runnable, element, MethodFlag.BACKGROUND) )
00116                 return (Color) runnable.getResult();
00117         }
00118 
00119         return super.getBackground(element);
00120     }
00121 
00131     public Color getForeground(final Object element) {
00132 
00133         if (!(element instanceof Scope)) {
00134             return null;
00135         }
00136 
00137         if (runnable != null) {
00138             if ( this.runExtension(runnable, element, MethodFlag.FOREGROUND) )
00139                 return (Color) runnable.getResult();
00140         }
00141         
00142         return super.getForeground(element);
00143     }
00144 
00145     /***
00146      * run all register extensions of metric label provider
00147      * 
00148      * @param run
00149      * @param element
00150      * @param mf
00151      */
00152     private boolean runExtension( ExtensionSafeRunnable run, Object element, MethodFlag mf ) {
00153         
00154         boolean isCalled = false;
00155         
00156         for (IMetricLabelProvider ext: this.extLabelProvider) {
00157             
00158             if (ext != null && ext.isEnabled()) {
00159                 run.setInfo(ext, element, mf);
00160                 ext.setScope(element);
00161                 SafeRunner.run(run);
00162                 isCalled = true;
00163             }
00164         }
00165         return isCalled;
00166     }
00167     
00174     private class ExtensionSafeRunnable implements ISafeRunnable {
00175         private Object element;
00176         private MethodFlag mf;
00177         private IMetricLabelProvider labelProvider;
00178 
00179         private Object result;
00180         
00181         public void setInfo(IMetricLabelProvider _labelProvider, Object _element, MethodFlag _mf) {
00182             labelProvider = _labelProvider;
00183             mf = _mf;
00184             element = _element;
00185         }
00186         
00187         public void handleException(Throwable exception) {
00188             System.out.println("Exception in label provider extension.");
00189         }
00190         public void run() throws Exception {
00191             switch(mf) {
00192             case TEXT:
00193                 result = labelProvider.getText(element);
00194                 break;
00195             case FONT:
00196                 result = labelProvider.getFont(element);
00197                 break;
00198             case FOREGROUND:
00199                 result = labelProvider.getForeground(element);
00200                 break;
00201             case BACKGROUND:
00202                 result = labelProvider.getBackground(element);
00203                 break;
00204             }
00205         }
00206         
00207         Object getResult() {
00208             return result;
00209         }
00210     }
00211 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1