org.eclipse.swt.widgets.Text validation decorator in eclipse SWT
Asked Answered
E

2

5

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

Epictetus answered 30/5, 2013 at 7:45 Comment(2)
What do you mean with "custom APIs"? SWT isn't even part of default Java.Kohima
I mean using only SWT. Not some custom made libraries, like here: eclipse.dzone.com/articles/adding-swt-input-validation-ea .Epictetus
E
11

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

Epictetus answered 30/5, 2013 at 9:0 Comment(0)
E
5

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.

Embryotomy answered 21/4, 2015 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.