插件开发中的定制单选下拉框

本文介绍了在插件开发中如何实现一个具有双重功能的下拉框:既可以选取预设选项,又允许用户自定义并选择新选项。通过修改cellBox的源代码,成功创建了一个满足此类需求的定制化单选下拉框。
摘要由CSDN通过智能技术生成

     

有时候在插件开发的时候需要做到以下功能的下拉框:

         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>
   效果图如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值