KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > NavigatorPlugin


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
27  * The main plugin class for the Navigator.
28  *
29  * @since 3.2
30  */

31 public class NavigatorPlugin extends AbstractUIPlugin {
32     // The shared instance.
33
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 JavaDoc[] getListeners() {
43                 Object JavaDoc[] mesgs = super.getListeners();
44                 clear();
45                 return mesgs;
46             }
47         };
48
49         
50         /**
51          * Creates a Job which offloads the logging work into a non-UI thread.
52          *
53          */

54         public LogJob() {
55             super(""); //$NON-NLS-1$
56
setSystem(true);
57         }
58         
59         /* (non-Javadoc)
60          * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
61          */

62         protected IStatus run(IProgressMonitor monitor) {
63             
64             Object JavaDoc[] 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         /**
74          * @param mesg The message to add to the Plugin's log.
75          */

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     /** The id of the orge.eclipse.ui.navigator plugin. */
86     public static String JavaDoc PLUGIN_ID = "org.eclipse.ui.navigator"; //$NON-NLS-1$
87

88     private BundleListener bundleListener = new BundleListener() {
89         public void bundleChanged(BundleEvent event) {
90             NavigatorSaveablesService.bundleChanged(event);
91         }
92     };
93
94     /**
95      * Creates a new instance of the receiver
96      */

97     public NavigatorPlugin() {
98         super();
99         plugin = this;
100     }
101
102     /**
103      * @return the shared instance.
104      */

105     public static NavigatorPlugin getDefault() {
106         return plugin;
107     }
108
109     /**
110      * Returns an image descriptor for the image file at the given plug-in
111      * relative path.
112      *
113      * @param path
114      * the path
115      * @return the image descriptor
116      */

117     public static ImageDescriptor getImageDescriptor(String JavaDoc path) {
118         return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path);
119     }
120     
121
122     /**
123      * Returns an image descriptor for the image file at the given plug-in
124      * relative path.
125      *
126      * @param path
127      * the path
128      * @return the image
129      */

130     public Image getImage(String JavaDoc 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     /**
142      * Record an error against this plugin's log.
143      *
144      * @param aCode
145      * @param aMessage
146      * @param anException
147      */

148     public static void logError(int aCode, String JavaDoc aMessage,
149             Throwable JavaDoc anException) {
150         getDefault().getLog().log(
151                 createErrorStatus(aCode, aMessage, anException));
152     }
153
154     /**
155      *
156      * Record a message against this plugin's log.
157      *
158      * @param severity
159      * @param aCode
160      * @param aMessage
161      * @param exception
162      */

163     public static void log(int severity, int aCode, String JavaDoc aMessage,
164             Throwable JavaDoc exception) {
165         log(createStatus(severity, aCode, aMessage, exception));
166     }
167
168     /**
169      *
170      * Record a status against this plugin's log.
171      *
172      * @param aStatus
173      */

174     public static void log(IStatus aStatus) {
175         //getDefault().getLog().log(aStatus);
176
logJob.log(aStatus);
177         logJob.schedule(LOG_DELAY);
178     }
179
180     /**
181      * Create a status associated with this plugin.
182      *
183      * @param severity
184      * @param aCode
185      * @param aMessage
186      * @param exception
187      * @return A status configured with this plugin's id and the given parameters.
188      */

189     public static IStatus createStatus(int severity, int aCode,
190             String JavaDoc aMessage, Throwable JavaDoc exception) {
191         return new Status(severity, PLUGIN_ID, aCode,
192                 aMessage != null ? aMessage : "No message.", exception); //$NON-NLS-1$
193
}
194
195     /**
196      *
197      * @param aCode
198      * @param aMessage
199      * @param exception
200      * @return A status configured with this plugin's id and the given parameters.
201      */

202     public static IStatus createErrorStatus(int aCode, String JavaDoc aMessage,
203             Throwable JavaDoc exception) {
204         return createStatus(IStatus.ERROR, aCode, aMessage, exception);
205     }
206
207     public void start(BundleContext context) throws Exception JavaDoc {
208         // System.out.println("Navigator plugin starting"); //$NON-NLS-1$
209
super.start(context);
210         context.addBundleListener(bundleListener);
211     }
212
213     public void stop(BundleContext context) throws Exception JavaDoc {
214         context.removeBundleListener(bundleListener);
215         super.stop(context);
216         // System.out.println("Navigator plugin stopped"); //$NON-NLS-1$
217
}
218     
219 }
220
Popular Tags