001: /*******************************************************************************
002: * Copyright (c) 2000, 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.pde.internal.ui;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.net.URL;
014: import java.util.Hashtable;
015:
016: import org.eclipse.core.resources.IWorkspace;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.debug.core.DebugPlugin;
022: import org.eclipse.debug.core.ILaunchConfigurationListener;
023: import org.eclipse.jface.dialogs.ErrorDialog;
024: import org.eclipse.jface.preference.IPreferenceStore;
025: import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationListener;
026: import org.eclipse.pde.internal.ui.launcher.LaunchListener;
027: import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
028: import org.eclipse.pde.internal.ui.launcher.OSGiFrameworkManager;
029: import org.eclipse.pde.internal.ui.util.SWTUtil;
030: import org.eclipse.swt.widgets.Display;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.ui.IWorkbenchPage;
033: import org.eclipse.ui.IWorkbenchWindow;
034: import org.eclipse.ui.editors.text.TextFileDocumentProvider;
035: import org.eclipse.ui.forms.FormColors;
036: import org.eclipse.ui.plugin.AbstractUIPlugin;
037: import org.eclipse.ui.texteditor.IDocumentProvider;
038: import org.osgi.framework.BundleContext;
039:
040: public class PDEPlugin extends AbstractUIPlugin implements
041: IPDEUIConstants {
042:
043: // Shared instance
044: private static PDEPlugin fInstance;
045:
046: // Launches listener
047: private LaunchListener fLaunchListener;
048:
049: private BundleContext fBundleContext;
050:
051: private java.util.Hashtable fCounters;
052:
053: // Shared colors for all forms
054: private FormColors fFormColors;
055: private PDELabelProvider fLabelProvider;
056: private ILaunchConfigurationListener fLaunchConfigurationListener;
057:
058: /**
059: * The shared text file document provider.
060: * @since 3.2
061: */
062: private IDocumentProvider fTextFileDocumentProvider;
063:
064: private OSGiFrameworkManager fOSGiFrameworkManager;
065:
066: public PDEPlugin() {
067: fInstance = this ;
068: }
069:
070: public URL getInstallURL() {
071: return getDefault().getBundle().getEntry("/"); //$NON-NLS-1$
072: }
073:
074: public static IWorkbenchPage getActivePage() {
075: return getDefault().internalGetActivePage();
076: }
077:
078: public static Shell getActiveWorkbenchShell() {
079: IWorkbenchWindow window = getActiveWorkbenchWindow();
080: if (window != null) {
081: return window.getShell();
082: }
083: return null;
084: }
085:
086: public static IWorkbenchWindow getActiveWorkbenchWindow() {
087: return getDefault().getWorkbench().getActiveWorkbenchWindow();
088: }
089:
090: public static PDEPlugin getDefault() {
091: return fInstance;
092: }
093:
094: public Hashtable getDefaultNameCounters() {
095: if (fCounters == null)
096: fCounters = new Hashtable();
097: return fCounters;
098: }
099:
100: public static String getPluginId() {
101: return getDefault().getBundle().getSymbolicName();
102: }
103:
104: public static IWorkspace getWorkspace() {
105: return ResourcesPlugin.getWorkspace();
106: }
107:
108: private IWorkbenchPage internalGetActivePage() {
109: return getWorkbench().getActiveWorkbenchWindow()
110: .getActivePage();
111: }
112:
113: public static void log(IStatus status) {
114: ResourcesPlugin.getPlugin().getLog().log(status);
115: }
116:
117: public static void logErrorMessage(String message) {
118: log(new Status(IStatus.ERROR, getPluginId(), IStatus.ERROR,
119: message, null));
120: }
121:
122: public static void logException(Throwable e, final String title,
123: String message) {
124: if (e instanceof InvocationTargetException) {
125: e = ((InvocationTargetException) e).getTargetException();
126: }
127: IStatus status = null;
128: if (e instanceof CoreException)
129: status = ((CoreException) e).getStatus();
130: else {
131: if (message == null)
132: message = e.getMessage();
133: if (message == null)
134: message = e.toString();
135: status = new Status(IStatus.ERROR, getPluginId(),
136: IStatus.OK, message, e);
137: }
138: ResourcesPlugin.getPlugin().getLog().log(status);
139: Display display = SWTUtil.getStandardDisplay();
140: final IStatus fstatus = status;
141: display.asyncExec(new Runnable() {
142: public void run() {
143: ErrorDialog.openError(null, title, null, fstatus);
144: }
145: });
146: }
147:
148: public static void logException(Throwable e) {
149: logException(e, null, null);
150: }
151:
152: public static void log(Throwable e) {
153: if (e instanceof InvocationTargetException)
154: e = ((InvocationTargetException) e).getTargetException();
155: IStatus status = null;
156: if (e instanceof CoreException)
157: status = ((CoreException) e).getStatus();
158: else
159: status = new Status(IStatus.ERROR, getPluginId(),
160: IStatus.OK, e.getMessage(), e);
161: log(status);
162: }
163:
164: public FormColors getFormColors(Display display) {
165: if (fFormColors == null) {
166: fFormColors = new FormColors(display);
167: fFormColors.markShared();
168: }
169: return fFormColors;
170: }
171:
172: /* (non-Javadoc)
173: * @see org.eclipse.core.runtime.Plugin#start(org.osgi.framework.BundleContext)
174: */
175: public void start(BundleContext context) throws Exception {
176: super .start(context);
177: this .fBundleContext = context;
178: fLaunchConfigurationListener = new LaunchConfigurationListener();
179: DebugPlugin.getDefault().getLaunchManager()
180: .addLaunchConfigurationListener(
181: fLaunchConfigurationListener);
182: }
183:
184: public BundleContext getBundleContext() {
185: return fBundleContext;
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
190: */
191: public void stop(BundleContext context) throws Exception {
192: if (fLaunchListener != null)
193: fLaunchListener.shutdown();
194: if (fFormColors != null) {
195: fFormColors.dispose();
196: fFormColors = null;
197: }
198: if (fLabelProvider != null) {
199: fLabelProvider.dispose();
200: fLabelProvider = null;
201: }
202: if (fLaunchConfigurationListener != null) {
203: DebugPlugin.getDefault().getLaunchManager()
204: .removeLaunchConfigurationListener(
205: fLaunchConfigurationListener);
206: fLaunchConfigurationListener = null;
207: }
208: LauncherUtils.shutdown();
209: super .stop(context);
210: }
211:
212: public PDELabelProvider getLabelProvider() {
213: if (fLabelProvider == null)
214: fLabelProvider = new PDELabelProvider();
215: return fLabelProvider;
216: }
217:
218: public LaunchListener getLaunchListener() {
219: if (fLaunchListener == null)
220: fLaunchListener = new LaunchListener();
221: return fLaunchListener;
222: }
223:
224: public OSGiFrameworkManager getOSGiFrameworkManager() {
225: if (fOSGiFrameworkManager == null)
226: fOSGiFrameworkManager = new OSGiFrameworkManager();
227: return fOSGiFrameworkManager;
228: }
229:
230: public static boolean isFullNameModeEnabled() {
231: IPreferenceStore store = getDefault().getPreferenceStore();
232: return store.getString(IPreferenceConstants.PROP_SHOW_OBJECTS)
233: .equals(IPreferenceConstants.VALUE_USE_NAMES);
234: }
235:
236: /**
237: * Returns the shared text file document provider for this plug-in.
238: *
239: * @return the shared text file document provider
240: * @since 3.2
241: */
242: public synchronized IDocumentProvider getTextFileDocumentProvider() {
243: if (fTextFileDocumentProvider == null) {
244: fTextFileDocumentProvider = new TextFileDocumentProvider() {
245: protected FileInfo createFileInfo(Object element)
246: throws CoreException {
247: FileInfo info = super.createFileInfo(element);
248: if (info != null && info.fTextFileBuffer != null)
249: info.fModel = info.fTextFileBuffer
250: .getAnnotationModel();
251: setUpSynchronization(info);
252: return info;
253: }
254: };
255: }
256: return fTextFileDocumentProvider;
257: }
258: }
|