setTextAndTip(new Label(parent, SWT.NONE), label, tip);
return newCCombo(parent, tip);
}
protected CCombo newCCombo(Composite parent, String tip) {
final CCombo cc = new CCombo(parent, SWT.FLAT | SWT.BORDER | SWT.READ_ONLY);
cc.setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
cc.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// without these next lines, the height of the ccombo is too small
// especially on a Mac
FontData [] fontData = cc.getFont().getFontData();
((GridData) cc.getLayoutData()).heightHint = 2 * fontData[0].getHeight();
cc.addListener(SWT.Selection, this);
cc.setToolTipText(tip);
cc.addKeyListener(new KeyListener() {
private final StringBuffer b = new StringBuffer();
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.BS) {
if (b.length() > 0)
b.deleteCharAt(b.length() - 1);
} else if (Character.isJavaIdentifierPart(e.character) || e.character == '.')
b.append(e.character);
else
return;
final String[] ccItems = cc.getItems();
final String partial = b.toString();
int iBefore = -1;
for (int i = 0; i < ccItems.length; i++) {
if (ccItems[i].startsWith(partial)) {
iBefore = i;
break;
}
}
if (iBefore >= 0)
cc.setText(cc.getItem(iBefore));
}
});
return cc;
}