Home » Eclipse Projects » Eclipse Platform » [DataBinding]Problem on ValidateStatus Change Listener
| |
Re: [DataBinding]Problem on ValidateStatus Change Listener [message #328049 is a reply to message #328020] |
Tue, 13 May 2008 07:52 ![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) |
Ellis Yu![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=ellisyu%40sinaman.com) 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 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 |
|
|
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 Go to previous message](theme/Solstice/images/up.png) |
Ellis Yu![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=ellisyu%40sinaman.com) 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
|
|
|
Goto Forum:
Current Time: Wed Sep 04 21:26:16 GMT 2024
Powered by FUDForum. Page generated in 0.03601 seconds
|