/eclipse-plugin/plugins/com.google.test.metric.eclipse.ui/src/main/java/com/google/test/metric/eclipse/ui/plugin/Activator.java

http://testability-explorer.googlecode.com/ · Java · 101 lines · 48 code · 11 blank · 42 comment · 3 complexity · 4bfd90a5c5bd0b07fd926240aa15676a MD5 · raw file

  1. /*
  2. * Copyright 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.eclipse.ui.plugin;
  17. import org.eclipse.jface.resource.ImageDescriptor;
  18. import org.eclipse.swt.graphics.Image;
  19. import org.eclipse.ui.plugin.AbstractUIPlugin;
  20. import org.osgi.framework.BundleContext;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * The activator class controls the plug-in life cycle
  27. */
  28. public class Activator extends AbstractUIPlugin {
  29. private Map<String, Image> images = new HashMap<String, Image>();
  30. // The plug-in ID
  31. public static final String PLUGIN_ID = "com.google.test.metric.eclipse.ui";
  32. // The shared instance
  33. private static Activator plugin;
  34. /**
  35. * The constructor
  36. */
  37. public Activator() {
  38. }
  39. /*
  40. * (non-Javadoc)
  41. *
  42. * @see
  43. * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
  44. * )
  45. */
  46. @Override
  47. public void start(BundleContext context) throws Exception {
  48. super.start(context);
  49. plugin = this;
  50. }
  51. /*
  52. * (non-Javadoc)
  53. *
  54. * @see
  55. * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
  56. * )
  57. */
  58. @Override
  59. public void stop(BundleContext context) throws Exception {
  60. plugin = null;
  61. super.stop(context);
  62. }
  63. /**
  64. * Returns the shared instance
  65. *
  66. * @return the shared instance
  67. */
  68. public static Activator getDefault() {
  69. return plugin;
  70. }
  71. public Image getImage(String path) throws ImageNotFoundException {
  72. Image image = images.get(path);
  73. if (image == null) {
  74. String pluginLocation = Activator.getDefault().getBundle().getLocation();
  75. if (pluginLocation.startsWith("reference:")) {
  76. pluginLocation = pluginLocation.substring(10);
  77. }
  78. URL url;
  79. try {
  80. url = new URL(pluginLocation + path);
  81. } catch (MalformedURLException e) {
  82. throw new ImageNotFoundException("Image : " + path + " not found");
  83. }
  84. ImageDescriptor projectImageDescriptor = ImageDescriptor.createFromURL(url);
  85. image = projectImageDescriptor.createImage();
  86. images.put(path, image);
  87. }
  88. return image;
  89. }
  90. }