KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > BrowserCommonActivator


1 package org.apache.directory.ldapstudio.browser.common;
2
3
4 import java.io.IOException JavaDoc;
5 import java.net.URL JavaDoc;
6
7 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
8 import org.apache.directory.ldapstudio.browser.core.events.EventRunner;
9 import org.eclipse.core.runtime.FileLocator;
10 import org.eclipse.core.runtime.IConfigurationElement;
11 import org.eclipse.core.runtime.IExtension;
12 import org.eclipse.core.runtime.IExtensionPoint;
13 import org.eclipse.core.runtime.Path;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.resource.ColorRegistry;
16 import org.eclipse.jface.resource.FontRegistry;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.text.templates.ContextTypeRegistry;
19 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
20 import org.eclipse.jface.text.templates.persistence.TemplateStore;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.RGB;
26 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
27 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
28 import org.eclipse.ui.plugin.AbstractUIPlugin;
29 import org.osgi.framework.BundleContext;
30
31
32 /**
33  * The activator class controls the plug-in life cycle
34  */

35 public class BrowserCommonActivator extends AbstractUIPlugin
36 {
37
38     /** The plug-in ID */
39     public static final String JavaDoc PLUGIN_ID = "org.apache.directory.ldapstudio.browser.common";
40
41     /** The shared instance */
42     private static BrowserCommonActivator plugin;
43
44     /** The font registry */
45     private FontRegistry fontRegistry;
46
47     /** The color registry */
48     private ColorRegistry colorRegistry;
49
50     /** The event dispatcher */
51     private ExceptionHandler exceptionHandler;
52
53     /** The value editor preferences */
54     private ValueEditorsPreferences valueEditorPreferences;
55
56     /** The filter template store. */
57     private ContributionTemplateStore filterTemplateStore;
58
59     /** The filter template context type registry. */
60     private ContributionContextTypeRegistry filterTemplateContextTypeRegistry;
61
62     /** The event runner. */
63     private EventRunner eventRunner;
64
65
66     /**
67      * The constructor
68      */

69     public BrowserCommonActivator()
70     {
71         plugin = this;
72     }
73
74
75     /**
76      * {@inheritDoc}
77      */

78     public void start( BundleContext context ) throws Exception JavaDoc
79     {
80         super.start( context );
81
82         if ( eventRunner == null )
83         {
84             eventRunner = new UiThreadEventRunner();
85         }
86
87         if ( fontRegistry == null )
88         {
89             fontRegistry = new FontRegistry( getWorkbench().getDisplay() );
90         }
91
92         if ( colorRegistry == null )
93         {
94             colorRegistry = new ColorRegistry( getWorkbench().getDisplay() );
95         }
96
97         if ( exceptionHandler == null )
98         {
99             exceptionHandler = new ExceptionHandler();
100         }
101
102         valueEditorPreferences = new ValueEditorsPreferences();
103
104         if ( filterTemplateContextTypeRegistry == null )
105         {
106             filterTemplateContextTypeRegistry = new ContributionContextTypeRegistry();
107             filterTemplateContextTypeRegistry.addContextType( BrowserCommonConstants.FILTER_TEMPLATE_ID );
108             filterTemplateContextTypeRegistry.getContextType( BrowserCommonConstants.FILTER_TEMPLATE_ID ).addResolver(
109                 new GlobalTemplateVariables.Cursor() );
110         }
111
112         if ( filterTemplateStore == null )
113         {
114             filterTemplateStore = new ContributionTemplateStore( getFilterTemplateContextTypeRegistry(),
115                 getPreferenceStore(), "templates" );
116             try
117             {
118                 filterTemplateStore.load();
119             }
120             catch ( IOException JavaDoc e )
121             {
122                 e.printStackTrace();
123             }
124         }
125
126         BrowserCorePlugin.getDefault().setAuthHandler( new BrowserCommonAuthHandler() );
127         BrowserCorePlugin.getDefault().setReferralHandler( new BrowserCommonReferralHandler() );
128     }
129
130
131     /**
132      * {@inheritDoc}
133      */

134     public void stop( BundleContext context ) throws Exception JavaDoc
135     {
136         plugin = null;
137         super.stop( context );
138
139         if ( eventRunner != null )
140         {
141             eventRunner = null;
142         }
143
144         if ( fontRegistry != null )
145         {
146             fontRegistry = null;
147         }
148
149         if ( colorRegistry != null )
150         {
151             colorRegistry = null;
152         }
153
154         if ( exceptionHandler != null )
155         {
156             exceptionHandler = null;
157         }
158
159         if ( filterTemplateContextTypeRegistry != null )
160         {
161             filterTemplateContextTypeRegistry = null;
162         }
163
164         if ( filterTemplateStore != null )
165         {
166             try
167             {
168                 filterTemplateStore.save();
169             }
170             catch ( IOException JavaDoc e )
171             {
172                 e.printStackTrace();
173             }
174             filterTemplateStore = null;
175         }
176     }
177
178
179     /**
180      * Returns the shared instance
181      *
182      * @return the shared instance
183      */

184     public static BrowserCommonActivator getDefault()
185     {
186         return plugin;
187     }
188
189
190     /**
191      * Use this method to get SWT images. Use the IMG_ constants from
192      * BrowserWidgetsConstants for the key.
193      *
194      * @param key
195      * The key (relative path to the image im filesystem)
196      * @return The image discriptor or null
197      */

198     public ImageDescriptor getImageDescriptor( String JavaDoc key )
199     {
200         if ( key != null )
201         {
202             URL JavaDoc url = FileLocator.find( getBundle(), new Path( key ), null );
203             if ( url != null )
204                 return ImageDescriptor.createFromURL( url );
205             else
206                 return null;
207         }
208         else
209         {
210             return null;
211         }
212     }
213
214
215     /**
216      * Use this method to get SWT images. Use the IMG_ constants from
217      * BrowserWidgetsConstants for the key. A ImageRegistry is used to manage the
218      * the key->Image mapping.
219      * <p>
220      * Note: Don't dispose the returned SWT Image. It is disposed
221      * automatically when the plugin is stopped.
222      *
223      * @param key
224      * The key (relative path to the image im filesystem)
225      * @return The SWT Image or null
226      * @see BrowserCommonConstants
227      */

228     public Image getImage( String JavaDoc key )
229     {
230         Image image = getImageRegistry().get( key );
231         if ( image == null )
232         {
233             ImageDescriptor id = getImageDescriptor( key );
234             if ( id != null )
235             {
236                 image = id.createImage();
237                 getImageRegistry().put( key, image );
238             }
239         }
240         return image;
241     }
242
243
244     /**
245      * Use this method to get SWT fonts. A FontRegistry is used to manage
246      * the FontData[]->Font mapping.
247      * <p>
248      * Note: Don't dispose the returned SWT Font. It is disposed
249      * automatically when the plugin is stopped.
250      *
251      * @param fontData
252      * the font data
253      * @return The SWT Font
254      */

255
256     public Font getFont( FontData[] fontData )
257     {
258         if ( !fontRegistry.hasValueFor( fontData[0].toString() ) )
259         {
260             fontRegistry.put( fontData[0].toString(), fontData );
261         }
262
263         return fontRegistry.get( fontData[0].toString() );
264     }
265
266
267     /**
268      * Use this method to get SWT colors. A ColorRegistry is used to manage
269      * the RGB->Color mapping.
270      * <p>
271      * Note: Don't dispose the returned color. It is disposed automatically
272      * when the plugin is stopped.
273      *
274      * @param rgb
275      * the rgb color data
276      * @return The SWT Color
277      */

278     public Color getColor( RGB rgb )
279     {
280         if ( !colorRegistry.hasValueFor( rgb.toString() ) )
281         {
282             colorRegistry.put( rgb.toString(), rgb );
283         }
284
285         return colorRegistry.get( rgb.toString() );
286     }
287
288
289     /**
290      *
291      * @return The exception handler
292      */

293     public ExceptionHandler getExceptionHandler()
294     {
295         return exceptionHandler;
296     }
297
298
299     /**
300      * Gets the value editors preferences.
301      *
302      * @return the value editors preferences
303      */

304     public ValueEditorsPreferences getValueEditorsPreferences()
305     {
306         return valueEditorPreferences;
307     }
308
309
310     /**
311      *
312      * @return The filter template store
313      */

314     public TemplateStore getFilterTemplateStore()
315     {
316         return filterTemplateStore;
317     }
318
319
320     /**
321      *
322      * @return The filter template context type registry
323      */

324     public ContextTypeRegistry getFilterTemplateContextTypeRegistry()
325     {
326         return filterTemplateContextTypeRegistry;
327     }
328
329
330     /**
331      * Checks, if this plugins runs in the Eclipse IDE or in RCP environment.
332      * This is done by looking for the Resource perspective extensions.
333      *
334      * @return true if this plugin runs in IDE environment
335      */

336     public static boolean isIDEEnvironment()
337     {
338         IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(
339             "org.eclipse.ui.perspectives" );
340         if ( extensionPoint != null )
341         {
342             IExtension[] extensions = extensionPoint.getExtensions();
343             if ( extensions != null )
344             {
345                 for ( int i = 0; i < extensions.length; i++ )
346                 {
347                     IExtension extension = extensions[i];
348                     IConfigurationElement[] elements = extension.getConfigurationElements();
349                     for ( int j = 0; j < elements.length; j++ )
350                     {
351                         IConfigurationElement element = elements[j];
352                         if ( element.getName().equals( "perspective" ) )
353                         {
354                             if ( "org.eclipse.ui.resourcePerspective".equals( element.getAttribute( "id" ) ) )
355                             {
356                                 return true;
357                             }
358                         }
359                     }
360                 }
361             }
362         }
363
364         return false;
365     }
366
367
368     /**
369      * Gets the event runner.
370      *
371      * @return the event runner
372      */

373     public EventRunner getEventRunner()
374     {
375         return eventRunner;
376     }
377
378 }
379
Popular Tags