首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。
扩展点【org.eclipse.ui.newWizards】
在【org.eclipse.ui.newWizards】中添加【wizard】,在【class】中指定向导页类,继承INewWizard
类。
DemoNewWizard
类
public class DemoNewWizard extends Wizard implements INewWizard {
private IWorkbench mWorkbench;
public DemoNewWizard() {
setWindowTitle("新建Demo");
}
@Override
public boolean performFinish() {
return false;
}
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
mWorkbench = workbench;
}
}
在菜单【File】->【New】->【Other…】
可以在菜单中调用代码直接打开
DemoNewWizard wizard = new DemoNewWizard();
wizard.init(mViewPart.getSite().getWorkbenchWindow().getWorkbench(), null);
WizardDialog dialog = new WizardDialog(mViewPart.getSite().getShell(), wizard);
dialog.open();
向导页一般都继承WizardPage
类
setTitle()
方法设置标题setDescription()
方法设置描述信息createControl()
方法创建界面setMessage()
方法设置消息setErrorMessage()
方法设置错误消息setPageComplete()
方法表示页面是否已经设置完成canFlipToNextPage()
方法控制向导页对话框中的【Next】按钮NewUserPage
类,设置用户名和年龄信息。
public class NewUserPage extends WizardPage implements IWizardPage {
private Text mNameText;
private Combo mCategoryCombo;
protected NewUserPage() {
super("NewUserPage");
setTitle("新建用户");
setDescription("设置注意事项");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
container.setLayout(new GridLayout(2, false));
setControl(container);
Group group = new Group(container, SWT.NONE);
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
group.setText("创建用户");
GridLayout groupLayout = new GridLayout(2, false);
groupLayout.marginTop = 10;
groupLayout.marginLeft = 10;
groupLayout.marginRight = 10;
group.setLayout(groupLayout);
GridData nameGd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
nameGd.widthHint = 40;
Label nameLabel = new Label(group, SWT.NONE);
nameLabel.setText("姓名:");
nameLabel.setLayoutData(nameGd);
mNameText = new Text(group, SWT.BORDER);
mNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
mNameText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
checkButtonStatus();
}
});
Label ageLabel = new Label(group, SWT.NONE);
ageLabel.setText("类别:");
ageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
mCategoryCombo = new Combo(group, SWT.BORDER|SWT.READ_ONLY);
mCategoryCombo.setItems(User.CATEGORY_ARRAY);
GridData categoryGd = new GridData(SWT.LEFT, SWT.CENTER, true, false);
categoryGd.widthHint = 60;
mCategoryCombo.setLayoutData(categoryGd);
mCategoryCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
checkButtonStatus();
}
});
checkButtonStatus();
}
private void checkButtonStatus() {
if (mNameText.getText() == null || mNameText.getText().isEmpty()) {
setErrorMessage("姓名不能为空");
setPageComplete(false);
return;
}
String name = mNameText.getText();
for (Object obj : UserManager.getInstance().getElements()) {
User user = (User)obj;
if (user.getName().equals(name)) {
setErrorMessage("该姓名已经存在");
setPageComplete(false);
return;
}
}
if (mCategoryCombo.getText() == null || mCategoryCombo.getText().isEmpty()) {
setErrorMessage("类别不能为空");
setPageComplete(false);
return;
}
setErrorMessage(null);
setPageComplete(true);
}
public String getName() {
return mNameText.getText();
}
public String getCategory() {
return mCategoryCombo.getText();
}
}
显示如下
UserBasicInfoPage
类,设置用户基本信息
public class UserBasicInfoPage extends WizardPage implements IWizardPage {
private Combo mSexComb;
private Text mAgeText;
protected UserBasicInfoPage() {
super("UserBasicInfoPage");
setTitle("用户基本信息");
setDescription("用户基本信息注意事项");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
final GridLayout containerLayout = new GridLayout(2, false);
container.setLayout(containerLayout);
setControl(container);
Group group = new Group(container, SWT.NONE);
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
group.setText("用户基本信息");
GridLayout groupLayout = new GridLayout(2, false);
groupLayout.marginTop = 10;
groupLayout.marginLeft = 10;
groupLayout.marginRight = 10;
group.setLayout(groupLayout);
GridData sexGd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
sexGd.widthHint = 40;
Label sexLabel = new Label(group, SWT.NONE);
sexLabel.setText("性别:");
sexLabel.setLayoutData(sexGd);
mSexComb = new Combo(group, SWT.BORDER|SWT.READ_ONLY);
mSexComb.setItems(new String[]{"男", "女"});
GridData gd = new GridData(SWT.LEFT, SWT.CENTER, true, false);
gd.widthHint = 60;
mSexComb.setLayoutData(gd);
mSexComb.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
checkButtonStatus();
}
});
Label ageLabel = new Label(group, SWT.NONE);
ageLabel.setText("年龄:");
ageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
mAgeText = new Text(group, SWT.BORDER);
mAgeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
mAgeText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
checkButtonStatus();
}
});
checkButtonStatus();
}
private void checkButtonStatus() {
if (mSexComb.getText() == null || mSexComb.getText().isEmpty()) {
setErrorMessage("性别不能为空");
setPageComplete(false);
return;
}
if (mAgeText.getText() == null || mSexComb.getText().isEmpty()) {
setErrorMessage("年龄不能为空");
setPageComplete(false);
return;
}
try {
Integer.parseInt(mAgeText.getText());
} catch (NumberFormatException e) {
setErrorMessage("年龄格式错误");
setPageComplete(false);
return;
}
setErrorMessage(null);
setPageComplete(true);
}
public boolean isMale() {
return mSexComb.getSelectionIndex() == 0;
}
public int getAge() {
return Integer.parseInt(mAgeText.getText());
}
}
显示如下
UserContractInfoPage
类,设置用户联系信息
public class UserContractInfoPage extends WizardPage implements IWizardPage {
private Text mPhoneText;
private Text mAddressText;
protected UserContractInfoPage() {
super("UserContractInfoPage");
setTitle("用户联系信息");
setDescription("用户联系信息注意事项");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
container.setLayout(new GridLayout(2, false));
setControl(container);
Group group = new Group(container, SWT.NONE);
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
group.setText("创建用户");
GridLayout groupLayout = new GridLayout(2, false);
groupLayout.marginTop = 10;
groupLayout.marginLeft = 10;
groupLayout.marginRight = 10;
group.setLayout(groupLayout);
GridData phoneGd = new GridData(SWT.LEFT, SWT.CENTER, false, false);
phoneGd.widthHint = 40;
Label phoneLabel = new Label(group, SWT.NONE);
phoneLabel.setText("手机号:");
phoneLabel.setLayoutData(phoneGd);
mPhoneText = new Text(group, SWT.BORDER);
mPhoneText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
mPhoneText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
checkButtonStatus();
}
});
Label addressLabel = new Label(group, SWT.NONE);
addressLabel.setText("住址:");
addressLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
mAddressText = new Text(group, SWT.BORDER);
mAddressText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
mAddressText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
checkButtonStatus();
}
});
checkButtonStatus();
}
private void checkButtonStatus() {
if (mPhoneText.getText() == null || mPhoneText.getText().isEmpty()) {
setErrorMessage("手机号不能为空");
setPageComplete(false);
return;
}
if (mAddressText.getText() == null || mAddressText.getText().isEmpty()) {
setErrorMessage("地址不能为空");
setPageComplete(false);
return;
}
setErrorMessage(null);
setPageComplete(true);
}
public String getPhone() {
return mPhoneText.getText();
}
public String getAddress() {
return mAddressText.getText();
}
}
按钮状态由DemoNewWizard
类控制
addPages()
方法添加向导页getStartingPage()
方法设置首页getPreviousPage()
方法设置当前页的前一页getNextPage()
方法设置当前页的后一页canFinish()
方法设置【Finish】按钮状态performFinish()
方法处理【Finish】按钮事件DemoNewWizard
类
public class DemoNewWizard extends Wizard implements INewWizard {
private IWorkbench mWorkbench;
private NewUserPage mNewUserPage;
private UserBasicInfoPage mBasicInfoPage;
private UserContractInfoPage mContractInfoPage;
public DemoNewWizard() {
setWindowTitle("新建用户");
}
@Override
public void addPages() {
super.addPages();
mNewUserPage = new NewUserPage();
addPage(mNewUserPage);
mBasicInfoPage = new UserBasicInfoPage();
addPage(mBasicInfoPage);
mContractInfoPage = new UserContractInfoPage();
addPage(mContractInfoPage);
}
@Override
public boolean canFinish() {
if (getContainer().getCurrentPage() == mNewUserPage
&& mNewUserPage.isPageComplete()) {
return true;
}
return super.canFinish();
}
@Override
public boolean performFinish() {
if (getContainer().getCurrentPage() == mNewUserPage) {
String name = mNewUserPage.getName();
String category = mNewUserPage.getCategory();
saveUser(name, category);
} else {
String name = mNewUserPage.getName();
String category = mNewUserPage.getCategory();
boolean sex = mBasicInfoPage.isMale();
int age = mBasicInfoPage.getAge();
String phone = mContractInfoPage.getPhone();
String address = mContractInfoPage.getAddress();
saveUserInfo(name, category, sex, age, phone, address);
}
return true;
}
private User saveUser(String name, String category) {
if (mWorkbench != null && mWorkbench.getActiveWorkbenchWindow() != null) {
IWorkbenchWindow window = mWorkbench.getActiveWorkbenchWindow();
try {
window.getActivePage().showView(
"com.plugin.blog.demo.view.DemoViewPart");
} catch (PartInitException e) {
e.printStackTrace();
}
}
User user = new User(name, category);
UserManager.getInstance().addObject(user);
return user;
}
private void saveUserInfo(String name, String category, boolean male,
int age, String phone, String address) {
User user = saveUser(name, category);
UserInfoManager manager = UserInfoManager.getInstance(name);
UserBasicInfo basicInfo = (UserBasicInfo) manager.getUserInfo(0);
basicInfo.setMale(male);
basicInfo.setAge(age);
UserContractInfo contractInfo = (UserContractInfo) manager.getUserInfo(1);
contractInfo.setPhone(phone);
contractInfo.setAddress(address);
manager.doSave(null);
if (mWorkbench != null && mWorkbench.getActiveWorkbenchWindow() != null) {
IWorkbenchWindow window = mWorkbench.getActiveWorkbenchWindow();
try {
DemoEditorInput input = new DemoEditorInput(user);
window.getActivePage().openEditor(input,
"com.plugin.blog.demo.edit.DemoEditorPart");
} catch (PartInitException e) {
e.printStackTrace();
}
}
}
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
mWorkbench = workbench;
}
}
相关文章
Java Plug-in(一) 创建插件项目
Java Plug-in(二) 菜单
Java Plug-in(三) 视图
Java Plug-in(四) 视图菜单
Java Plug-in(五) 编辑器
Java Plug-in(六) 编辑器菜单
Java Plug-in(七) 透视图
Java Plug-in(八) 向导页
Java Plug-in(九) 首选项
Java Plug-in(十) 自定义扩展点
Java Plug-in(十一) 创建RCP项目