001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.internal.texteditor.spelling;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.Platform;
019:
020: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
021: import org.eclipse.ui.texteditor.spelling.SpellingEngineDescriptor;
022:
023: /**
024: * A spelling engine registry used to access the
025: * {@link SpellingEngineDescriptor}s that describe the spelling engine
026: * extensions.
027: *
028: * @see SpellingEngineDescriptor
029: * @since 3.1
030: */
031: public class SpellingEngineRegistry {
032:
033: /**
034: * Extension id of spelling engine extension point.
035: * (value <code>"spellingEngine"</code>).
036: */
037: public static final String SPELLING_ENGINE_EXTENSION_POINT = "spellingEngine"; //$NON-NLS-1$
038:
039: /** Ids mapped to descriptors */
040: private Map fDescriptorsMap;
041:
042: /** Default descriptor or <code>null</code> */
043: private SpellingEngineDescriptor fDefaultDescriptor;
044:
045: /** All descriptors */
046: private SpellingEngineDescriptor[] fDescriptors;
047:
048: /** <code>true</code> iff the extensions have been loaded at least once */
049: private boolean fLoaded = false;
050:
051: /**
052: * Returns the descriptor with the given id or <code>null</code> if
053: * none could be found.
054: *
055: * @param id the id
056: * @return the descriptor with the given id or <code>null</code>
057: */
058: public SpellingEngineDescriptor getDescriptor(String id) {
059: ensureExtensionsLoaded();
060: return (SpellingEngineDescriptor) fDescriptorsMap.get(id);
061: }
062:
063: /**
064: * Returns the default descriptor.
065: *
066: * @return the default descriptor
067: */
068: public SpellingEngineDescriptor getDefaultDescriptor() {
069: ensureExtensionsLoaded();
070: return fDefaultDescriptor;
071: }
072:
073: /**
074: * Returns all descriptors.
075: *
076: * @return all descriptors
077: */
078: public SpellingEngineDescriptor[] getDescriptors() {
079: ensureExtensionsLoaded();
080: return fDescriptors;
081: }
082:
083: /**
084: * Reads all extensions.
085: * <p>
086: * This method can be called more than once in order to reload
087: * from a changed extension registry.
088: * </p>
089: */
090: public synchronized void reloadExtensions() {
091: List descriptors = new ArrayList();
092: fDescriptorsMap = new HashMap();
093: fDefaultDescriptor = null;
094: IConfigurationElement[] elements = Platform
095: .getExtensionRegistry().getConfigurationElementsFor(
096: TextEditorPlugin.PLUGIN_ID,
097: SPELLING_ENGINE_EXTENSION_POINT);
098: for (int i = 0; i < elements.length; i++) {
099: SpellingEngineDescriptor descriptor = new SpellingEngineDescriptor(
100: elements[i]);
101: descriptors.add(descriptor);
102: fDescriptorsMap.put(descriptor.getId(), descriptor);
103: if (fDefaultDescriptor == null && descriptor.isDefault())
104: fDefaultDescriptor = descriptor;
105: }
106: fDescriptors = (SpellingEngineDescriptor[]) descriptors
107: .toArray(new SpellingEngineDescriptor[descriptors
108: .size()]);
109: fLoaded = true;
110: }
111:
112: /**
113: * Ensures the extensions have been loaded at least once.
114: */
115: private void ensureExtensionsLoaded() {
116: if (!fLoaded)
117: reloadExtensions();
118: }
119: }
|