posts - 36, comments - 30, trackbacks - 0, articles - 3
           這一節(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
          這樣運行程序,效果如圖


          主站蜘蛛池模板: 建始县| 永德县| 通渭县| 中超| 宿迁市| 汨罗市| 九江市| 太湖县| 石城县| 高淳县| 闻喜县| 博湖县| 南川市| 盘山县| 三台县| 南陵县| 碌曲县| 西华县| 沙湾县| 阿拉善盟| 邛崃市| 油尖旺区| 闻喜县| 新野县| 威远县| 容城县| 巨鹿县| 房山区| 兰坪| 伊宁县| 新平| 精河县| 崇州市| 茂名市| 于都县| 昌黎县| 綦江县| 固镇县| 敦煌市| 锦屏县| 进贤县|