這一節主要介紹如何通過建立嚮導,來建立我們擴充的檔案(.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這樣運行程式,效果