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.jdt.core.manipulation;
15 
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.preferences.DefaultScope;
18 import org.eclipse.core.runtime.preferences.InstanceScope;
19 
20 import org.eclipse.core.resources.ProjectScope;
21 
22 import org.eclipse.text.templates.ContextTypeRegistry;
23 import org.eclipse.text.templates.TemplateStoreCore;
24 
25 import org.eclipse.jdt.core.IJavaProject;
26 
27 /**
28  * Central access point for the Java Manipulation plug-in (id <code>"org.eclipse.jdt.core.manipulation"</code>).
29  */
30 public class JavaManipulation {
31 
32 	/**
33 	 * The id of the Java Manipulation plug-in (value <code>"org.eclipse.jdt.core.manipulation"</code>).
34 	 */
35 	public static final String ID_PLUGIN= "org.eclipse.jdt.core.manipulation"; //$NON-NLS-1$
36 
37 	/**
38 	 * Some operations can be performed without UI, but their preferences are stored
39 	 * in the JDT UI preference node. If JDT UI is not present in runtime, there are
40 	 * sane defaults, but if it exists, the preference node should be checked.
41 	 */
42 	private static String fgPreferenceNodeId;
43 
44 	private static TemplateStoreCore fTemplateStore;
45 
46 	private static ContextTypeRegistry fCodeTemplateContextTypeRegistry;
47 
48 	/**
49 	 * @return The id of the preference node for some basic Java preferences.
50 	 * Generally this will be <code>"org.eclipse.jdt.ui"</code> but some
51 	 * environments may not have the 'org.eclipse.jdt.ui' bundle, so a
52 	 * different one can be set.
53 	 * @since 1.10
54 	 */
getPreferenceNodeId()55 	public static final String getPreferenceNodeId () {
56 		return fgPreferenceNodeId;
57 	}
58 
59 	/**
60 	 * Sets the preference node to be used for basic Java preferences.
61 	 * The client should set the value back to null when finished.
62 	 *
63 	 * @param id the Id to use for the preference node
64 	 * @since 1.10
65 	 */
setPreferenceNodeId(String id)66 	public static final void setPreferenceNodeId (String id) {
67 		Assert.isLegal(fgPreferenceNodeId == null || id == null, "Preference node already set"); //$NON-NLS-1$
68 		fgPreferenceNodeId= id;
69 	}
70 
71 	/**
72 	 * @since 1.11
73 	 */
getCodeTemplateStore()74 	public static final TemplateStoreCore getCodeTemplateStore () {
75 		return fTemplateStore;
76 	}
77 
78 	/**
79 	 * @since 1.11
80 	 */
setCodeTemplateStore(TemplateStoreCore in)81 	public static final void setCodeTemplateStore (TemplateStoreCore in) {
82 		fTemplateStore= in;
83 	}
84 
85 	/**
86 	 * @since 1.11
87 	 */
getCodeTemplateContextRegistry()88 	public static final ContextTypeRegistry getCodeTemplateContextRegistry () {
89 		return fCodeTemplateContextTypeRegistry;
90 	}
91 
92 	/**
93 	 * @since 1.11
94 	 */
setCodeTemplateContextRegistry(ContextTypeRegistry in)95 	public static final void setCodeTemplateContextRegistry (ContextTypeRegistry in) {
96 		fCodeTemplateContextTypeRegistry= in;
97 	}
98 
99 	/**
100 	 * Returns the value for the given key in the given context for the JDT UI plug-in.
101 	 * @param key The preference key
102 	 * @param project The current context or <code>null</code> if no context is available and the
103 	 * workspace setting should be taken. Note that passing <code>null</code> should
104 	 * be avoided.
105 	 * @return Returns the current value for the string.
106 	 * @since 1.10
107 	 */
getPreference(String key, IJavaProject project)108 	public static String getPreference(String key, IJavaProject project) {
109 		String val;
110 		if (project != null) {
111 			val= new ProjectScope(project.getProject()).getNode(JavaManipulation.fgPreferenceNodeId).get(key, null);
112 			if (val != null) {
113 				return val;
114 			}
115 		}
116 		val= InstanceScope.INSTANCE.getNode(JavaManipulation.fgPreferenceNodeId).get(key, null);
117 		if (val != null) {
118 			return val;
119 		}
120 		return DefaultScope.INSTANCE.getNode(JavaManipulation.fgPreferenceNodeId).get(key, null);
121 	}
122 }
123