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 Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 #331113 is a reply to message #331112] |
Mon, 25 August 2008 21:00 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Matthew Hall![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=qualidafial%40gmail.com) 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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
|
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
|
|
| |
Goto Forum:
Current Time: Sat Sep 07 11:10:55 GMT 2024
Powered by FUDForum. Page generated in 0.04885 seconds
|