Eclipse's process monitor & Container Selection Dialog

Posted on 2005-07-10 10:35 罗明 阅读(272) 评论(0)  编辑  收藏 所属分类: Eclipse

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());
      }

    }

  }

 



版权所有 罗明

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-07-10 15:19 编辑过
 
成果网帮您增加网站收入
 
相关链接:
网站导航: