Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java
index 6ec8b87..ae823a7 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java
@@ -14,6 +14,7 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
+import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.junit.*;
@@ -21,7 +22,7 @@
import com.google.eclipse.protobuf.ui.util.Resources;
/**
- * Tests for <code>{@link SingleDirectoryFileResolver#resolveUri(String, URI, PathsPreferences)}</code>.
+ * Tests for <code>{@link SingleDirectoryFileResolver#resolveUri(String, URI, IProject, PathsPreferences)}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@@ -45,19 +46,19 @@
@Test public void should_resolve_import_URI_if_missing_scheme() {
when(resources.fileExists(any(URI.class))).thenReturn(true);
- String resolved = resolver.resolveUri("folder1/address.proto", resourceUri, preferences);
+ String resolved = resolver.resolveUri("folder1/address.proto", resourceUri, null, preferences);
assertThat(resolved, equalTo("platform:/resource/src/proto/folder1/address.proto"));
}
@Test public void should_resolve_import_URI_even_if_overlapping_folders_with_resource_URI() {
when(resources.fileExists(any(URI.class))).thenReturn(true);
- String resolved = resolver.resolveUri("src/proto/folder1/address.proto", resourceUri, preferences);
+ String resolved = resolver.resolveUri("src/proto/folder1/address.proto", resourceUri, null, preferences);
assertThat(resolved, equalTo("platform:/resource/src/proto/folder1/address.proto"));
}
@Test public void should_resolve_import_URI_even_if_overlapping_one_folder_only_with_resource_URI() {
when(resources.fileExists(any(URI.class))).thenReturn(true);
- String resolved = resolver.resolveUri("src/proto/read-only/address.proto", resourceUri, preferences);
+ String resolved = resolver.resolveUri("src/proto/read-only/address.proto", resourceUri, null, preferences);
assertThat(resolved, equalTo("platform:/resource/src/proto/read-only/address.proto"));
}
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/PreferenceAndPropertyPage.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/PreferenceAndPropertyPage.java
index 6c354ed..a7c5518 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/PreferenceAndPropertyPage.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/PreferenceAndPropertyPage.java
@@ -199,5 +199,14 @@
setErrorMessage(errorMessage);
setValid(false);
}
+
+ /**
+ * Returns the project whose properties are being changed.
+ * @return the project whose properties are being changed, or {@code null} if this page is updating workspace-level
+ * preferences.
+ */
+ protected final IProject project() {
+ return project;
+ }
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
index e06a8be..94dcc4b 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
@@ -13,14 +13,15 @@
import static org.eclipse.jface.dialogs.IDialogConstants.OK_ID;
import static org.eclipse.xtext.util.Strings.isEmpty;
-import com.google.eclipse.protobuf.ui.preferences.InputDialog;
-
+import org.eclipse.core.resources.IProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
+import com.google.eclipse.protobuf.ui.preferences.InputDialog;
+
/**
* Dialog where users can select a path (in the workspace or file system) to be included in resolution of imports.
*
@@ -38,8 +39,9 @@
/**
* Creates a new </code>{@link AddDirectoryDialog}</code>.
* @param parent a shell which will be the parent of the new instance.
+ * @param project the project whose properties the user is modifying.
*/
- public AddDirectoryDialog(Shell parent) {
+ public AddDirectoryDialog(Shell parent, IProject project) {
super(parent, includeDirectoryTitle);
}
@@ -136,7 +138,7 @@
private static Color getColor(int systemColorID) {
return Display.getCurrent().getSystemColor(systemColorID);
}
-
+
/**
* Returns the path selected by the user.
* @return the path selected by the user.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
index e268f64..6f4694c 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPathsEditor.java
@@ -15,6 +15,7 @@
import java.util.*;
import java.util.List;
+import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
@@ -32,6 +33,8 @@
*/
public class DirectoryPathsEditor extends Composite {
+ private final IProject project;
+
private final Table tblDirectoryPaths;
private final TableViewer tblVwrDirectoryPaths;
private final Button btnAdd;
@@ -43,8 +46,9 @@
private DataChangedListener dataChangedListener;
- public DirectoryPathsEditor(Composite parent, PluginImageHelper imageHelper) {
+ public DirectoryPathsEditor(Composite parent, IProject project, PluginImageHelper imageHelper) {
super(parent, SWT.NONE);
+ this.project = project;
// generated by WindowBuilder
setLayout(new GridLayout(2, false));
@@ -93,7 +97,7 @@
});
btnAdd.addSelectionListener(new SelectionAdapter() {
@Override public void widgetSelected(SelectionEvent e) {
- AddDirectoryDialog dialog = new AddDirectoryDialog(getShell());
+ AddDirectoryDialog dialog = new AddDirectoryDialog(getShell(), project);
if (dialog.open() == OK) {
importPaths.add(dialog.selectedPath());
updateTable();
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectorySelectionDialogs.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectorySelectionDialogs.java
index c9aa6e2..c8d3172 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectorySelectionDialogs.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectorySelectionDialogs.java
@@ -15,15 +15,14 @@
import static org.eclipse.jface.window.Window.OK;
import static org.eclipse.ui.views.navigator.ResourceComparator.NAME;
+import java.net.URI;
+
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.DirectoryDialog;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
-import org.eclipse.ui.dialogs.ISelectionStatusValidator;
-import org.eclipse.ui.model.WorkbenchContentProvider;
-import org.eclipse.ui.model.WorkbenchLabelProvider;
+import org.eclipse.swt.widgets.*;
+import org.eclipse.ui.dialogs.*;
+import org.eclipse.ui.model.*;
import org.eclipse.ui.views.navigator.ResourceComparator;
/**
@@ -39,14 +38,14 @@
static String showWorkspaceDirectoryDialog(Shell shell, String initialPath, IProject project) {
String currentPathText = initialPath.replaceAll("\"", "");
- IPath path = new Path(currentPathText);
+ URI uri = URI.create(currentPathText);
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(),
new WorkbenchContentProvider());
dialog.setInput(project == null ? workspaceRoot() : project);
dialog.setComparator(new ResourceComparator(NAME));
IResource container = null;
- if (path.isAbsolute()) {
- IContainer containers[] = workspaceRoot().findContainersForLocation(path);
+ if (uri.isAbsolute()) {
+ IContainer containers[] = workspaceRoot().findContainersForLocationURI(uri);
if (containers != null && containers.length > 0) container = containers[0];
}
dialog.setInitialSelection(container);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencePage.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencePage.java
index 6f395a7..70fe2cf 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencePage.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencePage.java
@@ -15,21 +15,18 @@
import static java.util.Collections.unmodifiableList;
import static org.eclipse.xtext.util.Strings.*;
-import java.util.ArrayList;
+import java.util.*;
import java.util.List;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.events.*;
+import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.xtext.ui.PluginImageHelper;
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
-import com.google.eclipse.protobuf.ui.preferences.DataChangedListener;
-import com.google.eclipse.protobuf.ui.preferences.PreferenceAndPropertyPage;
+import com.google.eclipse.protobuf.ui.preferences.*;
import com.google.inject.Inject;
/**
@@ -72,7 +69,7 @@
btnMultipleFolders.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
btnMultipleFolders.setText(filesInMultipleDirectories);
- directoryPathsEditor = new DirectoryPathsEditor(grpResolutionOfImported, imageHelper);
+ directoryPathsEditor = new DirectoryPathsEditor(grpResolutionOfImported, project(), imageHelper);
directoryPathsEditor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
new Label(contents, SWT.NONE);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java
index 7f46b50..261d16b 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java
@@ -8,6 +8,7 @@
*/
package com.google.eclipse.protobuf.ui.scoping;
+import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.URI;
@@ -21,5 +22,5 @@
String PREFIX = "platform:/resource";
char SEPARATOR = IPath.SEPARATOR;
- String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences);
+ String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences);
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
index 2e00a09..9000aa8 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
@@ -15,8 +15,7 @@
import org.eclipse.emf.ecore.resource.Resource;
import com.google.eclipse.protobuf.scoping.IFileUriResolver;
-import com.google.eclipse.protobuf.ui.preferences.paths.PathsPreferences;
-import com.google.eclipse.protobuf.ui.preferences.paths.PathsPreferencesProvider;
+import com.google.eclipse.protobuf.ui.preferences.paths.*;
import com.google.eclipse.protobuf.ui.util.Resources;
import com.google.inject.Inject;
@@ -27,7 +26,7 @@
*/
public class FileUriResolver implements IFileUriResolver {
- @Inject private PathsPreferencesProvider preferenceReader;
+ @Inject private PathsPreferencesProvider preferenceProvider;
@Inject private FileResolverStrategies resolvers;
@Inject private Resources resources;
@@ -60,8 +59,9 @@
private String resolveUri(String importUri, URI resourceUri) {
IProject project = resources.project(resourceUri);
if (project == null) project = resources.activeProject();
- PathsPreferences preferences = preferenceReader.getPreferences(project);
- return resolver(preferences).resolveUri(importUri, resourceUri, preferences);
+ if (project == null) throw new IllegalStateException("Unable to find current project");
+ PathsPreferences preferences = preferenceProvider.getPreferences(project);
+ return resolver(preferences).resolveUri(importUri, resourceUri, project, preferences);
}
private FileResolverStrategy resolver(PathsPreferences preferences) {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java
index 0e5c309..b4f7db7 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java
@@ -11,6 +11,7 @@
import java.util.List;
import org.eclipse.core.filesystem.*;
+import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
@@ -33,7 +34,7 @@
}
/** {@inheritDoc} */
- public String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences) {
+ public String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences) {
List<DirectoryPath> directoryPaths = preferences.directoryPaths();
for (DirectoryPath directoryPath : directoryPaths) {
String resolved = resolveUri(importUri, directoryPath);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java
index 04ccf86..c863a21 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java
@@ -13,6 +13,7 @@
import java.util.*;
+import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.util.Pair;
@@ -30,7 +31,7 @@
this.resources = resources;
}
- public String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences) {
+ public String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences) {
List<String> resourceUriSegments = removeFirstAndLast(declaringResourceUri.segmentsList());
Pair<String, List<String>> importUriPair = pair(importUri, createURI(importUri).segmentsList());
return resolveUri(importUriPair, resourceUriSegments);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
index 777cbab..bfa04d0 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
@@ -8,11 +8,9 @@
*/
package com.google.eclipse.protobuf.ui.util;
-import org.eclipse.core.filesystem.EFS;
-import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.filesystem.*;
import org.eclipse.core.resources.*;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.*;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.*;
@@ -31,7 +29,7 @@
public class Resources {
public static final String URI_SCHEME_FOR_FILES = "file";
-
+
/**
* Returns the project that contains the resource at the given URI.
* @param resourceUri the given URI.
@@ -49,8 +47,8 @@
IViewPart part = reference.getView(false);
if (!(part instanceof ResourceNavigator)) continue;
ResourceNavigator navigator = (ResourceNavigator) part;
- StructuredSelection sel = (StructuredSelection) navigator.getTreeViewer().getSelection();
- IResource resource = (IResource) sel.getFirstElement();
+ StructuredSelection selection = (StructuredSelection) navigator.getTreeViewer().getSelection();
+ IResource resource = (IResource) selection.getFirstElement();
return resource.getProject();
}
return null;
@@ -90,7 +88,7 @@
IEditorInput editorInput = new FileStoreEditorInput(fileStore);
return openFile(editorInput/*"org.eclipse.ui.DefaultTextEditor"*/);
}
-
+
private IEditorPart openFile(IEditorInput editorInput) throws PartInitException {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
return page.openEditor(editorInput, "com.google.eclipse.protobuf.Protobuf");
@@ -99,7 +97,7 @@
/**
* Returns a handle to a workspace file identified by the given URI.
* @param uri the given URI.
- * @return a handle to a workspace file identified by the given URI or {@code null} if the URI does not belong to a
+ * @return a handle to a workspace file identified by the given URI or {@code null} if the URI does not belong to a
* workspace file.
*/
public IFile file(URI uri) {