What would be the basic way (without custom APIs) of enabling such a decorator for a Text / Combo variable?
org.eclipse.swt.widgets.Text validation decorator in eclipse SWT
Asked Answered
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
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.
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.
© 2022 - 2024 — McMap. All rights reserved.