Eclipse插件开发-自定义向导

自定义向导

向导一般是指新建工程文件时所用到的界面,在Eclipse插件中用来实现向导功能的扩展点是  org.eclipse.ui.newWizards ,具体实现如下:

一、新建一个插件工程,扩展 org.eclipse.ui.newWizards 扩展点

Eclipse插件开发-自定义向导_第1张图片

二、TestWizard.java 用来实现具体的向导界面和逻辑,该类必须实现INewWizard接口

package com.myplugin.test.ui.wizard;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

public class TestWizard extends Wizard implements INewWizard {

	private TestWizardPage1 page1;
	private TestWizardPage2 page2;

	public TestWizard() {
		setWindowTitle("新建Test工程");
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// selection 表示资源视图中当前选中的元素
	}

	@Override
	public void addPages() {
		page1 = new TestWizardPage1(); // 向导中的第一个页面
		this.addPage(page1);

		page2 = new TestWizardPage2(); // 向导中的第二个页面
		this.addPage(page2);
	}

	@Override
	public boolean performFinish() {
		MessageDialog.openInformation(this.getShell(), "提示", page1.getProjectName() + "工程新建完成!");
		return true;
	}
}

三、TestWizardPage1.java 类实现具体的向导页面(TestWizardPage2.Java 类似)

package com.myplugin.test.ui.wizard;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class TestWizardPage1 extends WizardPage {
	private Text textName;

	public TestWizardPage1() {
		super("新建Test工程");
		setTitle("新建Test工程");
		setDescription("输入Test工程的基本信息");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		setControl(container);
		container.setLayout(new GridLayout(2, false));
		Label labelName = new Label(container, SWT.NONE);
		labelName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		labelName.setText("工程名");

		textName = new Text(container, SWT.BORDER);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if (!textName.getText().isEmpty()) {
					setPageComplete(true);
				} else {
					setPageComplete(false);
				}
			}
		});
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}

	@Override
	public boolean isPageComplete() {
		return this.textName.getText().isEmpty() ? false : true;
	}

	public String getProjectName() {
		return textName.getText();
	}
}

四、运行效果

Eclipse插件开发-自定义向导_第2张图片

 Eclipse插件开发-自定义向导_第3张图片

你可能感兴趣的:(Eclipse插件,RCP开发,java)