| get my current project object. [message #193450] |
Mon, 18 June 2007 06:03  |
Eclipse User |
|
|
|
Originally posted by: oren_b1.verifone.com
Hello,
How can I get the project object of the currently opened project on eclipse?
Thanks
Oren.
|
|
|
|
|
|
|
|
|
| Re: get my current project object. [message #193967 is a reply to message #193943] |
Mon, 25 June 2007 13:25  |
Eclipse User |
|
|
|
Originally posted by: oren_b1.verifone.com
Thanks.
"Derek Morris" <dmsubs@NOSPAM.consertum.com> wrote in message
news:f5oonr$nrq$1@build.eclipse.org...
> Here is some code that should help you get on your way...
>
> public static final String S_C_PROJECTS_VIEW = "org.eclipse.cdt.ui.CView";
>
> IStructuredSelection ssel = null;
> IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
> .getActiveWorkbenchWindow();
> if (activeWindow != null) {
> ISelection sel = activeWindow.getSelectionService()
> .getSelection(S_C_PROJECTS_VIEW);
> if (sel instanceof IStructuredSelection) {
> ssel = (IStructuredSelection) sel;
> }
> IResource resource = null;
> Object obj = ssel.getFirstElement();
> if (obj instanceof IResource)
> resource = (IResource) obj;
> else if (obj instanceof IBinaryContainer) {
> IBinaryContainer bincon = (IBinaryContainer) obj;
> resource = bincon.getUnderlyingResource();
> } else if (obj instanceof IAdaptable) {
> IAdaptable adaptable = (IAdaptable) obj;
> resource = (IResource) adaptable.getAdapter(IResource.class);
> }
> }
>
> --
> Derek
>
>
> oren barzilai wrote:
>> But how do I get the currently selected resource?
>>
>>
>> "Derek Morris" <dmsubs@NOSPAM.consertum.com> wrote in message
>> news:f5fvkf$tdq$1@build.eclipse.org...
>>> You can get the currently selected IResource, and from that you can get
>>> the project:
>>>
>>> private IContainer getProject(IResource resource) {
>>> IContainer container = null;
>>> if (resource != null) {
>>> int type = resource.getType();
>>> switch (type) {
>>> case IResource.FILE :
>>> container = resource.getParent();
>>> break;
>>> case IResource.FOLDER :
>>> container = resource.getProject();
>>> break;
>>> case IResource.PROJECT :
>>> container = (IContainer) resource;
>>> break;
>>> case IResource.ROOT :
>>> break;
>>> }
>>> }
>>> return container;
>>> }
>>>
>>> --
>>> Derek
>>>
>>>
>>> Chris Recoskie wrote:
>>>> oren barzilai wrote:
>>>>> I can get hold on my project obj during creation process, But after I
>>>>> restart the eclipse I can't find a way to get hold on my project
>>>>> object again.
>>>>>
>>>>> Let's say that currently there is 2 open projects on my workspace, I
>>>>> need a method like "getCurrentlyActiveProject()" I can't think of any
>>>>> other way because I don't know on which project the user is currently
>>>>> working on, I don't even know the project's name.
>>>>>
>>>>> Any idea? anyone?
>>>> You can get the currently selected one, but that will only tell you
>>>> when one is actually selected. It's possible to be logically working
>>>> in a project (e.g. editing a file) without actually having the project
>>>> selected in the project explorer. Without knowing what you are doing I
>>>> can't comment as to whether this heuristic (selection) works for your
>>>> situation or not, or if there is more to your workflow.
>>>>
>>>> ===========================
>>>>
>>>> Chris Recoskie
>>>> Team Lead, IBM CDT Team
>>>> IBM Toronto
>>>> http://www.eclipse.org/cdt
>>
|
|
|
Powered by
FUDForum. Page generated in 0.03569 seconds