Commit 9c451c3a authored by Romain Bioteau's avatar Romain Bioteau
Browse files

fix xtext resource

parent 42b3d85a
......@@ -32,6 +32,7 @@ import org.bonitasoft.studio.condition.conditionModel.Expression_ProcessRef;
import org.bonitasoft.studio.condition.conditionModel.Operation_Compare;
import org.bonitasoft.studio.condition.ui.i18n.Messages;
import org.bonitasoft.studio.condition.ui.internal.ConditionModelActivator;
import org.bonitasoft.studio.condition.validation.ConditionModelJavaValidator;
import org.bonitasoft.studio.expression.editor.ExpressionEditorPlugin;
import org.bonitasoft.studio.expression.editor.constant.ConstantTypeLabelProvider;
import org.bonitasoft.studio.expression.editor.constant.ExpressionReturnTypeContentProvider;
......@@ -84,9 +85,6 @@ import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditor;
import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditorFactory;
import org.eclipse.xtext.ui.editor.embedded.EmbeddedEditorFactory.Builder;
import org.eclipse.xtext.ui.editor.embedded.IEditedResourceProvider;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
import com.google.inject.Injector;
......@@ -123,10 +121,16 @@ public class ComparisonExpressionEditor extends SelectionAwareExpressionEditor i
private boolean isPageFlowContext = false;
private final XtextComparisonExpressionLoader xtextComparisonExpressionLoader;
public ComparisonExpressionEditor(final Resource eResource, final EObject context) {
this.context = context;
adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
adapterLabelProvider = new AdapterFactoryLabelProvider(adapterFactory);
final Injector injector = ConditionModelActivator.getInstance().getInjector(ConditionModelActivator.ORG_BONITASOFT_STUDIO_CONDITION_CONDITIONMODEL);
final ConditionModelJavaValidator validator = injector.getInstance(ConditionModelJavaValidator.class);
validator.setCurrentResourceSet(context.eResource().getResourceSet());
xtextComparisonExpressionLoader = new XtextComparisonExpressionLoader(injector);
}
/*
......@@ -266,17 +270,12 @@ public class ComparisonExpressionEditor extends SelectionAwareExpressionEditor i
protected void updateDependencies() {
inputExpression.getReferencedElements().clear();
final Injector injector = ConditionModelActivator.getInstance().getInjector(ConditionModelActivator.ORG_BONITASOFT_STUDIO_CONDITION_CONDITIONMODEL);
final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final List<Issue> issues = xtextResourceChecker.validate(resource, CheckMode.NORMAL_AND_FAST, null);
if (issues.isEmpty()) {// Validation is OK
final Operation_Compare compareOp = (Operation_Compare) resource.getContents().get(0);
if (compareOp != null) {
final List<Expression_ProcessRef> references = ModelHelper.getAllItemsOfType(compareOp, ConditionModelPackage.Literals.EXPRESSION_PROCESS_REF);
for (final Expression_ProcessRef ref : references) {
final EObject dep = getResolvedDependency(ref);
inputExpression.getReferencedElements().add(ExpressionHelper.createDependencyFromEObject(dep));
}
final Operation_Compare compareOp = xtextComparisonExpressionLoader.resolveProxies(resource, context.eResource().getResourceSet());
if (compareOp != null) {
final List<Expression_ProcessRef> references = ModelHelper.getAllItemsOfType(compareOp, ConditionModelPackage.Literals.EXPRESSION_PROCESS_REF);
for (final Expression_ProcessRef ref : references) {
final EObject dep = getResolvedDependency(ref);
inputExpression.getReferencedElements().add(ExpressionHelper.createDependencyFromEObject(dep));
}
}
}
......
......@@ -26,6 +26,7 @@ import org.bonitasoft.studio.condition.conditionModel.ConditionModelPackage;
import org.bonitasoft.studio.condition.conditionModel.Expression_ProcessRef;
import org.bonitasoft.studio.condition.conditionModel.Operation_Compare;
import org.bonitasoft.studio.condition.ui.internal.ConditionModelActivator;
import org.bonitasoft.studio.condition.validation.ConditionModelJavaValidator;
import org.bonitasoft.studio.expression.editor.ExpressionEditorPlugin;
import org.bonitasoft.studio.expression.editor.provider.IExpressionValidator;
import org.bonitasoft.studio.model.expression.Expression;
......@@ -76,6 +77,8 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final MultiStatus status = new MultiStatus(ExpressionEditorPlugin.PLUGIN_ID, 0, "", null);
final ConditionModelJavaValidator validator = injector.getInstance(ConditionModelJavaValidator.class);
validator.setCurrentResourceSet(context.eResource().getResourceSet());
final List<Issue> issues = xtextResourceChecker.validate(resource, CheckMode.FAST_ONLY, null);
if(issues.isEmpty()){
......
......@@ -16,19 +16,20 @@
*/
package org.bonitasoft.studio.condition.ui.expression;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.bonitasoft.studio.condition.conditionModel.ConditionModelPackage;
import org.bonitasoft.studio.condition.conditionModel.Expression_ProcessRef;
import org.bonitasoft.studio.condition.conditionModel.Operation_Compare;
import org.bonitasoft.studio.condition.scoping.ConditionModelGlobalScopeProvider;
import org.bonitasoft.studio.model.parameter.Parameter;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.Data;
import org.bonitasoft.studio.model.process.MainProcess;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
......@@ -37,8 +38,7 @@ import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.ui.resource.XtextResourceSetProvider;
import org.eclipse.xtext.util.CancelIndicator;
......@@ -64,17 +64,28 @@ public class XtextComparisonExpressionLoader {
if (contents.isEmpty()) {
throw new ComparisonExpressionLoadException("Failed to laod comparison expression " + comparisonExpression);
}
if (context != null && context.eResource() != null) {
return resolveProxies(resource, context.eResource().getResourceSet());
}
return (Operation_Compare) contents.get(0);
}
protected IFile createTmpFile(final String content, final IProject project) throws UnsupportedEncodingException, CoreException {
protected IFile createTmpFile(final String content, final IProject project) throws IOException {
final IFile file = project.getFile("somefile.cmodel");
if (file.exists()) {
file.delete(true, null);
try {
file.delete(true, null);
} catch (final CoreException e) {
throw new IOException(e);
}
}
if (content != null && !content.isEmpty()) {
final InputStream is = new StringInputStream(content, "UTF-8");
file.create(is, true, null);
try {
file.create(is, true, null);
} catch (final CoreException e) {
throw new IOException(e);
}
}
return file;
}
......@@ -97,15 +108,10 @@ public class XtextComparisonExpressionLoader {
final XtextResourceSetProvider xtextResourceSetProvider = injector.getInstance(XtextResourceSetProvider.class);
final IProject project = RepositoryManager.getInstance().getCurrentRepository().getProject();
final ResourceSet resourceSet = xtextResourceSetProvider.get(project);
if (context != null) {
addContextProcessInResourceSet(context, resourceSet);
}
IFile file;
try {
file = createTmpFile(comparisonExpression, project);
} catch (final UnsupportedEncodingException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
} catch (final CoreException e) {
} catch (final IOException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
}
Resource resource = null;
......@@ -117,24 +123,21 @@ public class XtextComparisonExpressionLoader {
final ConditionModelGlobalScopeProvider globalScopeProvider = injector.getInstance(ConditionModelGlobalScopeProvider.class);
globalScopeProvider.setAccessibleEObjects(getAccessibleReferences(context));
if (comparisonExpression != null && !comparisonExpression.isEmpty()) {
//Resolve reference proxies
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
}
return resource;
}
protected void addContextProcessInResourceSet(final EObject context, final ResourceSet resourceSet) {
final Copier copier = new Copier(false);
final MainProcess mainProcess = ModelHelper.getMainProcess(context);
if (mainProcess != null) {
final EObject root = copier.copy(mainProcess);
final Resource resource = new XMIResourceFactoryImpl().createResource(URI.createFileURI("tmp.proc"));
resource.getContents().add(root);
resourceSet.getResources().add(resource);
public Operation_Compare resolveProxies(final Resource resource, final ResourceSet resourceSet) {
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
final Operation_Compare compareOp = (Operation_Compare) resource.getContents().get(0);
final List<Expression_ProcessRef> allRefs = ModelHelper.getAllItemsOfType(compareOp, ConditionModelPackage.Literals.EXPRESSION_PROCESS_REF);
for (final Expression_ProcessRef ref : allRefs) {
final EObject proxy = ref.getValue();
if (proxy.eIsProxy()) {
ref.eSet(ConditionModelPackage.Literals.EXPRESSION_PROCESS_REF__VALUE, EcoreUtil.resolve(proxy, resourceSet));
}
}
return compareOp;
}
}
......@@ -21,6 +21,8 @@ import org.bonitasoft.studio.model.process.LongType;
import org.bonitasoft.studio.model.process.StringType;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.RootNode;
import org.eclipse.xtext.validation.Check;
......@@ -29,6 +31,8 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
public static final String INVALID_EQUALITY_SIGN = "org.bonitasoft.studio.condition.quickfix.InvalidEqualitySign";
private ResourceSet resourceSet;
@Check
public void checkCompatibleTypes(final Operation_Compare operation) {
final String errorMessage = new ConditionModelSwitch<String>() {
......@@ -101,7 +105,10 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
}
private String getDataType(final Expression_ProcessRef e) {
final EObject reference = e.getValue();
EObject reference = e.getValue();
if (reference.eIsProxy()) {
reference = EcoreUtil.resolve(reference, resourceSet);
}
if (reference instanceof JavaObjectData) {
final JavaObjectData javaData = (JavaObjectData) reference;
final String className = javaData.getClassName();
......@@ -172,5 +179,8 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
return null;
}
public void setCurrentResourceSet(final ResourceSet resourceSet) {
this.resourceSet = resourceSet;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment