插件向导开发最好的例子莫过于Eclipse中本身一些向导,但如何去找不易记住
一些常规界面组件,如:容器、类、项目、包等,可以参考以下类:
NewAnnotationWizardPage
NewClassWizardPage
NewContainerWizardPage
NewElementWizardPage
NewEnumWizardPage
NewInterfaceWizardPage
NewJavaProjectWizardPage
NewPackageWizardPage
NewTypeWizardPage
。。。。。。。。
这些类位于org.eclipse.jdt.ui.wizards包下,javadoc可参考 http://www.jdocs.com/eclipse/3.1/api-index.html?m=package&p=org.eclipse.core.expressions&render=classic
例子:
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.ui.wizards.NewContainerWizardPage;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import com.cownew.studio.Activator;
import com.cownew.studio.modelDev.codeGen.CodeGeneratorInfo;
import com.cownew.studio.modelDev.codeGen.CodeGeneratorLoader;
import com.cownew.studio.modelDev.codeGen.IEntityModelCodeGenerator;
public class CodeGenOptionWizardPage extends NewContainerWizardPage {
private final String SETTINGPREFIX = CodeGenOptionWizardPage.class.getName();
// 是否覆盖已有文件
private final String OVERRIDEEXISTS = SETTINGPREFIX + "OVERRIDEEXISTS";
// 上次选中的代码生成器索引
private final String CODEGENINDEX = SETTINGPREFIX + "CODEGENINDEX";
private Combo comboCodeGen;
private Button btnCheckBoxOverrideExistsFile;
private IStructuredSelection structSelect;
private CodeGeneratorInfo[] codeGenerators;
public CodeGenOptionWizardPage(String name, IStructuredSelection structSelect) {
super(name);
setTitle("code generation option");
this.structSelect = structSelect;
}
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite = new Composite(parent, SWT.NONE);
composite.setFont(parent.getFont());
int nColumns = 3;
GridLayout layout = new GridLayout();
layout.numColumns = 3;
composite.setLayout(layout);
Label label = new Label(composite, SWT.WRAP);
label.setText("please select the source folder to store the code");
GridData gd = new GridData();
gd.widthHint = convertWidthInCharsToPixels(60);
gd.horizontalSpan = 3;
label.setLayoutData(gd);
createContainerControls(composite, nColumns);
initContainerPage(getInitialJavaElement(structSelect));
createOptionControls(composite);
setControl(composite);
Dialog.applyDialogFont(composite);
}
private void createOptionControls(Composite composite) {
// 得到对话框设置
IDialogSettings settings = Activator.getDefault().getDialogSettings();
Label lTargetORM = new Label(composite, SWT.WRAP);
lTargetORM.setText("Target ORM:");
GridData gdLTargetORM = new GridData();
gdLTargetORM.horizontalSpan = 1;
gdLTargetORM.horizontalAlignment = GridData.FILL;
lTargetORM.setLayoutData(gdLTargetORM);
comboCodeGen = new Combo(composite, SWT.READ_ONLY);
// 取得所有代码生成器
codeGenerators = CodeGeneratorLoader.loadAll();
for (int i = 0, n = codeGenerators.length; i < n; i++) {
CodeGeneratorInfo codeGen = codeGenerators[i];
// 将代码生成器属性对象以代码生成器的索引为key
// 保存到控件的用户数据区中
comboCodeGen.setData(Integer.toString(i), codeGen);
// 添加项
comboCodeGen.add(codeGen.getName());
}
if (settings.get(CODEGENINDEX) == null) {
// 如果“上次选中的代码生成器索引”为空,则选择第一项
comboCodeGen.select(0);
} else {
// 如果“上次选中的代码生成器索引”不为空,则选择上次选中的项
int index = settings.getInt(CODEGENINDEX);
comboCodeGen.select(index);
}
GridData gdComboTargetORM = new GridData();
gdComboTargetORM.horizontalSpan = 2;
gdComboTargetORM.horizontalAlignment = GridData.FILL;
comboCodeGen.setLayoutData(gdComboTargetORM);
btnCheckBoxOverrideExistsFile = new Button(composite, SWT.CHECK);
btnCheckBoxOverrideExistsFile.setText("override existing file");
btnCheckBoxOverrideExistsFile.setSelection(true);
// 如果“是否覆盖已有文件”不为空,则将“是否覆盖已有文件”
// 选择框设置为和上次一样的设置
if (settings.get(OVERRIDEEXISTS) != null) {
boolean overwrite = settings.getBoolean(OVERRIDEEXISTS);
btnCheckBoxOverrideExistsFile.setSelection(overwrite);
}
GridData gdOverrideExists = new GridData();
gdOverrideExists.horizontalSpan = 3;
gdOverrideExists.horizontalAlignment = GridData.FILL;
btnCheckBoxOverrideExistsFile.setLayoutData(gdOverrideExists);
}
protected IStatus containerChanged() {
IStatus status = super.containerChanged();
updateStatus(status);
return status;
}
/**
* 得到用户选择的代码生成器的实例
*
* @return
*/
public IEntityModelCodeGenerator getCodeGenerator() {
CodeGeneratorInfo gen = codeGenerators[comboCodeGen.getSelectionIndex()];
String className = gen.getClassName();
try {
Class clz = Class.forName(className);
return (IEntityModelCodeGenerator) clz.newInstance();
} catch (ClassNotFoundException e) {
Activator.logException(e);
} catch (InstantiationException e) {
Activator.logException(e);
} catch (IllegalAccessException e) {
Activator.logException(e);
}
return null;
}
public boolean isOverrideExistsFile() {
return btnCheckBoxOverrideExistsFile.getSelection();
}
/**
* 保存用户本次的设置
*/
public void saveConfig() {
IDialogSettings settings = Activator.getDefault().getDialogSettings();
settings.put(OVERRIDEEXISTS, isOverrideExistsFile());
settings.put(CODEGENINDEX, comboCodeGen.getSelectionIndex());
}
}