001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor.spelling;
011:
012: import org.osgi.framework.Bundle;
013:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.Platform;
018:
019: import org.eclipse.ui.internal.texteditor.spelling.EmptySpellingPreferenceBlock;
020:
021: /**
022: * Describes an extension to the <code>spellingEngine</code> extension point.
023: * <p>
024: * This class is not intended to be subclassed by clients.
025: * </p>
026: *
027: * @since 3.1
028: */
029: public class SpellingEngineDescriptor {
030:
031: /** Name of the <code>label</code> attribute. */
032: private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
033: /** Name of the <code>class</code> attribute. */
034: private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
035: /** Name of the <code>id</code> attribute. */
036: private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
037: /** Name of the <code>default</code> attribute. */
038: private static final String DEFAULT_ATTRIBUTE = "default"; //$NON-NLS-1$
039: /** Name of the <code>preferencesClass</code> attribute. */
040: private static final String PREFERENCES_CLASS_ATTRIBUTE = "preferencesClass"; //$NON-NLS-1$
041:
042: /** The configuration element describing this extension. */
043: private IConfigurationElement fConfiguration;
044: /** The value of the <code>label</code> attribute, if read. */
045: private String fLabel;
046: /** The value of the <code>id</code> attribute, if read. */
047: private String fId;
048: /** The value of the <code>default</code> attribute, if read. */
049: private Boolean fDefault;
050: /** The bundle where this extension was defined. */
051: private Bundle fBundle;
052: /** <code>true</code> iff a preferences class has been specified */
053: private Boolean fHasPreferences;
054:
055: /**
056: * Creates a new descriptor for <code>element</code>.
057: * <p>
058: * This method is for internal use only.
059: * </p>
060: *
061: * @param element the extension point element to be described.
062: */
063: public SpellingEngineDescriptor(IConfigurationElement element) {
064: Assert.isLegal(element != null);
065: fConfiguration = element;
066: }
067:
068: /**
069: * Reads (if needed) and returns the label of this extension.
070: *
071: * @return the label for this extension.
072: */
073: public String getLabel() {
074: if (fLabel == null) {
075: fLabel = fConfiguration.getAttribute(LABEL_ATTRIBUTE);
076: Assert.isNotNull(fLabel);
077: }
078: return fLabel;
079: }
080:
081: /**
082: * Reads (if needed) and returns the id of this extension.
083: *
084: * @return the id for this extension.
085: */
086: public String getId() {
087: if (fId == null) {
088: fId = fConfiguration.getAttribute(ID_ATTRIBUTE);
089: Assert.isNotNull(fId);
090: }
091: return fId;
092: }
093:
094: /**
095: * Creates a spelling engine as described in the extension's xml.
096: *
097: * @return the created spelling engine
098: * @throws CoreException if the creation failed
099: */
100: public ISpellingEngine createEngine() throws CoreException {
101: return (ISpellingEngine) fConfiguration
102: .createExecutableExtension(CLASS_ATTRIBUTE);
103: }
104:
105: /**
106: * Returns <code>true</code> iff a preferences class has been
107: * specified for this engine.
108: *
109: * @return <code>true</code> iff a preferences class has been
110: * specified for this engine
111: */
112: private boolean hasPreferences() {
113: if (fHasPreferences == null)
114: if (fConfiguration
115: .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()
129: throws CoreException {
130: if (hasPreferences())
131: return (ISpellingPreferenceBlock) fConfiguration
132: .createExecutableExtension(PREFERENCES_CLASS_ATTRIBUTE);
133: return new EmptySpellingPreferenceBlock();
134: }
135:
136: /**
137: * States whether the plug-in declaring this extension has been loaded already.
138: *
139: * @return <code>true</code> if the extension point's plug-in has been loaded, <code>false</code> otherwise.
140: */
141: public boolean isPluginLoaded() {
142: if (fBundle == null)
143: fBundle = Platform.getBundle(fConfiguration
144: .getContributor().getName());
145: return (fBundle != null && fBundle.getState() == Bundle.ACTIVE);
146: }
147:
148: /**
149: * Reads (if needed) and returns the default attribute value of this extension.
150: *
151: * @return the default attribute value for this extension.
152: */
153: public boolean isDefault() {
154: if (fDefault == null) {
155: String def = fConfiguration.getAttribute(DEFAULT_ATTRIBUTE);
156: if ("true".equalsIgnoreCase(def)) //$NON-NLS-1$
157: fDefault = Boolean.TRUE;
158: else
159: fDefault = Boolean.FALSE;
160: }
161: return fDefault.booleanValue();
162: }
163: }
|