有时候在插件开发的时候需要做到以下功能的下拉框:
1.可以选择已有的初始化选项;
2.添加用户自定义的选项并选择;
要实现以上功能,可以修改cellBox的源代码,获得定制的具有以上功能的下拉框:
<span style="font-size:14px;">package com.benli.editor.cellboxedit;
import java.text.MessageFormat;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
public class ComboBoxCellEditorFactory extends CellEditor {
private String[] items;
int selection;
private String result;
CCombo comboBox;
private static final int defaultStyle = SWT.NONE;
public ComboBoxCellEditorFactory() {
setStyle(defaultStyle);
}
public ComboBoxCellEditorFactory(Composite parent, String[] items) {
this(parent, items, defaultStyle);
}
public ComboBoxCellEditorFactory(Composite parent, String[] items, int style) {
super(parent, style);
setItems(items);
}
public ComboBoxCellEditorFactory(Composite parent, List<String> list, int style) {
super(parent, style);
String[] items=new String[list.size()];
for(int i=0;i<list.size();i++){
items[i]=list.get(i);
}
setItems(items);
}
public String[] getItems() {
return this.items;
}
public void setItems(String[] items) {
Assert.isNotNull(items);
this.items = items;
populateComboBoxItem<strong>s</strong>()<strong>;
</strong>}
protected Control createControl(Composite parent) {
comboBox = new CCombo(parent, getStyle());
comboBox.setFont(parent.getFont());
populateComboBoxItems();
comboBox.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
keyReleaseOccured(e);
}
});
comboBox.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
editOccured(e);
}
});
comboBox.addSelectionListener(new SelectionAdapter() {
public void widgetDefaultSelected(SelectionEvent event) {
applyEditorValueAndDeactivate();
}
public void widgetSelected(SelectionEvent event) {
selection = comboBox.getSelectionIndex();
result=items[selection];
}
});
comboBox.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_ESCAPE
|| e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false;
}
}
});
comboBox.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
ComboBoxCellEditorFactory.this.focusLost();
}
});
return comboBox;
}
protected void editOccured(ModifyEvent e) {
result = comboBox.getText();
}
protected Object doGetValue() {
return result;
}
protected void doSetFocus() {
comboBox.setFocus();
}
public LayoutData getLayoutData() {
LayoutData layoutData = super.getLayoutData();
if ((comboBox == null) || comboBox.isDisposed()) {
layoutData.minimumWidth = 60;
} else {
// make the comboBox 10 characters wide
GC gc = new GC(comboBox);
layoutData.minimumWidth = (gc.getFontMetrics()
.getAverageCharWidth() * 10) + 10;
gc.dispose();
}
return layoutData;
}
protected void doSetValue(Object value) {//TODO
if(isExist(value, comboBox.getItems())) return;//修改doSetValue(<span style="font-family: FangSong_GB2312;">Object value</span><span style="font-family: FangSong_GB2312;">)方法</span>
comboBox.add((String) value);
items=comboBox.getItems();
comboBox.select(1);
}
private void populateComboBoxItems() {
if (comboBox != null && items != null) {
comboBox.removeAll();
for (int i = 0; i < items.length; i++) {
comboBox.add(items[i], i);
}
setValueValid(true);
selection = 0;
}
}
void applyEditorValueAndDeactivate() {
// must set the selection before getting value
selection = comboBox.getSelectionIndex();
Object newValue = doGetValue();
markDirty();
boolean isValid = isCorrect(newValue);
setValueValid(isValid);
if (!isValid) {
// Only format if the 'index' is valid
if (items.length > 0 && selection >= 0 && selection < items.length) {
// try to insert the current value into the error message.
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[] { items[selection] }));
} else {<strong>
</strong>// Since we don't have a valid index, assume we're using an
// 'edit'
// combo so format using its text value
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[] { comboBox.getText() }));
}
}
fireApplyEditorValue();
deactivate();
}
protected void focusLost() {
if (isActivated()) {
applyEditorValueAndDeactivate();
}
}
protected void keyReleaseOccured(KeyEvent keyEvent) {
if (keyEvent.character == '\u001b') { // Escape character
fireCancelEditor();
} else if (keyEvent.character == '\t') { // tab key
applyEditorValueAndDeactivate();
}
}
protected boolean isExist(Object content,String[] items) {
boolean isExist=false;
for(int i=0;i<items.length;i++){
if(items[i].equals(content)){
isExist=true;
}
}
return isExist;
}
}</span>
效果图如下图: