How to access eclipse workspace?

  Abstract: How to access eclipse workspace? 

  : 

  How to access eclipse workspace? 

  Reprint: cleverpig 

  Original author: kobye 

  Original author website: http://blog.csdn.net/jsports/ 

  Abstract: 

  In the development of eclipse pluin when, in some cases, we need access to eclipse workspace, such as: plug-in approach to programming called ant order, visit eclipse workspace in the project, such as.    Even an online encounter kobye originators of the paper, who is jsports project development, this very carefully, there is this language of the common and public. 

  First, the basic work - in the plug-in approach to programming called ant order: 

  In the development of eclipse pluin when, in some cases, we need access to eclipse workspace, such as: plug-in approach to programming called ant orders. 

  How to do that? 

  Public void execute () ( 
  IWorkspace ws = ResourcesPlugin.getWorkspace (); 
  IProject ws.getRoot [] ps = (). GetProjects (); 
  System.out.println (ws.getRoot (). GetFullPath (). MakeAbsolute (). ToOSString ()); 

  For (int i = 0; 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; 
  ) 

  ) 

  Above code (not through a separate compiler, the name of the ant transposition target) can be put in the plug-in code. 

  The meaning of the code above: 

  The eclipse workspace was quoted on the workspace for the pronjects cycle, if the project under the build.xml and documents in the name of the target then the ant approach to the call, and the output of running the ant output to eclipse the console. 

  Second, how to access current project: 

  On an up to the visit of the eclipse plugin eclipse workspace, thereby visit to the workspace of all project proposals, as well as related categories WorkSpace does not provide direct access to current project approach, we can only go the other way. 

  In our plugin, we are going to provide import interface, such as, for example PopMenu ActionMenu like, 
  These interfaces entrance is to achieve some of the interface, such as: PopMenu to achieve IObjectActionDelegate, 
  This interface has several methods, including public void selectionChanged (IAction action, ISelection 
  Selection); 

  Important early this method can be obtained through ISelection choice in the current Project. 

  ISelection total of three sub-interfaces, corresponding realization of the three categories, then the judge ISelection through the actual type of interface can be invoked his son, 
  Then its traversal through getAdaptor access to all methods of choice IResource citations, 
  Further identify the type IResource conducted by IResource.PROJECT types of elements which is IProject are cited. 

  Below are the procedures: 

  Import java.lang.reflect.Array; 
  Import java.util.ArrayList; 
  Import java.util.Iterator; 

  Import org.eclipse.core.resources.IProject; 
  Import org.eclipse.core.resources.IResource; 
  Import org.eclipse.core.runtime.IAdaptable; 
  Import org.eclipse.jface.action.IAction; 
  Import org.eclipse.jface.dialogs.MessageDialog; 
  Import org.eclipse.jface.viewers.ISelection; 
  Import org.eclipse.jface.viewers.IStructuredSelection; 

  Import org.eclipse.swt.widgets.Shell; 
  Import org.eclipse.ui.IObjectActionDelegate; 
  Import org.eclipse.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" 
  "New Action 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; 
  ) 

  / *** 
  * Below this method and the method is very important. 
  * @ Param selection 
  * @ Param c 
  * @ Return 
  * / 
  Private Object [] getSelectedResources (IStructuredSelection selection, the Class c) ( 
  Return getSelectedAdaptables (selection, c); 
  ) 

  Private static Object [] getSelectedAdaptables (ISelection selection, the Class c) ( 
  ArrayList result = null; 
  If (! Selection.isEmpty ()) ( 
  Result = new ArrayList (); 
  Iterator elements = ((IStructuredSelection) selection). Iterator (); 
  While (elements.hasNext ()) ( 
  Object adapter = getAdapter (elements.next (), c); 
  If (c.isInstance (adapter)) ( 
  Result.add (adapter); 
  ) 
  ) 
  ) 
  If (result! = Null & &! Result.isEmpty ()) ( 
  Return result.toArray ((Object []) Array.newInstance (c, result.size ())); 
  ) 
  Return (Object []) Array.newInstance (c, 0); 
  ) 

  / ** 
  * This method preserves ISelection citations, 
  * Please note: the actual type ISelection for different applications, the actual type may be different, 
  * There are three possible, please refer to eclipse API. 
*
  * @ See IActionDelegate # selectionChanged (IAction, ISelection) 
  * / 
  Public void selectionChanged (IAction action, ISelection selection) ( 
  This.selection = (IStructuredSelection) selection; 
  System.out.println ( "current project name ==="+ this.getProject (). GetName ()); 
  ) 

  / ** 
  * This method can be current project. 
*
  * @ Return 
  * / 
  Private IProject getProject () ( 
  IResource rs = [] 
  (IResource []) getSelectedResources ((IStructuredSelection) selection, IResource.class); 
  IProject project = null; 
  For (int i = 0; i    IResource r [i] = rs; 
  If (r.getType () == IResource.PROJECT) ( 
  Project = (IProject) r; 
  Break; 
  ) 
  ) 
  Return project; 
  ) 
  ) 

  Third, contact the author: 

  Author: kobye 
  Kobye the jsports projects Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 

  How to access eclipse workspace? 

  Reprint: cleverpig 

  Original author: kobye 

  Original author website: http://blog.csdn.net/jsports/ 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply