Skip to content

Commit

Permalink
Initialize the ProjectPreferences with the workspace
Browse files Browse the repository at this point in the history
This makes the current Workspace visible to the ProjectPreferences right
after we change the search order.

Beside that, the workspace is passed / fetched from known sources if
possible

Fix #124
  • Loading branch information
laeubi committed May 11, 2022
1 parent 88f5817 commit bb52483
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Markus Schorn (Wind River) - [108066] Project prefs marked dirty on read
* James Blackburn (Broadcom Corp.) - ongoing development
* Lars Vogel <[email protected]> - Bug 473427, 483529
* Christoph Läubrich - Issue #124
*******************************************************************************/
package org.eclipse.core.internal.resources;

Expand Down Expand Up @@ -66,6 +67,7 @@ public class ProjectPreferences extends EclipsePreferences {

// cache
private int segmentCount;
private Workspace workspace;

static void deleted(IFile file) throws CoreException {
IPath path = file.getFullPath();
Expand Down Expand Up @@ -191,7 +193,7 @@ private static Properties loadProperties(IFile file) throws BackingStoreExceptio
}

private static void preferencesChanged(IProject project) {
Workspace workspace = ((Workspace) ResourcesPlugin.getWorkspace());
Workspace workspace = (Workspace) project.getWorkspace();
workspace.getCharsetManager().projectPreferencesChanged(project);
workspace.getContentDescriptionManager().projectPreferencesChanged(project);
}
Expand Down Expand Up @@ -361,8 +363,9 @@ public ProjectPreferences() {
super(null, null);
}

private ProjectPreferences(EclipsePreferences parent, String name) {
private ProjectPreferences(EclipsePreferences parent, String name, Workspace workspace) {
super(parent, name);
setWorkspace(workspace);

// cache the segment count
String path = absolutePath();
Expand All @@ -374,7 +377,7 @@ private ProjectPreferences(EclipsePreferences parent, String name) {
// cache the project name
String projectName = getSegment(path, 1);
if (projectName != null)
project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
project = getWorkspace().getRoot().getProject(projectName);

// cache the qualifier
if (segmentCount > 2)
Expand Down Expand Up @@ -486,7 +489,7 @@ protected IPath getLocation() {

@Override
protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) {
return new ProjectPreferences(nodeParent, nodeName);
return new ProjectPreferences(nodeParent, nodeName, workspace);
}

@Override
Expand All @@ -507,7 +510,7 @@ protected String internalPut(String key, String newValue) {
silentLoad();
if ((segmentCount == 3) && PREFS_REGULAR_QUALIFIER.equals(qualifier) && (project != null)) {
if (ResourcesPlugin.PREF_SEPARATE_DERIVED_ENCODINGS.equals(key)) {
CharsetManager charsetManager = ((Workspace) ResourcesPlugin.getWorkspace()).getCharsetManager();
CharsetManager charsetManager = getWorkspace().getCharsetManager();
if (Boolean.parseBoolean(newValue))
charsetManager.splitEncodingPreferences(project);
else
Expand Down Expand Up @@ -621,7 +624,7 @@ public boolean nodeExists(String path) throws BackingStoreException {
return super.nodeExists(path);
// if we are checking existance of a single segment child of /project, base the answer on
// whether or not it exists in the workspace.
return ResourcesPlugin.getWorkspace().getRoot().getProject(path).exists() || super.nodeExists(path);
return getWorkspace().getRoot().getProject(path).exists() || super.nodeExists(path);
}

@Override
Expand All @@ -632,7 +635,7 @@ public void remove(String key) {
super.remove(key);
if ((segmentCount == 3) && PREFS_REGULAR_QUALIFIER.equals(qualifier) && (project != null)) {
if (ResourcesPlugin.PREF_SEPARATE_DERIVED_ENCODINGS.equals(key)) {
CharsetManager charsetManager = ((Workspace) ResourcesPlugin.getWorkspace()).getCharsetManager();
CharsetManager charsetManager = getWorkspace().getCharsetManager();
if (ResourcesPlugin.DEFAULT_PREF_SEPARATE_DERIVED_ENCODINGS)
charsetManager.splitEncodingPreferences(project);
else
Expand Down Expand Up @@ -720,8 +723,8 @@ protected void save() throws BackingStoreException {
};
//don't bother with scheduling rules if we are already inside an operation
try {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
if (((Workspace) workspace).getWorkManager().isLockAlreadyAcquired()) {
Workspace workspace = getWorkspace();
if (workspace.getWorkManager().isLockAlreadyAcquired()) {
operation.run(null);
} else {
IResourceRuleFactory factory = workspace.getRuleFactory();
Expand Down Expand Up @@ -756,4 +759,16 @@ private void silentLoad() {
node.setLoading(false);
}
}

public void setWorkspace(Workspace workspace) {
this.workspace = workspace;
}

private Workspace getWorkspace() {
if (workspace != null) {
return workspace;
}
// last resort...
return (Workspace) ResourcesPlugin.getWorkspace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* Serge Beauchamp (Freescale Semiconductor) - [229633] Group and Project Path Variable Support
* Broadcom Corporation - ongoing development
* Lars Vogel <[email protected]> - Bug 473427
* Christoph Läubrich - Issue #77 - SaveManager access the ResourcesPlugin.getWorkspace at init phase
* - Issue #86 - Cyclic dependency between ProjectPreferences and Workspace init
* Christoph Läubrich - Issue #77, Issue #86, Issue #124
*******************************************************************************/
package org.eclipse.core.internal.resources;

Expand All @@ -25,6 +24,7 @@
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -50,6 +50,7 @@
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
import org.osgi.service.prefs.Preferences;
import org.xml.sax.InputSource;

/**
Expand Down Expand Up @@ -1861,14 +1862,23 @@ protected void initializeMoveDeleteHook() {
* Add the project scope to the preference service's default look-up order so
* people get it for free
*/
private void initializePreferenceLookupOrder() {
private void initializePreferenceLookupOrder() throws CoreException {
PreferencesService service = PreferencesService.getDefault();
String[] original = service.getDefaultDefaultLookupOrder();
List<String> newOrder = new ArrayList<>();
// put the project scope first on the list
newOrder.add(ProjectScope.SCOPE);
newOrder.addAll(Arrays.asList(original));
service.setDefaultDefaultLookupOrder(newOrder.toArray(new String[newOrder.size()]));
Preferences node = service.getRootNode().node(ProjectScope.SCOPE);
if (node instanceof ProjectPreferences) {
ProjectPreferences projectPreferences = (ProjectPreferences) node;
projectPreferences.setWorkspace(this);
} else {
throw new CoreException(Status.error(MessageFormat.format(
"Internal error while open workspace, expected ProjectPreferences for the scope {0} but got {1}", //$NON-NLS-1$
ProjectScope.SCOPE, node.getClass().getSimpleName())));
}
}

/**
Expand Down

0 comments on commit bb52483

Please sign in to comment.