Home » Eclipse Projects » Eclipse Platform » [DataBinding]Problem on ValidateStatus Change Listener 
| [DataBinding]Problem on ValidateStatus Change Listener [message #327942] | 
Thu, 08 May 2008 22:27   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 03:52    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 14:34    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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] | 
Tue, 13 May 2008 23:38   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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: Tue Nov 04 10:19:11 EST 2025 
 Powered by  FUDForum. Page generated in 0.04565 seconds  
 |