001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.navigator;
011:
012: import org.eclipse.core.runtime.ILog;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.ListenerList;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.core.runtime.jobs.Job;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.ui.plugin.AbstractUIPlugin;
021: import org.osgi.framework.BundleContext;
022: import org.osgi.framework.BundleEvent;
023: import org.osgi.framework.BundleListener;
024:
025: /**
026: * The main plugin class for the Navigator.
027: *
028: * @since 3.2
029: */
030: public class NavigatorPlugin extends AbstractUIPlugin {
031: // The shared instance.
032: private static NavigatorPlugin plugin;
033:
034: private static final int LOG_DELAY = 100;
035:
036: private static class LogJob extends Job {
037:
038: private ListenerList messages = new ListenerList() {
039:
040: public synchronized Object[] getListeners() {
041: Object[] mesgs = super .getListeners();
042: clear();
043: return mesgs;
044: }
045: };
046:
047: /**
048: * Creates a Job which offloads the logging work into a non-UI thread.
049: *
050: */
051: public LogJob() {
052: super (""); //$NON-NLS-1$
053: setSystem(true);
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
058: */
059: protected IStatus run(IProgressMonitor monitor) {
060:
061: Object[] mesgs = messages.getListeners();
062: ILog pluginLog = getDefault().getLog();
063: for (int i = 0; i < mesgs.length; i++) {
064: pluginLog.log((IStatus) mesgs[i]);
065: }
066: return Status.OK_STATUS;
067:
068: }
069:
070: /**
071: * @param mesg The message to add to the Plugin's log.
072: */
073: public void log(IStatus mesg) {
074: messages.add(mesg);
075:
076: }
077:
078: }
079:
080: private static final LogJob logJob = new LogJob();
081:
082: /** The id of the orge.eclipse.ui.navigator plugin. */
083: public static String PLUGIN_ID = "org.eclipse.ui.navigator"; //$NON-NLS-1$
084:
085: private BundleListener bundleListener = new BundleListener() {
086: public void bundleChanged(BundleEvent event) {
087: NavigatorSaveablesService.bundleChanged(event);
088: }
089: };
090:
091: /**
092: * Creates a new instance of the receiver
093: */
094: public NavigatorPlugin() {
095: super ();
096: plugin = this ;
097: }
098:
099: /**
100: * @return the shared instance.
101: */
102: public static NavigatorPlugin getDefault() {
103: return plugin;
104: }
105:
106: /**
107: * Returns an image descriptor for the image file at the given plug-in
108: * relative path.
109: *
110: * @param path
111: * the path
112: * @return the image descriptor
113: */
114: public static ImageDescriptor getImageDescriptor(String path) {
115: return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID,
116: path);
117: }
118:
119: /**
120: * Returns an image descriptor for the image file at the given plug-in
121: * relative path.
122: *
123: * @param path
124: * the path
125: * @return the image
126: */
127: public Image getImage(String path) {
128: Image image = getImageRegistry().get(path);
129: if (image == null) {
130: ImageDescriptor descriptor = getImageDescriptor(path);
131: if (descriptor != null) {
132: getImageRegistry().put(path,
133: image = descriptor.createImage());
134: }
135: }
136: return image;
137: }
138:
139: /**
140: * Record an error against this plugin's log.
141: *
142: * @param aCode
143: * @param aMessage
144: * @param anException
145: */
146: public static void logError(int aCode, String aMessage,
147: Throwable anException) {
148: getDefault().getLog().log(
149: createErrorStatus(aCode, aMessage, anException));
150: }
151:
152: /**
153: *
154: * Record a message against this plugin's log.
155: *
156: * @param severity
157: * @param aCode
158: * @param aMessage
159: * @param exception
160: */
161: public static void log(int severity, int aCode, String aMessage,
162: Throwable exception) {
163: log(createStatus(severity, aCode, aMessage, exception));
164: }
165:
166: /**
167: *
168: * Record a status against this plugin's log.
169: *
170: * @param aStatus
171: */
172: public static void log(IStatus aStatus) {
173: //getDefault().getLog().log(aStatus);
174: logJob.log(aStatus);
175: logJob.schedule(LOG_DELAY);
176: }
177:
178: /**
179: * Create a status associated with this plugin.
180: *
181: * @param severity
182: * @param aCode
183: * @param aMessage
184: * @param exception
185: * @return A status configured with this plugin's id and the given parameters.
186: */
187: public static IStatus createStatus(int severity, int aCode,
188: String aMessage, Throwable exception) {
189: return new Status(severity, PLUGIN_ID, aCode,
190: aMessage != null ? aMessage : "No message.", exception); //$NON-NLS-1$
191: }
192:
193: /**
194: *
195: * @param aCode
196: * @param aMessage
197: * @param exception
198: * @return A status configured with this plugin's id and the given parameters.
199: */
200: public static IStatus createErrorStatus(int aCode, String aMessage,
201: Throwable exception) {
202: return createStatus(IStatus.ERROR, aCode, aMessage, exception);
203: }
204:
205: public void start(BundleContext context) throws Exception {
206: // System.out.println("Navigator plugin starting"); //$NON-NLS-1$
207: super .start(context);
208: context.addBundleListener(bundleListener);
209: }
210:
211: public void stop(BundleContext context) throws Exception {
212: context.removeBundleListener(bundleListener);
213: super .stop(context);
214: // System.out.println("Navigator plugin stopped"); //$NON-NLS-1$
215: }
216:
217: }
|