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.ui.console;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.jface.dialogs.ErrorDialog;
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.swt.widgets.Display;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.internal.console.ConsoleManager;
021: import org.eclipse.ui.internal.console.ConsolePluginImages;
022: import org.eclipse.ui.plugin.AbstractUIPlugin;
023: import org.osgi.framework.BundleContext;
024:
025: /**
026: * The console plug-in class.
027: *
028: * @since 3.0
029: */
030:
031: public class ConsolePlugin extends AbstractUIPlugin {
032:
033: /**
034: * Singleton console manager
035: */
036: private IConsoleManager fConsoleManager = null;
037:
038: /**
039: * The singleton console plug-in instance
040: */
041: private static ConsolePlugin fgPlugin = null;
042:
043: /**
044: * Unique identifier constant (value <code>"org.eclipse.ui.console"</code>)
045: * for the UI Console plug-in.
046: */
047: private static final String PI_UI_CONSOLE = "org.eclipse.ui.console"; //$NON-NLS-1$
048:
049: /**
050: * Returns the singleton instance of the console plug-in.
051: */
052: public static ConsolePlugin getDefault() {
053: return fgPlugin;
054: }
055:
056: public ConsolePlugin() {
057: super ();
058: fgPlugin = this ;
059: }
060:
061: /**
062: * Convenience method which returns the unique identifier of this plug-in.
063: */
064: public static String getUniqueIdentifier() {
065: return PI_UI_CONSOLE;
066: }
067:
068: /**
069: * Logs the specified status with this plug-in's log.
070: *
071: * @param status status to log
072: */
073: public static void log(IStatus status) {
074: getDefault().getLog().log(status);
075: }
076:
077: /**
078: * Logs the specified throwable with this plug-in's log.
079: *
080: * @param t throwable to log
081: */
082: public static void log(Throwable t) {
083: if (t instanceof CoreException) {
084: log(((CoreException) t).getStatus());
085: } else {
086: log(newErrorStatus("Error logged from Console plug-in: ", t)); //$NON-NLS-1$
087: }
088: }
089:
090: /**
091: * Returns a new error status for this plug-in with the given message
092: * @param message the message to be included in the status
093: * @param exception the exception to be included in the status or <code>null</code> if none
094: * @return a new error status
095: */
096: public static IStatus newErrorStatus(String message,
097: Throwable exception) {
098: return new Status(IStatus.ERROR, getUniqueIdentifier(),
099: IConsoleConstants.INTERNAL_ERROR, message, exception);
100: }
101:
102: /**
103: * Returns the console manager. The manager will be created lazily on
104: * the first access.
105: *
106: * @return IConsoleManager
107: */
108: public IConsoleManager getConsoleManager() {
109: if (fConsoleManager == null) {
110: fConsoleManager = new ConsoleManager();
111: }
112: return fConsoleManager;
113: }
114:
115: /**
116: * Returns the standard display to be used. The method first checks, if
117: * the thread calling this method has an associated display. If so, this
118: * display is returned. Otherwise the method returns the default display.
119: */
120: public static Display getStandardDisplay() {
121: Display display = Display.getCurrent();
122: if (display == null) {
123: display = Display.getDefault();
124: }
125: return display;
126: }
127:
128: /**
129: * Utility method with conventions
130: */
131: public static void errorDialog(Shell shell, String title,
132: String message, Throwable t) {
133: IStatus status;
134: if (t instanceof CoreException) {
135: status = ((CoreException) t).getStatus();
136: // if the 'message' resource string and the IStatus' message are the same,
137: // don't show both in the dialog
138: if (status != null && message.equals(status.getMessage())) {
139: message = null;
140: }
141: } else {
142: status = new Status(IStatus.ERROR, getUniqueIdentifier(),
143: IConsoleConstants.INTERNAL_ERROR,
144: "Error within Debug UI: ", t); //$NON-NLS-1$
145: log(status);
146: }
147: ErrorDialog.openError(shell, title, message, status);
148: }
149:
150: /**
151: * Returns the <code>Image</code> identified by the given key,
152: * or <code>null</code> if it does not exist.
153: *
154: * @return the <code>Image</code> identified by the given key,
155: * or <code>null</code> if it does not exist
156: * @since 3.1
157: */
158: public static Image getImage(String key) {
159: return ConsolePluginImages.getImage(key);
160: }
161:
162: /**
163: * Returns the <code>ImageDescriptor</code> identified by the given key,
164: * or <code>null</code> if it does not exist.
165: *
166: * @return the <code>ImageDescriptor</code> identified by the given key,
167: * or <code>null</code> if it does not exist
168: * @since 3.1
169: */
170: public static ImageDescriptor getImageDescriptor(String key) {
171: return ConsolePluginImages.getImageDescriptor(key);
172: }
173:
174: /* (non-Javadoc)
175: * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
176: */
177: public void stop(BundleContext context) throws Exception {
178: if (fConsoleManager != null) {
179: IConsole[] consoles = fConsoleManager.getConsoles();
180: if (consoles != null) {
181: fConsoleManager.removeConsoles(consoles);
182: }
183: }
184: super.stop(context);
185: }
186:
187: }
|