Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[eclipse] Display an error message when the Xtext output configuratio…
…n is not matching the Java source folders.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 4, 2014
1 parent 5d7e00c commit 4bdc182
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 12 deletions.
Expand Up @@ -22,7 +22,6 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;

import org.eclipse.core.runtime.CoreException;

/**
Expand All @@ -36,7 +35,7 @@
public class SARLProjectNature implements IProjectNature {

private IProject project;

@Override
public void configure() throws CoreException {
//
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
@@ -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.
Expand Down
Expand Up @@ -26,13 +26,15 @@

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;
import org.eclipse.xtext.generator.OutputConfiguration;
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.
Expand Down Expand Up @@ -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 <code>null</code>.
*
* @param project - the project.
* @return the output path for SARL compiler if the project has a specific configuration,
* otherwise <code>null</code>.
*/
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;
}

}

0 comments on commit 4bdc182

Please sign in to comment.