KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > LdifEditorActivator


1 package org.apache.directory.ldapstudio.ldifeditor;
2
3
4 import java.io.IOException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.MissingResourceException JavaDoc;
7 import java.util.ResourceBundle JavaDoc;
8
9 import org.eclipse.core.runtime.FileLocator;
10 import org.eclipse.core.runtime.Path;
11 import org.eclipse.jface.resource.ColorRegistry;
12 import org.eclipse.jface.resource.ImageDescriptor;
13 import org.eclipse.jface.text.templates.ContextTypeRegistry;
14 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
15 import org.eclipse.jface.text.templates.persistence.TemplateStore;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
20 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
21 import org.eclipse.ui.plugin.AbstractUIPlugin;
22 import org.osgi.framework.BundleContext;
23
24
25 /**
26  * The activator class controls the plug-in life cycle
27  */

28 public class LdifEditorActivator extends AbstractUIPlugin
29 {
30
31     /** The plugin ID */
32     public static final String JavaDoc PLUGIN_ID = "org.apache.directory.ldapstudio.ldifeditor";
33
34     /** The shared instance */
35     private static LdifEditorActivator plugin;
36
37     /** Resource bundle */
38     private ResourceBundle JavaDoc resourceBundle;
39     
40     /** The color registry */
41     private ColorRegistry colorRegistry;
42
43     /** The template store */
44     private ContributionTemplateStore ldifTemplateStore;
45
46     /** The context type registry */
47     private ContributionContextTypeRegistry ldifTemplateContextTypeRegistry;
48
49
50     /**
51      * The constructor
52      */

53     public LdifEditorActivator()
54     {
55         plugin = this;
56         
57         try
58         {
59             resourceBundle = ResourceBundle.getBundle( "org.apache.directory.ldapstudio.ldifeditor.messages" );
60         }
61         catch ( MissingResourceException JavaDoc x )
62         {
63             resourceBundle = null;
64         }
65     }
66
67
68     /**
69      * {@inheritDoc}
70      */

71     public void start( BundleContext context ) throws Exception JavaDoc
72     {
73         super.start( context );
74
75         if ( colorRegistry == null )
76         {
77             colorRegistry = new ColorRegistry( getWorkbench().getDisplay() );
78         }
79
80         if ( ldifTemplateContextTypeRegistry == null )
81         {
82             ldifTemplateContextTypeRegistry = new ContributionContextTypeRegistry();
83
84             ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_FILE_TEMPLATE_ID );
85             ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_FILE_TEMPLATE_ID ).addResolver(
86                 new GlobalTemplateVariables.Cursor() );
87
88             ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_ATTR_VAL_RECORD_TEMPLATE_ID );
89             ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_ATTR_VAL_RECORD_TEMPLATE_ID )
90                 .addResolver( new GlobalTemplateVariables.Cursor() );
91
92             ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID );
93             ldifTemplateContextTypeRegistry.getContextType( LdifEditorConstants.LDIF_MODIFICATION_RECORD_TEMPLATE_ID )
94                 .addResolver( new GlobalTemplateVariables.Cursor() );
95
96             ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODIFICATION_ITEM_TEMPLATE_ID );
97
98             ldifTemplateContextTypeRegistry.addContextType( LdifEditorConstants.LDIF_MODDN_RECORD_TEMPLATE_ID );
99         }
100
101         if ( ldifTemplateStore == null )
102         {
103             ldifTemplateStore = new ContributionTemplateStore( getLdifTemplateContextTypeRegistry(),
104                 getPreferenceStore(), "templates" );
105             try
106             {
107                 ldifTemplateStore.load();
108             }
109             catch ( IOException JavaDoc e )
110             {
111                 e.printStackTrace();
112             }
113         }
114     }
115
116
117     /**
118      * {@inheritDoc}
119      */

120     public void stop( BundleContext context ) throws Exception JavaDoc
121     {
122         plugin = null;
123         super.stop( context );
124
125         if ( colorRegistry != null )
126         {
127             colorRegistry = null;
128         }
129
130         if ( ldifTemplateContextTypeRegistry != null )
131         {
132             ldifTemplateContextTypeRegistry = null;
133         }
134
135         if ( ldifTemplateStore != null )
136         {
137             try
138             {
139                 ldifTemplateStore.save();
140             }
141             catch ( IOException JavaDoc e )
142             {
143                 e.printStackTrace();
144             }
145             ldifTemplateStore = null;
146         }
147     }
148
149
150     /**
151      * Returns the shared instance
152      *
153      * @return the shared instance
154      */

155     public static LdifEditorActivator getDefault()
156     {
157         return plugin;
158     }
159
160
161     /**
162      * Use this method to get SWT colors. A ColorRegistry is used to manage
163      * the RGB->Color mapping.
164      * <p>
165      * Note: Don't dispose the returned color. It is disposed automatically
166      * when the plugin is stopped.
167      *
168      * @param rgb
169      * the rgb color data
170      * @return The SWT Color
171      */

172     public Color getColor( RGB rgb )
173     {
174         if ( !colorRegistry.hasValueFor( rgb.toString() ) )
175         {
176             colorRegistry.put( rgb.toString(), rgb );
177         }
178
179         return colorRegistry.get( rgb.toString() );
180     }
181
182
183     /**
184      * Use this method to get SWT images. Use the IMG_ constants from
185      * LdifEditorConstants for the key.
186      *
187      * @param key
188      * The key (relative path to the image im filesystem)
189      * @return The image discriptor or null
190      */

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

221     public Image getImage( String JavaDoc key )
222     {
223         Image image = getImageRegistry().get( key );
224         if ( image == null )
225         {
226             ImageDescriptor id = getImageDescriptor( key );
227             if ( id != null )
228             {
229                 image = id.createImage();
230                 getImageRegistry().put( key, image );
231             }
232         }
233         return image;
234     }
235
236
237     /**
238      *
239      * @return The LDIF template context type registry
240      */

241     public ContextTypeRegistry getLdifTemplateContextTypeRegistry()
242     {
243         return ldifTemplateContextTypeRegistry;
244     }
245     
246
247     /**
248      *
249      * @return The LDIF template store
250      */

251     public TemplateStore getLdifTemplateStore()
252     {
253         return ldifTemplateStore;
254     }
255     
256     
257     /**
258      * Gets the resource bundle.
259      *
260      * @return the resource bundle
261      */

262     public ResourceBundle JavaDoc getResourceBundle()
263     {
264         return resourceBundle;
265     }
266
267 }
268
Popular Tags