KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > TeamUIPlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.internal.ui;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.util.IPropertyChangeListener;
26 import org.eclipse.jface.util.PropertyChangeEvent;
27 import org.eclipse.swt.custom.BusyIndicator;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.team.core.RepositoryProviderType;
31 import org.eclipse.team.core.subscribers.Subscriber;
32 import org.eclipse.team.internal.ui.mapping.StreamMergerDelegate;
33 import org.eclipse.team.internal.ui.mapping.WorkspaceTeamStateProvider;
34 import org.eclipse.team.internal.ui.synchronize.SynchronizeManager;
35 import org.eclipse.team.internal.ui.synchronize.TeamSynchronizingPerspective;
36 import org.eclipse.team.internal.ui.synchronize.actions.GlobalRefreshAction;
37 import org.eclipse.team.ui.ISharedImages;
38 import org.eclipse.team.ui.mapping.ITeamStateProvider;
39 import org.eclipse.team.ui.synchronize.*;
40 import org.eclipse.ui.*;
41 import org.eclipse.ui.plugin.AbstractUIPlugin;
42 import org.osgi.framework.Bundle;
43 import org.osgi.framework.BundleContext;
44
45 /**
46  * TeamUIPlugin is the plugin for generic, non-provider specific,
47  * team UI functionality in the workbench.
48  */

49 public class TeamUIPlugin extends AbstractUIPlugin {
50
51     private static TeamUIPlugin instance;
52     
53     // image paths
54
public static final String JavaDoc ICON_PATH = "$nl$/icons/full/"; //$NON-NLS-1$
55

56     public static final String JavaDoc ID = "org.eclipse.team.ui"; //$NON-NLS-1$
57

58     // plugin id
59
public static final String JavaDoc PLUGIN_ID = "org.eclipse.team.ui"; //$NON-NLS-1$
60

61     public static final String JavaDoc TRIGGER_POINT_ID = "org.eclipse.team.ui.activityTriggerPoint"; //$NON-NLS-1$
62

63     private static List propertyChangeListeners = new ArrayList(5);
64     
65     private Hashtable imageDescriptors = new Hashtable(20);
66     
67     private WorkspaceTeamStateProvider provider;
68
69     private Map decoratedStateProviders = new HashMap();
70     
71     // manages synchronize participants
72
private SynchronizeManager synchronizeManager;
73     
74     /**
75      * Creates a new TeamUIPlugin.
76      */

77     public TeamUIPlugin() {
78         super();
79         instance = this;
80     }
81
82     /**
83      * Creates an extension. If the extension plugin has not
84      * been loaded a busy cursor will be activated during the duration of
85      * the load.
86      *
87      * @param element the config element defining the extension
88      * @param classAttribute the name of the attribute carrying the class
89      * @return the extension object
90      */

91     public static Object JavaDoc createExtension(final IConfigurationElement element, final String JavaDoc classAttribute) throws CoreException {
92         // If plugin has been loaded create extension.
93
// Otherwise, show busy cursor then create extension.
94
Bundle bundle = Platform.getBundle(element.getNamespace());
95         if (bundle.getState() == org.osgi.framework.Bundle.ACTIVE) {
96             return element.createExecutableExtension(classAttribute);
97         } else {
98             final Object JavaDoc [] ret = new Object JavaDoc[1];
99             final CoreException [] exc = new CoreException[1];
100             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
101                 public void run() {
102                     try {
103                         ret[0] = element.createExecutableExtension(classAttribute);
104                     } catch (CoreException e) {
105                         exc[0] = e;
106                     }
107                 }
108             });
109             if (exc[0] != null)
110                 throw exc[0];
111             else
112                 return ret[0];
113         }
114     }
115     
116     /**
117      * Convenience method to get the currently active workbench page. Note that
118      * the active page may not be the one that the usr perceives as active in
119      * some situations so this method of obtaining the activae page should only
120      * be used if no other method is available.
121      *
122      * @return the active workbench page
123      */

124     public static IWorkbenchPage getActivePage() {
125         IWorkbenchWindow window = getPlugin().getWorkbench().getActiveWorkbenchWindow();
126         if (window == null) return null;
127         return window.getActivePage();
128     }
129     
130     /**
131      * Return the default instance of the receiver. This represents the runtime plugin.
132      *
133      * @return the singleton plugin instance
134      */

135     public static TeamUIPlugin getPlugin() {
136         return instance;
137     }
138     /**
139      * Initializes the preferences for this plugin if necessary.
140      */

141     protected void initializeDefaultPluginPreferences() {
142         IPreferenceStore store = getPreferenceStore();
143         store.setDefault(IPreferenceIds.SYNCVIEW_VIEW_SYNCINFO_IN_LABEL, false);
144         store.setDefault(IPreferenceIds.SHOW_AUTHOR_IN_COMPARE_EDITOR, false);
145         store.setDefault(IPreferenceIds.MAKE_FILE_WRITTABLE_IF_CONTEXT_MISSING, false);
146         store.setDefault(IPreferenceIds.REUSE_OPEN_COMPARE_EDITOR, true);
147         store.setDefault(IPreferenceIds.RUN_IMPORT_IN_BACKGROUND, false);
148         store.setDefault(IPreferenceIds.SYNCVIEW_COMPRESS_FOLDERS, true);
149         store.setDefault(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT, IPreferenceIds.COMPRESSED_LAYOUT);
150         store.setDefault(IPreferenceIds.SYNCVIEW_DEFAULT_PERSPECTIVE, TeamSynchronizingPerspective.ID);
151         store.setDefault(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT, GlobalRefreshAction.NO_DEFAULT_PARTICPANT);
152         store.setDefault(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT_SEC_ID, GlobalRefreshAction.NO_DEFAULT_PARTICPANT);
153         store.setDefault(IPreferenceIds.SYNCHRONIZING_COMPLETE_PERSPECTIVE, MessageDialogWithToggle.PROMPT);
154         store.setDefault(IPreferenceIds.SYNCVIEW_REMOVE_FROM_VIEW_NO_PROMPT, false);
155         store.setDefault(IPreferenceIds.PREF_WORKSPACE_FIRST_TIME, true);
156         
157         // Convert the old compressed folder preference to the new layout preference
158
if (!store.isDefault(IPreferenceIds.SYNCVIEW_COMPRESS_FOLDERS) && !store.getBoolean(IPreferenceIds.SYNCVIEW_COMPRESS_FOLDERS)) {
159             // Set the compress folder preference to the defautl true) \
160
// so will will ignore it in the future
161
store.setToDefault(IPreferenceIds.SYNCVIEW_COMPRESS_FOLDERS);
162             // Set the layout to tree (which was used when compress folder was false)
163
store.setDefault(IPreferenceIds.SYNCVIEW_DEFAULT_LAYOUT, IPreferenceIds.TREE_LAYOUT);
164         }
165     }
166     
167     /**
168      * Convenience method for logging statuses to the plugin log
169      *
170      * @param status the status to log
171      */

172     public static void log(IStatus status) {
173         getPlugin().getLog().log(status);
174     }
175     
176     /**
177      * Convenience method for logging a TeamException in such a way that the
178      * stacktrace is logged as well.
179      * @param e
180      */

181     public static void log(CoreException e) {
182         IStatus status = e.getStatus();
183         log (status.getSeverity(), status.getMessage(), e);
184     }
185     
186     /**
187      * Log the given exception along with the provided message and severity indicator
188      */

189     public static void log(int severity, String JavaDoc message, Throwable JavaDoc e) {
190         log(new Status(severity, ID, 0, message, e));
191     }
192     
193     /**
194      * @see Plugin#start(BundleContext)
195      */

196     public void start(BundleContext context) throws Exception JavaDoc {
197         super.start(context);
198         
199         initializeImages(this);
200
201         // This is a backwards compatibility check to ensure that repository
202
// provider capability are enabled automatically if an old workspace is
203
// opened for the first time and contains projects shared with a disabled
204
// capability. We defer the actual processing of the projects to another
205
// job since it is not critical to the startup of the team ui plugin.
206
IPreferenceStore store = getPreferenceStore();
207         if (store.getBoolean(IPreferenceIds.PREF_WORKSPACE_FIRST_TIME)) {
208             Job capabilityInitializer = new Job("") { //$NON-NLS-1$
209
protected IStatus run(IProgressMonitor monitor) {
210                     TeamCapabilityHelper.getInstance();
211                     getPreferenceStore().setValue(IPreferenceIds.PREF_WORKSPACE_FIRST_TIME, false);
212                     return Status.OK_STATUS;
213                 }
214                 public boolean shouldRun() {
215                     // Only initialize the capability helper if the UI is running (bug 76348)
216
return PlatformUI.isWorkbenchRunning();
217                 }
218             };
219             capabilityInitializer.setSystem(true);
220             capabilityInitializer.setPriority(Job.DECORATE);
221             capabilityInitializer.schedule(1000);
222         }
223         
224         StreamMergerDelegate.start();
225     }
226     
227     /* (non-Javadoc)
228      * @see Plugin#stop(BundleContext)
229      */

230     public void stop(BundleContext context) throws Exception JavaDoc {
231         try {
232             if (synchronizeManager != null)
233                 synchronizeManager.dispose();
234         } finally {
235             super.stop(context);
236         }
237         if (provider != null) {
238             provider.dispose();
239         }
240         for (Iterator iter = decoratedStateProviders.values().iterator(); iter.hasNext();) {
241             SubscriberTeamStateProvider sdsp = (SubscriberTeamStateProvider) iter.next();
242             sdsp.dispose();
243         }
244     }
245
246     /**
247      * Register for changes made to Team properties.
248      */

249     public static void addPropertyChangeListener(IPropertyChangeListener listener) {
250         propertyChangeListeners.add(listener);
251     }
252     
253     /**
254      * Deregister as a Team property changes.
255      */

256     public static void removePropertyChangeListener(IPropertyChangeListener listener) {
257         propertyChangeListeners.remove(listener);
258     }
259     
260     /**
261      * Broadcast a Team property change.
262      */

263     public static void broadcastPropertyChange(PropertyChangeEvent event) {
264         for (Iterator it = propertyChangeListeners.iterator(); it.hasNext();) {
265             IPropertyChangeListener listener = (IPropertyChangeListener)it.next();
266             listener.propertyChange(event);
267         }
268     }
269     
270     /**
271      * Creates an image and places it in the image registry.
272      *
273      * @param id the identifier for the image
274      * @param baseURL the base URL for the image
275      */

276     private static void createImageDescriptor(TeamUIPlugin plugin, String JavaDoc id) {
277         // Delegate to the plugin instance to avoid concurrent class loading problems
278
plugin.privateCreateImageDescriptor(id);
279     }
280     private void privateCreateImageDescriptor(String JavaDoc id) {
281         ImageDescriptor desc = ImageDescriptor.createFromURL(getImageUrl(id));
282         imageDescriptors.put(id, desc);
283     }
284     
285     /**
286      * Returns the image descriptor for the given image ID.
287      * Returns null if there is no such image.
288      *
289      * @param id the identifier for the image to retrieve
290      * @return the image associated with the given ID
291      */

292     public static ImageDescriptor getImageDescriptor(String JavaDoc id) {
293         // Delegate to the plugin instance to avoid concurrent class loading problems
294
return getPlugin().privateGetImageDescriptor(id);
295     }
296     private ImageDescriptor privateGetImageDescriptor(String JavaDoc id) {
297         if(! imageDescriptors.containsKey(id)) {
298             createImageDescriptor(getPlugin(), id);
299         }
300         return (ImageDescriptor)imageDescriptors.get(id);
301     }
302
303     /**
304      * Convenience method to get an image descriptor for an extension
305      *
306      * @param extension the extension declaring the image
307      * @param subdirectoryAndFilename the path to the image
308      * @return the image
309      */

310     public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String JavaDoc subdirectoryAndFilename) {
311         URL JavaDoc fullPathString = Platform.find(Platform.getBundle(extension.getNamespace()), new Path(subdirectoryAndFilename));
312         return ImageDescriptor.createFromURL(fullPathString);
313     }
314     /*
315      * Initializes the table of images used in this plugin. The plugin is
316      * provided because this method is called before the plugin staic
317      * variable has been set. See the comment on the getPlugin() method
318      * for a description of why this is required.
319      */

320     private void initializeImages(TeamUIPlugin plugin) {
321         // Overlays
322
createImageDescriptor(plugin, ISharedImages.IMG_DIRTY_OVR);
323         createImageDescriptor(plugin, ISharedImages.IMG_CONFLICT_OVR);
324         createImageDescriptor(plugin, ISharedImages.IMG_CHECKEDIN_OVR);
325         createImageDescriptor(plugin, ISharedImages.IMG_CHECKEDOUT_OVR);
326         createImageDescriptor(plugin, ISharedImages.IMG_ERROR_OVR);
327         createImageDescriptor(plugin, ISharedImages.IMG_WARNING_OVR);
328         createImageDescriptor(plugin, ISharedImages.IMG_HOURGLASS_OVR);
329         
330         // Target Management Icons
331
createImageDescriptor(plugin, ITeamUIImages.IMG_SITE_ELEMENT);
332         
333         // Sync View Icons
334
createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_INCOMING);
335         createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_OUTGOING);
336         createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_CONFLICTING);
337         createImageDescriptor(plugin, ITeamUIImages.IMG_REFRESH);
338         createImageDescriptor(plugin, ITeamUIImages.IMG_CHANGE_FILTER);
339         createImageDescriptor(plugin, ITeamUIImages.IMG_IGNORE_WHITESPACE);
340         createImageDescriptor(plugin, ITeamUIImages.IMG_COLLAPSE_ALL);
341         createImageDescriptor(plugin, ITeamUIImages.IMG_COLLAPSE_ALL_ENABLED);
342
343         createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_INCOMING_DISABLED);
344         createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_OUTGOING_DISABLED);
345         createImageDescriptor(plugin, ITeamUIImages.IMG_DLG_SYNC_CONFLICTING_DISABLED);
346         createImageDescriptor(plugin, ITeamUIImages.IMG_REFRESH_DISABLED);
347         createImageDescriptor(plugin, ITeamUIImages.IMG_IGNORE_WHITESPACE_DISABLED);
348
349         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_CATCHUP);
350         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_RELEASE);
351         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_FREE);
352
353         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_CATCHUP_DISABLED);
354         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_RELEASE_DISABLED);
355         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_FREE_DISABLED);
356
357         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_CATCHUP_ENABLED);
358         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_RELEASE_ENABLED);
359         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_MODE_FREE_ENABLED);
360
361         // Wizard banners
362
createImageDescriptor(plugin, ITeamUIImages.IMG_PROJECTSET_IMPORT_BANNER);
363         createImageDescriptor(plugin, ITeamUIImages.IMG_PROJECTSET_EXPORT_BANNER);
364         createImageDescriptor(plugin, ITeamUIImages.IMG_WIZBAN_SHARE);
365         
366         // Live Sync View icons
367
createImageDescriptor(plugin, ITeamUIImages.IMG_COMPRESSED_FOLDER);
368         createImageDescriptor(plugin, ITeamUIImages.IMG_SYNC_VIEW);
369         createImageDescriptor(plugin, ITeamUIImages.IMG_HIERARCHICAL);
370         
371         // Local History Page
372
createImageDescriptor(plugin, ITeamUIImages.IMG_DATES_CATEGORY);
373         createImageDescriptor(plugin, ITeamUIImages.IMG_COMPARE_VIEW);
374         createImageDescriptor(plugin, ITeamUIImages.IMG_LOCALREVISION_TABLE);
375     }
376
377     private URL JavaDoc getImageUrl(String JavaDoc relative) {
378         return Platform.find(Platform.getBundle(PLUGIN_ID), new Path(ICON_PATH + relative));
379     }
380
381     /**
382      * Returns the standard display to be used. The method first checks, if
383      * the thread calling this method has an associated display. If so, this
384      * display is returned. Otherwise the method returns the default display.
385      */

386     public static Display getStandardDisplay() {
387         Display display= Display.getCurrent();
388         if (display == null) {
389             display= Display.getDefault();
390         }
391         return display;
392     }
393     
394     public Image getImage(String JavaDoc key) {
395         Image image = getImageRegistry().get(key);
396         if(image == null) {
397             ImageDescriptor d = getImageDescriptor(key);
398             image = d.createImage();
399             getImageRegistry().put(key, image);
400         }
401         return image;
402     }
403     
404     public static void run(IRunnableWithProgress runnable) {
405         try {
406             PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, runnable);
407         } catch (InvocationTargetException JavaDoc e) {
408             Utils.handleError(getStandardDisplay().getActiveShell(), e, null, null);
409         } catch (InterruptedException JavaDoc e2) {
410             // Nothing to be done
411
}
412     }
413
414     public org.osgi.service.prefs.Preferences getInstancePreferences() {
415         return new InstanceScope().getNode(getBundle().getSymbolicName());
416     }
417
418     public synchronized TeamStateProvider getDecoratedStateProvider(RepositoryProviderType rpt) {
419         TeamStateProvider provider = (TeamStateProvider)decoratedStateProviders.get(rpt.getID());
420         if (provider != null)
421             return provider;
422         Subscriber subscriber = rpt.getSubscriber();
423         if (subscriber != null) {
424             provider = new SubscriberTeamStateProvider(subscriber);
425             decoratedStateProviders.put(rpt.getID(), provider);
426             return provider;
427         }
428         return null;
429     }
430     
431     /**
432      * Return a decorated state provider that delegates to the appropriate team
433      * provider.
434      * @return a decorated state provider that delegates to the appropriate team
435      * provider
436      */

437     public synchronized ITeamStateProvider getDecoratedStateProvider() {
438         if (provider == null)
439             provider = new WorkspaceTeamStateProvider();
440         return provider;
441     }
442     
443     public ISynchronizeManager getSynchronizeManager() {
444         if (synchronizeManager == null) {
445             synchronizeManager = new SynchronizeManager();
446         }
447         return synchronizeManager;
448     }
449 }
450
Popular Tags