I am trying to write a eclipse plugin, where I have to find the path of current selected
project. One importent thing is that this is a C or C++ project not a java project that I have to select. I tried some codes-
File currDir = new File(".");
String path = currDir.getAbsolutePath();
path = path.substring(0, path.length()-1);
System.out.println(path);
But this one is giving the path of eclipse plugin directory not current project.
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject[] projects = root.getProjects();
for (IProject project : projects) {
try {
printProjectInfo(project);
} catch (CoreException e) {
e.printStackTrace();
}
}
private void printPackageInfos(IJavaProject javaProject)
throws JavaModelException {
IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE)
{
printICompilationUnitInfo(mypackage);
}
}
}
But this one is selecting only java project.
So is there any way to do it .....
public static IProject getCurrentProject(){
ISelectionService selectionService =
Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
ISelection selection = selectionService.getSelection();
IProject project = null;
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
if (element instanceof IResource) {
project= ((IResource)element).getProject();
} else if (element instanceof PackageFragmentRootContainer) {
IJavaProject jProject =
((PackageFragmentRootContainer)element).getJavaProject();
project = jProject.getProject();
} else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
}
return project;
}
Hope it works :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With