KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > PDEPlugin


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.pde.internal.ui;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Hashtable JavaDoc;
16
17 import org.eclipse.core.resources.IWorkspace;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.ILaunchConfigurationListener;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationListener;
27 import org.eclipse.pde.internal.ui.launcher.LaunchListener;
28 import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
29 import org.eclipse.pde.internal.ui.launcher.OSGiFrameworkManager;
30 import org.eclipse.pde.internal.ui.util.SWTUtil;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
36 import org.eclipse.ui.forms.FormColors;
37 import org.eclipse.ui.plugin.AbstractUIPlugin;
38 import org.eclipse.ui.texteditor.IDocumentProvider;
39 import org.osgi.framework.BundleContext;
40
41 public class PDEPlugin extends AbstractUIPlugin implements IPDEUIConstants {
42
43     // Shared instance
44
private static PDEPlugin fInstance;
45     
46     // Launches listener
47
private LaunchListener fLaunchListener;
48     
49     private BundleContext fBundleContext;
50
51     private java.util.Hashtable JavaDoc fCounters;
52     
53     // Shared colors for all forms
54
private FormColors fFormColors;
55     private PDELabelProvider fLabelProvider;
56     private ILaunchConfigurationListener fLaunchConfigurationListener;
57
58     /**
59      * The shared text file document provider.
60      * @since 3.2
61      */

62     private IDocumentProvider fTextFileDocumentProvider;
63
64     private OSGiFrameworkManager fOSGiFrameworkManager;
65
66     public PDEPlugin() {
67         fInstance = this;
68     }
69     
70     public URL JavaDoc getInstallURL() {
71         return getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
72
}
73     
74     public static IWorkbenchPage getActivePage() {
75         return getDefault().internalGetActivePage();
76     }
77     public static Shell getActiveWorkbenchShell() {
78         IWorkbenchWindow window = getActiveWorkbenchWindow();
79         if (window != null) {
80             return window.getShell();
81         }
82         return null;
83     }
84     public static IWorkbenchWindow getActiveWorkbenchWindow() {
85         return getDefault().getWorkbench().getActiveWorkbenchWindow();
86     }
87     public static PDEPlugin getDefault() {
88         return fInstance;
89     }
90     public Hashtable JavaDoc getDefaultNameCounters() {
91         if (fCounters == null)
92             fCounters = new Hashtable JavaDoc();
93         return fCounters;
94     }
95     public static String JavaDoc getPluginId() {
96         return getDefault().getBundle().getSymbolicName();
97     }
98     
99     public static IWorkspace getWorkspace() {
100         return ResourcesPlugin.getWorkspace();
101     }
102     
103     private IWorkbenchPage internalGetActivePage() {
104         return getWorkbench().getActiveWorkbenchWindow().getActivePage();
105     }
106
107     public static void log(IStatus status) {
108         ResourcesPlugin.getPlugin().getLog().log(status);
109     }
110
111     public static void logErrorMessage(String JavaDoc message) {
112         log(new Status(IStatus.ERROR, getPluginId(), IStatus.ERROR, message, null));
113     }
114
115     public static void logException(
116         Throwable JavaDoc e,
117         final String JavaDoc title,
118         String JavaDoc message) {
119         if (e instanceof InvocationTargetException JavaDoc) {
120             e = ((InvocationTargetException JavaDoc) e).getTargetException();
121         }
122         IStatus status = null;
123         if (e instanceof CoreException)
124             status = ((CoreException) e).getStatus();
125         else {
126             if (message == null)
127                 message = e.getMessage();
128             if (message == null)
129                 message = e.toString();
130             status = new Status(IStatus.ERROR, getPluginId(), IStatus.OK, message, e);
131         }
132         ResourcesPlugin.getPlugin().getLog().log(status);
133         Display display = SWTUtil.getStandardDisplay();
134         final IStatus fstatus = status;
135         display.asyncExec(new Runnable JavaDoc() {
136             public void run() {
137                 ErrorDialog.openError(null, title, null, fstatus);
138             }
139         });
140     }
141
142     public static void logException(Throwable JavaDoc e) {
143         logException(e, null, null);
144     }
145
146     public static void log(Throwable JavaDoc e) {
147         if (e instanceof InvocationTargetException JavaDoc)
148             e = ((InvocationTargetException JavaDoc) e).getTargetException();
149         IStatus status = null;
150         if (e instanceof CoreException)
151             status = ((CoreException) e).getStatus();
152         else
153             status =
154                 new Status(IStatus.ERROR, getPluginId(), IStatus.OK, e.getMessage(), e);
155         log(status);
156     }
157     
158     public FormColors getFormColors(Display display) {
159         if (fFormColors == null) {
160             fFormColors = new FormColors(display);
161             fFormColors.markShared();
162         }
163         return fFormColors;
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.core.runtime.Plugin#start(org.osgi.framework.BundleContext)
168      */

169     public void start(BundleContext context) throws Exception JavaDoc {
170         super.start(context);
171         this.fBundleContext = context;
172         fLaunchConfigurationListener = new LaunchConfigurationListener();
173         DebugPlugin.getDefault().getLaunchManager().addLaunchConfigurationListener(fLaunchConfigurationListener);
174     }
175     
176     public BundleContext getBundleContext() {
177         return fBundleContext;
178     }
179     
180     /* (non-Javadoc)
181      * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
182      */

183     public void stop(BundleContext context) throws Exception JavaDoc {
184         if (fLaunchListener != null)
185             fLaunchListener.shutdown();
186         if (fFormColors != null) {
187             fFormColors.dispose();
188             fFormColors = null;
189         }
190         if (fLabelProvider != null) {
191             fLabelProvider.dispose();
192             fLabelProvider = null;
193         }
194         if (fLaunchConfigurationListener != null) {
195             DebugPlugin.getDefault().getLaunchManager().removeLaunchConfigurationListener(fLaunchConfigurationListener);
196             fLaunchConfigurationListener = null;
197         }
198         LauncherUtils.shutdown();
199         super.stop(context);
200     }
201
202     public PDELabelProvider getLabelProvider() {
203         if (fLabelProvider == null)
204             fLabelProvider = new PDELabelProvider();
205         return fLabelProvider;
206     }
207     
208     public LaunchListener getLaunchListener() {
209         if (fLaunchListener == null)
210             fLaunchListener = new LaunchListener();
211         return fLaunchListener;
212     }
213     
214     public OSGiFrameworkManager getOSGiFrameworkManager() {
215         if (fOSGiFrameworkManager == null)
216             fOSGiFrameworkManager = new OSGiFrameworkManager();
217         return fOSGiFrameworkManager;
218     }
219     
220     public static boolean isFullNameModeEnabled() {
221         IPreferenceStore store = getDefault().getPreferenceStore();
222         return store.getString(IPreferenceConstants.PROP_SHOW_OBJECTS).equals(IPreferenceConstants.VALUE_USE_NAMES);
223     }
224     
225     /**
226      * Returns the shared text file document provider for this plug-in.
227      *
228      * @return the shared text file document provider
229      * @since 3.2
230      */

231     public synchronized IDocumentProvider getTextFileDocumentProvider() {
232         if (fTextFileDocumentProvider == null) {
233             fTextFileDocumentProvider = new TextFileDocumentProvider() {
234                 protected FileInfo createFileInfo(Object JavaDoc element) throws CoreException {
235                     FileInfo info = super.createFileInfo(element);
236                     if (info != null && info.fTextFileBuffer != null)
237                         info.fModel = info.fTextFileBuffer.getAnnotationModel();
238                     setUpSynchronization(info);
239                     return info;
240                 }
241             };
242         }
243         return fTextFileDocumentProvider;
244     }
245 }
246
Popular Tags