public class FilterWidget {
protected IFilterListener filterListener;
protected Composite container;


protected Label label;
protected Text text;

public void setFilterListener(IFilterListener filterListener){
this.filterListener= filterListener;
}

public Composite create(Composite parent, FormToolkit toolkit) {
container = toolkit.createComposite(parent);
GridLayout gl = new GridLayout();
gl.marginWidth = 0;
gl.marginHeight = 0;
gl.verticalSpacing = 0;
container.setLayout(gl);

label = toolkit.createLabel(container, “Filter (?=any character, *=any String).”);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
text = toolkit.createText(container, “*”);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
text.addKeyListener(new KeyAdapter() {

public void keyReleased(KeyEvent e) {
if(e.keyCode == SWT.ARROW_DOWN) {
// set the focus on the next control
}
}
});

text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
filterChanged();
}
});
return container;
}

protected void filterChanged() {
String filter = text.getText().trim();
if(“”.equals(filter)) {
filter = “*”;
}
if(filter.endsWith(“<“)) {
filter = filter.substring(filter.length() – 1);
}else if(“”.equals(filter) == false && filter.endsWith(“*”) == false) {
filter = filter + “*”;
}
// convert the pattern syntax into something palattable
filter = filter.replaceAll(“\\.”, “\\\\.”);
filter = filter.replaceAll(“\\*”, “.*”);
filter = filter.replaceAll(“\\?”, “.?”);
Pattern p = Pattern.compile(filter, Pattern.CASE_INSENSITIVE);
filterListener.filterChanged(p);
}

public void dispose() {
label.dispose();
text.dispose();
container.dispose();
}
}
} public interface IFilterListener {
} public void filterChanged(Pattern pattern);
}
}}

Advertisements