Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [Databinding] Trigger validation via my code
[Databinding] Trigger validation via my code [message #330941] Mon, 18 August 2008 19:53 Go to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi,

can I trigger the validation for the UI fields via code?

I have a "copy" function which creates a copy of an existing object and
opens an editor which uses databinding for the model -> UI connectivity.
I have my own validator which checks the "name" field so that it does
not contain a name which is already in use.

But the check is only performed if the user enters something in the
field (SWT.Modify). As far as I know SWT.FocusOut or SWT.None would also
not help.

So how can I trigger the validation via my code without something
ridiculous like this:

// Trigger the validation
// Remember the old value
String text = nameText.getText();
// Put in nonsense
nameText.setText("Hello");
// And back to the old value
nameText.setText(text);

Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #330943 is a reply to message #330941] Mon, 18 August 2008 21:47 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Have you tried Binding.validateTargetToModel()?

Matthew

Lars Vogel wrote:
> Hi,
>
> can I trigger the validation for the UI fields via code?
>
> I have a "copy" function which creates a copy of an existing object and
> opens an editor which uses databinding for the model -> UI connectivity.
> I have my own validator which checks the "name" field so that it does
> not contain a name which is already in use.
>
> But the check is only performed if the user enters something in the
> field (SWT.Modify). As far as I know SWT.FocusOut or SWT.None would also
> not help.
>
> So how can I trigger the validation via my code without something
> ridiculous like this:
>
> // Trigger the validation
> // Remember the old value
> String text = nameText.getText();
> // Put in nonsense
> nameText.setText("Hello");
> // And back to the old value
> nameText.setText(text);
>
> Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #330965 is a reply to message #330943] Tue, 19 August 2008 18:21 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Matthew Hall wrote:
> Have you tried Binding.validateTargetToModel()?
>

Hi Matthew,

Thank you for the suggestion.

I don't think this works unless I did something wrong. Here is what I tried:


bindingContext = new DataBindingContext();
bindingContext.bindValue(SWTObservables.observeText(clientTe xt,
SWT.Modify), BeansObservables.observeValue(system, "client"),
update, null);

IObservableList bindings = bindingContext.getBindings();
for (Object object : bindings) {
if (object instanceof Binding) {
((Binding) object).validateTargetToModel();
}
}

Any further advice?

Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #330967 is a reply to message #330965] Tue, 19 August 2008 19:57 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Lars,

What are you using to display the validation status?

Matthew

Lars Vogel wrote:
> Matthew Hall wrote:
>> Have you tried Binding.validateTargetToModel()?
>>
>
> Hi Matthew,
>
> Thank you for the suggestion.
>
> I don't think this works unless I did something wrong. Here is what I
> tried:
>
>
> bindingContext = new DataBindingContext();
> bindingContext.bindValue(SWTObservables.observeText(clientTe xt,
> SWT.Modify), BeansObservables.observeValue(system,
> "client"),
> update, null);
>
> IObservableList bindings = bindingContext.getBindings();
> for (Object object : bindings) {
> if (object instanceof Binding) {
> ((Binding) object).validateTargetToModel();
> }
> }
>
> Any further advice?
>
> Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #330984 is a reply to message #330967] Wed, 20 August 2008 13:54 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Matthew Hall wrote:
> Lars,
>
> What are you using to display the validation status?
>

Hi Matthew,

AggregateValidationStatus, e.g.:


final IObservableValue validationStatus = new AggregateValidationStatus(
bindingContext.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
validationStatus.addChangeListener(new IChangeListener() {
public void handleChange(ChangeEvent event) {
IStatus status = (IStatus) validationStatus.getValue();
if (status.isOK()) {
form.setMessage("", IMessageProvider.NONE);
} else {

form
.setMessage(status.getMessage(),
IMessageProvider.ERROR);
}
}
});

Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #331001 is a reply to message #330984] Wed, 20 August 2008 22:05 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Lars,

Could you provide a standalone snippet demonstrating the problem? It's
hard to guess the source of your problem without seeing more code.

Matthew

Lars Vogel wrote:
> Matthew Hall wrote:
>> Lars,
>>
>> What are you using to display the validation status?
>>
>
> Hi Matthew,
>
> AggregateValidationStatus, e.g.:
>
>
> final IObservableValue validationStatus = new AggregateValidationStatus(
> bindingContext.getBindings(),
> AggregateValidationStatus.MAX_SEVERITY);
> validationStatus.addChangeListener(new IChangeListener() {
> public void handleChange(ChangeEvent event) {
> IStatus status = (IStatus) validationStatus.getValue();
> if (status.isOK()) {
> form.setMessage("", IMessageProvider.NONE);
> } else {
>
> form
> .setMessage(status.getMessage(),
> IMessageProvider.ERROR);
> }
> }
> });
>
> Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #331040 is a reply to message #331001] Thu, 21 August 2008 20:08 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Matthew,

ok, I'll extract an example and post it as soon as possible.

Best regards, Lars

Matthew Hall wrote:
> Lars,
>
> Could you provide a standalone snippet demonstrating the problem? It's
> hard to guess the source of your problem without seeing more code.
>
> Matthew
>
> Lars Vogel wrote:
>> Matthew Hall wrote:
>>> Lars,
>>>
>>> What are you using to display the validation status?
>>>
>>
>> Hi Matthew,
>>
>> AggregateValidationStatus, e.g.:
>>
>>
>> final IObservableValue validationStatus = new AggregateValidationStatus(
>> bindingContext.getBindings(),
>> AggregateValidationStatus.MAX_SEVERITY);
>> validationStatus.addChangeListener(new IChangeListener() {
>> public void handleChange(ChangeEvent event) {
>> IStatus status = (IStatus) validationStatus.getValue();
>> if (status.isOK()) {
>> form.setMessage("", IMessageProvider.NONE);
>> } else {
>>
>> form
>> .setMessage(status.getMessage(),
>> IMessageProvider.ERROR);
>> }
>> }
>> });
>>
>> Best regards, Lars
Re: [Databinding] Trigger validation via my code [message #331104 is a reply to message #331001] Mon, 25 August 2008 12:56 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Matthew,

attached a small example which is a small modification of:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup

Best regards, Lars

----------------



package org.eclipse.jface.examples.databinding.snippets;

import org.eclipse.core.databinding.AggregateValidationStatus;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList ;
import org.eclipse.core.databinding.observable.value.IObservableVal ue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
* Snippet that displays how to bind the validation error of the
* {@link DataBindingContext} to a label. http://www.eclipse.org
*
* @since 3.2
*/
public class Snippet004ModifiedDataBindingContext {

public static void main(String[] args) {
final Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
MyClass test = new MyClass();
test.setFirstName("Jim");
Shell shell = new Shell(display);
shell.setText("Data Binding Snippet 004 - Modified");
shell.setLayout(new GridLayout(2, false));

new Label(shell, SWT.NONE).setText("Enter 'Joe' to be valid:");

Text text = new Text(shell, SWT.BORDER);
new Label(shell, SWT.NONE).setText("Error:");

DataBindingContext dbc = new DataBindingContext();

// Bind the text to the value.

dbc.bindValue(SWTObservables.observeText(text, SWT.Modify),
PojoObservables.observeValue(test, "firstName"),
new UpdateValueStrategy()
.setAfterGetValidator(new FiveValidator()),
null);

final IObservableValue validationStatus = new AggregateValidationStatus(
dbc.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
validationStatus.addChangeListener(new IChangeListener() {
public void handleChange(ChangeEvent event) {
IStatus status = (IStatus) validationStatus.getValue();
if (!status.isOK()) {
MessageDialog.openError(new Shell(display),
"Error", status.getMessage());
}
}
});

IObservableList bindings = dbc.getBindings();
for (Object object : bindings) {
if (object instanceof Binding) {
((Binding) object).validateTargetToModel();
}
}

shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
});
display.dispose();
}

/**
* Validator that returns validation errors for any value other than 5.
*
* @since 3.2
*/
private static class FiveValidator implements IValidator {
public IStatus validate(Object value) {
return ("Joe".equals(value)) ? Status.OK_STATUS : ValidationStatus
.error("the value was '" + value + "', not 'Joe'");
}
}

public static class MyClass {
private String firstName = "";

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

}
}
Re: [Databinding] Trigger validation via my code [message #331112 is a reply to message #331104] Mon, 25 August 2008 17:55 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030100080007060207000603
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Lars,

The change listener doesn't work because AggregateValidationStatus
computes the initial status (which is an error in this case) when it is
constructed. So there is no change event being fired (and thus no error
message being displayed) until the *next* change in the text field. A
few possible ways to fix this:

1) Construct the AggregateValidationStatus before creating any bindings,
and add the listener at that time. This works for this small example
but may be brittle in production use.
2) (Recommended) Use a value binding to bind the validation status to a
label. See the attached revised snippet to see what I mean.

Hope this helps,

Matthew

Lars Vogel wrote:
> Hi Matthew,
>
> attached a small example which is a small modification of:
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup
>
>
> Best regards, Lars


--------------030100080007060207000603
Content-Type: text/plain;
name="Snippet004ModifiedDataBindingContext.java"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="Snippet004ModifiedDataBindingContext.java"

cGFja2FnZSBvcmcuZWNsaXBzZS5qZmFjZS5leGFtcGxlcy5kYXRhYmluZGlu Zy5zbmlwcGV0
czsNCg0KaW1wb3J0IG9yZy5lY2xpcHNlLmNvcmUuZGF0YWJpbmRpbmcuQWdn cmVnYXRlVmFs
aWRhdGlvblN0YXR1czsNCmltcG9ydCBvcmcuZWNsaXBzZS5jb3JlLmRhdGFi aW5kaW5nLkRh
dGFCaW5kaW5nQ29udGV4dDsNCmltcG9ydCBvcmcuZWNsaXBzZS5jb3JlLmRh dGFiaW5kaW5n
LlVwZGF0ZVZhbHVlU3RyYXRlZ3k7DQppbXBvcnQgb3JnLmVjbGlwc2UuY29y ZS5kYXRhYmlu
ZGluZy5iZWFucy5Qb2pvT2JzZXJ2YWJsZXM7DQppbXBvcnQgb3JnLmVjbGlw c2UuY29yZS5k
YXRhYmluZGluZy5vYnNlcnZhYmxlLlJlYWxtOw0KaW1wb3J0IG9yZy5lY2xp cHNlLmNvcmUu
ZGF0YWJpbmRpbmcudmFsaWRhdGlvbi5JVmFsaWRhdG9yOw0KaW1wb3J0IG9y Zy5lY2xpcHNl
LmNvcmUuZGF0YWJpbmRpbmcudmFsaWRhdGlvbi5WYWxpZGF0aW9uU3RhdHVz Ow0KaW1wb3J0
IG9yZy5lY2xpcHNlLmNvcmUucnVudGltZS5JU3RhdHVzOw0KaW1wb3J0IG9y Zy5lY2xpcHNl
LmNvcmUucnVudGltZS5TdGF0dXM7DQppbXBvcnQgb3JnLmVjbGlwc2UuamZh Y2UuZGF0YWJp
bmRpb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ZSApICkgPyBT dGF0dXMuT0tf
U1RBVFVTIDogVmFsaWRhdGlvblN0YXR1cw0KICAgICAgICAgIC5lcnJvcigg InRoZSB2YWx1
ZSB3YXMgJyIgKyB2YWx1ZSArICInLCBub3QgJ0pvZSciICk7DQogICAgfQ0K ICB9DQoNCiAg
cHVibGljIHN0YXRpYyBjbGFzcyBNeUNsYXNzIHsNCiAgICBwcml2YXRlIFN0 cmluZyBmaXJz
dE5hbWUgPSAiIjsNCg0KICAgIHB1YmxpYyB2b2lkIHNldEZpcnN0TmFtZSgg U3RyaW5nIGZp
cnN0TmFtZSApIHsNCiAgICAgIHRoaXMuZmlyc3ROYW1lID0gZmlyc3ROYW1l Ow0KICAgIH0N
Cg0KICAgIHB1YmxpYyBTdHJpbmcgZ2V0Rmlyc3ROYW1lKCkgew0KICAgICAg cmV0dXJuIGZp
cnN0TmFtZTsNCiAgICB9DQoNCiAgfQ0KfQ==
--------------030100080007060207000603--
Re: [Databinding] Trigger validation via my code [message #331113 is a reply to message #331112] Mon, 25 August 2008 21:00 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Matthew,

I'm using Eclipse forms and I'm using the message manager. Binding the
validation a label would be a step back. Also the early binding doesn't
work for me as in my real example I'm providing the data to the
composite after creating the UI.

So there is really no other way to trigger the validation then this:




Matthew Hall wrote:
> Lars,
>
> The change listener doesn't work because AggregateValidationStatus
> computes the initial status (which is an error in this case) when it is
> constructed. So there is no change event being fired (and thus no error
> message being displayed) until the *next* change in the text field. A
> few possible ways to fix this:
>
> 1) Construct the AggregateValidationStatus before creating any bindings,
> and add the listener at that time. This works for this small example
> but may be brittle in production use.
> 2) (Recommended) Use a value binding to bind the validation status to a
> label. See the attached revised snippet to see what I mean.
>
> Hope this helps,
>
> Matthew
>
> Lars Vogel wrote:
>> Hi Matthew,
>>
>> attached a small example which is a small modification of:
>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup
>>
>>
>> Best regards, Lars
>
Re: [Databinding] Trigger validation via my code [message #331114 is a reply to message #331113] Mon, 25 August 2008 21:02 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Sorry got send during writing it.

So there is no other way then triggering the validation other then my
initial example?

/ Trigger the validation
// Remember the old value
String text = nameText.getText();
// Put in nonsense
nameText.setText("Hello");
// And back to the old value
nameText.setText(text);

Lars Vogel wrote:
> Hi Matthew,
>
> I'm using Eclipse forms and I'm using the message manager. Binding the
> validation a label would be a step back. Also the early binding doesn't
> work for me as in my real example I'm providing the data to the
> composite after creating the UI.
>
> So there is really no other way to trigger the validation then this:
>
>
>
>
> Matthew Hall wrote:
>> Lars,
>>
>> The change listener doesn't work because AggregateValidationStatus
>> computes the initial status (which is an error in this case) when it
>> is constructed. So there is no change event being fired (and thus no
>> error message being displayed) until the *next* change in the text
>> field. A few possible ways to fix this:
>>
>> 1) Construct the AggregateValidationStatus before creating any
>> bindings, and add the listener at that time. This works for this
>> small example but may be brittle in production use.
>> 2) (Recommended) Use a value binding to bind the validation status to
>> a label. See the attached revised snippet to see what I mean.
>>
>> Hope this helps,
>>
>> Matthew
>>
>> Lars Vogel wrote:
>>> Hi Matthew,
>>>
>>> attached a small example which is a small modification of:
>>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup
>>>
>>>
>>> Best regards, Lars
>>


--
Lars Vogel
http://www.vogella.de/eclipse.html - Tutorials about Eclipse
http://www.vogella.de/articles/RichClientPlatform/article.ht ml - Eclipse
RCP Tutorial
Re: [Databinding] Trigger validation via my code [message #331115 is a reply to message #331113] Mon, 25 August 2008 23:00 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Lars,

Try this:

IObservableValue statusObservable = new WritableValue();
statusObservable.addChangeListener(new IChangeListener() {
// your change listener code
});

dbc.bindValue(statusObservable, new AggregateValidationStatus(
dbc.getBindings(), AggregateValidationStatus.MAX_SEVERITY),
null, null);

Matthew

Lars Vogel wrote:
> Hi Matthew,
>
> I'm using Eclipse forms and I'm using the message manager. Binding the
> validation a label would be a step back. Also the early binding doesn't
> work for me as in my real example I'm providing the data to the
> composite after creating the UI.
>
> So there is really no other way to trigger the validation then this:
>
>
>
>
> Matthew Hall wrote:
>> Lars,
>>
>> The change listener doesn't work because AggregateValidationStatus
>> computes the initial status (which is an error in this case) when it
>> is constructed. So there is no change event being fired (and thus no
>> error message being displayed) until the *next* change in the text
>> field. A few possible ways to fix this:
>>
>> 1) Construct the AggregateValidationStatus before creating any
>> bindings, and add the listener at that time. This works for this
>> small example but may be brittle in production use.
>> 2) (Recommended) Use a value binding to bind the validation status to
>> a label. See the attached revised snippet to see what I mean.
>>
>> Hope this helps,
>>
>> Matthew
>>
>> Lars Vogel wrote:
>>> Hi Matthew,
>>>
>>> attached a small example which is a small modification of:
>>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup
>>>
>>>
>>> Best regards, Lars
>>
Re: [Databinding] Trigger validation via my code [message #331116 is a reply to message #331115] Mon, 25 August 2008 22:33 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Matthew,

cool, this works.

Thank you very much.

Best regards, Lars

Matthew Hall wrote:
> Lars,
>
> Try this:
>
> IObservableValue statusObservable = new WritableValue();
> statusObservable.addChangeListener(new IChangeListener() {
> // your change listener code
> });
>
> dbc.bindValue(statusObservable, new AggregateValidationStatus(
> dbc.getBindings(), AggregateValidationStatus.MAX_SEVERITY),
> null, null);
>
> Matthew
>
> Lars Vogel wrote:
>> Hi Matthew,
>>
>> I'm using Eclipse forms and I'm using the message manager. Binding the
>> validation a label would be a step back. Also the early binding
>> doesn't work for me as in my real example I'm providing the data to
>> the composite after creating the UI.
>>
>> So there is really no other way to trigger the validation then this:
>>
>>
>>
>>
>> Matthew Hall wrote:
>>> Lars,
>>>
>>> The change listener doesn't work because AggregateValidationStatus
>>> computes the initial status (which is an error in this case) when it
>>> is constructed. So there is no change event being fired (and thus no
>>> error message being displayed) until the *next* change in the text
>>> field. A few possible ways to fix this:
>>>
>>> 1) Construct the AggregateValidationStatus before creating any
>>> bindings, and add the listener at that time. This works for this
>>> small example but may be brittle in production use.
>>> 2) (Recommended) Use a value binding to bind the validation status to
>>> a label. See the attached revised snippet to see what I mean.
>>>
>>> Hope this helps,
>>>
>>> Matthew
>>>
>>> Lars Vogel wrote:
>>>> Hi Matthew,
>>>>
>>>> attached a small example which is a small modification of:
>>>> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.e xamples.databinding/src/org/eclipse/jface/examples/databindi ng/snippets/Snippet004DataBindingContextErrorLabel.java?view =markup
>>>>
>>>>
>>>> Best regards, Lars
>>>


--
Lars Vogel
http://www.vogella.de/eclipse.html - Tutorials about Eclipse
http://www.vogella.de/articles/RichClientPlatform/article.ht ml - Eclipse
RCP Tutorial
Re: [Databinding] Trigger validation via my code [message #331118 is a reply to message #331116] Tue, 26 August 2008 01:36 Go to previous message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Sweet!
Previous Topic:Activities - Should It Work With A launchDelegates Conribution?
Next Topic:Changing Marker Hilight Color
Goto Forum:
  


Current Time: Mon Jul 08 14:28:30 GMT 2024

Powered by FUDForum. Page generated in 0.04570 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top