KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > AntUIPlugin


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  * John-Mason P. Shackelford (john-mason.shackelford@pearson.com) - bug 53547
11  *******************************************************************************/

12 package org.eclipse.ant.internal.ui;
13
14
15 import java.util.Locale JavaDoc;
16
17 import org.eclipse.ant.internal.ui.editor.DecayCodeCompletionDataStructuresThread;
18 import org.eclipse.ant.internal.ui.editor.text.AntEditorDocumentProvider;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferenceConverter;
23 import org.eclipse.jface.resource.ImageRegistry;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.editors.text.EditorsUI;
29 import org.eclipse.ui.plugin.AbstractUIPlugin;
30 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
31 import org.eclipse.ui.texteditor.IDocumentProvider;
32 import org.osgi.framework.BundleContext;
33
34 /**
35  * The plug-in runtime class for the Ant UI plug-in.
36  */

37 public class AntUIPlugin extends AbstractUIPlugin {
38
39     /**
40      * Status code indicating an unexpected internal error.
41      * @since 2.1
42      */

43     public static final int INTERNAL_ERROR = 120;
44     
45     /**
46      * The single instance of this plug-in runtime class.
47      */

48     private static AntUIPlugin plugin;
49
50     /**
51      * Unique identifier constant (value <code>"org.eclipse.ant.ui"</code>)
52      * for the Ant UI plug-in.
53      */

54     public static final String JavaDoc PI_ANTUI = "org.eclipse.ant.ui"; //$NON-NLS-1$
55

56     private static final String JavaDoc EMPTY_STRING= ""; //$NON-NLS-1$
57

58     /**
59      * The combined preference store.
60      * @since 3.1
61      */

62     private IPreferenceStore fCombinedPreferenceStore;
63
64     private IDocumentProvider fDocumentProvider;
65
66     /**
67      * Constructs an instance of this plug-in runtime class.
68      * <p>
69      * An instance of this plug-in runtime class is automatically created
70      * when the facilities provided by the Ant Core plug-in are required.
71      * <b>Clients must never explicitly instantiate a plug-in runtime class.</b>
72      * </p>
73      */

74     public AntUIPlugin() {
75         super();
76         plugin = this;
77     }
78
79     /* (non-Javadoc)
80      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
81      */

82     public void stop(BundleContext context) throws Exception JavaDoc {
83         try {
84             AntUIImages.disposeImageDescriptorRegistry();
85             DecayCodeCompletionDataStructuresThread.cancel();
86         } finally {
87             super.stop(context);
88         }
89     }
90
91     /**
92      * Returns this plug-in instance.
93      *
94      * @return the single instance of this plug-in runtime class
95      */

96     public static AntUIPlugin getDefault() {
97         return plugin;
98     }
99     
100     /**
101      * Convenience method which returns the unique identifier of this plugin.
102      */

103     public static String JavaDoc getUniqueIdentifier() {
104         return PI_ANTUI;
105     }
106     
107     /**
108      * Logs the specified throwable with this plug-in's log.
109      *
110      * @param t throwable to log
111      */

112     public static void log(Throwable JavaDoc t) {
113         IStatus status= new Status(IStatus.ERROR, PI_ANTUI, INTERNAL_ERROR, "Error logged from Ant UI: ", t); //$NON-NLS-1$
114
log(status);
115     }
116     
117     /**
118      * Logs the specified status with this plug-in's log.
119      *
120      * @param status status
121      */

122     public static void log(IStatus status) {
123         getDefault().getLog().log(status);
124     }
125     
126     /**
127      * Writes the message to the plug-in's log
128      *
129      * @param message the text to write to the log
130      */

131     public static void log(String JavaDoc message, Throwable JavaDoc exception) {
132         IStatus status = newErrorStatus(message, exception);
133         log(status);
134     }
135     
136     /**
137      * Returns a new <code>IStatus</code> for this plug-in
138      */

139     public static IStatus newErrorStatus(String JavaDoc message, Throwable JavaDoc exception) {
140         if (message == null) {
141             message= EMPTY_STRING;
142         }
143         return new Status(IStatus.ERROR, IAntUIConstants.PLUGIN_ID, 0, message, exception);
144     }
145         
146     /**
147      * Returns the standard display to be used. The method first checks, if
148      * the thread calling this method has an associated display. If so, this
149      * display is returned. Otherwise the method returns the default display.
150      */

151     public static Display getStandardDisplay() {
152         Display display = Display.getCurrent();
153         if (display == null) {
154             display = Display.getDefault();
155         }
156         return display;
157     }
158     
159     /* (non-Javadoc)
160      * @see org.eclipse.ui.plugin.AbstractUIPlugin#createImageRegistry()
161      */

162     protected ImageRegistry createImageRegistry() {
163         return AntUIImages.initializeImageRegistry();
164     }
165     
166     /**
167      * Returns the preference color, identified by the given preference.
168      */

169     public static Color getPreferenceColor(String JavaDoc pref) {
170         return ColorManager.getDefault().getColor(PreferenceConverter.getColor(getDefault().getCombinedPreferenceStore(), pref));
171     }
172     
173     /**
174     * Returns the active workbench page or <code>null</code> if none.
175     */

176    public static IWorkbenchPage getActivePage() {
177        IWorkbenchWindow window= getActiveWorkbenchWindow();
178        if (window != null) {
179            return window.getActivePage();
180        }
181        return null;
182    }
183
184    /**
185     * Returns the active workbench window or <code>null</code> if none
186     */

187    public static IWorkbenchWindow getActiveWorkbenchWindow() {
188        return getDefault().getWorkbench().getActiveWorkbenchWindow();
189    }
190    
191    /**
192     * Returns whether the current OS claims to be Mac
193     */

194    public static boolean isMacOS() {
195         String JavaDoc osname= System.getProperty("os.name").toLowerCase(Locale.US); //$NON-NLS-1$
196
return osname.indexOf("mac") != -1; //$NON-NLS-1$
197
}
198    
199    /**
200      * Returns a combined preference store, this store is read-only.
201      *
202      * @return the combined preference store
203      *
204      * @since 3.1
205      */

206     public IPreferenceStore getCombinedPreferenceStore() {
207         if (fCombinedPreferenceStore == null) {
208             IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore();
209             fCombinedPreferenceStore= new ChainedPreferenceStore(new IPreferenceStore[] { getPreferenceStore(), generalTextStore });
210         }
211         return fCombinedPreferenceStore;
212     }
213     
214    /**
215      * Returns the document provider for use in the Ant editor.
216      *
217      * @return the Ant editor document provider
218      *
219      * @since 3.1
220      */

221     public synchronized IDocumentProvider getDocumentProvider() {
222         if (fDocumentProvider == null)
223             fDocumentProvider= new AntEditorDocumentProvider();
224         return fDocumentProvider;
225     }
226 }
227
Popular Tags