KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > spelling > SpellingEngineDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.texteditor.spelling;
12
13 import org.osgi.framework.Bundle;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.Platform;
19
20
21 import org.eclipse.ui.internal.texteditor.spelling.EmptySpellingPreferenceBlock;
22
23 /**
24  * Describes an extension to the <code>spellingEngine</code> extension point.
25  * <p>
26  * This class is not intended to be subclassed by clients.
27  * </p>
28  *
29  * @since 3.1
30  */

31 public class SpellingEngineDescriptor {
32
33     /** Name of the <code>label</code> attribute. */
34     private static final String JavaDoc LABEL_ATTRIBUTE= "label"; //$NON-NLS-1$
35
/** Name of the <code>class</code> attribute. */
36     private static final String JavaDoc CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
37
/** Name of the <code>id</code> attribute. */
38     private static final String JavaDoc ID_ATTRIBUTE= "id"; //$NON-NLS-1$
39
/** Name of the <code>default</code> attribute. */
40     private static final String JavaDoc DEFAULT_ATTRIBUTE= "default"; //$NON-NLS-1$
41
/** Name of the <code>preferencesClass</code> attribute. */
42     private static final String JavaDoc PREFERENCES_CLASS_ATTRIBUTE= "preferencesClass"; //$NON-NLS-1$
43

44     /** The configuration element describing this extension. */
45     private IConfigurationElement fConfiguration;
46     /** The value of the <code>label</code> attribute, if read. */
47     private String JavaDoc fLabel;
48     /** The value of the <code>id</code> attribute, if read. */
49     private String JavaDoc fId;
50     /** The value of the <code>default</code> attribute, if read. */
51     private Boolean JavaDoc fDefault;
52     /** The bundle where this extension was defined. */
53     private Bundle fBundle;
54     /** <code>true</code> iff a preferences class has been specified */
55     private Boolean JavaDoc fHasPreferences;
56
57     /**
58      * Creates a new descriptor for <code>element</code>.
59      * <p>
60      * This method is for internal use only.
61      * </p>
62      *
63      * @param element the extension point element to be described.
64      */

65     public SpellingEngineDescriptor(IConfigurationElement element) {
66         Assert.isLegal(element != null);
67         fConfiguration= element;
68     }
69
70     /**
71      * Reads (if needed) and returns the label of this extension.
72      *
73      * @return the label for this extension.
74      */

75     public String JavaDoc getLabel() {
76         if (fLabel == null) {
77             fLabel= fConfiguration.getAttribute(LABEL_ATTRIBUTE);
78             Assert.isNotNull(fLabel);
79         }
80         return fLabel;
81     }
82
83     /**
84      * Reads (if needed) and returns the id of this extension.
85      *
86      * @return the id for this extension.
87      */

88     public String JavaDoc getId() {
89         if (fId == null) {
90             fId= fConfiguration.getAttribute(ID_ATTRIBUTE);
91             Assert.isNotNull(fId);
92         }
93         return fId;
94     }
95
96     /**
97      * Creates a spelling engine as described in the extension's xml.
98      *
99      * @return the created spelling engine
100      * @throws CoreException if the creation failed
101      */

102     public ISpellingEngine createEngine() throws CoreException {
103         return (ISpellingEngine)fConfiguration.createExecutableExtension(CLASS_ATTRIBUTE);
104     }
105
106     /**
107      * Returns <code>true</code> iff a preferences class has been
108      * specified for this engine.
109      *
110      * @return <code>true</code> iff a preferences class has been
111      * specified for this engine
112      */

113     private boolean hasPreferences() {
114         if (fHasPreferences == null)
115             if (fConfiguration.getAttribute(PREFERENCES_CLASS_ATTRIBUTE) == null)
116                 fHasPreferences= Boolean.FALSE;
117             else
118                 fHasPreferences= Boolean.TRUE;
119         return fHasPreferences.booleanValue();
120     }
121
122     /**
123      * Creates a spelling preferences block as described in the extension's xml.
124      *
125      * @return the created spelling preferences block
126      * @throws CoreException if the creation failed
127      */

128     public ISpellingPreferenceBlock createPreferences() throws CoreException {
129         if (hasPreferences())
130             return (ISpellingPreferenceBlock) fConfiguration.createExecutableExtension(PREFERENCES_CLASS_ATTRIBUTE);
131         return new EmptySpellingPreferenceBlock();
132     }
133
134     /**
135      * States whether the plug-in declaring this extension has been loaded already.
136      *
137      * @return <code>true</code> if the extension point's plug-in has been loaded, <code>false</code> otherwise.
138      */

139     public boolean isPluginLoaded() {
140         if (fBundle == null)
141             fBundle= Platform.getBundle(fConfiguration.getContributor().getName());
142         return (fBundle != null && fBundle.getState() == Bundle.ACTIVE);
143     }
144
145     /**
146      * Reads (if needed) and returns the default attribute value of this extension.
147      *
148      * @return the default attribute value for this extension.
149      */

150     public boolean isDefault() {
151         if (fDefault == null) {
152             String JavaDoc def= fConfiguration.getAttribute(DEFAULT_ATTRIBUTE);
153             if ("true".equalsIgnoreCase(def)) //$NON-NLS-1$
154
fDefault= Boolean.TRUE;
155             else
156                 fDefault= Boolean.FALSE;
157         }
158         return fDefault.booleanValue();
159     }
160 }
Popular Tags