Home > eclipse rcp, Jface > Inline Validations in Eclipse RCP – Field Assists

Inline Validations in Eclipse RCP – Field Assists

Inline validations has been there quite a while now , and in eclipse rcp applications too we can achieve that using control decorators.
What are we going to see in this post is a control decoration , which helps to notify the user if somethings goes wrong when some action is triggered on a control.
Let us consider a real time example , let us take a pincode text field.
If an user is typing in a non-numeric field , then an error icon should be shown to the user.
Let us create a text field on which we ll be adding the decoration.


Label label = new Label(parent, SWT.NONE);
label.setText("Please Enter Pincode:");
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		
Text  txtPincode = new Text(parent , SWT.NONE);
txtPincode.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		
//Adding the decorator
final ControlDecoration txtDecorator = new ControlDecoration(txtPincode, SWT.TOP|SWT.RIGHT);
FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry .DEC_ERROR);
Image img = fieldDecoration.getImage();
txtDecorator.setImage(img);
txtDecorator.setDescriptionText("Pls enter only numeric fields");
// hiding it initially
txtDecorator.hide();
		
txtPincode.addKeyListener(new KeyAdapter() {
			
  @Override
 public void keyReleased(KeyEvent e) {
     Text text = (Text)e.getSource();			
     String string = text.getText();
     char[] chars = new char[string.length()];
     string.getChars(0, chars.length, chars, 0);
      for (int i = 0; i < chars.length; i++)
        {
            if (!('0' <= chars[i] && chars[i] <= '9'))
            {
           	txtDecorator.show();
            }
            else
            {
            	txtDecorator.hide();
            }

        }
		       
}
});
		

Sample screenshot:

On a short note just giving a quick reference for decorators here.
Decorators are often used for other purposes with views also.
The above reference link and this eclipse article will help you cover that.

Happy coding… 🙂

Advertisements
Categories: eclipse rcp, Jface Tags: ,
  1. February 20, 2013 at 11:51 am

    you really saved my day…:):)..thank u

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: