流程设计器开发八(新建向导部分)
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
这样运行程序,效果如图
<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
这样运行程序,效果如图
相关文章推荐
- 流程设计器开发一(模型部分)
- 流程设计器开发二(编辑器部分)
- 流程设计器开发三(策略和命令部分)
- 流程设计器开发四(改变活动的位置部分)
- 流程设计器开发五(转移的增加和删除部分)
- 流程设计器开发五(拐点部分)
- 流程设计器开发六(大纲视图部分)
- 流程设计器开发一(控制器和视图部分)
- 流程设计器开发七(属性页部分)
- 使用模型驱动开发和基于模式的工程来设计 SOA之第 4 部分
- 新产品设计开发流程《射频电路设计 第二版》《RF Circuit Design》 (Richard Chi Hsi Li)
- 互联网项目开发里程碑及各阶段交付资料 郑澍璋 郑澍璋 3 个月前 互联网项目的开发有一套专业的设计和开发流程,知乎上关于这方面的文章也有很多,但大多数文章偏专业性。以下以匠邦互联网公司内部的开发流程为
- 对Java开发的思考(代码组织、设计模式、开发流程)
- ASIC开发设计流程
- SharePoint无代码工作流设计开发实例——交通费报销流程(二)
- SharePoint无代码工作流设计开发实例——交通费报销流程(三)
- [SharePoint 工作流] 如何设计一个通用的多级多审核工作流程(三):工作流开发
- 分布式集群系统的设计开发--设计部分
- SOA 术语概述,第 2 部分: 开发流程、模型和资产
- 详解设计测试驱动开发TDD技术总体流程