KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > model > ExternalToolsPlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.externaltools.internal.model;
12
13
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.resource.ImageRegistry;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29
30 /**
31  * External tools plug-in class
32  */

33 public final class ExternalToolsPlugin extends AbstractUIPlugin {
34     /**
35      * Status representing no problems encountered during operation.
36      */

37     public static final IStatus OK_STATUS = new Status(IStatus.OK, IExternalToolConstants.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
38

39     private static ExternalToolsPlugin plugin;
40     
41     private static final String JavaDoc EMPTY_STRING= ""; //$NON-NLS-1$
42

43     /**
44      * Create an instance of the External Tools plug-in.
45      */

46     public ExternalToolsPlugin() {
47         super();
48         plugin = this;
49     }
50
51     /**
52      * Returns the default instance of the receiver.
53      * This represents the runtime plugin.
54      */

55     public static ExternalToolsPlugin getDefault() {
56         return plugin;
57     }
58
59     /**
60      * Returns a new <code>IStatus</code> for this plug-in
61      */

62     public static IStatus newErrorStatus(String JavaDoc message, Throwable JavaDoc exception) {
63         if (message == null) {
64             message= EMPTY_STRING;
65         }
66         return new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception);
67     }
68
69     /**
70      * Returns a new <code>CoreException</code> for this plug-in
71      */

72     public static CoreException newError(String JavaDoc message, Throwable JavaDoc exception) {
73         return new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, 0, message, exception));
74     }
75
76     /**
77      * Writes the message to the plug-in's log
78      *
79      * @param message the text to write to the log
80      */

81     public void log(String JavaDoc message, Throwable JavaDoc exception) {
82         IStatus status = newErrorStatus(message, exception);
83         getLog().log(status);
84     }
85     
86     public void log(Throwable JavaDoc exception) {
87         //this message is intentionally not internationalized, as an exception may
88
// be due to the resource bundle itself
89
getLog().log(newErrorStatus("Internal error logged from External Tools UI: ", exception)); //$NON-NLS-1$
90
}
91
92     /**
93      * Returns the ImageDescriptor for the icon with the given path
94      *
95      * @return the ImageDescriptor object
96      */

97     public ImageDescriptor getImageDescriptor(String JavaDoc path) {
98         try {
99             Bundle bundle= getDefault().getBundle();
100             URL JavaDoc installURL = bundle.getEntry("/"); //$NON-NLS-1$
101
URL JavaDoc url = new URL JavaDoc(installURL, path);
102             return ImageDescriptor.createFromURL(url);
103         } catch (MalformedURLException JavaDoc e) {
104             return null;
105         }
106     }
107
108     /**
109      * Returns the active workbench window or <code>null</code> if none
110      */

111     public static IWorkbenchWindow getActiveWorkbenchWindow() {
112         return getDefault().getWorkbench().getActiveWorkbenchWindow();
113     }
114     
115     /**
116      * Returns the active workbench page or <code>null</code> if none.
117      */

118     public static IWorkbenchPage getActivePage() {
119         IWorkbenchWindow window= getActiveWorkbenchWindow();
120         if (window != null) {
121             return window.getActivePage();
122         }
123         return null;
124     }
125     
126     /**
127      * Returns the active workbench shell or <code>null</code> if none.
128      */

129     public static Shell getActiveWorkbenchShell() {
130         IWorkbenchWindow window = getActiveWorkbenchWindow();
131         if (window != null) {
132             return window.getShell();
133         }
134         return null;
135     }
136
137     /**
138      * Returns the standard display to be used. The method first checks, if
139      * the thread calling this method has an associated display. If so, this
140      * display is returned. Otherwise the method returns the default display.
141      */

142     public static Display getStandardDisplay() {
143         Display display = Display.getCurrent();
144         if (display == null) {
145             display = Display.getDefault();
146         }
147         return display;
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
152      */

153     protected ImageRegistry createImageRegistry() {
154         return ExternalToolsImages.initializeImageRegistry();
155     }
156
157     /* (non-Javadoc)
158      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
159      */

160     public void stop(BundleContext context) throws Exception JavaDoc {
161         try {
162             ExternalToolsImages.disposeImageDescriptorRegistry();
163         } finally {
164             super.stop(context);
165         }
166     }
167 }
168
Popular Tags