/*****************************************************************************
 *  Limpet - the Lightweight InforMation ProcEssing Toolkit
 *  http://limpet.info
 *
 *  (C) 2015-2016, Deep Blue C Technologies Ltd
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the Eclipse Public License v1.0
 *  (http://www.eclipse.org/legal/epl-v10.html)
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *****************************************************************************/
package info.limpet.ui;

import info.limpet.operations.spatial.GeoSupport;
import info.limpet.operations.spatial.GeotoolsCalculator;

import java.util.ArrayList;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin
{

  // The plug-in ID
  public static final String PLUGIN_ID = "info.limpet.ui"; //$NON-NLS-1$

  // The shared instance
  private static Activator plugin;

  private ImageRegistry _imageRegistry;

  /**
   * the adapters we know about
   * 
   */
  private ArrayList<IAdapterFactory> _adapters;

  /**
   * The constructor
   */
  public Activator()
  {
  }

  private static ImageRegistry getRegistry()
  {
    return plugin._imageRegistry;
  }

  public static Image getImageFromRegistry(final ImageDescriptor name)
  {
    Image res = null;

    // do we already have an image
    if (getRegistry() == null)
    {
      plugin._imageRegistry = new ImageRegistry();
    }

    // ok - do we have it already?
    res = getRegistry().get(name.toString());

    if (res == null)
    {
      getRegistry().put(name.toString(), name);
      res = getRegistry().get(name.toString());
    }

    // and return it..
    return res;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
   * )
   */
  public void start(BundleContext context) throws Exception
  {
    super.start(context);
    plugin = this;

    // ok, setup the geo tools service
    GeoSupport.setCalculator(new GeotoolsCalculator());
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
   * )
   */
  public void stop(BundleContext context) throws Exception
  {
    plugin = null;
    super.stop(context);
  }

  /**
   * Returns the shared instance
   * 
   * @return the shared instance
   */
  public static Activator getDefault()
  {
    return plugin;
  }

  /**
   * Returns an image descriptor for the image file at the given plug-in
   * relative path
   * 
   * @param path
   *          the path
   * @return the image descriptor
   */
  public static ImageDescriptor getImageDescriptor(String path)
  {
    return imageDescriptorFromPlugin(PLUGIN_ID, path);
  }

  public static void logError(int statCode, String message, Exception e)
  {
    IStatus status = new Status(statCode, PLUGIN_ID, message, e);
    getDefault().getLog().log(status);
  }

  public static void log(Exception e)
  {
    IStatus status = new Status(IStatus.WARNING, PLUGIN_ID, e.getMessage(), e);
    getDefault().getLog().log(status);
  }

  /**
   * @param limpetAdapters
   */
  public void addAdapterFactory(IAdapterFactory newFactory)
  {
    if (_adapters == null)
    {
      _adapters = new ArrayList<IAdapterFactory>();
    }
    _adapters.add(newFactory);

  }

  /**
   * @return
   */
  public ArrayList<IAdapterFactory> getAdapters()
  {
    return _adapters;
  }

}

@@ -2259,37 +2259,32 @@
 %0A   * %0A   * @see
-%0A   *
  org.eclipse.ui.
@@ -2337,37 +2337,32 @@
 rk.BundleContext
-%0A   *
  )%0A   */%0A  publi
@@ -2603,21 +2603,16 @@
   * @see
-%0A   *
  org.ecl
@@ -2680,21 +2680,16 @@
 eContext
-%0A   *
  )%0A   */
@@ -3030,21 +3030,16 @@
  plug-in
-%0A   *
  relativ
@@ -3406,46 +3406,245 @@
 e);%0A
+%0A
     
-getDefault().getLog().log(status);
+final Activator default1 = getDefault();%0A    if (default1 != null)%0A    %7B%0A      default1.getLog().log(status);%0A    %7D%0A    else%0A    %7B%0A      System.err.println(%22Logger not assigned. Message:%22 + message);%0A      e.printStackTrace();%0A    %7D
 %0A  %7D

handle missing default activator (as with unit testing)