1 8 package com.mountainminds.eclemma.internal.ui; 9 10 import java.net.URL ; 11 12 import org.eclipse.core.runtime.CoreException; 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.core.runtime.Status; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.jface.resource.ImageRegistry; 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.widgets.Shell; 19 import org.eclipse.ui.IViewPart; 20 import org.eclipse.ui.IWorkbenchPage; 21 import org.eclipse.ui.IWorkbenchWindow; 22 import org.eclipse.ui.PartInitException; 23 import org.eclipse.ui.plugin.AbstractUIPlugin; 24 import org.osgi.framework.BundleContext; 25 26 import com.mountainminds.eclemma.core.CoverageTools; 27 import com.mountainminds.eclemma.core.ICoverageSession; 28 import com.mountainminds.eclemma.core.ISessionListener; 29 import com.mountainminds.eclemma.internal.ui.annotation.EditorTracker; 30 import com.mountainminds.eclemma.internal.ui.coverageview.CoverageView; 31 32 37 public class EclEmmaUIPlugin extends AbstractUIPlugin { 38 39 public static final String ID = "com.mountainminds.eclemma.ui"; 41 42 public static final String ID_COVERAGE_LAUNCH_GROUP = ID + ".launchGroup.coverage"; 44 45 47 public static final String ELCL_REFRESH = "icons/full/elcl16/refresh.gif"; public static final String DLCL_REFRESH = "icons/full/dlcl16/refresh.gif"; public static final String ELCL_IMPORT = "icons/full/elcl16/import.gif"; public static final String DLCL_IMPORT = "icons/full/dlcl16/import.gif"; public static final String ELCL_EXPORT = "icons/full/elcl16/export.gif"; public static final String DLCL_EXPORT = "icons/full/dlcl16/export.gif"; public static final String ELCL_REMOVE = "icons/full/elcl16/remove.gif"; public static final String DLCL_REMOVE = "icons/full/dlcl16/remove.gif"; public static final String ELCL_REMOVEALL = "icons/full/elcl16/removeall.gif"; public static final String DLCL_REMOVEALL = "icons/full/dlcl16/removeall.gif"; public static final String ELCL_MERGESESSIONS = "icons/full/elcl16/mergesessions.gif"; public static final String DLCL_MERGESESSIONS = "icons/full/dlcl16/mergesessions.gif"; public static final String ELCL_RELAUNCH = "icons/full/elcl16/relaunch.gif"; public static final String DLCL_RELAUNCH = "icons/full/dlcl16/relaunch.gif"; public static final String ELCL_SESSION = "icons/full/elcl16/session.gif"; public static final String DLCL_SESSION = "icons/full/dlcl16/session.gif"; public static final String ELCL_SHOWPROJECTS = "icons/full/elcl16/showprojects.gif"; public static final String ELCL_SHOWPACKAGEROOTS = "icons/full/elcl16/showpackageroots.gif"; public static final String ELCL_SHOWPACKAGES = "icons/full/elcl16/showpackages.gif"; public static final String ELCL_SHOWTYPES = "icons/full/elcl16/showtypes.gif"; public static final String ELCL_COLLAPSEALL = "icons/full/elcl16/collapseall.gif"; public static final String ELCL_LINKED = "icons/full/elcl16/linked.gif"; 70 public static final String EVIEW_COVERAGE = "icons/full/eview16/coverage.gif"; 72 public static final String OBJ_SESSION = "icons/full/obj16/session.gif"; 74 private static final String [] OBJ_COVERAGE = new String [] { 75 "icons/full/obj16/coverage00.gif", "icons/full/obj16/coverage01.gif", "icons/full/obj16/coverage02.gif", "icons/full/obj16/coverage03.gif", "icons/full/obj16/coverage04.gif", "icons/full/obj16/coverage05.gif", "icons/full/obj16/coverage06.gif", "icons/full/obj16/coverage07.gif", "icons/full/obj16/coverage08.gif", "icons/full/obj16/coverage09.gif", "icons/full/obj16/coverage10.gif", "icons/full/obj16/coverage11.gif", "icons/full/obj16/coverage12.gif" }; 89 90 private static final String [] OBJ_COVERAGE_OVERLAY = new String [] { 91 "icons/full/ovr16/coverage00.gif", "icons/full/ovr16/coverage01.gif", "icons/full/ovr16/coverage02.gif", "icons/full/ovr16/coverage03.gif", "icons/full/ovr16/coverage04.gif", "icons/full/ovr16/coverage05.gif", "icons/full/ovr16/coverage06.gif", "icons/full/ovr16/coverage07.gif" }; 100 101 public static final String WIZBAN_EXPORT_SESSION = "icons/full/wizban/export_session.gif"; public static final String WIZBAN_IMPORT_SESSION = "icons/full/wizban/import_session.gif"; 104 105 private static EclEmmaUIPlugin instance; 106 107 private EditorTracker editorTracker; 108 109 private ISessionListener sessionListener = new ISessionListener() { 110 public void sessionAdded(ICoverageSession addedSession) { 111 if (getPreferenceStore().getBoolean(UIPreferences.PREF_SHOW_COVERAGE_VIEW)) { 112 getWorkbench().getDisplay().asyncExec(new Runnable () { 113 public void run() { 114 showCoverageView(); 115 } 116 }); 117 } 118 } 119 public void sessionRemoved(ICoverageSession removedSession) { 120 } 121 public void sessionActivated(ICoverageSession session) { 122 } 123 }; 124 125 public void start(BundleContext context) throws Exception { 126 super.start(context); 127 CoverageTools.setPreferences(UIPreferences.CORE_PREFERENCES); 128 CoverageTools.getSessionManager().addSessionListener(sessionListener); 129 editorTracker = new EditorTracker(getWorkbench()); 130 instance = this; 131 } 132 133 public void stop(BundleContext context) throws Exception { 134 instance = null; 135 editorTracker.dispose(); 136 CoverageTools.getSessionManager().removeSessionListener(sessionListener); 137 super.stop(context); 138 } 139 140 public static EclEmmaUIPlugin getInstance() { 141 return instance; 142 } 143 144 private void showCoverageView() { 145 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow(); 146 if (window == null) return; 147 IWorkbenchPage page = window.getActivePage(); 148 if (page != null) { 149 try { 150 IViewPart view = page.showView(CoverageView.ID, null, IWorkbenchPage.VIEW_CREATE); 151 page.bringToTop(view); 152 } catch (PartInitException e) { 153 log(e); 154 } 155 } 156 } 157 158 public Shell getShell() { 159 return getWorkbench().getActiveWorkbenchWindow().getShell(); 160 } 161 162 public static IStatus errorStatus(String message, Throwable t) { 163 return new Status(IStatus.ERROR, ID, IStatus.ERROR, message, t); 164 } 165 166 public static void log(Throwable t) { 167 String message = t.getMessage(); 168 if (message == null) { 169 message = "Internal Error"; } 171 instance.getLog().log(errorStatus(message, t)); 172 } 173 174 public static void log(CoreException t) { 175 instance.getLog().log(t.getStatus()); 176 } 177 178 public static ImageDescriptor getImageDescriptor(String key) { 179 return loadImage(key).getDescriptor(key); 180 } 181 182 public static Image getImage(String key) { 183 return loadImage(key).get(key); 184 } 185 186 public static Image getCoverageImage(double ratio) { 187 int idx = (int) Math.round(ratio * OBJ_COVERAGE.length); 188 if (idx < 0) idx = 0; 189 if (idx >= OBJ_COVERAGE.length) idx = OBJ_COVERAGE.length - 1; 190 return getImage(OBJ_COVERAGE[idx]); 191 } 192 193 public static ImageDescriptor getCoverageOverlay(double ratio) { 194 int idx = (int) Math.round(ratio * OBJ_COVERAGE_OVERLAY.length); 195 if (idx < 0) idx = 0; 196 if (idx >= OBJ_COVERAGE_OVERLAY.length) idx = OBJ_COVERAGE_OVERLAY.length - 1; 197 return getImageDescriptor(OBJ_COVERAGE_OVERLAY[idx]); 198 } 199 200 private static ImageRegistry loadImage(String path) { 201 ImageRegistry reg = getInstance().getImageRegistry(); 202 if (reg.getDescriptor(path) == null) { 203 URL url = instance.getBundle().getEntry(path); 204 reg.put(path, ImageDescriptor.createFromURL(url)); 205 } 206 return reg; 207 } 208 209 } 210 | Popular Tags |