Re: setting package on new Java objects

Cliff Draper <[email protected]> Wed, 13 Aug 2003 12:22:02 -0700 (PDT)
Newsgroups gmane.comp.java.netbeans.modules.projects.devel
Message-ID <[email protected]>
Am I way off base here, it seems like the following code using good
old fashioned SourceElement and Identifier should work, but the
package comes out as
".home.cliffwd.cvs.sequoia.ffjuserdir.samples.src.pkg", instead of the
expected "pkg".

        ProjectMember javaObject = ...
        SourceCookie sourceCookie = (SourceCookie) javaObject.getLookup().lookup(SourceCookie.class);

        SourceElement sourceElement = sourceCookie.getSource();
        sourceElement.prepare().waitFinished();
        Identifier packageIdentifier = sourceElement.getPackage();
		packageIdentifier.getSourceName()...

Cliff Draper    Sun Microsystems, Forte Tools
My opinions may or may not reflect those of my employer.
---------------------------- food for thought ---------------------------
"Beware of bugs in the above code; I have only proved it correct, not
tried it."  -- Donald Knuth


From: Svata Dedic <[email protected]>
Date: 05 Aug 2003 07:49:32 +0200
> On Tue, 2003-08-05 at 01:48, Peter Wisnovsky wrote:
> > I have a wizard in which I create some Java files. It used to be that
> > the package could be computed easily from the path to the root of the
> > mountpoint. Now the path I get is relative to the root. So I end up
> > with files with package declarations like
> > 
> > package /home/pswisnov/Projects/webmod/src/ws;
> > 
> > I'm trying to figure out if there is an encapsulated way to do
> > this. There seems to be an implementation in JavaWizardData.setTarget
> > which I can copy over, but is there or could there be an entry point
> > provided by the Java module for computing the package name from a
> > project member? 
> > 
> Assuming the ProjectMember is one that describes a java source file for
> the project (== the ProjectMember is contained within some java source
> descriptor), you could use:
> 	ProjectPath.find(theProjectMember, ProjectPath.PATH_SOURCE)
> to get the source path effective for the file, then you can do:
> 	ClassPath.getResourceName(theProjectMember.getOriginal().getParent(),
> '.', false)
> to get the file's parent folder translated to a package name.
> 
> There's an abstraction of JavaPackage and JavaResource, which produce
> resource/package names and manage folder contents merging/hiding inside
> Java module, but I was not sure whether that could be an usable API at
> the time of writing.
> 
> -Svata