Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sapphire » Handle to Project
Handle to Project [message #726051] Fri, 16 September 2011 09:24 Go to next message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
I am writing a RelativePathService, I want to return all the Java source
paths that is available for the project as roots for searching the file
that will be assigned o the property having the RelativePathService.

Can anyone tell me how to do this ?

Also in the same scenario,

What is the best way to convert "/" to "." during serialization do we need
to do CustomXmlValueBinding ?

~ Kamesh
Re: Handle to Project [message #726068 is a reply to message #726051] Fri, 16 September 2011 09:51 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> I am writing a RelativePathService, I want to return all the Java source
> paths that is available for the project as roots for searching the file
> that will be assigned o the property having the RelativePathService.

IProject proj = element.adapt( IProject.class );
IJavaProject jproj = JavaCore.create( proj );

for( IClasspathEntry entry : jproj.getRawClasspath() )
{
   ...
}


> What is the best way to convert "/" to "." during serialization do we need
> to do CustomXmlValueBinding ?

If you want the text box to show dots, but have slashes written to file, then you need to write a custom binding. If you'd rather see dots in the text box as well, but the problem is that the browse dialog is returning slashes, see RelativePathService.convertToRelative/convertToAbsolute API.

I am assuming that you aren't trying to do this to browse for Java types, as the framework has built-in support for that that is better than trying to use RelativePathService.

- Konstantin
Re: Handle to Project [message #726090 is a reply to message #726068] Fri, 16 September 2011 10:30 Go to previous messageGo to next message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
Thanks Konstantin. No not java Types, Actually I am trying to browse for
Resource Bundles (*.properties) that is available in the classpath.

Also when I saw your reply for getting IProject , am now forced to ask me a
long standing query, how to do I access the model element inside the Service
classes or any other place ?

~Kamesh



~ Kamesh
"Konstantin Komissarchik" wrote in message
news:j4vjj9$v7l$1@news.eclipse.org...

> I am writing a RelativePathService, I want to return all the Java source
> paths that is available for the project as roots for searching the file
> that will be assigned o the property having the RelativePathService.

IProject proj = element.adapt( IProject.class );
IJavaProject jproj = JavaCore.create( proj );

for( IClasspathEntry entry : jproj.getRawClasspath() )
{
...
}


> What is the best way to convert "/" to "." during serialization do we need
> to do CustomXmlValueBinding ?

If you want the text box to show dots, but have slashes written to file,
then you need to write a custom binding. If you'd rather see dots in the
text box as well, but the problem is that the browse dialog is returning
slashes, see RelativePathService.convertToRelative/convertToAbsolute API.

I am assuming that you aren't trying to do this to browse for Java types, as
the framework has built-in support for that that is better than trying to
use RelativePathService.

- Konstantin
Re: Handle to Project [message #726104 is a reply to message #726090] Fri, 16 September 2011 11:12 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> how to do I access the model element inside the Service classes

IModelElement element = context( IModelElement.class );

> or any other place ?

Other places may have different API...
Re: Handle to Project [message #726105 is a reply to message #726090] Fri, 16 September 2011 11:14 Go to previous messageGo to next message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
I found the way the Service class provides the adapt() method which can be
used to make it to adapt to IProject :)

So let me rephrase my question what is the general way to get model element
if I I have,

1. ModelElementType
2.ModelProperty

~ Kamesh

"Kamesh Sampath" wrote in message news:j4vld6$1p2$1@news.eclipse.org...

Thanks Konstantin. No not java Types, Actually I am trying to browse for
Resource Bundles (*.properties) that is available in the classpath.

Also when I saw your reply for getting IProject , am now forced to ask me a
long standing query, how to do I access the model element inside the Service
classes or any other place ?

~Kamesh



~ Kamesh
"Konstantin Komissarchik" wrote in message
news:j4vjj9$v7l$1@news.eclipse.org...

> I am writing a RelativePathService, I want to return all the Java source
> paths that is available for the project as roots for searching the file
> that will be assigned o the property having the RelativePathService.

IProject proj = element.adapt( IProject.class );
IJavaProject jproj = JavaCore.create( proj );

for( IClasspathEntry entry : jproj.getRawClasspath() )
{
...
}


> What is the best way to convert "/" to "." during serialization do we need
> to do CustomXmlValueBinding ?

If you want the text box to show dots, but have slashes written to file,
then you need to write a custom binding. If you'd rather see dots in the
text box as well, but the problem is that the browse dialog is returning
slashes, see RelativePathService.convertToRelative/convertToAbsolute API.

I am assuming that you aren't trying to do this to browse for Java types, as
the framework has built-in support for that that is better than trying to
use RelativePathService.

- Konstantin
Re: Handle to Project [message #726107 is a reply to message #726105] Fri, 16 September 2011 11:18 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> So let me rephrase my question what is the general way to get model element
> if I I have,
>
> 1. ModelElementType
> 2. ModelProperty

If that's all you have, it isn't possible, but you can get access to IModelElement quite easily from a service. See my other response.
Re: Handle to Project [message #726207 is a reply to message #726105] Fri, 16 September 2011 15:20 Go to previous messageGo to next message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
"IProject proj = element.adapt( IProject.class );
IJavaProject jproj = JavaCore.create( proj );

for( IClasspathEntry entry : jproj.getRawClasspath() )
{
...
}"

Based on the above logic I wrote the the logic to make the roots as follows,

ProjectRelativePathService
.....
roots.add( new Path(iClasspathEntry.getPath() ).toPortableString() ));
.....

and my model property definition

// *** ResourceBundle ***
@Type( base = Path.class )
@MustExist
@ValidFileSystemResourceType( FileSystemResourceType.FILE )
@Service( impl = ProjectRelativePathService.class )
@FileExtensions( expr = "properties" )
ValueProperty PROP_RESOURCE_BUNDLE = new ValueProperty( TYPE,
"ResourceBundle" );

Value<Path> getResourceBundle();

void setResourceBundle( String value );

void setResourceBundle( Path value );

When I run my editor, and click on the browse I see just text for the
classpath entries and no file or folders displayed, but it has files and
folders + filters for .properties file. Not sure what mistake am making.

I also tried doing the following way but same result,

for ( IClasspathEntry iClasspathEntry : cpEntries ) {
if ( IClasspathEntry.CPE_SOURCE ==
iClasspathEntry.getEntryKind() ) {

IPath projectPath =
ResourcesPlugin.getWorkspace().getRoot().getLocation();

System.out.println( "Portable String" +
projectPath.append(
iClasspathEntry.getPath() ).toPortableString() );
File file = new File( projectPath.append(
iClasspathEntry.getPath() ).toPortableString() );
roots.add( new Path( file.getAbsolutePath() ) );

}
}


Can you please clarify what mistake am making ??

~ Kamesh
"Kamesh Sampath" wrote in message news:j4vo4d$4js$1@news.eclipse.org...

I found the way the Service class provides the adapt() method which can be
used to make it to adapt to IProject :)

So let me rephrase my question what is the general way to get model element
if I I have,

1. ModelElementType
2.ModelProperty

~ Kamesh

"Kamesh Sampath" wrote in message news:j4vld6$1p2$1@news.eclipse.org...

Thanks Konstantin. No not java Types, Actually I am trying to browse for
Resource Bundles (*.properties) that is available in the classpath.

Also when I saw your reply for getting IProject , am now forced to ask me a
long standing query, how to do I access the model element inside the Service
classes or any other place ?

~Kamesh



~ Kamesh
"Konstantin Komissarchik" wrote in message
news:j4vjj9$v7l$1@news.eclipse.org...

> I am writing a RelativePathService, I want to return all the Java source
> paths that is available for the project as roots for searching the file
> that will be assigned o the property having the RelativePathService.

IProject proj = element.adapt( IProject.class );
IJavaProject jproj = JavaCore.create( proj );

for( IClasspathEntry entry : jproj.getRawClasspath() )
{
...
}


> What is the best way to convert "/" to "." during serialization do we need
> to do CustomXmlValueBinding ?

If you want the text box to show dots, but have slashes written to file,
then you need to write a custom binding. If you'd rather see dots in the
text box as well, but the problem is that the browse dialog is returning
slashes, see RelativePathService.convertToRelative/convertToAbsolute API.

I am assuming that you aren't trying to do this to browse for Java types, as
the framework has built-in support for that that is better than trying to
use RelativePathService.

- Konstantin
Re: Handle to Project [message #726215 is a reply to message #726207] Fri, 16 September 2011 16:23 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> roots.add( new Path(iClasspathEntry.getPath() ).toPortableString() ));

You need to return absolute paths for roots. Unless your project setup is very unusual, iClasspathEntry.getPath() will not yield an absolute path. It will be something like "/proj/src". You need to locate that folder using IWorkspace API. Once you have the IFolder, call getLocation(). That will give you an absolute path that the API requires.

- Konstantin
Re: Handle to Project [message #726219 is a reply to message #726207] Fri, 16 September 2011 16:26 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> roots.add( new Path(iClasspathEntry.getPath() ).toPortableString() ));

Unless your project setup is very unusual, this will not produce absolute paths that are required for roots. More like "/proj/src". You will need to convert this path to an IFolder using IWorkspace API then call getLocation() on IFolder to produce an absolute path that's required.

- Konstantin
Re: Handle to Project [message #726220 is a reply to message #726207] Fri, 16 September 2011 16:26 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> roots.add( new Path(iClasspathEntry.getPath() ).toPortableString() ));

Unless your project setup is very unusual, this will not produce absolute paths that are required for roots. More like "/proj/src". You will need to convert this path to an IFolder using IWorkspace API then call getLocation() on IFolder to produce an absolute path that's required.

- Konstantin
Re: Handle to Project [message #726222 is a reply to message #726207] Fri, 16 September 2011 16:28 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 937
Registered: July 2009
Senior Member
> roots.add( new Path(iClasspathEntry.getPath() ).toPortableString() ));

Unless your project setup is very unusual, this will not produce absolute paths that are required for roots. More like "/proj/src". You will need to convert this path to an IFolder using IWorkspace API then call getLocation() on IFolder to produce an absolute path that's required.

- Konstantin
Re: Handle to Project [message #726309 is a reply to message #726219] Sat, 17 September 2011 03:40 Go to previous message
Kamesh Sampath is currently offline Kamesh Sampath
Messages: 213
Registered: July 2009
Senior Member
<HTML><HEAD></HEAD>
<BODY dir=ltr>
<DIV dir=ltr>
<DIV style="FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: 12pt">
<DIV>I am not sure what is the reason -&nbsp; My project was WebProject, as you
said the ClasspathEntry was not returning the absolute path, hence I resorted
your suggestion of using Workspace API and did as follows,</DIV>
<DIV>&nbsp;</DIV>
<DIV>
</DIV>
<DIV>for ( IClasspathEntry iClasspathEntry : cpEntries ) {</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if ( IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind() ) {</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
IWorkspace workspace = ResourcesPlugin.getWorkspace();</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
IWorkspaceRoot wroot = workspace.getRoot();</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
IPath entryPath = wroot.getFolder( iClasspathEntry.getPath() 
).getLocation();</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
roots.add( new Path( entryPath.toPortableString() ) );</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</DIV>
<DIV>
</DIV>
<DIV>&nbsp;</DIV>
<DIV>After this I was able to get all my folders for browsing and filtered by my
@FilterExtensions.</DIV>
<DIV>&nbsp;</DIV>
<DIV>~ Kamesh</DIV>
<DIV>&nbsp;</DIV>
<DIV>"Konstantin Komissarchik"&nbsp; wrote in message
news:j50anj$ocm$1@news.eclipse.org... </DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt; roots.add( new Path(iClasspathEntry.getPath() ).toPortableString()
));</DIV>
<DIV>&nbsp;</DIV>
<DIV>Unless your project setup is very unusual, this will not produce absolute
paths that are required for roots. More like "/proj/src". You will need to
convert this path to an IFolder using IWorkspace API then call getLocation() on
IFolder to produce an absolute path that's required.</DIV>
<DIV>&nbsp;</DIV>
<DIV>- Konstantin</DIV></DIV></DIV></BODY></HTML>
Previous Topic:Test Post
Next Topic:Switching XSD
Goto Forum:
  


Current Time: Sat Aug 02 00:35:51 EDT 2014

Powered by FUDForum. Page generated in 0.02632 seconds