流程設(shè)計器開發(fā)十(新建向?qū)Р糠郑?/a>
Posted on 2008-01-15 08:45 笑看人生 閱讀(1718) 評論(0) 編輯 收藏 所屬分類: Java插件開發(fā) 這一節(jié)主要介紹如何通過新建向?qū)В瑏硇陆ㄎ覀償U展的文件(.workflow),要在新建增加內(nèi)容,必須擴展org.eclipse.ui.newWizards,因此我們要修改plugin.xml文件,增加內(nèi)容如下:(代碼下載)
<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,組標(biāo)識是com.example.workflow,組類似于文件夾的性質(zhì),然后定義了一個向?qū)В鱾€屬性的含義如下:
category屬性指定組標(biāo)識
class屬性向?qū)?yīng)的類
icon屬性指定向?qū)э@示的圖標(biāo)
id屬性指定向?qū)У臉?biāo)識,應(yīng)該唯一
name屬性指定向?qū)э@示的名稱
接下來我們定義向?qū)ь悾捎谙驅(qū)е邪粋€或者多個向?qū)ы摚鶕?jù)實際情況而定,所以我們還要定義向?qū)ы摚a如下:
向?qū)ь?br />
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{
//定義一個向?qū)ы?/span>
private FileCreationPage page1;
//給向?qū)Ъ由舷驅(qū)ы?/span>
public void addPages() {
addPage(page1);
}
//在向?qū)宵c完成時,調(diào)用向?qū)ы摰膄inish()方法。
public boolean performFinish() {
return page1.finish();
}
//向?qū)С跏蓟臅r候,設(shè)置向?qū)У臉?biāo)題,新建向?qū)ы?/span>
public void init(IWorkbench workbench, IStructuredSelection selection) {
setWindowTitle("Create Workflow File");
page1 = new FileCreationPage(workbench, selection);
}
}
向?qū)ы擃?br />



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;
//設(shè)置向?qū)ы摰臉?biāo)題和描述
setTitle("Create a new " + DEFAULT_EXTENSION + " file");
setDescription("Create a new " + DEFAULT_EXTENSION + " file");
}
public void createControl(Composite parent) {
super.createControl(parent);
//設(shè)置向?qū)ы撝械奈募?/span>
setFileName("workflowExample" + fileCount + DEFAULT_EXTENSION);
//設(shè)置向?qū)У耐瓿砂粹o是否可用
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();
}
/* 得到文件的初始內(nèi)容
* @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屬性指定組標(biāo)識
class屬性向?qū)?yīng)的類
icon屬性指定向?qū)э@示的圖標(biāo)
id屬性指定向?qū)У臉?biāo)識,應(yīng)該唯一
name屬性指定向?qū)э@示的名稱
接下來我們定義向?qū)ь悾捎谙驅(qū)е邪粋€或者多個向?qū)ы摚鶕?jù)實際情況而定,所以我們還要定義向?qū)ы摚a如下:
向?qū)ь?br />






























向?qū)ы擃?br />

























































































































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

