您的位置:首页 > 其它

流程设计器开发八(新建向导部分)

2007-03-22 16:09 417 查看
这一节主要介绍如何通过新建向导,来新建我们扩展的文件(.workflow),要在新建增加内容,必须扩展org.eclipse.ui.newWizards,因此我们要修改plugin.xml文件,增加内容如下:
<extension
point="org.eclipse.ui.newWizards">
<category
id="com.example.workflow"
name="Workflow"/>
<wizard
category="com.example.workflow"
class="com.example.workflow.wizard.FileNewWizard"
icon="src/com/example/workflow/icons/file.gif"
id="com.example.workflow.filewizard"
name="Workflow文件 "
/>
</extension>
我们首先定义了一个组,组名是Workflow,组标识是com.example.workflow,组类似于文件夹的性质,然后定义了一个向导,各个属性的含义如下:
category属性指定组标识
class属性向导对应的类
icon属性指定向导显示的图标
id属性指定向导的标识,应该唯一
name属性指定向导显示的名称
接下来我们定义向导类,由于向导中包含一个或者多个向导页,根据实际情况而定,所以我们还要定义向导页,代码如下:
向导类
package com.example.workflow.wizard;

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

/**
* Create new new .workflow-file.
* Those files can be used with the WorkflowProcessEditor (see plugin.xml).
*/
public class FileNewWizard extends Wizard implements INewWizard{
//定义一个向导页
private FileCreationPage page1;
//给向导加上向导页
public void addPages() {
addPage(page1);
}
//在向导上点完成时,调用向导页的finish()方法。
public boolean performFinish() {
return page1.finish();
}
//向导初始化的时候,设置向导的标题,新建向导页
public void init(IWorkbench workbench, IStructuredSelection selection) {
setWindowTitle("Create Workflow File");
page1 = new FileCreationPage(workbench, selection);
}

}

向导页类
package com.example.workflow.wizard;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.ide.IDE;

import com.example.workflow.model.WorkflowProcess;

public class FileCreationPage extends WizardNewFileCreationPage{
//定义缺省的文件扩展名
private static final String DEFAULT_EXTENSION = ".workflow";
private final IWorkbench workbench;
private static int fileCount = 1;

/**
* Create a new wizard page instance.
* @param workbench the current workbench
* @param selection the current object selection
* @see ShapesCreationWizard#init(IWorkbench, IStructuredSelection)
*/
public FileCreationPage(IWorkbench workbench, IStructuredSelection selection) {
super("workflowCreationPage1", selection);
this.workbench = workbench;
//设置向导页的标题和描述
setTitle("Create a new " + DEFAULT_EXTENSION + " file");
setDescription("Create a new " + DEFAULT_EXTENSION + " file");
}

public void createControl(Composite parent) {
super.createControl(parent);
//设置向导页中的文件名
setFileName("workflowExample" + fileCount + DEFAULT_EXTENSION);
//设置向导的完成按钮是否可用
setPageComplete(validatePage());
}

/**
*判断文件的扩展名是否为workflow
* Return true, if the file name entered in this page is valid.
*/
private boolean validateFilename() {
if (getFileName() != null && getFileName().endsWith(DEFAULT_EXTENSION)) {
return true;
}
setErrorMessage("The 'file' name must end with " + DEFAULT_EXTENSION);
return false;
}

/* (non-Javadoc)
* @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validatePage()
*/
protected boolean validatePage() {
return super.validatePage()&& validateFilename();
}

/** Return a new WorkflowProcess instance. */
private Object createDefaultContent(){
return new WorkflowProcess();
}

/* 得到文件的初始内容
* @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#getInitialContents()
*/
protected InputStream getInitialContents() {
ByteArrayInputStream bais = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(createDefaultContent()); // argument must be Serializable
oos.flush();
oos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
} catch (IOException ioe) {
ioe.printStackTrace();
}
return bais;
}

/**
* This method will be invoked, when the "Finish" button is pressed.
* @see FileCreationWizard#performFinish()
*/
boolean finish(){
//create a new file, result != null if successful
IFile newFile = createNewFile();
fileCount++;

//在编辑器中打开新建的文件
IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
if (newFile != null && page != null) {
try {
IDE.openEditor(page, newFile, true);
} catch (PartInitException e) {
e.printStackTrace();
return false;
}
}
return true;
}

}

同时在插件依赖项中增加 org.eclipse.core.resources
这样运行程序,效果如图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: