這一節主要介紹如何通過新建向導,來新建我們擴展的文件(.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
這樣運行程序,效果如圖














category屬性指定組標識
class屬性向導對應的類
icon屬性指定向導顯示的圖標
id屬性指定向導的標識,應該唯一
name屬性指定向導顯示的名稱
接下來我們定義向導類,由于向導中包含一個或者多個向導頁,根據實際情況而定,所以我們還要定義向導頁,代碼如下:
向導類






























向導頁類

























































































































同時在插件依賴項中增加 org.eclipse.core.resources
這樣運行程序,效果如圖

