Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TitleAreaDialog using databinding validation to disable the OK button(OK button remains enabled while invalid data has been entered.)
TitleAreaDialog using databinding validation to disable the OK button [message #647707] Fri, 07 January 2011 17:18 Go to next message
Martin  is currently offline Martin
Messages: 3
Registered: January 2011
Junior Member
Hi Folks,

I needed to create a few simple dialogs with text fields to allow users to input data. Examples are like inputing a Persons details like forename, surname, age, address etc. The dialogs just have Ok and Cancel buttons.

Extending TitleAreaDialog I was able to create a dialog laid out as required, the validation I've done myself, disabling the OK button for erroneous data entries. Now I'd like to use JFace databinding on the dialog but have seen that the TitleAreaDialogSupport does not disable the OK button when a validation error occurs, it just shows up the error message.

I have tried using a WizardDialog with one page and changing the name of the Finish button to OK. This works but is surely not the way to go for my simple common use case.

I think it may be possible to extend TitleAreaDialogSupport to also disable the OK button. However the lack of OK button disablement on validation failure is such an obvious omission from TitleAreaDialogSupport that I think I'm missing the point somewhere. Maybe I should not be using TitleAreaDialog at all whenever validation is needed.

This is such a common GUI use case I wanted to check that I'm not doing more work than necessary.

thanks,
Martin.
Re: TitleAreaDialog using databinding validation to disable the OK button [message #647721 is a reply to message #647707] Fri, 07 January 2011 22:36 Go to previous messageGo to next message
Animesh Kumar is currently offline Animesh Kumar
Messages: 79
Registered: September 2010
Location: Bangalore
Member
Hi,
It is fine if you are using TitleAreaDialog. You just have to add enabling and disabling of Ok button in case of validations. You can do one simple thing, and that is, adding ModifyListeners and then enabling and disabling okButton in it. For eg.

firstNameText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validate();
			}
});


In validate() method, you can check for validation and if there is an invalid Text, you can do the following:-
setErrorMessage("Invalid Input");
okButton.setEnabled(false); 


And if the text is valid, then:-
setErrorMessage(null);
okButton.setEnabled(true);


Hope this helps.
Regards,
Animesh


Regards,
Animesh
Re: TitleAreaDialog using databinding validation to disable the OK button [message #647745 is a reply to message #647721] Sat, 08 January 2011 09:29 Go to previous messageGo to next message
Martin  is currently offline Martin
Messages: 3
Registered: January 2011
Junior Member
Hi Folks,

Using JFace databinding on a WizardDialog gives the functionality that I need without adding the ModifyListeners that you suggest. Therefore I think that I should not have to do much to get the same working for TitleAreaDialog.

Can JFace Databinding be used to disable the OK button on a TitleAreaDialog, as it does for the Finish button of a WizardDialog?

I've two solutions.
1. Just use a WizardDialog with one page, rename the Finish button to 'OK' and try to get it looking like a TitleAreaDialog.

2. Use the TitleAreaDialog and somehow update the TitleAreaDialogSupport to also disable/enable the OK button.

I might have to pick one of these two options and got with it, but I'm sure that there is a better solution.

thanks,
Martin

Re: TitleAreaDialog using databinding validation to disable the OK button [message #647768 is a reply to message #647707] Sat, 08 January 2011 13:06 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5256
Registered: July 2009
Senior Member
http://tomsondev.bestsolution.at/2011/01/08/enhanced-rcp-usa ge-of-titlearedialogsupport/
Am 07.01.11 23:18, schrieb Martin:
> Hi Folks,
>
> I needed to create a few simple dialogs with text fields to allow users
> to input data. Examples are like inputing a Persons details like
> forename, surname, age, address etc. The dialogs just have Ok and
> Cancel buttons.
>
> Extending TitleAreaDialog I was able to create a dialog laid out as
> required, the validation I've done myself, disabling the OK button for
> erroneous data entries. Now I'd like to use JFace databinding on the
> dialog but have seen that the TitleAreaDialogSupport does not disable
> the OK button when a validation error occurs, it just shows up the error
> message.
>
> I have tried using a WizardDialog with one page and changing the name of
> the Finish button to OK. This works but is surely not the way to go for
> my simple common use case.
>
> I think it may be possible to extend TitleAreaDialogSupport to also
> disable the OK button. However the lack of OK button disablement on
> validation failure is such an obvious omission from
> TitleAreaDialogSupport that I think I'm missing the point somewhere.
> Maybe I should not be using TitleAreaDialog at all whenever validation
> is needed.
> This is such a common GUI use case I wanted to check that I'm not doing
> more work than necessary.
>
> thanks,
> Martin.
Re: TitleAreaDialog using databinding validation to disable the OK button [message #647799 is a reply to message #647768] Sun, 09 January 2011 06:29 Go to previous message
Martin  is currently offline Martin
Messages: 3
Registered: January 2011
Junior Member
Thanks Tom,

I've gone with the solution below. I'll got to yours if I find problems with the code below later.

many thanks,
Martin.



public class TitleDialog extends TitleAreaDialog {

    private static final Logger log = Logger.getLogger(TitleDialog.class);
    public static final String title = "Enter the values for Stock Code and Name.";

    private String message = null;
    private int messageType;

    public TitleDialog(Shell parentShell) {
        super(parentShell);
        log.debug("TitleDialog constructed");
    }
   
            
    @Override
    public void setErrorMessage(String newErrorMessage) {
        Button okButton = getButton(IDialogConstants.OK_ID);
        if (okButton != null) 
            okButton.setEnabled(newErrorMessage == null);
        super.setErrorMessage(newErrorMessage);
    }
    @Override
    public void setMessage(String newMessage, int type){
        log.debug("setMessage called with '" + newMessage + "'");
        if (this.message == null){
            this.message = newMessage;/// Done just once
            this.messageType = type;
        }
        if (newMessage == null && this.message != null){
            newMessage = this.message;
            type = this.messageType;
        }
        super.setMessage(newMessage, type);
    }
}
Previous Topic:CheckedTreeSelectionDialog and ICheckStateProvider issue
Next Topic:[DataBinding] add non-model intermediary elements with ObservableListTreeContentProvider
Goto Forum:
  


Current Time: Wed Jul 30 03:08:13 EDT 2014

Powered by FUDForum. Page generated in 0.01907 seconds