The wizard in this code is a standard wizard that displays pages, then generates code in the doFinish method. What is most interesting about this class is that in performFinish, it demonstrates how to interact with Eclipse's process monitor for tasks that take a long time.

          /**
             * This method is called when 'Finish' button is pressed 
             * in the wizard. We will create an operation and run it
             * using wizard as execution context.
             
          */

            
          public boolean performFinish() {
              final String containerName 
          = page.getContainerName();
              final String fileName 
          = page.getFileName();
              IRunnableWithProgress op 
          = new IRunnableWithProgress() 
              
          {
                
          public void run(IProgressMonitor monitor) 
                    throws InvocationTargetException 
                
          {
                  
          try {
                    doFinish(containerName, fileName, monitor);
                  }
           catch (CoreException e) {
                    
          throw new InvocationTargetException(e);
                  }
           finally {
                    monitor.done();
                  }

                }

              }
          ;
              
          try {
                getContainer().run(
          truefalse, op);
              }
           catch (InterruptedException e) {
                
          return false;
              }
           catch (InvocationTargetException e) {
                Throwable realException 
          = e.getTargetException();
                MessageDialog.openError(
                    getShell(), 
                    
          "Error", realException.getMessage());
                
          return false;
              }

              
          return true;
            }


          /*  */
          /**
             * The worker method. It will find the container, create
             * the file if missing or just replace its contents, and 
             * open the editor on the newly created file.
             
          */


            
          private void doFinish(
                String containerName,
                String fileName,
                IProgressMonitor monitor)
                throws CoreException 
            
          {
              
          // create a sample file
              monitor.beginTask("Creating " + fileName, 2);
              IWorkspaceRoot root 
          = 
                  ResourcesPlugin.getWorkspace().getRoot();
              IResource resource 
          = root.findMember(
                  
          new Path(containerName));
              
          if (!resource.exists() || 
                  
          !(resource instanceof IContainer)) 
              
          {
                throwCoreException(
          "Container \"" + 
                    containerName + "\" does not exist.");
              }

              IContainer container 
          = (IContainer) resource;
              final IFile file 
          = 
                  container.getFile(
          new Path(fileName));
              
          try {
                InputStream stream 
          = openContentStream();
                
          if (file.exists()) {
                  file.setContents(stream, 
          truetrue, monitor);
                }
           else {
                  file.create(stream, 
          true, monitor);
                }

                stream.close();
              }
           catch (IOException e) {
              }

              monitor.worked(
          1);
              monitor.setTaskName(
          "Opening file for editing");
              getShell().getDisplay().asyncExec(
          new Runnable() {
                  
          public void run() {
                    IWorkbenchPage page 
          =
                        PlatformUI.getWorkbench().
                        getActiveWorkbenchWindow().getActivePage();
                    
          try {
                      IDE.openEditor(page, file, 
          true);
                    }
           catch (PartInitException e) {
                    }

                  }

                }
          );
              monitor.worked(
          1);
            }


          This code is a simple SWT wizard page. The most interesting characteristic of this class is in the handleBrowse method. This method uses Eclipse's ContainerSelectionDialog for displaying the folders in the workspace.

          /**
             * Uses the standard container selection dialog to
             * choose the new value for the container field.
             
          */

            
          private void handleBrowse() {
              ContainerSelectionDialog dialog 
          =
                  
          new ContainerSelectionDialog(
                      getShell(),
                      ResourcesPlugin.getWorkspace().getRoot(),
                      
          false,
                      
          "Select new file container");
              
          if (dialog.open() == ContainerSelectionDialog.OK) {
                Object[] result 
          = dialog.getResult();
                
          if (result.length == 1{
                  containerText.setText(
                      ((Path)result[
          0]).toOSString());
                }

              }

            }

           



          版權所有 羅明
          posted on 2005-07-10 10:35 羅明 閱讀(751) 評論(0)  編輯  收藏 所屬分類: Eclipse
           
          主站蜘蛛池模板: 桐城市| 顺平县| 仙居县| 南皮县| 天镇县| 仁布县| 兴化市| 开阳县| 太保市| 德安县| 万年县| 晋宁县| 息烽县| 江华| 巴马| 深州市| 成武县| 新乡市| 宜黄县| 龙游县| 成安县| 东城区| 宣威市| 新乡市| 香河县| 安龙县| 新余市| 咸阳市| 襄樊市| 堆龙德庆县| 安顺市| 长岭县| 冕宁县| 清河县| 金门县| 阳东县| 二手房| 闻喜县| 呼伦贝尔市| 博客| 馆陶县|