⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eclipsefieldeditorfactory.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
字号:
package net.sf.component.config;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.util.StringUtil;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class EclipseFieldEditorFactory implements IConfigItemEditor{
	public String key;
	public IPreferenceStore ps;
	public FieldEditor fe;
	private Composite parent;
	@Override
	public void init(Composite parent, String configKey, String configPromp, String defaultValue,Map<String, String> params) {
		this.key=configKey;
		this.parent=parent;
		//获取类型定义
		String editorType="String";
		if(params != null  && params.get("type") != null) editorType=params.get("type");
		
		ps=new PreferenceStore();
		ps.putValue(key, defaultValue);
		
		//依类型构造editor
		if("Directory".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			fe=new DirectoryFieldEditor(configKey,"",composite);
			((DirectoryFieldEditor)fe).setChangeButtonText("浏览...");
			deleteOtherLabel(composite);
		}else if("File".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			fe=new FileFieldEditor(configKey,"",composite);
			((FileFieldEditor)fe).setChangeButtonText("浏览...");
			deleteOtherLabel(composite);
		}else if("Integer".equals(editorType)){
			fe=new IntegerFieldEditor(configKey,configPromp,parent);
			//只允许输入数字
			((IntegerFieldEditor)fe).getTextControl(parent).addVerifyListener(new VerifyListener() {
                public void verifyText(VerifyEvent e) {
                    Pattern pattern = Pattern.compile("[0-9]\\d*");
                    Matcher matcher = pattern.matcher(e.text);
                    if(matcher.matches()) { //numberical
                        e.doit = true;
                    }else if(e.text.length() > 0){ //character
                        e.doit = false;
                    }else { //control key
                        e.doit = true;
                    }
                }
            });
		}else if("Boolean".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			fe=new BooleanFieldEditor(configKey,"",composite);
		}else if("RadioGroup".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			//生成合乎要求的数组
			String[] candidates=StringUtil.toArray(params.get("candidates"));
			String[][] ss=new String[candidates.length][2];
			for(int i=0;i<candidates.length;i++){
				ss[i][0]=candidates[i];
				ss[i][1]=candidates[i];
			}
			fe=new RadioGroupFieldEditor(configKey,"",3,ss,composite);
			deleteOtherLabel(composite);
		}else if("StringList".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			fe=new DialogStringListEditor(configKey,configPromp,composite);
			deleteOtherLabel(composite);
		}else if("StringMap".equals(editorType)){
			createCustomLabel(parent, configPromp);
			Composite composite = createOtherParent(parent);
			fe=new DialogStringMapEditor(configKey,configPromp,params,composite);
			deleteOtherLabel(composite);
		}else{//String
			fe=new StringFieldEditor(configKey,configPromp,parent);
		}
		
		fe.setPreferenceStore(ps);
		fe.load();
	}

	//去掉自带的label控件,以美观定位
	private void deleteOtherLabel(Composite composite) {
		fe.getLabelControl(composite).dispose();
	}

	//创建一层父容器,便于布局
	private Composite createOtherParent(Composite parent) {
		Composite composite = new Composite(parent,SWT.NULL);
		GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
		return composite;
	}

	//使用自定义的label,便于布局
	private void createCustomLabel(Composite parent, String configPromp) {
		Label dummy=new Label(parent,SWT.NULL);
		dummy.setText(configPromp);
	}
	
	@Override
	public Map<String, String> getValues() {
		try{
			fe.store();
		}catch(Exception ex){
			;//可能会有如整型格式不对等错误
		}
		HashMap<String,String> hashMap = new HashMap<String,String>();
		hashMap.put(key, ps.getString(key));
		return hashMap;
	}

	@Override
	public Text getTextControl() {
		if(fe.getClass().equals(StringFieldEditor.class))
			return ((StringFieldEditor)fe).getTextControl(parent);
		return null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -