问题
I am creating an Eclipse Plugin. I am stuck in the part where I need to get the class name as soon as the user selects/clicks on classname in the package explorer and then I want to display it in a custom view. (Broadly I want to call API of another software in the custom view corresponding to that class name). But I am not able to extract the class name on user's click. This feature is similar to the "Types" view, where corresponding class file appears in the view as soon as user clicks .java in the project explorer. I am quite new to plugin development, please guide me.Thanks in advance.
回答1:
First find the view:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart viewPart = page.findView(id);
The id for the Packages Explorer View is org.eclipse.jdt.ui.PackageExplorer
If the view is not open viewPart will be null.
Get the selection provider from the view site:
ISelectionProvider selProvider = viewPart.getSite().getSelectionProvider();
You can now use addSelectionChangedListener
to listen for selection changes.
In the selection event getSelection()
will normally return an instance of IStructuredSelection
containing the current selections. Use IStructuredSelection.iterator()
to iterate through the selection objects.
In the Packages Explorer View each selected object will be an object of a type such as IProject
or IFile
or perhaps something specific to the JDT code.
回答2:
The Package-Explorer is a Plugin depending on JDT Plugin is depending on .....
You must depend your plugin from the Package-Explorer-Plugin. On startup you must find the type-hierarchys extending-point where a type-hierarchy-registry creates new typehierarchy-View on demand, add your own click-listener and evaluate the result.
Not a work of a newbe in plugin-development. Have luck.
来源:https://stackoverflow.com/questions/20328468/eclipse-plugin-get-the-class-name-on-users-click-selection