Skip to content

Commit

Permalink
502147: Validation support in generated code
Browse files Browse the repository at this point in the history
Generated code signatures
Delegation to DataBindingHelper
Using new DataBindingHelper in generated methods 
Polimorphic dispatcher updated
Comments and packaging

Change-Id: I9560188605279ca4824cb5b8ca8e5ca5f6ce6907
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=502147
Signed-off-by: Francesco Guidieri <francesco.guidieri@gmail.com>
  • Loading branch information
fraguid committed Oct 17, 2016
1 parent df5e73a commit aa83bd3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 30 deletions.
Expand Up @@ -1060,9 +1060,9 @@ expectedFormControlFactory =
package my.empty.binding;
import java.util.List;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.parsley.composite.FormControlFactory;
import org.eclipse.emf.parsley.examples.library.Book;
import org.eclipse.emf.parsley.examples.library.Borrower;
Expand Down Expand Up @@ -1098,9 +1098,10 @@ public class EmptyFormControlFactory extends FormControlFactory {
return _createLabel;
}
public Control control_Writer_name(final DataBindingContext dataBindingContext, final IObservableValue observableValue) {
public Control control_Writer_name(final IObservableValue observableValue, final EStructuralFeature feature) {
Control control = createControl_Writer_name();
dataBindingContext.bindValue(
bindValue(
feature,
createTarget_Writer_name(control),
observableValue);
return control;
Expand All @@ -1115,9 +1116,10 @@ public class EmptyFormControlFactory extends FormControlFactory {
return DatabindingUtil.observeText(it);
}
public Control control_Writer_firstName(final DataBindingContext dataBindingContext, final IObservableValue observableValue) {
public Control control_Writer_firstName(final IObservableValue observableValue, final EStructuralFeature feature) {
Control control = createControl_Writer_firstName();
dataBindingContext.bindValue(
bindValue(
feature,
createTarget_Writer_firstName(control),
observableValue);
return control;
Expand Down Expand Up @@ -1192,9 +1194,9 @@ expectedDialogControlFactory =
package my.empty.binding;
import java.util.List;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.parsley.composite.DialogControlFactory;
import org.eclipse.emf.parsley.examples.library.Book;
import org.eclipse.emf.parsley.examples.library.Borrower;
Expand Down Expand Up @@ -1229,9 +1231,10 @@ public class EmptyDialogControlFactory extends DialogControlFactory {
return _createLabel;
}
public Control control_Writer_name(final DataBindingContext dataBindingContext, final IObservableValue observableValue) {
public Control control_Writer_name(final IObservableValue observableValue, final EStructuralFeature feature) {
Control control = createControl_Writer_name();
dataBindingContext.bindValue(
bindValue(
feature,
createTarget_Writer_name(control),
observableValue);
return control;
Expand All @@ -1246,9 +1249,10 @@ public class EmptyDialogControlFactory extends DialogControlFactory {
return DatabindingUtil.observeText(it);
}
public Control control_Writer_firstName(final DataBindingContext dataBindingContext, final IObservableValue observableValue) {
public Control control_Writer_firstName(final IObservableValue observableValue, final EStructuralFeature feature) {
Control control = createControl_Writer_firstName();
dataBindingContext.bindValue(
bindValue(
feature,
createTarget_Writer_firstName(control),
observableValue);
return control;
Expand Down
Expand Up @@ -16,7 +16,6 @@ import com.google.inject.Injector
import com.google.inject.Provider
import java.io.IOException
import java.util.List
import org.eclipse.core.databinding.DataBindingContext
import org.eclipse.core.databinding.observable.value.IObservableValue
import org.eclipse.emf.common.notify.AdapterFactory
import org.eclipse.emf.common.util.URI
Expand Down Expand Up @@ -608,17 +607,18 @@ class EmfParsleyDslJvmModelInferrer extends AbstractModelInferrer {
members += spec.
control_EClass_EStructuralFeature() [
parameters += spec.toParameter(
"dataBindingContext", typeRef(DataBindingContext)
"observableValue", typeRef(IObservableValue)
)
parameters += spec.toParameter(
"observableValue", typeRef(IObservableValue)
"feature", typeRef(EStructuralFeature)
)
body = [
append(typeRef(Control).type)
append(''' control = «createControlMethodName»();''').newLine
append(
'''
dataBindingContext.bindValue(
bindValue(
feature,
«createTargetMethodName»(control),
observableValue);'''
).newLine
Expand Down
Expand Up @@ -30,8 +30,7 @@
import org.eclipse.emf.parsley.EmfParsleyConstants;
import org.eclipse.emf.parsley.edit.IEditingStrategy;
import org.eclipse.emf.parsley.edit.TextUndoRedo;
import org.eclipse.emf.parsley.internal.databinding.DatabindingValidationUtil;
import org.eclipse.emf.parsley.internal.databinding.EmfValidationTargetToModelUpdateValueStrategy;
import org.eclipse.emf.parsley.internal.databinding.DataBindingHelper;
import org.eclipse.emf.parsley.runtime.util.PolymorphicDispatcherExtensions;
import org.eclipse.emf.parsley.ui.provider.ComboViewerLabelProvider;
import org.eclipse.emf.parsley.ui.provider.FeatureLabelCaptionProvider;
Expand All @@ -40,7 +39,6 @@
import org.eclipse.emf.parsley.widgets.IWidgetFactory;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
Expand Down Expand Up @@ -74,6 +72,7 @@
*
* @author Dennis Huebner initial code
* @author Lorenzo Bettini refactoring for Emf Parsley
* @author Francesco Guidieri added validation support
*
*/
public abstract class AbstractControlFactory implements IWidgetFactory {
Expand All @@ -99,7 +98,7 @@ public abstract class AbstractControlFactory implements IWidgetFactory {
private ProposalCreator proposalCreator;

@Inject
private DatabindingValidationUtil databindingValidationUtil;
private DataBindingHelper dataBindingHelper;

private EObject owner;
private Resource resource;
Expand Down Expand Up @@ -344,18 +343,13 @@ protected Control createAndBindValue(EStructuralFeature feature, boolean withPol
return retVal;
}

/**
* @since 1.1
*/
@SuppressWarnings("rawtypes")
private Binding bindValue(EStructuralFeature feature, IObservableValue target,
protected Binding bindValue(EStructuralFeature feature, IObservableValue target,
IObservableValue source) {
EmfValidationTargetToModelUpdateValueStrategy targetToModelUpdateValueStrategy =
new EmfValidationTargetToModelUpdateValueStrategy(
owner, feature, databindingValidationUtil);

Binding bindValue = edbc.bindValue(
target, source, targetToModelUpdateValueStrategy,
null);
ControlDecorationSupport.create(bindValue, SWT.TOP | SWT.LEFT);
return bindValue;
return dataBindingHelper.bindValue(feature, target, source, owner, edbc);
}

protected ControlObservablePair createControlAndObservableValue(
Expand Down Expand Up @@ -511,7 +505,8 @@ private ControlObservablePair polymorphicGetObservableControl(

/**
* Polymorphically invokes a create_EClass_feature(DataBindingContext,
* IObservableValue)
* IObservableValue), trying to get the new version with validation support.
* If not found, the old version of the method is searched, for backward compatibility.
*
* @param feature
* @param featureObservable
Expand All @@ -520,6 +515,13 @@ private ControlObservablePair polymorphicGetObservableControl(
@SuppressWarnings("rawtypes")
private Control polymorphicCreateControl(EStructuralFeature feature,
IObservableValue featureObservable) {
Control polymorphicInvokeNewVersion = PolymorphicDispatcherExtensions
.<Control> polymorphicInvokeBasedOnFeature(this,
owner.eClass(), feature, CONTROL_PREFIX,
featureObservable, feature);
if(polymorphicInvokeNewVersion!=null){
return polymorphicInvokeNewVersion;
}
return PolymorphicDispatcherExtensions
.<Control> polymorphicInvokeBasedOnFeature(this,
owner.eClass(), feature, CONTROL_PREFIX, edbc,
Expand Down
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2016 RCP Vision (http://www.rcp-vision.com) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Francesco Guidieri - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.parsley.internal.databinding;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.swt.SWT;

import com.google.inject.Inject;

/**
* Helper class for performing databinding
*
* @author Francesco Guidieri - initial API and implementation
*
* @noextend This class is not intended to be subclassed by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
* @since 1.1
*/
public class DataBindingHelper {

@Inject
private DatabindingValidationUtil databindingValidationUtil;

@SuppressWarnings("rawtypes")
public Binding bindValue(EStructuralFeature feature, IObservableValue target, IObservableValue source,
EObject owner, DataBindingContext dataBindingContext) {
EmfValidationTargetToModelUpdateValueStrategy targetToModelUpdateValueStrategy = new EmfValidationTargetToModelUpdateValueStrategy(
owner, feature, databindingValidationUtil);

Binding bindValue = dataBindingContext.bindValue(target, source, targetToModelUpdateValueStrategy, null);
ControlDecorationSupport.create(bindValue, SWT.TOP | SWT.LEFT);
return bindValue;
}

}
Expand Up @@ -27,6 +27,7 @@ import org.junit.Rule
import org.junit.Test

import static extension org.junit.Assert.*
import org.eclipse.core.databinding.observable.value.IObservableValue

class CustomDialogControlFactoryTest extends AbstractControlFactoryTest {

Expand Down Expand Up @@ -148,7 +149,7 @@ class CustomDialogControlFactoryTest extends AbstractControlFactoryTest {
}

/**
* As above, but for a without specifying SWT.Modify
* As above, but for a single feature without specifying SWT.Modify
*/
@Test
def void testCustomControlWithControlObservablePair2() {
Expand All @@ -169,6 +170,30 @@ class CustomDialogControlFactoryTest extends AbstractControlFactoryTest {
control.assertText("Foo")
}

/**
* As above, but using the two args signature
*/
@Test
def void testCustomControlWithFeatureAndObservableValue() {
val o1 = createBaseClassObject
val factory = new DialogControlFactory {
@SuppressWarnings("rawtypes")
def control_BaseClass_baseClassFeature(IObservableValue observableValue, EStructuralFeature f) {
val text = createText("")
// by default the editable is true, thus setting it to false
// gives us evidence that this method was called
text.editable = false
bindValue(f, DatabindingUtil.observeText(text), observableValue)
return text
}
} => [initialize(o1)]
val control = factory.createControl(testPackage.baseClass_BaseClassFeature)
control.assertTextEditable(false)
control.assertText("")
o1.baseClassFeature = "Foo"
control.assertText("Foo")
}

@Test
def void testCustomControlWithControlObservablePairAndLayout() {
// check that the layout data explicitly set is not overwritten
Expand Down

0 comments on commit aa83bd3

Please sign in to comment.