KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > spelling > SpellingEngineRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.ui.internal.texteditor.spelling;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.Platform;
21
22 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
23 import org.eclipse.ui.texteditor.spelling.SpellingEngineDescriptor;
24
25 /**
26  * A spelling engine registry used to access the
27  * {@link SpellingEngineDescriptor}s that describe the spelling engine
28  * extensions.
29  *
30  * @see SpellingEngineDescriptor
31  * @since 3.1
32  */

33 public class SpellingEngineRegistry {
34
35     /**
36      * Extension id of spelling engine extension point.
37      * (value <code>"spellingEngine"</code>).
38      */

39     public static final String JavaDoc SPELLING_ENGINE_EXTENSION_POINT= "spellingEngine"; //$NON-NLS-1$
40

41     /** Ids mapped to descriptors */
42     private Map JavaDoc fDescriptorsMap;
43
44     /** Default descriptor or <code>null</code> */
45     private SpellingEngineDescriptor fDefaultDescriptor;
46
47     /** All descriptors */
48     private SpellingEngineDescriptor[] fDescriptors;
49
50     /** <code>true</code> iff the extensions have been loaded at least once */
51     private boolean fLoaded= false;
52
53     /**
54      * Returns the descriptor with the given id or <code>null</code> if
55      * none could be found.
56      *
57      * @param id the id
58      * @return the descriptor with the given id or <code>null</code>
59      */

60     public SpellingEngineDescriptor getDescriptor(String JavaDoc id) {
61         ensureExtensionsLoaded();
62         return (SpellingEngineDescriptor) fDescriptorsMap.get(id);
63     }
64
65     /**
66      * Returns the default descriptor.
67      *
68      * @return the default descriptor
69      */

70     public SpellingEngineDescriptor getDefaultDescriptor() {
71         ensureExtensionsLoaded();
72         return fDefaultDescriptor;
73     }
74
75     /**
76      * Returns all descriptors.
77      *
78      * @return all descriptors
79      */

80     public SpellingEngineDescriptor[] getDescriptors() {
81         ensureExtensionsLoaded();
82         return fDescriptors;
83     }
84
85     /**
86      * Reads all extensions.
87      * <p>
88      * This method can be called more than once in order to reload
89      * from a changed extension registry.
90      * </p>
91      */

92     public synchronized void reloadExtensions() {
93         List JavaDoc descriptors= new ArrayList JavaDoc();
94         fDescriptorsMap= new HashMap JavaDoc();
95         fDefaultDescriptor= null;
96         IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(TextEditorPlugin.PLUGIN_ID, SPELLING_ENGINE_EXTENSION_POINT);
97         for (int i= 0; i < elements.length; i++) {
98             SpellingEngineDescriptor descriptor= new SpellingEngineDescriptor(elements[i]);
99             descriptors.add(descriptor);
100             fDescriptorsMap.put(descriptor.getId(), descriptor);
101             if (fDefaultDescriptor == null && descriptor.isDefault())
102                 fDefaultDescriptor= descriptor;
103         }
104         fDescriptors= (SpellingEngineDescriptor[]) descriptors.toArray(new SpellingEngineDescriptor[descriptors.size()]);
105         fLoaded= true;
106     }
107
108     /**
109      * Ensures the extensions have been loaded at least once.
110      */

111     private void ensureExtensionsLoaded() {
112         if (!fLoaded)
113             reloadExtensions();
114     }
115 }
116
Popular Tags