Reputation: 21
i am developing one eclipse plugin. now when user click on any file/folder and click icon(added by me) then i have to get that file location.
can you please help me regarding this one.
Thanks in advance.
Upvotes: 2
Views: 881
Reputation: 3723
package com.packpub.e4.menu.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
/**
* Our sample handler extends AbstractHandler, an IHandler base class.
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public class SampleHandler extends AbstractHandler {
/**
* The constructor.
*/
public SampleHandler() {
}
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
ISelection selection = window.getSelectionService().getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
Object obj = ssel.getFirstElement();
IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj, IFile.class);
if (file != null) {
if (obj instanceof IAdaptable) {
file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
String path = file.getRawLocation().toOSString();
String fileName = "File=" + file.getName();
String filePath = "\nPath: " + path;
MessageDialog.openInformation(
window.getShell(),
"Menu",
fileName+ filePath);
}
}
}
return null;
}
}
Upvotes: 0
Reputation: 12257
Every IWorkbenchPartSite has a method
/**
* Sets the selection provider for this workbench site.
*
* @param provider
* the selection provider, or <code>null</code> to clear it
*/
public void setSelectionProvider(ISelectionProvider provider);
When something is selected inside the part site you should get a notification to handle the event.
You can get the Selection by calling
/**
* Returns the current selection for this provider.
*
* @return the current selection
*/
public ISelection getSelection();
I your case the ISelection object should be an instance of IStructuredSelection and the selected objects you can get by calling
public interface IStructuredSelection extends ISelection {
/**
* Returns the first element in this selection, or <code>null</code>
* if the selection is empty.
*
* @return an element, or <code>null</code> if none
*/
public Object getFirstElement();
/**
* Returns an iterator over the elements of this selection.
*
* @return an iterator over the selected elements
*/
public Iterator iterator();
...
}
You should get the IResource directly, when the user selects it in the package explorer for example.
You also can get the current used ISelectionProvider from the PartSite the IResource is selected, after the user pressed your Icon-Button.
You can get some Provider of some open view by calling
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("VIEW-ID").getViewSite().getSelectionProvider();
Views:
Upvotes: 1