Factoring For Eclipse
by Marcus Kestler


Listing One

package com.kestler.metrics;

import org.eclipse.core.runtime.IAdaptable;

/** @author Marcus Kestler. This Interface defines a metric. */
public interface IMetric extends IAdaptable {
    /** The event id for REFRESH. This will be called when the metric is 
     * instanciated. The element passed with this event has not been modified.
     */
    public final static int REFRESH = 100;
    /** @return The calcuated value for the metric. */
    public String getValue();
    /** Main event for metrics. @param notifyFlag one of the following events
     *              IResourceDelta.REMOVED
     *              IResourceDelta.CHANGED
     *              IResourceDelta.ADDED
     *              IMetric.REFRESH
     * @param _adaptable The element that has been changed, removed, etc
     */
    public void calculate(int notifyFlag, IAdaptable _adaptable);
    /** Adds a listener that will be notfied when the value of the metric
     * has been changed. 
     * @param _listener The listener
     */
    public void addValueListener(IValueListener _listener);
    /** Cleanup method */
    public void dispose();
    /** This will be called with the id set in the extension point
     * @param id The id of the metric defined in its extension point
     */
    public void setId(String id);
    /** @return Should return the id set in <code>setId()</code> */
    public String getId();
}


Listing Two

<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="com.kestler.metrics.com.kestler.metrics">
<annotation>
      <appInfo>
         <meta.schema plugin="com.kestler.metrics.com.kestler.metrics" 
                                              id="Metrics" name="Metric"/>
      </appInfo>
      <documentation>
         Adds a new Metric to be displayed by the Metrics Plugin
      </documentation>
   </annotation>
   <element name="extension">
      <annotation>
         <documentation>
            A new metric that implements the IMetric interface.
         </documentation>
      </annotation>
      <complexType>
         <sequence>
            <element ref="Metric" minOccurs="1" maxOccurs="unbounded"/>
            <element ref="category" minOccurs="0" maxOccurs="unbounded"/>
            <element ref="AggregateMetric" 
                                   minOccurs="0" maxOccurs="unbounded"/>
         </sequence>
         <attribute name="point" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="id" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="name" type="string">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
      </complexType>
   </element>

   <element name="Metric">
      <annotation>
         <appInfo>
            <meta.element labelAttribute="label"/>
         </appInfo>
      </annotation>
      <complexType>
         <attribute name="class" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
               <appInfo>
                  <meta.attribute kind="java" 
                                    basedOn="com.kestler.metrics.IMetric"/>
               </appInfo>
            </annotation>
         </attribute>
         <attribute name="label" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="categoryId" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="id" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="TypeFilter" type="string">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="icon" type="string">
            <annotation>
               <documentation>
               </documentation>
               <appInfo>
                  <meta.attribute kind="resource"/>
               </appInfo>
            </annotation>
         </attribute>
      </complexType>
   </element>
   <element name="category">
      <complexType>
         <attribute name="label" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="icon" type="string">
            <annotation>
               <documentation>
               </documentation>
               <appInfo>
                  <meta.attribute kind="resource"/>
               </appInfo>
            </annotation>
         </attribute>
         <attribute name="id" type="string">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
      </complexType>
   </element>
   <element name="AggregateMetric">
      <complexType>
         <sequence>
            <element ref="MetricParticipant" minOccurs="1" 
                                                  maxOccurs="unbounded"/>
         </sequence>
         <attribute name="id" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="categoryId" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="class" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
               <appInfo>
                  <meta.attribute kind="java" 
                          basedOn="com.kestler.metrics.IAggregateMetric"/>
               </appInfo>
            </annotation>
         </attribute>
         <attribute name="label" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
         <attribute name="icon" type="string">
            <annotation>
               <documentation>
               </documentation>
               <appInfo>
                  <meta.attribute kind="resource"/>
               </appInfo>
            </annotation>
         </attribute>
      </complexType>
   </element>
   <element name="MetricParticipant">
      <complexType>
         <attribute name="metricId" type="string" use="required">
            <annotation>
               <documentation>
               </documentation>
            </annotation>
         </attribute>
      </complexType>
   </element>
   <annotation>
      <appInfo>
         <meta.section type="since"/>
      </appInfo>
      <documentation>
         1.0.0
      </documentation>
   </annotation>
   <annotation>
      <appInfo>
         <meta.section type="examples"/>
      </appInfo>
      <documentation>
         [Enter extension point usage example here.]
      </documentation>
   </annotation>
   <annotation>
      <appInfo>
         <meta.section type="apiInfo"/>
      </appInfo>
      <documentation>
         [Enter API information here.]
      </documentation>
   </annotation>
   <annotation>
      <appInfo>
         <meta.section type="implementation"/>
      </appInfo>
      <documentation>
         [Enter information about supplied implementation 
                                                   of this extension point.]
      </documentation>
   </annotation>
   <annotation>
      <appInfo>
         <meta.section type="copyright"/>
      </appInfo>
      <documentation>
         
      </documentation>
   </annotation>
</schema>

Listing Three

private void processExtensions()
{
    IExtensionRegistry _registry = Platform.getExtensionRegistry();
    IExtensionPoint _ext = _registry.getExtensionPoint(EXT_ID);

    IConfigurationElement[] _cfgs = _ext.getConfigurationElements();
    for (int i = 0; i < _cfgs.length; i++) {

        IDefinition _def = ExtensionDefinitionFactory.getDefinition(_cfgs[i]);
        if (_def instanceof IMetricDefinition) {
            m_metricDefs.put(_def.getId(), _def);

        } else if (_def instanceof ICategoryDefinition) {
            m_categories.put(_def.getId(), _def);
            ICategoryDefinition cDef = (ICategoryDefinition)_def;
            if (cDef.getImage() != null) {
                ImageDescriptor _desc = cDef.getImage();
                m_images.put(_def.getId(), _desc.createImage());
            }
        } else if (_def instanceof IAggregateDefinition) {
            m_aggDefs.put(_def.getId(), _def);
        }
    }
}






5


