[转载]如何加入到eclipse workspace方式

阅读: 评论:0

[转载]如何加入到eclipse workspace方式

[转载]如何加入到eclipse workspace方式

如何加入到eclipse workspace方式
摘要:
  
  在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令、访问eclipse workspace中的project等。一次在网上偶遇到本文的原创者kobye,此人正在进行jsports项目的开发,对此颇有心地,故在此行文与 众人共同探讨之。
  
   一、基础工作-在插件中以编程的方式调用ant命令:
  
  在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令等。
  
  如何做到这一点?
  
  public void execute(){
  IWorkspace ws = Workspace();
  IProject[] ps = ws.getRoot().getProjects();
  System.out.Root().getFullPath().makeAbsolute().toOSString());
  for(int i=0;i  IProject p = ps[i];
  IPath location = p.getLocation();
  IFile ifile = p.getFile(&#l");
  System.out.Location().toFile().getAbsolutePath());
  File f = new Location().toFile().getAbsolutePath());
  if(!f.exists()){
  continue;
  }
  Project pro = new Project();
  pro.File().getAbsolutePath());
  pro.init();
  ProjectHelper helper = ProjectHelper();
  helper.parse(pro, f);
  Hashtable tars = Targets();
  System.out.println("name==="+name);
  Target t = (Target) (name);
  if(t==null){
  return;
  }
  DefaultLogger consoleLogger = new DefaultLogger();
  consoleLogger.);
  consoleLogger.setOutputPrintStream(System.out);
  consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
  pro.addBuildListener(consoleLogger);
  uteTarget(this.name);
  break;
  }
  }
  
  以上代码(单独编译不会通过,请把 name换位ant 的target)可以放到插件的代码中。
  
  以上代码的含义:
  
   获得eclipse workspace的引用,对workspace下的pronjects进行循环,如果该project下有l并且该文件中有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的引用.
  
  下面是程序:
  
  import flect.Array;import java.util.ArrayList;
  import java.util.Iterator;sources.IProject;
  sources.IResource;
  untime.IAdaptable;
  lipse.jface.action.IAction;
  lipse.jface.dialogs.MessageDialog;
  lipse.jface.viewers.ISelection;
   lipse.jface.viewers.IStructuredSelection;lipse.swt.widgets.Shell;lipse.ui.IObjectActionDelegate;
  lipse.ui.IWorkbenchPart;
  /** * @author Kobye */public class TestPopMenu implements IObjectActionDelegate {
  private IStructuredSelection selection;
  /** * Constructor for Action1.
  */ public TestPopMenu () { super();
  } /** * @see IObjectActionDelegate#setActivePart(IAction IWorkbenchPart)
  */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { }
  /**
  * @see IActionDelegate#run(IAction) */ public void run(IAction action) { Shell shell = new Shell();
  MessageDialog.openInformation( shell, "Pop Plug-in", "NewAction was executed.");
   } public static Object getAdapter(Object adaptable, Class c) { if (c.isInstance(adaptable)) { return adaptable; } if (adaptable instanceof IAdaptable) { IAdaptable a = (IAdaptable) adaptable;
  Object adapter = a.getAdapter(c);
  if (c.isInstance(adapter)) {  return adapter;
  } } return null;
  } /*** * 这个方法和下面的方法很重要。
   * @param selection * @param c * @return */ private Object[] getSelectedResources(IStructuredSelection selection,Class c) { return getSelectedAdaptables(selection, c);
  } private static Object[] getSelectedAdaptables(ISelection selection, Class c) { ArrayList result = null;
  if (!selection.isEmpty()) { result = new ArrayList();
  Iterator elements = ((IStructuredSelection) selection).iterator();
  while (elements.hasNext()) {  Object adapter = (), c);
  if (c.isInstance(adapter)) {  result.add(adapter);
   } } } if (result != null && !result.isEmpty()) { Array((Object[])wInstance(c, result.size())); } return (Object[])wInstance(c, 0);
  } /** * 这个方法保存了ISelection的引用, * 请注意:ISelection的实际类型因不同的应用,其实际类型可能不同, * 共有三种可能,请查阅eclipse API。 * * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) {  this.selection = (IStructuredSelection) selection;
  System.out.println("current project name==="&#Project().getName());
   } /** * 这个方法可以得到current project。 * * @return */ private IProject getProject(){  IResource[]rs =(IResource[])getSelectedResources((IStructuredSelection)selection,IResource.class);
  IProject project = null;
  for(int i =0;i  IResource r = rs[i];
  Type()==IResource.PROJECT){
  project = (IProject) r;
  break;
  }
  }
  return project; }}

来自 “ ITPUB博客 ” ,链接:/,如需转载,请注明出处,否则将追究法律责任。

转载于:/

本文发布于:2024-01-29 18:57:42,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170652586517568.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:方式   eclipse   workspace
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23