From 4bdc182f7fdf4563fa8d7698606073ed1a7564de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Thu, 4 Dec 2014 20:30:01 +0100 Subject: [PATCH] [eclipse] Display an error message when the Xtext output configuration is not matching the Java source folders. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../eclipse/natures/SARLProjectNature.java | 3 +- .../wizards/newproject/BuildSettingPage.java | 2 +- .../eclipse/wizards/newproject/Messages.java | 8 +- .../newproject/SARLProjectCreationWizard.java | 79 +++++++++++++++++-- .../wizards/newproject/messages.properties | 3 + .../preferences/SARLProjectPreferences.java | 28 +++++++ 6 files changed, 111 insertions(+), 12 deletions(-) diff --git a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/natures/SARLProjectNature.java b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/natures/SARLProjectNature.java index b3e5d4e072..a4cc8cf361 100644 --- a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/natures/SARLProjectNature.java +++ b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/natures/SARLProjectNature.java @@ -22,7 +22,6 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectNature; - import org.eclipse.core.runtime.CoreException; /** @@ -36,7 +35,7 @@ public class SARLProjectNature implements IProjectNature { private IProject project; - + @Override public void configure() throws CoreException { // diff --git a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/BuildSettingPage.java b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/BuildSettingPage.java index 0878d11700..6d193ca74c 100644 --- a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/BuildSettingPage.java +++ b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/BuildSettingPage.java @@ -598,7 +598,7 @@ public void performFinish(IProgressMonitor monitor) throws CoreException, Interr } String newProjectCompliance = this.fKeepContent ? null : this.fFirstPage.getCompilerCompliance(); - configureJavaProject(newProjectCompliance, new SubProgressMonitor(monitor, 2)); + configureJavaProject(newProjectCompliance, new SubProgressMonitor(monitor, 1)); } catch (Throwable e) { if (this.fCurrProject != null) { removeProvisonalProject(); diff --git a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/Messages.java b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/Messages.java index c88829f79a..78c7ce475c 100644 --- a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/Messages.java +++ b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/Messages.java @@ -34,7 +34,13 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = "io.sarl.eclipse.wizards.newproject.messages"; //$NON-NLS-1$ - public static String MainProjectPage_0;public static String SARLProjectNewWizard_0; + public static String JavaProjectWizard_op_error_create_message; + + public static String JavaProjectWizard_op_error_title; + + public static String MainProjectPage_0;public static String SARLProjectCreationWizard_0; + + public static String SARLProjectNewWizard_0; public static String SARLProjectNewWizard_1; public static String SARLProjectNewWizard_2; public static String SARLProjectNewWizard_3; diff --git a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/SARLProjectCreationWizard.java b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/SARLProjectCreationWizard.java index ffe675a3a0..e29c010649 100644 --- a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/SARLProjectCreationWizard.java +++ b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/SARLProjectCreationWizard.java @@ -24,8 +24,10 @@ import io.sarl.eclipse.SARLEclipsePlugin; import io.sarl.eclipse.properties.RuntimeEnvironmentPropertyPage; import io.sarl.eclipse.runtime.ISREInstall; +import io.sarl.lang.ui.preferences.SARLProjectPreferences; import java.lang.reflect.InvocationTargetException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -36,13 +38,14 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExecutableExtension; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; import org.eclipse.jdt.internal.ui.util.ExceptionHandler; @@ -113,6 +116,67 @@ public void addPages() { this.fFirstPage.init(getSelection(), getActivePart()); } + private static boolean hasSourcePath(IJavaProject javaProject, IPath path) { + if (path != null) { + IPath pathInProject = javaProject.getProject().getFullPath().append(path); + try { + for(IClasspathEntry entry : javaProject.getRawClasspath()) { + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE + && pathInProject.equals(entry.getPath())) { + return true; + } + } + } catch (Throwable _) { + // + } + } + return false; + } + + /** Validate the SARL properties of the new projects. + * + * @param element - the created element + * @return validity + */ + protected boolean validateSARLSpecificElements(IJavaElement element) { + IJavaProject javaProject = (IJavaProject) element; + // Check if the "SARL" generation directory is a source folder. + IPath projectPath = SARLProjectPreferences.getSARLOutputPathFor(javaProject.getProject()); + IPath outputPath = projectPath; + IPath generalPath = null; + if (outputPath == null) { + generalPath = SARLProjectPreferences.getSARLOutputPathFor(null); + outputPath = generalPath; + } + if (!hasSourcePath(javaProject, outputPath)) { + StringBuilder sourceFolders = new StringBuilder(); + try { + for(IClasspathEntry entry : javaProject.getRawClasspath()) { + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { + sourceFolders.append("\t"); //$NON-NLS-1$ + sourceFolders.append(entry.getPath().toOSString()); + sourceFolders.append("\n"); //$NON-NLS-1$ + } + } + } catch (Throwable _) { + // + } + + String message = MessageFormat.format( + Messages.SARLProjectCreationWizard_0, + outputPath == null ? null : outputPath.toOSString(), + projectPath == null ? null : projectPath.toOSString(), + generalPath == null ? null : generalPath.toOSString(), + sourceFolders.toString()); + IStatus status = SARLEclipsePlugin.createStatus(IStatus.ERROR, message); + + handleFinishException(getShell(), new InvocationTargetException(new CoreException(status))); + + return false; + } + return true; + } + @Override protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException { // use the full progress monitor @@ -131,6 +195,11 @@ public boolean performFinish() { return false; } + // Validate the SARL specific elements + if (!validateSARLSpecificElements(newElement)) { + return false; + } + IWorkingSet[] workingSets = this.fFirstPage.getWorkingSets(); if (workingSets.length > 0) { PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(newElement, workingSets); @@ -192,14 +261,8 @@ public IJavaElement getCreatedElement() { try { addNatures(javaProject.getProject()); - } catch (JavaModelException e) { - SARLEclipsePlugin.log(e); - } catch (CoreException e) { - SARLEclipsePlugin.log(e); - } - // Set the SRE configuration - try { + // Set the SRE configuration IProject project = javaProject.getProject(); ISREInstall sre = this.fFirstPage.getSRE(); boolean useDefaultSRE = (sre == null || this.fFirstPage.isSystemDefaultSRE()); diff --git a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/messages.properties b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/messages.properties index 69205aaa88..06e2265741 100644 --- a/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/messages.properties +++ b/plugins/io.sarl.eclipse/src/io/sarl/eclipse/wizards/newproject/messages.properties @@ -1,4 +1,7 @@ +JavaProjectWizard_op_error_create_message= +JavaProjectWizard_op_error_title= MainProjectPage_0=SRE +SARLProjectCreationWizard_0=The SARL output path {0} for the generated sources is not a source folder in the project. The source folders are:\n{3}Project path={1}\nGeneral path={2} SARLProjectNewWizard_0=New SARL Project SARLProjectNewWizard_1=Create a SARL Project. SARLProjectNewWizard_2=Define the SARL build settings. diff --git a/plugins/io.sarl.lang.ui/src/io/sarl/lang/ui/preferences/SARLProjectPreferences.java b/plugins/io.sarl.lang.ui/src/io/sarl/lang/ui/preferences/SARLProjectPreferences.java index 6523549552..9058213732 100644 --- a/plugins/io.sarl.lang.ui/src/io/sarl/lang/ui/preferences/SARLProjectPreferences.java +++ b/plugins/io.sarl.lang.ui/src/io/sarl/lang/ui/preferences/SARLProjectPreferences.java @@ -26,6 +26,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.xtext.builder.EclipseOutputConfigurationProvider; import org.eclipse.xtext.builder.preferences.BuilderPreferenceAccess; @@ -33,6 +34,7 @@ import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess; import org.eclipse.xtext.ui.preferences.OptionsConfigurationBlock; +import com.google.common.base.Strings; import com.google.inject.Injector; /** Utilities related to the preferences on a SARL project. @@ -145,4 +147,30 @@ public static void setSpecificSARLConfigurationFor( } } + /** Replies the output path for the generated sources that is registered inside the project's preferences. + * If the project has no specific configuration, replies null. + * + * @param project - the project. + * @return the output path for SARL compiler if the project has a specific configuration, + * otherwise null. + */ + public static IPath getSARLOutputPathFor( + IProject project) { + IPreferenceStore preferenceStore = SARLProjectPreferences.getSARLPreferencesFor(project); + if (preferenceStore.getBoolean(OptionsConfigurationBlock.IS_PROJECT_SPECIFIC)) { + String key; + for (OutputConfiguration projectConfiguration + : SARLProjectPreferences.getXtextConfigurationsFor(project)) { + key = BuilderPreferenceAccess.getKey( + projectConfiguration, + EclipseOutputConfigurationProvider.OUTPUT_DIRECTORY); + String path = preferenceStore.getString(key); + if (!Strings.isNullOrEmpty(path)) { + return Path.fromOSString(path); + } + } + } + return null; + } + }