| package org.eclipse.cdt.make.internal.ui; |
| |
| import java.lang.reflect.InvocationTargetException; |
| import java.util.MissingResourceException; |
| import java.util.ResourceBundle; |
| |
| import org.eclipse.cdt.make.internal.ui.editor.IMakefileDocumentProvider; |
| import org.eclipse.cdt.make.internal.ui.editor.MakefileDocumentProvider; |
| import org.eclipse.cdt.make.internal.ui.editor.WorkingCopyManager; |
| import org.eclipse.cdt.make.ui.IWorkingCopyManager; |
| import org.eclipse.cdt.make.ui.actions.UpdateMakeProjectAction; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IWorkspace; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IPluginDescriptor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.jface.dialogs.ErrorDialog; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.ui.IStartup; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.plugin.AbstractUIPlugin; |
| |
| /** |
| * The main plugin class to be used in the desktop. |
| */ |
| public class MakeUIPlugin extends AbstractUIPlugin implements IStartup { |
| //The shared instance. |
| private static MakeUIPlugin plugin; |
| //Resource bundle. |
| private ResourceBundle resourceBundle; |
| |
| private IWorkingCopyManager fWorkingCopyManager; |
| private IMakefileDocumentProvider fMakefileDocumentProvider; |
| |
| /** |
| * The constructor. |
| */ |
| public MakeUIPlugin(IPluginDescriptor descriptor) { |
| super(descriptor); |
| plugin = this; |
| try { |
| resourceBundle = ResourceBundle.getBundle("org.eclipse.cdt.make.internal.ui.MakeResources"); //$NON-NLS-1$ |
| } catch (MissingResourceException x) { |
| resourceBundle = null; |
| } |
| } |
| |
| /** |
| * Returns the shared instance. |
| */ |
| public static MakeUIPlugin getDefault() { |
| return plugin; |
| } |
| |
| /** |
| * Returns the Uniqu idenetifier for this plugin. |
| */ |
| public static String getPluginId() { |
| return getDefault().getDescriptor().getUniqueIdentifier(); |
| } |
| |
| /** |
| * Returns the workspace instance. |
| */ |
| public static IWorkspace getWorkspace() { |
| return ResourcesPlugin.getWorkspace(); |
| } |
| |
| public static Shell getActiveWorkbenchShell() { |
| IWorkbenchWindow window = getActiveWorkbenchWindow(); |
| if (window != null) { |
| return window.getShell(); |
| } |
| return null; |
| } |
| |
| /** |
| * Returns the active workbench window or <code>null</code> if none |
| */ |
| public static IWorkbenchWindow getActiveWorkbenchWindow() { |
| return getDefault().getWorkbench().getActiveWorkbenchWindow(); |
| } |
| |
| /** |
| * Returns the active workbench page or <code>null</code> if none. |
| */ |
| public static IWorkbenchPage getActivePage() { |
| IWorkbenchWindow window= getActiveWorkbenchWindow(); |
| if (window != null) { |
| return window.getActivePage(); |
| } |
| return null; |
| } |
| |
| /** |
| * Returns the string from the plugin's resource bundle, |
| * or 'key' if not found. |
| */ |
| public static String getResourceString(String key) { |
| ResourceBundle bundle = MakeUIPlugin.getDefault().getResourceBundle(); |
| try { |
| return bundle.getString(key); |
| } catch (MissingResourceException e) { |
| return key; |
| } |
| } |
| |
| /** |
| * Returns the plugin's resource bundle, |
| */ |
| public ResourceBundle getResourceBundle() { |
| return resourceBundle; |
| } |
| |
| /** |
| * Convenience method which returns the unique identifier of this plugin. |
| */ |
| public static String getUniqueIdentifier() { |
| if (getDefault() == null) { |
| // If the default instance is not yet initialized, |
| // return a static identifier. This identifier must |
| // match the plugin id defined in plugin.xml |
| return "org.eclipse.cdt.make.ui"; //$NON-NLS-1$ |
| } |
| return getDefault().getDescriptor().getUniqueIdentifier(); |
| } |
| |
| public static void log(IStatus status) { |
| ResourcesPlugin.getPlugin().getLog().log(status); |
| } |
| |
| public static void logErrorMessage(String message) { |
| log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, message, null)); |
| } |
| |
| public static void logException(Throwable e, final String title, String message) { |
| if (e instanceof InvocationTargetException) { |
| e = ((InvocationTargetException) e).getTargetException(); |
| } |
| IStatus status = null; |
| if (e instanceof CoreException) |
| status = ((CoreException) e).getStatus(); |
| else { |
| if (message == null) |
| message = e.getMessage(); |
| if (message == null) |
| message = e.toString(); |
| status = new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.OK, message, e); |
| } |
| ResourcesPlugin.getPlugin().getLog().log(status); |
| Display display; |
| display = Display.getCurrent(); |
| if (display == null) |
| display = Display.getDefault(); |
| final IStatus fstatus = status; |
| display.asyncExec(new Runnable() { |
| public void run() { |
| ErrorDialog.openError(null, title, null, fstatus); |
| } |
| }); |
| } |
| |
| public static void logException(Throwable e) { |
| logException(e, null, null); |
| } |
| |
| public static void log(Throwable e) { |
| if (e instanceof InvocationTargetException) |
| e = ((InvocationTargetException) e).getTargetException(); |
| IStatus status = null; |
| if (e instanceof CoreException) |
| status = ((CoreException) e).getStatus(); |
| else |
| status = new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.OK, e.getMessage(), e); |
| log(status); |
| } |
| |
| /** |
| * Utility method with conventions |
| */ |
| public static void errorDialog(Shell shell, String title, String message, IStatus s) { |
| log(s); |
| // if the 'message' resource string and the IStatus' message are the same, |
| // don't show both in the dialog |
| if (s != null && message.equals(s.getMessage())) { |
| message = null; |
| } |
| ErrorDialog.openError(shell, title, message, s); |
| } |
| |
| /** |
| * Utility method with conventions |
| */ |
| public static void errorDialog(Shell shell, String title, String message, Throwable t) { |
| log(t); |
| IStatus status; |
| if (t instanceof CoreException) { |
| status = ((CoreException) t).getStatus(); |
| // if the 'message' resource string and the IStatus' message are the same, |
| // don't show both in the dialog |
| if (status != null && message.equals(status.getMessage())) { |
| message = null; |
| } |
| } else { |
| status = new Status(IStatus.ERROR, MakeUIPlugin.getUniqueIdentifier(), -1, "Internal Error: ", t); //$NON-NLS-1$ |
| } |
| ErrorDialog.openError(shell, title, message, status); |
| } |
| |
| public void earlyStartup() { |
| final IProject[] oldProject = UpdateMakeProjectAction.getOldProjects(); |
| if (oldProject.length > 0) { |
| Display.getDefault().asyncExec(new Runnable() { |
| public void run() { |
| if (MessageDialog |
| .openQuestion( |
| getShell(), |
| "Update make projects", |
| "Older 'make' projects have been detected in your workspace. \n" |
| + "These projects are no longer supported, " |
| + "would you like to convert these now?") |
| == true) { |
| ProgressMonitorDialog pd = new ProgressMonitorDialog(getShell()); |
| UpdateMakeProjectAction.run(false, pd, oldProject); |
| } |
| } |
| }); |
| } |
| return; |
| |
| } |
| |
| protected Shell getShell() { |
| if (getActiveWorkbenchShell() != null) { |
| return getActiveWorkbenchShell(); |
| } else { |
| IWorkbenchWindow[] windows = getDefault().getWorkbench().getWorkbenchWindows(); |
| return windows[0].getShell(); |
| } |
| } |
| |
| public synchronized IMakefileDocumentProvider getMakefileDocumentProvider() { |
| if (fMakefileDocumentProvider == null) { |
| fMakefileDocumentProvider= new MakefileDocumentProvider(); |
| } |
| return fMakefileDocumentProvider; |
| } |
| |
| public synchronized IWorkingCopyManager getWorkingCopyManager() { |
| if (fWorkingCopyManager == null) { |
| IMakefileDocumentProvider provider= getMakefileDocumentProvider(); |
| fWorkingCopyManager= new WorkingCopyManager(provider); |
| } |
| return fWorkingCopyManager; |
| } |
| |
| public void shutdown() throws CoreException { |
| super.shutdown(); |
| if (fWorkingCopyManager != null) { |
| fWorkingCopyManager.shutdown(); |
| fWorkingCopyManager= null; |
| } |
| } |
| |
| } |