1 11 package org.eclipse.ui.internal.navigator; 12 13 import org.eclipse.core.runtime.ILog; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.ListenerList; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.ui.plugin.AbstractUIPlugin; 22 import org.osgi.framework.BundleContext; 23 import org.osgi.framework.BundleEvent; 24 import org.osgi.framework.BundleListener; 25 26 31 public class NavigatorPlugin extends AbstractUIPlugin { 32 private static NavigatorPlugin plugin; 34 35 private static final int LOG_DELAY = 100; 36 37 private static class LogJob extends Job { 38 39 40 private ListenerList messages = new ListenerList() { 41 42 public synchronized Object [] getListeners() { 43 Object [] mesgs = super.getListeners(); 44 clear(); 45 return mesgs; 46 } 47 }; 48 49 50 54 public LogJob() { 55 super(""); setSystem(true); 57 } 58 59 62 protected IStatus run(IProgressMonitor monitor) { 63 64 Object [] mesgs = messages.getListeners(); 65 ILog pluginLog = getDefault().getLog(); 66 for (int i = 0; i < mesgs.length; i++) { 67 pluginLog.log((IStatus)mesgs[i]); 68 } 69 return Status.OK_STATUS; 70 71 } 72 73 76 public void log(IStatus mesg) { 77 messages.add(mesg); 78 79 } 80 81 } 82 83 private static final LogJob logJob = new LogJob(); 84 85 86 public static String PLUGIN_ID = "org.eclipse.ui.navigator"; 88 private BundleListener bundleListener = new BundleListener() { 89 public void bundleChanged(BundleEvent event) { 90 NavigatorSaveablesService.bundleChanged(event); 91 } 92 }; 93 94 97 public NavigatorPlugin() { 98 super(); 99 plugin = this; 100 } 101 102 105 public static NavigatorPlugin getDefault() { 106 return plugin; 107 } 108 109 117 public static ImageDescriptor getImageDescriptor(String path) { 118 return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); 119 } 120 121 122 130 public Image getImage(String path) { 131 Image image = getImageRegistry().get(path); 132 if(image == null) { 133 ImageDescriptor descriptor = getImageDescriptor(path); 134 if(descriptor != null) { 135 getImageRegistry().put(path, image = descriptor.createImage()); 136 } 137 } 138 return image; 139 } 140 141 148 public static void logError(int aCode, String aMessage, 149 Throwable anException) { 150 getDefault().getLog().log( 151 createErrorStatus(aCode, aMessage, anException)); 152 } 153 154 163 public static void log(int severity, int aCode, String aMessage, 164 Throwable exception) { 165 log(createStatus(severity, aCode, aMessage, exception)); 166 } 167 168 174 public static void log(IStatus aStatus) { 175 logJob.log(aStatus); 177 logJob.schedule(LOG_DELAY); 178 } 179 180 189 public static IStatus createStatus(int severity, int aCode, 190 String aMessage, Throwable exception) { 191 return new Status(severity, PLUGIN_ID, aCode, 192 aMessage != null ? aMessage : "No message.", exception); } 194 195 202 public static IStatus createErrorStatus(int aCode, String aMessage, 203 Throwable exception) { 204 return createStatus(IStatus.ERROR, aCode, aMessage, exception); 205 } 206 207 public void start(BundleContext context) throws Exception { 208 super.start(context); 210 context.addBundleListener(bundleListener); 211 } 212 213 public void stop(BundleContext context) throws Exception { 214 context.removeBundleListener(bundleListener); 215 super.stop(context); 216 } 218 219 } 220 | Popular Tags |