eclipse SWT中的org.eclipse.swt.widgets.Text验证装饰器

DrK*_*liN 5 java swt eclipse-plugin

为Text/Combo变量启用这样的装饰器的基本方法(没有自定义API)是什么? 在此输入图像描述

DrK*_*liN 10

以下是我设法做到的方法:

        //---> 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();
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

DEC_ERROR用于FieldDecorationRegistry设置错误图标.

在此输入图像描述


Yur*_*riy 5

JFace 数据绑定将自动为您修饰无效输入:

 ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);
Run Code Online (Sandbox Code Playgroud)

这将用图标装饰控件并将工具提示文本设置为验证状态描述。在您的情况下,您不必手动处理所有这些修改侦听器。