RCP File Path von selected File in Package Explorer für RCP Plugin

javator

Mitglied
Hallo ich habe folgende Problem ich brauch den absoluten Dateipfad der selektierten Datei im Package Explorer
Das habe ich bisher:

Code:
IWorkbenchWindow window =    PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		
ISelectionService service = window.getSelectionService();
		
IStructuredSelection structured = (IStructuredSelection) service.getSelection("org.eclipse.jdt.ui.PackageExplorer");
		
				
IFile file =(IFile) structured.getFirstElement();
		
IPath path = file.getLocation();
		
System.out.println(path.toPortableString());

Aber die wirft folgenden Fehler:

Code:
java.lang.ClassCastException: org.eclipse.jdt.internal.core.CompilationUnit incompatible with org.eclipse.core.resources.IFile
	at testpopupmenu.handlers.SampleHandler.execute(SampleHandler.java:54)
	at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290)
	at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
	at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
	at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
	at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
	at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:829)
	at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:815)
	at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:805)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
	at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
	at java.lang.reflect.Method.invoke(Method.java:611)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

Habt Ihr eine Idee was mein Fehler ist bzw. eine andere gute Lösung

Danke jetzt schon mal
 

Joergel

Mitglied
Ich hab's so gemacht.
Java:
ISelection sel = HandlerUtil.getActiveMenuSelection(event);
IStructuredSelection selection = (IStructuredSelection) sel;
		
if (selection.getFirstElement() instanceof IFile) {
	IFile file = (IFile) selection.getFirstElement();
	IPath path = file.getLocation();
	System.out.println(path.toPortableString());
}
 

Ähnliche Java Themen

Neue Themen


Oben