您的位置:jsp学习站首页 >> 工具/平台 >> Eclipse >> 如何加入到eclipse workspace方式

如何加入到eclipse workspace方式 (1)

[ 来源:互网络 | 更新日期:2007-10-03 00:22:27 | 浏览次数:13278]
简介:getWorkspace();  IProject[] ps = ws
  在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令、访问eclipse workspace中的project等。一次在网上偶遇到本文的原创者kobye,此人正在进行jsports项目的开发,对此颇有心地,故在此行文与众人共同探讨之。
  
  一、基础工作-在插件中以编程的方式调用ant命令:
  
  在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令等。
  
  如何做到这一点?
  
  public void execute(){
  IWorkspace ws = ResourcesPlugin.getWorkspace();
  IProject[] ps = ws.getRoot().getProjects();
  System.out.println(ws.getRoot().getFullPath().makeAbsolute().toOSString());
  for(int i=0;i<ps.length;i++){
  IProject p = ps[i];
  IPath location = p.getLocation();
  IFile ifile = p.getFile("build.xml");
  System.out.println(ifile.getLocation().toFile().getAbsolutePath());
  File f = new File(ifile.getLocation().toFile().getAbsolutePath());
  if(!f.exists()){
  continue;
  }
  Project pro = new Project();
  pro.setBasedir(location.toFile().getAbsolutePath());
  pro.init();
  ProjectHelper helper = ProjectHelper.getProjectHelper();
  helper.parse(pro, f);
  Hashtable tars = pro.getTargets();
  System.out.println("name==="+name);
  Target t = (Target) tars.get(name);
  if(t==null){
  return;
  }
  DefaultLogger consoleLogger = new DefaultLogger();
  consoleLogger.setErrorPrintStream(System.err);
  consoleLogger.setOutputPrintStream(System.out);
  consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
  pro.addBuildListener(consoleLogger);
  pro.executeTarget(this.name);
  break;
  }
  }
  
  以上代码(单独编译不会通过,请把 name换位ant 的target)可以放到插件的代码中。
  
  以上代码的含义:
  
  获得eclipse workspace的引用,对workspace下的pronjects进行循环,如果该project下有build.xml并且该文件中有name的target那么就以ant的方式调用,并把ant运行的输出输出到eclipse的console。
  
  二、如何访问current project:
  
  上一节给出来在eclipse plugin 中访问eclipse workspace, 从而访问该workspace下所有project的方案,WorkSpace以及相关的类不提供直接访问current project的方法,所以只能走其他途径.
  
  在我们的plugin中,我们要提供界面入口,比如 PopMenu ActionMenu 等之类的,
  
  这些界面入口是要实现一些接口的,例如:PopMenu要实现IObjectActionDelegate,
  
  这个接口有几个方法,其中 public void selectionChanged(IAction action, ISelection
  selection) ;
  
  这个方法很早重要,可以通过ISelection获得当前选择中的Project.
  
  ISelection共有三个子接口,分别对应三个实现类,那么通过判断ISelection的实际类型可以获得其子接口的引用,
  
  然后对其遍历,通过getAdaptor方法获得所有的选择的IResource的引用,
  
  再进一步对IResource进行类型识别,得到IResource.PROJECT类型的元素即为IProject的引用.
  
  下
[1] [2] [3]
Tags:关键字:如何加入到eclipse workspace方式
责任编辑:glen