Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [DataBinding]Problem on ValidateStatus Change Listener
[DataBinding]Problem on ValidateStatus Change Listener [message #327942] Fri, 09 May 2008 02:27 Go to next message
Ellis Yu is currently offline Ellis YuFriend
Messages: 15
Registered: July 2009
Junior Member
Dear All,

Sorry that I repost it here, since I found that I forgot to add
"databinding" on subject.

I got a problem on binding.getValidationStatus().addChangeListener(new
IChangeListener() event. If there's a text field didn't get any change in
a newly created window and I ran updateModels() to explicit update when I
clicked ok. I traced the coding that it ran the validate() in my
Validator. My validation is to check whether the field is blank or not.
Since I didn't make any change on this text field and it's blank in
initial value, the it will return the ValidateError at the end, however
that listener never get trigger. Since I binded the field with event
"SWT_FOCUSOUT", it work if I pretent to type sthg inside and clear it. The
validate process will be run when I leave the focus on other field, then
the listener will be triggered also. Thus, anything I do it wrong or
missing to do? Please kindly help.

Best Rdgs
Ellis
Re: [DataBinding]Problem on ValidateStatus Change Listener [message #328020 is a reply to message #327942] Mon, 12 May 2008 17:23 Go to previous messageGo to next message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
I am not sure I understand - could you post a snippet with which we can
reproduce the problem?

Boris

"Ellis Yu" <ellisyu@sinaman.com> wrote in message
news:5c033dc68d3a17253abd9bf1bc180a36$1@www.eclipse.org...
> Dear All,
>
> Sorry that I repost it here, since I found that I forgot to add
> "databinding" on subject.
> I got a problem on binding.getValidationStatus().addChangeListener(new
> IChangeListener() event. If there's a text field didn't get any change in
> a newly created window and I ran updateModels() to explicit update when I
> clicked ok. I traced the coding that it ran the validate() in my
> Validator. My validation is to check whether the field is blank or not.
> Since I didn't make any change on this text field and it's blank in
> initial value, the it will return the ValidateError at the end, however
> that listener never get trigger. Since I binded the field with event
> "SWT_FOCUSOUT", it work if I pretent to type sthg inside and clear it. The
> validate process will be run when I leave the focus on other field, then
> the listener will be triggered also. Thus, anything I do it wrong or
> missing to do? Please kindly help.
>
> Best Rdgs
> Ellis
>
Re: [DataBinding]Problem on ValidateStatus Change Listener [message #328049 is a reply to message #328020] Tue, 13 May 2008 07:52 Go to previous messageGo to next message
Ellis Yu is currently offline Ellis YuFriend
Messages: 15
Registered: July 2009
Junior Member
Here's my code. I based on author fahrmeyer's FormManager to build. Thanks
his coding here and save me lots of time :-)

private void connectBindingToMessageManager(final Control control,
final DataBindingContext dbc, final Binding binding) {

binding.init(dbc);

binding.getValidationStatus().addChangeListener(new IChangeListener() {

public void handleChange(final ChangeEvent event) {
IObservableValue validationStatus = (IObservableValue) event
.getSource();
IStatus bindStatus = (IStatus) validationStatus.getValue();
// erzeugt einen eindeutigen Schluessel fuer das Control, das
// wird im MessageManager
// als message key verwendet, damit die nachrichten fuer
// einzelne Controls auseinander
// gehalten werden koennen
String msgKey = createMsgKey(control);
int errorCode = IMessageProvider.NONE;

if (bindStatus.getSeverity() == IStatus.ERROR) {
errorCode = IMessageProvider.ERROR;

} else if (bindStatus.getSeverity() == IStatus.WARNING) {
errorCode = IMessageProvider.WARNING;

} else if (bindStatus.getSeverity() == IStatus.INFO) {
errorCode = IMessageProvider.INFORMATION;
}

if (errorCode != IMessageProvider.NONE) {
mMessageManager.addMessage(msgKey, bindStatus.getMessage(),
null, errorCode, control);
}
}

});

binding.getValidationStatus().addValueChangeListener(
new IValueChangeListener() {

public void handleValueChange(final ValueChangeEvent event) {
// das ValueChangeevent lieft den alten und neuen Wert

ValueDiff diff = event.diff;
IStatus oldValue = (IStatus) diff.getOldValue();
IStatus newValue = (IStatus) diff.getNewValue();

if (oldValue.getSeverity() != IStatus.OK
&& newValue.getSeverity() == IStatus.OK) {
String msgKey = createMsgKey(control);
mMessageManager.removeMessage(msgKey, control);
}

}

});
}

Supposing there's ValidationError in my validator. The below coding should
be triggered and show the error in the MessageManager. I'm checking my
text control whether is blank or not when it's lost focus. It's fine if I
delete the text in the control and error shown when I leave the control.

However if the control is blank in the start and I didn't modify anything
in the field. I tried to run the explicit update when it's saved. My
validator return the ValidateError. But this time, I can't see the error
since the above handleChange function didn't trigger. My problem is how
can I trigger this function even without make any change on the control.
Please help. Thanks

Best Rdgs
Ellis
Re: [DataBinding]Problem on ValidateStatus Change Listener [message #328075 is a reply to message #328049] Tue, 13 May 2008 18:34 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Ellis Yu wrote:
> Here's my code. I based on author fahrmeyer's FormManager to build.
> Thanks his coding here and save me lots of time :-)
> private void connectBindingToMessageManager(final Control control,
> final DataBindingContext dbc, final Binding binding) {
>
> binding.init(dbc);

Binding.init() is already being called in the
DataBindingContext.bindValue() method--unless you're doing something
exotic there should be no need for you to call it explicitly.

>
> binding.getValidationStatus().addChangeListener(new
> IChangeListener() {
>
> public void handleChange(final ChangeEvent event) {
> IObservableValue validationStatus = (IObservableValue)
> event
> .getSource();
> IStatus bindStatus = (IStatus) validationStatus.getValue();
> // erzeugt einen eindeutigen Schluessel fuer das
> Control, das
> // wird im MessageManager
> // als message key verwendet, damit die nachrichten fuer
> // einzelne Controls auseinander
> // gehalten werden koennen
> String msgKey = createMsgKey(control);
> int errorCode = IMessageProvider.NONE;
>
> if (bindStatus.getSeverity() == IStatus.ERROR) {
> errorCode = IMessageProvider.ERROR;
>
> } else if (bindStatus.getSeverity() == IStatus.WARNING) {
> errorCode = IMessageProvider.WARNING;
>
> } else if (bindStatus.getSeverity() == IStatus.INFO) {
> errorCode = IMessageProvider.INFORMATION;
> }
>
> if (errorCode != IMessageProvider.NONE) {
> mMessageManager.addMessage(msgKey,
> bindStatus.getMessage(),
> null, errorCode, control);
> }
> }
>
> });
>
> binding.getValidationStatus().addValueChangeListener(
> new IValueChangeListener() {
>
> public void handleValueChange(final ValueChangeEvent
> event) {
> // das ValueChangeevent lieft den alten und
> neuen Wert
>
> ValueDiff diff = event.diff;
> IStatus oldValue = (IStatus) diff.getOldValue();
> IStatus newValue = (IStatus) diff.getNewValue();
>
> if (oldValue.getSeverity() != IStatus.OK
> && newValue.getSeverity() == IStatus.OK) {
> String msgKey = createMsgKey(control);
> mMessageManager.removeMessage(msgKey, control);
> }
>
> }
>
> });
> }
>
> Supposing there's ValidationError in my validator. The below coding
> should be triggered and show the error in the MessageManager. I'm
> checking my text control whether is blank or not when it's lost focus.
> It's fine if I delete the text in the control and error shown when I
> leave the control.
>
> However if the control is blank in the start and I didn't modify
> anything in the field. I tried to run the explicit update when it's
> saved. My validator return the ValidateError. But this time, I can't see
> the error since the above handleChange function didn't trigger. My
> problem is how can I trigger this function even without make any change
> on the control. Please help. Thanks

It seems you're trying to avoid updating the validation on every
keystroke. There is a new way to do this as of the 3.4 release cycle
(DataBinding from 3.4 is backward compatible to SWT/JFace 3.3 if that's
a concern):

IObservableValue myModelValue = ...
dbc.bindValue(SWTObservables.observeDelayedValue(400,
SWTObservables.observeText(textWidget, SWT.Modify)),
myModelValue,
null,
null);

In this manner, the target observable doesn't fire a change event until
400ms (or whatever delay you specify) after the user stops typing.

Does this help?

Matthew
Re: [DataBinding]Problem on ValidateStatus Change Listener [message #328103 is a reply to message #328075] Wed, 14 May 2008 03:38 Go to previous message
Ellis Yu is currently offline Ellis YuFriend
Messages: 15
Registered: July 2009
Junior Member
Hi Matthew,

Thanks for your prompt reply. I'm newbie on the databinding. Firstly,
I want to make sure my understanding is correct. In
binding.getValidationStatus().addChangeListener, it will keep track on the
validationStatus. It'll be triggered, if I raise any validationError. Am I
right?

Second is my exact problem about to run the validation when I click on
the "save" button. The validation is ok if i delete the text value in the
control and error msg is shown when the control is out of focus. But
problem is if the control never get focus and user click on "save" button,
I also want the validator to check whether the control contain value or
not before update on database.

Thus in the start of doSave function, I force the binding context to
updateModels and hope the validator will check it again. I found the
validator had returned the ValidationStatus.error. But the
addChangeListener didn't get triggered. Suppose it will be triggered when
I return the error, and show it in MessageManager. Below is my coding on
doSave.


@Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
manager.getDataBindingContext().updateModels();

isError = false;

if (manager.getMessagesCount() > 0) {
return;
}

monitor.beginTask("Start Saving...", 3);
try {
if (action == Action.UPDATE) {
userSrv.save(user);
monitor.worked(2);
tableViewer.refresh(user);

} else if (action == Action.NEW) {
userSrv.create(user);
monitor.worked(2);
tableViewer.add(user);
((List) tableViewer.getInput()).add(user);
}
monitor.worked(3);
;
} catch (RuntimeException ex) {
isError = true;

Status status = new Status(IStatus.ERROR, this.ID, 0, ex
.getMessage(), ex);
// Display the dialog
ErrorDialog.openError(Display.getCurrent().getActiveShell(),
"Fail on update", ex.getCause().getMessage(), status);
} finally {
monitor.done();
if (isDirty && !isError) {
isDirty = false;
firePropertyChange(IEditorPart.PROP_DIRTY);
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().closeEditor(this, true);
IActionBars bars = getEditorSite().getActionBars();
bars.getStatusLineManager().setMessage("Sucessfully Saved");

}
}
}


Is there anything I doing wrong?

Best Rdgs
Ellis
Previous Topic:Launch an external program
Next Topic:jface databinding snippet 13
Goto Forum:
  


Current Time: Mon Nov 11 08:36:16 GMT 2024

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

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

Back to the top