KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > EditorsPlugin


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 package org.eclipse.ui.internal.editors.text;
12
13 import org.osgi.framework.BundleContext;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.MultiStatus;
18 import org.eclipse.core.runtime.Status;
19
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22
23 import org.eclipse.jface.text.source.ISharedTextColors;
24
25 import org.eclipse.ui.editors.text.EditorsUI;
26
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.internal.texteditor.AnnotationTypeHierarchy;
29 import org.eclipse.ui.plugin.AbstractUIPlugin;
30 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
31 import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
32 import org.eclipse.ui.texteditor.AnnotationTypeLookup;
33 import org.eclipse.ui.texteditor.HyperlinkDetectorRegistry;
34 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
35 import org.eclipse.ui.texteditor.spelling.SpellingService;
36 import org.eclipse.ui.themes.IThemeManager;
37
38 /**
39  * Represents the editors plug-in. It provides a series of convenience methods such as
40  * access to the shared text colors and the log.
41  *
42  * @since 2.1
43  */

44 public class EditorsPlugin extends AbstractUIPlugin {
45     private static EditorsPlugin fgInstance;
46
47     public static EditorsPlugin getDefault() {
48         return fgInstance;
49     }
50
51     public static void log(IStatus status) {
52         getDefault().getLog().log(status);
53     }
54
55     public static void logErrorMessage(String JavaDoc message) {
56         if (message == null)
57             message= ""; //$NON-NLS-1$
58
log(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IEditorsStatusConstants.INTERNAL_ERROR, message, null));
59     }
60
61     public static void logErrorStatus(String JavaDoc message, IStatus status) {
62         if (status == null) {
63             logErrorMessage(message);
64             return;
65         }
66         MultiStatus multi= new MultiStatus(EditorsUI.PLUGIN_ID, IEditorsStatusConstants.INTERNAL_ERROR, message, null);
67         multi.add(status);
68         log(multi);
69     }
70
71     public static void log(Throwable JavaDoc e) {
72         log(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IEditorsStatusConstants.INTERNAL_ERROR, TextEditorMessages.EditorsPlugin_internal_error, e));
73     }
74
75
76     private ISharedTextColors fSharedTextColors;
77     private AnnotationTypeLookup fAnnotationTypeLookup;
78     private AnnotationPreferenceLookup fAnnotationPreferenceLookup;
79     private AnnotationTypeHierarchy fAnnotationTypeHierarchy;
80     private MarkerAnnotationPreferences fMarkerAnnotationPreferences;
81     /**
82      * Theme listener.
83      * @since 3.3
84      */

85     private IPropertyChangeListener fThemeListener;
86
87     /**
88      * Spelling service.
89      * @since 3.1
90      */

91     private SpellingService fSpellingService;
92     
93     /**
94      * The hyperlink detector registry.
95      * @since 3.3
96      */

97     private HyperlinkDetectorRegistry fHyperlinkDetectorRegistry;
98
99     public EditorsPlugin() {
100         Assert.isTrue(fgInstance == null);
101         fgInstance= this;
102     }
103
104     /**
105      * Returns the shared text colors of this plug-in.
106      *
107      * @return the shared text colors
108      * @since 3.0
109      */

110     public ISharedTextColors getSharedTextColors() {
111         if (fSharedTextColors == null)
112             fSharedTextColors= new SharedTextColors();
113         return fSharedTextColors;
114     }
115
116     /**
117      * Returns the annotation type lookup of this plug-in.
118      *
119      * @return the annotation type lookup
120      * @since 3.0
121      */

122     public AnnotationTypeLookup getAnnotationTypeLookup() {
123         if (fAnnotationTypeLookup == null)
124             fAnnotationTypeLookup= new AnnotationTypeLookup();
125         return fAnnotationTypeLookup;
126     }
127
128     /**
129      * Returns the annotation preference lookup of this plug-in.
130      *
131      * @return the annotation preference lookup
132      * @since 3.0
133      */

134     public AnnotationPreferenceLookup getAnnotationPreferenceLookup() {
135         if (fAnnotationPreferenceLookup == null)
136             fAnnotationPreferenceLookup= new AnnotationPreferenceLookup();
137         return fAnnotationPreferenceLookup;
138     }
139
140     /**
141      * Returns the annotation type hierarchy for this plug-in.
142      *
143      * @return the annotation type hierarchy
144      * @since 3.0
145      */

146     public AnnotationTypeHierarchy getAnnotationTypeHierarchy() {
147         if (fAnnotationTypeHierarchy == null)
148             fAnnotationTypeHierarchy= new AnnotationTypeHierarchy();
149         return fAnnotationTypeHierarchy;
150     }
151     
152     /**
153      * Sets the marker annotation preferences.
154      * <p>
155      * Note: This method must only be called once.
156      * </p>
157      *
158      * @param markerAnnotationPreferences the marker annotation preferences
159      * @since 3.1
160      */

161     public synchronized void setMarkerAnnotationPreferences(MarkerAnnotationPreferences markerAnnotationPreferences) {
162         Assert.isTrue(fMarkerAnnotationPreferences == null);
163         fMarkerAnnotationPreferences= markerAnnotationPreferences;
164     }
165     
166     /**
167      * Tells whether the marker annotation preferences are initialized.
168      *
169      * @return <code>true</code> if initialized
170      * @since 3.2
171      */

172     public boolean isMarkerAnnotationPreferencesInitialized() {
173         return fMarkerAnnotationPreferences != null;
174     }
175
176     /**
177      * Returns the marker annotation preferences.
178      *
179      * @return the marker annotation preferences
180      * @since 3.1
181      */

182     public synchronized MarkerAnnotationPreferences getMarkerAnnotationPreferences() {
183         if (!isMarkerAnnotationPreferencesInitialized())
184             new MarkerAnnotationPreferences().getAnnotationPreferences(); // force creation of shared preferences
185
return fMarkerAnnotationPreferences;
186     }
187     
188     /*
189      * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
190      * @since 3.3
191      */

192     public void start(BundleContext context) throws Exception JavaDoc {
193         super.start(context);
194         
195         if (PlatformUI.isWorkbenchRunning()) {
196             fThemeListener= new IPropertyChangeListener() {
197                 public void propertyChange(PropertyChangeEvent event) {
198                     if (IThemeManager.CHANGE_CURRENT_THEME.equals(event.getProperty()))
199                         AbstractDecoratedTextEditorPreferenceConstants.initializeDefaultValues(getPreferenceStore());
200                 }
201             };
202             PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(fThemeListener);
203         }
204     }
205
206     /*
207      * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
208      * @since 3.0
209      */

210     public void stop(BundleContext context) throws Exception JavaDoc {
211         if (fSharedTextColors != null) {
212             fSharedTextColors.dispose();
213             fSharedTextColors= null;
214         }
215
216         if (fThemeListener != null) {
217             if (PlatformUI.isWorkbenchRunning())
218                 PlatformUI.getWorkbench().getThemeManager().removePropertyChangeListener(fThemeListener);
219             fThemeListener= null;
220         }
221         
222         fAnnotationTypeLookup= null;
223         fAnnotationPreferenceLookup= null;
224         fAnnotationTypeHierarchy= null;
225         fMarkerAnnotationPreferences= null;
226         fHyperlinkDetectorRegistry= null;
227
228         super.stop(context);
229     }
230
231     /**
232      * Returns the spelling service.
233      *
234      * @return the spelling service
235      * @since 3.1
236      */

237     public SpellingService getSpellingService() {
238         if (fSpellingService == null)
239             fSpellingService= new SpellingService(getPreferenceStore());
240         return fSpellingService;
241     }
242
243     /**
244      * Returns the registry that contains the hyperlink detectors contributed
245      * by the <code>org.eclipse.ui.workbench.texteditor.hyperlinkDetectors</code>
246      * extension point.
247      *
248      * @return the hyperlink detector registry
249      * @since 3.3
250      */

251     public synchronized HyperlinkDetectorRegistry getHyperlinkDetectorRegistry() {
252         if (fHyperlinkDetectorRegistry == null)
253             fHyperlinkDetectorRegistry= new HyperlinkDetectorRegistry(getPreferenceStore());
254         return fHyperlinkDetectorRegistry;
255     }
256 }
257
Popular Tags