KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > Activator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.aciitemeditor;
21
22
23 import java.io.IOException JavaDoc;
24
25 import org.apache.directory.ldapstudio.aciitemeditor.sourceeditor.ACICodeScanner;
26 import org.apache.directory.ldapstudio.aciitemeditor.sourceeditor.ACITextAttributeProvider;
27 import org.apache.directory.shared.ldap.aci.ACIItemParser;
28 import org.eclipse.jface.dialogs.Dialog;
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.resource.JFaceResources;
32 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
33 import org.eclipse.swt.graphics.FontMetrics;
34 import org.eclipse.swt.graphics.GC;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
38 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
39 import org.eclipse.ui.plugin.AbstractUIPlugin;
40 import org.osgi.framework.BundleContext;
41
42
43 /**
44  * The activator class controls the plug-in life cycle
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class Activator extends AbstractUIPlugin
50 {
51
52     /** The plug-in ID */
53     public static final String JavaDoc PLUGIN_ID = "org.apache.directory.ldapstudio.aciitemeditor"; //$NON-NLS-1$
54

55     /** The shared instance */
56     private static Activator plugin;
57
58     /** The shared ACI Item parser */
59     private ACIItemParser aciItemParser;
60
61     /** The shared ACI Code Scanner */
62     private ACICodeScanner aciCodeScanner;
63
64     /** The shared ACI TextAttribute Provider */
65     private ACITextAttributeProvider textAttributeProvider;
66
67     /** The template store */
68     private ContributionTemplateStore aciTemplateStore;
69
70     /** The context type registry */
71     private ContributionContextTypeRegistry aciTemplateContextTypeRegistry;
72
73
74     /**
75      * The constructor
76      */

77     public Activator()
78     {
79         plugin = this;
80     }
81
82
83     /**
84      * {@inheritDoc}
85      */

86     public void start( BundleContext context ) throws Exception JavaDoc
87     {
88         super.start( context );
89
90         // ACI Template ContextType Registry initialization
91
if ( aciTemplateContextTypeRegistry == null )
92         {
93             aciTemplateContextTypeRegistry = new ContributionContextTypeRegistry();
94
95             aciTemplateContextTypeRegistry.addContextType( ACIITemConstants.ACI_ITEM_TEMPLATE_ID );
96             aciTemplateContextTypeRegistry.getContextType( ACIITemConstants.ACI_ITEM_TEMPLATE_ID ).addResolver(
97                 new GlobalTemplateVariables.Cursor() );
98         }
99
100         // ACI Template Store initialization
101
if ( aciTemplateStore == null )
102         {
103             aciTemplateStore = new ContributionTemplateStore( getAciTemplateContextTypeRegistry(),
104                 getPreferenceStore(), "templates" ); //$NON-NLS-1$
105
try
106             {
107                 aciTemplateStore.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
126
127     /**
128      * Returns the shared instance
129      *
130      * @return the shared instance
131      */

132     public static Activator getDefault()
133     {
134         return plugin;
135     }
136
137
138     /**
139      * Returns an image descriptor for the image file at the given
140      * plug-in relative path
141      *
142      * @param path the path
143      * @return the image descriptor
144      */

145     public static ImageDescriptor getImageDescriptor( String JavaDoc path )
146     {
147         return imageDescriptorFromPlugin( PLUGIN_ID, path );
148     }
149
150
151     /**
152      * Use this method to get SWT images. A ImageRegistry is used
153      * to manage the the path->Image mapping.
154      * <p>
155      * Note: Don't dispose the returned SWT Image. It is disposed
156      * automatically when the plugin is stopped.
157      *
158      * @param path the path
159      * @return The SWT Image or null
160      */

161     public Image getImage( String JavaDoc path )
162     {
163         Image image = getImageRegistry().get( path );
164         if ( image == null )
165         {
166             ImageDescriptor id = getImageDescriptor( path );
167             if ( id != null )
168             {
169                 image = id.createImage();
170                 getImageRegistry().put( path, image );
171             }
172         }
173         return image;
174     }
175
176
177     /**
178      * Returns the shared ACI item parser. Take care that
179      * the parser isn't used concurrently.
180      *
181      * @return the shared ACI item parser.
182      */

183     public ACIItemParser getACIItemParser()
184     {
185         if ( aciItemParser == null )
186         {
187             aciItemParser = new ACIItemParser( null );
188         }
189         return aciItemParser;
190     }
191
192
193     /**
194      * Returns the button with respect to the font metrics.
195      *
196      * @param control a control
197      * @return the button width
198      */

199     public static int getButtonWidth( Control control )
200     {
201         GC gc = new GC( control );
202         gc.setFont( JFaceResources.getDialogFont() );
203         FontMetrics fontMetrics = gc.getFontMetrics();
204         gc.dispose();
205
206         int width = Dialog.convertHorizontalDLUsToPixels( fontMetrics, IDialogConstants.BUTTON_WIDTH );
207         return width;
208     }
209
210
211     /**
212      * Returns the TextAttribute Provider
213      *
214      * @return
215      * the TextAttribute Provider
216      */

217     public ACITextAttributeProvider getTextAttributeProvider()
218     {
219         if ( textAttributeProvider == null )
220         {
221             textAttributeProvider = new ACITextAttributeProvider();
222         }
223         return textAttributeProvider;
224     }
225
226
227     /**
228      * Retuns the the Aci Code Scanner
229      *
230      * @return
231      * the the Aci Code Scanner
232      */

233     public ACICodeScanner getAciCodeScanner()
234     {
235         if ( aciCodeScanner == null )
236         {
237             aciCodeScanner = new ACICodeScanner( getTextAttributeProvider() );
238         }
239         return aciCodeScanner;
240     }
241
242
243     /**
244      * Gets the ACI Template ContextType Registry
245      *
246      * @return
247      * the ACI Template ContextType Registry
248      */

249     public ContributionContextTypeRegistry getAciTemplateContextTypeRegistry()
250     {
251         return aciTemplateContextTypeRegistry;
252     }
253
254
255     /**
256      * Gets the ACI Template Store
257      *
258      * @return
259      * the ACI Template Store
260      */

261     public ContributionTemplateStore getAciTemplateStore()
262     {
263         return aciTemplateStore;
264     }
265
266 }
267
Popular Tags