KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > ConsolePlugin


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
12 package org.eclipse.ui.console;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.ErrorDialog;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.internal.console.ConsoleManager;
23 import org.eclipse.ui.internal.console.ConsolePluginImages;
24 import org.eclipse.ui.plugin.AbstractUIPlugin;
25 import org.osgi.framework.BundleContext;
26
27 /**
28  * The console plug-in class.
29  *
30  * @since 3.0
31  */

32
33 public class ConsolePlugin extends AbstractUIPlugin {
34     
35     /**
36      * Singleton console manager
37      */

38     private IConsoleManager fConsoleManager = null;
39
40     /**
41      * The singleton console plug-in instance
42      */

43     private static ConsolePlugin fgPlugin= null;
44     
45     /**
46      * Unique identifier constant (value <code>"org.eclipse.ui.console"</code>)
47      * for the UI Console plug-in.
48      */

49     private static final String JavaDoc PI_UI_CONSOLE = "org.eclipse.ui.console"; //$NON-NLS-1$
50

51     /**
52      * Returns the singleton instance of the console plug-in.
53      */

54     public static ConsolePlugin getDefault() {
55         return fgPlugin;
56     }
57
58     public ConsolePlugin() {
59         super();
60         fgPlugin = this;
61     }
62     
63     /**
64      * Convenience method which returns the unique identifier of this plug-in.
65      */

66     public static String JavaDoc getUniqueIdentifier() {
67         return PI_UI_CONSOLE;
68     }
69
70     /**
71      * Logs the specified status with this plug-in's log.
72      *
73      * @param status status to log
74      */

75     public static void log(IStatus status) {
76         getDefault().getLog().log(status);
77     }
78
79     /**
80      * Logs the specified throwable with this plug-in's log.
81      *
82      * @param t throwable to log
83      */

84     public static void log(Throwable JavaDoc t) {
85         if (t instanceof CoreException) {
86             log(((CoreException)t).getStatus());
87         } else {
88             log(newErrorStatus("Error logged from Console plug-in: ", t)); //$NON-NLS-1$
89
}
90     }
91     
92     /**
93      * Returns a new error status for this plug-in with the given message
94      * @param message the message to be included in the status
95      * @param exception the exception to be included in the status or <code>null</code> if none
96      * @return a new error status
97      */

98     public static IStatus newErrorStatus(String JavaDoc message, Throwable JavaDoc exception) {
99         return new Status(IStatus.ERROR, getUniqueIdentifier(), 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 JavaDoc title, String JavaDoc message, Throwable JavaDoc t) {
132         IStatus status;
133         if (t instanceof CoreException) {
134             status= ((CoreException)t).getStatus();
135             // if the 'message' resource string and the IStatus' message are the same,
136
// don't show both in the dialog
137
if (status != null && message.equals(status.getMessage())) {
138                 message= null;
139             }
140         } else {
141             status= new Status(IStatus.ERROR, getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, "Error within Debug UI: ", t); //$NON-NLS-1$
142
log(status);
143         }
144         ErrorDialog.openError(shell, title, message, status);
145     }
146     
147     /**
148      * Returns the <code>Image</code> identified by the given key,
149      * or <code>null</code> if it does not exist.
150      *
151      * @return the <code>Image</code> identified by the given key,
152      * or <code>null</code> if it does not exist
153      * @since 3.1
154      */

155     public static Image getImage(String JavaDoc key) {
156         return ConsolePluginImages.getImage(key);
157     }
158     
159     /**
160      * Returns the <code>ImageDescriptor</code> identified by the given key,
161      * or <code>null</code> if it does not exist.
162      *
163      * @return the <code>ImageDescriptor</code> identified by the given key,
164      * or <code>null</code> if it does not exist
165      * @since 3.1
166      */

167     public static ImageDescriptor getImageDescriptor(String JavaDoc key) {
168         return ConsolePluginImages.getImageDescriptor(key);
169     }
170     
171     /* (non-Javadoc)
172      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
173      */

174     public void stop(BundleContext context) throws Exception JavaDoc {
175         if (fConsoleManager != null) {
176             IConsole[] consoles = fConsoleManager.getConsoles();
177             if (consoles != null) {
178                 fConsoleManager.removeConsoles(consoles);
179             }
180         }
181         super.stop(context);
182     }
183     
184     
185 }
186
Popular Tags