1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.text.templates;
15 
16 import java.util.Iterator;
17 import java.util.LinkedHashMap;
18 import java.util.Map;
19 
20 import org.eclipse.jface.text.templates.TemplateContextType;
21 
22 /**
23  * A registry for context types. Editor implementors will usually instantiate a
24  * registry and configure the context types available in their editor.
25  * <p>
26  * In order to pick up templates contributed using the <code>org.eclipse.ui.editors.templates</code>
27  * extension point, use a <code>ContributionContextTypeRegistry</code>.
28  * </p>
29  *
30  * @since 3.7
31  */
32 public class ContextTypeRegistry {
33 
34 	/** all known context types */
35 	private final Map<String, TemplateContextType> fContextTypes= new LinkedHashMap<>();
36 
37 	/**
38 	 * Adds a context type to the registry. If there already is a context type
39 	 * with the same ID registered, it is replaced.
40 	 *
41 	 * @param contextType the context type to add
42 	 */
addContextType(TemplateContextType contextType)43 	public void addContextType(TemplateContextType contextType) {
44 		fContextTypes.put(contextType.getId(), contextType);
45 	}
46 
47 	/**
48 	 * Returns the context type if the id is valid, <code>null</code> otherwise.
49 	 *
50 	 * @param id the id of the context type to retrieve
51 	 * @return the context type if <code>name</code> is valid, <code>null</code> otherwise
52 	 */
getContextType(String id)53 	public TemplateContextType getContextType(String id) {
54 		return fContextTypes.get(id);
55 	}
56 
57 	/**
58 	 * Returns an iterator over all registered context types.
59 	 *
60 	 * @return an iterator over all registered context types
61 	 */
contextTypes()62 	public Iterator<TemplateContextType> contextTypes() {
63 		return fContextTypes.values().iterator();
64 	}
65 }
66