Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.eclipse.swt.widgets.Text validation decorator in eclipse SWT

What would be the basic way (without custom APIs) of enabling such a decorator for a Text / Combo variable? enter image description here

like image 741
DrKaoliN Avatar asked May 30 '13 07:05

DrKaoliN


2 Answers

Here's how I managed to do it:

        //---> input        
        myPackage = new Text(grpConnection, SWT.BORDER);
        myPackage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        //---> input event
        myPackage.addModifyListener(new ModifyListener(){
            // decorator for UI warning
            ControlDecoration decorator;

            /*
             * In this anonymous constructor we will initialize what needs to be initialized only once, namely the decorator.
             */
            {
                decorator = new ControlDecoration(myPackage, SWT.CENTER);
                decorator.setDescriptionText("Not a valid package");
                Image image = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage();
                decorator.setImage(image);
            }

            @Override
            public void modifyText(ModifyEvent e) {
                if (true) { // place your condition here
                    decorator.show();
                }
                else {
                    decorator.hide();
                }
            }
        });

The DEC_ERROR for FieldDecorationRegistry sets the error icon.

enter image description here

like image 172
DrKaoliN Avatar answered Oct 05 '22 23:10

DrKaoliN


JFace databinding will automatically decorate the invalid inputs for you:

 ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);

This will decorate the control with icon and set the tooltip text to the validation status description. In your case you will not have to handle all those modify listeners manually.

like image 38
Yuriy Avatar answered Oct 06 '22 00:10

Yuriy