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 羅明 閱讀(750) 評論(0)  編輯  收藏 所屬分類: Eclipse
           
          主站蜘蛛池模板: 伊川县| 绍兴市| 峡江县| 手游| 定南县| 天台县| 河曲县| 沅陵县| 元氏县| 卢氏县| 平安县| 宁海县| 德兴市| 潍坊市| 江北区| 民勤县| 扎囊县| 中卫市| 游戏| 板桥市| 耿马| 龙里县| 涟水县| 平谷区| 台江县| 天镇县| 长武县| 苍溪县| 依安县| 沙田区| 河源市| 辉县市| 台北县| 兰溪市| 双牌县| 西充县| 贵州省| 富锦市| 宁波市| 秭归县| 师宗县|