下面列出了org.eclipse.core.runtime.preferences.IScopeContext#getNode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void addSettings(IProject project, String workspaceRoot, List<String> targets,
List<String> buildFlags) throws BackingStoreException {
IScopeContext projectScope = new ProjectScope(project);
Preferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
int i = 0;
for (String target : targets) {
projectNode.put("target" + i, target);
i++;
}
projectNode.put("workspaceRoot", workspaceRoot);
i = 0;
for (String flag : buildFlags) {
projectNode.put("buildFlag" + i, flag);
i++;
}
projectNode.flush();
}
/**
* Updates project related fields, and saves to the preferences.
*/
private void updateProjectRelated() {
IScopeContext context = new ProjectScope(project);
IEclipsePreferences preferences = context.getNode(CodeCheckerNature.NATURE_ID);
for (ConfigTypes ctp : ConfigTypes.PROJECT_TYPE){
String value = null;
switch (ctp) {
case CHECKER_WORKSPACE:
value = codeCheckerWorkspace.toString();
break;
case IS_GLOBAL:
value = Boolean.toString(isGlobal);
break;
default:
break;
}
preferences.put(ctp.toString(), value);
}
try {
preferences.flush();
} catch (BackingStoreException e) {
Logger.log(IStatus.ERROR, "Preferences cannot be saved!");
e.printStackTrace();
}
}
/**
* Update all formatter settings with the settings of the specified profile.
*
* @param profile
* The profile to write to the preference store
*/
private void writeToPreferenceStore(Profile profile, IScopeContext context) {
final Map profileOptions = profile.getSettings();
for (int i = 0; i < fKeySets.length; i++) {
updatePreferences(context.getNode(fKeySets[i].getNodeName()), fKeySets[i].getKeys(), profileOptions);
}
final IEclipsePreferences uiPrefs = context.getNode(getNodeId());
if (uiPrefs.getInt(fProfileVersionKey, 0) != fProfileVersioner.getCurrentVersion()) {
uiPrefs.putInt(fProfileVersionKey, fProfileVersioner.getCurrentVersion());
}
if (context.getName() == InstanceScope.SCOPE) {
uiPrefs.put(fProfileKey, profile.getID());
} else if (context.getName() == ProjectScope.SCOPE && !profile.isSharedProfile()) {
uiPrefs.put(fProfileKey, profile.getID());
}
}
@Override
protected void updateProfilesWithName(String oldName, Profile newProfile, boolean applySettings) {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (int i = 0; i < projects.length; i++) {
IScopeContext projectScope = fPreferencesAccess.getProjectScope(projects[i]);
IEclipsePreferences node = projectScope.getNode(getNodeId());
String profileId = node.get(fProfileKey, null);
if (oldName.equals(profileId)) {
if (newProfile == null) {
node.remove(fProfileKey);
} else {
if (applySettings) {
writeToPreferenceStore(newProfile, projectScope);
} else {
node.put(fProfileKey, newProfile.getID());
}
}
}
}
IScopeContext instanceScope = fPreferencesAccess.getInstanceScope();
final IEclipsePreferences uiPrefs = instanceScope.getNode(getNodeId());
if (newProfile != null && oldName.equals(uiPrefs.get(fProfileKey, null))) {
writeToPreferenceStore(newProfile, instanceScope);
}
}
/**
* List targets configure for <code>project</code>. Each project configured for Bazel is
* configured to track certain targets and this function fetch this list from the project
* preferences.
*/
public static List<String> getTargets(IProject project) throws BackingStoreException {
// Get the list of targets from the preferences
IScopeContext projectScope = new ProjectScope(project);
Preferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (String s : projectNode.keys()) {
if (s.startsWith("target")) {
builder.add(projectNode.get(s, ""));
}
}
return builder.build();
}
private void delete(IScopeContext context)
{
try
{
IEclipsePreferences prefs = context.getNode(ThemePlugin.PLUGIN_ID);
Preferences preferences = prefs.node(ThemeManager.THEMES_NODE);
preferences.remove(getName());
preferences.flush();
}
catch (BackingStoreException e)
{
IdeLog.logError(ThemePlugin.getDefault(), e);
}
}
/**
* Return the {@link BazelInstance} corresponding to the given <code>project</code>. It looks for
* the instance that runs for the workspace root configured for that project.
*
* @throws BazelNotFoundException
*/
public static BazelCommand.BazelInstance getBazelCommandInstance(IProject project)
throws BackingStoreException, IOException, InterruptedException, BazelNotFoundException {
IScopeContext projectScope = new ProjectScope(project.getProject());
Preferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
File workspaceRoot =
new File(projectNode.get("workspaceRoot", project.getLocation().toFile().toString()));
return Activator.getDefault().getCommand().getInstance(workspaceRoot);
}
/**
* Convert an Eclipse JDT project into an IntelliJ project view
*/
public static ProjectView getProjectView(IProject project)
throws BackingStoreException, JavaModelException {
com.google.devtools.bazel.e4b.projectviews.Builder builder = ProjectView.builder();
IScopeContext projectScope = new ProjectScope(project);
Preferences projectNode = projectScope.getNode(Activator.PLUGIN_ID);
for (String s : projectNode.keys()) {
if (s.startsWith("buildArgs")) {
builder.addBuildFlag(projectNode.get(s, ""));
} else if (s.startsWith("target")) {
builder.addTarget(projectNode.get(s, ""));
}
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for (IClasspathEntry entry : ((IJavaProject) project).getRawClasspath()) {
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_SOURCE:
IResource res = root.findMember(entry.getPath());
if (res != null) {
builder.addDirectory(res.getProjectRelativePath().removeFirstSegments(1).toOSString());
}
break;
case IClasspathEntry.CPE_CONTAINER:
String path = entry.getPath().toOSString();
if (path.startsWith(STANDARD_VM_CONTAINER_PREFIX)) {
builder.setJavaLanguageLevel(
Integer.parseInt(path.substring(STANDARD_VM_CONTAINER_PREFIX.length())));
}
break;
}
}
return builder.build();
}
public static void addChangeListener(IScopeContext context, IWorkingCopyManager manager,
String qualifier, IPreferenceChangeListener listener)
{
IEclipsePreferences node= context.getNode(qualifier);
if (manager != null) {
node = manager.getWorkingCopy(node);
}
node.addPreferenceChangeListener(listener);
}
public static void removeChangeListener(IScopeContext context, IWorkingCopyManager manager,
String qualifier, IPreferenceChangeListener listener)
{
IEclipsePreferences node= context.getNode(qualifier);
if (manager != null) {
node = manager.getWorkingCopy(node);
}
node.removePreferenceChangeListener(listener);
}
private IEclipsePreferences getNode(IScopeContext context, IWorkingCopyManager manager) {
IEclipsePreferences node= context.getNode(fQualifier);
if (manager != null) {
return manager.getWorkingCopy(node);
}
return node;
}
/**
* TODO : implement better architecture for storing preferences.
*
* @param scope
* @param qualifier
* @see org.osgi.service.prefs.Preferences#flush
*/
public static void flush(IScopeContext scope, String qualifier) {
try {
IEclipsePreferences node = scope.getNode(qualifier);
node.flush();
} catch (BackingStoreException e) {
LogHelper.logError(e);
}
}
/**
* Initializes project related fields.
* - Is the last used configuration was local or global.
*/
private void initProjectRelated() {
IScopeContext context = new ProjectScope(project);
IEclipsePreferences preferences = context.getNode(CodeCheckerNature.NATURE_ID);
isGlobal = preferences.getBoolean(ConfigTypes.IS_GLOBAL.toString(), true);
}
public void flush(IScopeContext context)
{
IEclipsePreferences node = context.getNode(fQualifier);
if (node != null)
{
try
{
node.flush();
}
catch (BackingStoreException e)
{
IdeLog.logError(FormatterPlugin.getDefault(), "Error flushing a node", e, IDebugScopes.DEBUG); //$NON-NLS-1$
}
}
}
/**
* Only to read project specific settings to find out to what profile it matches.
*
* @param context
* The project context
*/
private Map readFromPreferenceStore(IScopeContext context, Profile workspaceProfile) {
final Map profileOptions = new HashMap();
IEclipsePreferences uiPrefs = context.getNode(getNodeId());
int version = uiPrefs.getInt(fProfileVersionKey, fProfileVersioner.getFirstVersion());
if (version != fProfileVersioner.getCurrentVersion()) {
Map allOptions = new HashMap();
for (int i = 0; i < fKeySets.length; i++) {
addAll(context.getNode(fKeySets[i].getNodeName()), allOptions);
}
CustomProfile profile = new CustomProfile("tmp", allOptions, version, fProfileVersioner.getProfileKind()); //$NON-NLS-1$
fProfileVersioner.update(profile);
return profile.getSettings();
}
boolean hasValues = false;
for (int i = 0; i < fKeySets.length; i++) {
KeySet keySet = fKeySets[i];
IEclipsePreferences preferences = context.getNode(keySet.getNodeName());
for (final Iterator keyIter = keySet.getKeys().iterator(); keyIter.hasNext();) {
final String key = (String) keyIter.next();
Object val = preferences.get(key, null);
if (val != null) {
hasValues = true;
} else {
val = workspaceProfile.getSettings().get(key);
}
profileOptions.put(key, val);
}
}
if (!hasValues) {
return null;
}
setLatestCompliance(profileOptions);
return profileOptions;
}
private IEclipsePreferences getNode(IScopeContext context, IWorkingCopyManager manager) {
IEclipsePreferences node = context.getNode(fQualifier);
if (manager != null) {
return manager.getWorkingCopy(node);
}
return node;
}
protected String getPersistentValues(IProject project) {
IScopeContext projectScope = getPreferenceScope(project);
IEclipsePreferences node = projectScope.getNode(SCT_RESOURCE_NODE);
String string = node.get(SCT_BLACKLIST_KEY, SCT_BLACKLIST_DEFAULT);
return string;
}
private static IEclipsePreferences getProjectProperties(IProject project) {
IScopeContext projectScope = new ProjectScope(project);
return projectScope.getNode(GWTPlugin.PLUGIN_ID);
}
private static IEclipsePreferences getProjectProperties(IProject project) {
IScopeContext projectScope = new ProjectScope(project);
return projectScope.getNode(GWTPlugin.PLUGIN_ID);
}
private static IEclipsePreferences getProjectProperties(IProject project) {
IScopeContext projectScope = new ProjectScope(project);
return projectScope.getNode(CorePlugin.PLUGIN_ID);
}