下面列出了org.eclipse.ui.preferences.WorkingCopyManager#org.eclipse.core.runtime.preferences.IScopeContext 实例代码,或者点击链接到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();
}
/**
* Switches the search scope of the preference store to use [Project, Instance, Configuration] if values are project
* specific, and [Instance, Configuration] otherwise. This implementation requires that the given preference store
* is based on the Project preference store when the page is used as a Properties page. (This is done in
* {@link #doGetPreferenceStore()}).
*/
@SuppressWarnings("deprecation")
private void handleUseProjectSettings() {
// Note: uses the pre Eclipse 3.6 way of specifying search scopes (deprecated since 3.6)
boolean isUseProjectSettings = useProjectSettingsButton.getSelection();
link.setEnabled(!isUseProjectSettings);
if (!isUseProjectSettings) {
((FixedScopedPreferenceStore) getPreferenceStore()).setSearchContexts(new IScopeContext[] {
new InstanceScope(), new ConfigurationScope() });
} else {
((FixedScopedPreferenceStore) getPreferenceStore()).setSearchContexts(new IScopeContext[] {
new ProjectScope(currentProject()), new InstanceScope(), new ConfigurationScope() });
setProjectSpecificValues();
}
updateFieldEditors(isUseProjectSettings);
}
@Override
@SuppressWarnings("deprecation")
public IPreferenceStore getWritablePreferenceStore(Object context) {
lazyInitialize();
IProject project = getProject(context);
if (project == null) {
return getWritablePreferenceStore();
}
ProjectScope projectScope = new ProjectScope(project);
FixedScopedPreferenceStore result = new FixedScopedPreferenceStore(projectScope, getQualifier());
result.setSearchContexts(new IScopeContext[] {
projectScope,
new InstanceScope(),
new ConfigurationScope()
});
return result;
}
public boolean hasProjectSpecificOptions(IProject project)
{
if (project != null)
{
IScopeContext projectContext = new ProjectScope(project);
PreferenceKey[] allKeys = fAllKeys;
for (int i = 0; i < allKeys.length; i++)
{
if (allKeys[i].getStoredValue(projectContext, fManager) != null)
{
return true;
}
}
}
return false;
}
/**
* 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();
}
}
@SuppressWarnings("all")
@Override
public IPreferenceStore getContextPreferenceStore(Object context) {
IProject project = getProject(context);
if (project == null) return getPreferenceStore();
IPreferenceStore store = super.getContextPreferenceStore(context);
ProjectScope projectScope = new ProjectScope(project);
FixedScopedPreferenceStore jdtStore = new FixedScopedPreferenceStore(projectScope, JavaCore.PLUGIN_ID);
jdtStore.setSearchContexts(new IScopeContext[] {
projectScope,
new InstanceScope(),
new ConfigurationScope()
});
return new ChainedPreferenceStore(new IPreferenceStore[] {
store,
jdtStore,
PreferenceConstants.getPreferenceStore()
});
}
public AbstractFormatterSelectionBlock(IStatusChangeListener context, IProject project,
IWorkbenchPreferenceContainer container)
{
super(context, project, ProfileManager.collectPreferenceKeys(TEMP_LIST, true), container);
Collections.sort(TEMP_LIST, new Comparator<IScriptFormatterFactory>()
{
public int compare(IScriptFormatterFactory s1, IScriptFormatterFactory s2)
{
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
factories = TEMP_LIST.toArray(new IScriptFormatterFactory[TEMP_LIST.size()]);
TEMP_LIST = new ArrayList<IScriptFormatterFactory>();
sourcePreviewViewers = new ArrayList<SourceViewer>();
// Override the super preferences lookup order.
// All the changes to the formatter settings should go to the instance scope (no project scope here). Only the
// selected profile will be picked from the project scope and then the instance scope when requested.
fLookupOrder = new IScopeContext[] { EclipseUtil.instanceScope(), EclipseUtil.defaultScope() };
}
@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);
}
}
private boolean getChanges(IScopeContext currContext, List<Key> changedSettings) {
boolean needsBuild = false;
for (int i = 0; i < fAllKeys.length; i++) {
Key key = fAllKeys[i];
String oldVal = key.getStoredValue(currContext, null);
String val = key.getStoredValue(currContext, fManager);
if (val == null) {
if (oldVal != null) {
changedSettings.add(key);
needsBuild |= !oldVal.equals(key.getStoredValue(fLookupOrder, true, fManager));
}
} else if (!val.equals(oldVal)) {
changedSettings.add(key);
needsBuild |= oldVal != null || !val.equals(key.getStoredValue(fLookupOrder, true, fManager));
}
}
return needsBuild;
}
public static TimeZone getTimeZone ()
{
final IScopeContext[] scopeContext = new IScopeContext[] { ConfigurationScope.INSTANCE };
final String tzId = Platform.getPreferencesService ().getString ( PLUGIN_ID, TIME_ZONE_KEY, TimeZone.getDefault ().getID (), scopeContext );
if ( Arrays.asList ( TimeZone.getAvailableIDs () ).contains ( tzId ) )
{
return TimeZone.getTimeZone ( tzId );
}
return TimeZone.getDefault ();
}
/**
* 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();
}
/**
* List of build flags for <code>project</code>, taken from the project configuration
*/
public static List<String> getBuildFlags(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("buildArgs")) {
builder.add(projectNode.get(s, ""));
}
}
return builder.build();
}
/**
* 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();
}
/**
* Resolve a path like <code>instance://org.example.bundle/path/to/boolean</code> to an
* {@link IPreferenceStore}.
*
* @param preferenceUri the preference path
* @return the corresponding store
* @throws IllegalArgumentException if unable to resolve the URI
*/
public static IPreferenceStore resolve(URI preferenceUri) throws IllegalArgumentException {
IScopeContext context = resolveScopeContext(preferenceUri.getScheme());
String path = preferenceUri.getHost();
if (preferenceUri.getPath() != null) {
path += preferenceUri.getPath();
}
return new ScopedPreferenceStore(context, path);
}
@Override
public int open()
{
String saveDirty = Platform.getPreferencesService().getString(IDebugUIConstants.PLUGIN_ID,
IInternalDebugUIConstants.PREF_SAVE_DIRTY_EDITORS_BEFORE_LAUNCH, StringUtil.EMPTY,
new IScopeContext[] { EclipseUtil.instanceScope(), EclipseUtil.defaultScope() });
if (saveDirty.equals(MessageDialogWithToggle.ALWAYS))
{
setResult(dirtyResources);
return Window.OK;
}
return super.open();
}
@Override
@SuppressWarnings("deprecation")
public IPreferenceStore getWritablePreferenceStore() {
lazyInitialize();
FixedScopedPreferenceStore result = new FixedScopedPreferenceStore(new InstanceScope(), getQualifier());
result.setSearchContexts(new IScopeContext[] {
new InstanceScope(),
new ConfigurationScope()
});
return result;
}
public PreferenceStoreWrapper providePreferenceStore() {
IPreferenceStore[] preferenceStores = new IPreferenceStore[2];
preferenceStores[0] = Activator.getDefault().getPreferenceStore();
ScopedPreferenceStore jdtCorePreferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.eclipse.jdt.core");
if (currentJavaProject.isPresent()) { // if we are in a project add to search scope
jdtCorePreferenceStore.setSearchContexts(new IScopeContext[] { new ProjectScope(currentJavaProject.get().getProject()), InstanceScope.INSTANCE });
}
preferenceStores[1] = jdtCorePreferenceStore;
return new PreferenceStoreWrapper(new ChainedPreferenceStore(preferenceStores));
}
protected boolean isAutoBuildEnabled()
{
IPreferencesService service = Platform.getPreferencesService();
String qualifier = ResourcesPlugin.getPlugin().getBundle().getSymbolicName();
String key = "description.autobuilding";
IScopeContext[] contexts = { InstanceScope.INSTANCE, ConfigurationScope.INSTANCE };
return service.getBoolean(qualifier, key, false, contexts);
}
protected boolean isAutoBuildEnabled()
{
IPreferencesService service = Platform.getPreferencesService();
String qualifier = ResourcesPlugin.getPlugin().getBundle().getSymbolicName();
String key = "description.autobuilding";
IScopeContext[] contexts = { InstanceScope.INSTANCE, ConfigurationScope.INSTANCE};
return service.getBoolean( qualifier, key, false, contexts );
}
private IScopeContext[] getLookupScopes(IProject project)
{
List<IScopeContext> list = new ArrayList<IScopeContext>(3);
list.add(EclipseUtil.instanceScope());
list.add(EclipseUtil.defaultScope());
if (project != null)
{
list.add(0, new ProjectScope(project));
}
return list.toArray(new IScopeContext[list.size()]);
}
public String getStoredValue(IScopeContext[] lookupOrder, boolean ignoreTopScope, IWorkingCopyManager manager) {
for (int i= ignoreTopScope ? 1 : 0; i < lookupOrder.length; i++) {
String value= getStoredValue(lookupOrder[i], manager);
if (value != null) {
return value;
}
}
return null;
}
public void setStoredValue(IScopeContext context, String value, IWorkingCopyManager manager) {
if (value != null) {
getNode(context, manager).put(fKey, value);
} else {
getNode(context, manager).remove(fKey);
}
}
public void setStoredBoolean(IScopeContext context, Boolean value, IWorkingCopyManager manager) {
if (value != null) {
getNode(context, manager).putBoolean(fKey, value);
} else {
getNode(context, manager).remove(fKey);
}
}
public void setStoredInt(IScopeContext context, Integer value, IWorkingCopyManager manager) {
if (value != null) {
getNode(context, manager).putInt(fKey, value);
} else {
getNode(context, manager).remove(fKey);
}
}
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);
}
/**
* Checks the common editor plugin to see if the user has enabled hovers on content assist
*
* @return <code>true</code>, if hover is enabled; <code>false</code>, otherwise.
*/
public Boolean isHoverEnabled()
{
IScopeContext[] scopes = new IScopeContext[] { EclipseUtil.instanceScope(), EclipseUtil.defaultScope() };
return Platform.getPreferencesService().getBoolean(CommonEditorPlugin.PLUGIN_ID,
IPreferenceConstants.CONTENT_ASSIST_HOVER, true, scopes);
}
/**
* 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);
}
}
public static String ofWorkspace() {
IPreferencesService prefs = Platform.getPreferencesService();
IScopeContext[] scopeContext = new IScopeContext[] { InstanceScope.INSTANCE };
String lineSeparator = prefs.getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
if (lineSeparator == null) {
lineSeparator = System.getProperty("line.separator");
}
return toLabel(lineSeparator);
}