Bug 393161 - [type wizards] Pre-fill package name in the new class dialog
Summary: [type wizards] Pre-fill package name in the new class dialog
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.2.1   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: 4.3 M5   Edit
Assignee: Noopur Gupta CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday
Depends on:
Blocks:
 
Reported: 2012-10-30 11:35 EDT by Wim Jongman CLA
Modified: 2013-01-14 08:55 EST (History)
2 users (show)

See Also:
daniel_megert: review+


Attachments
Patch (6.29 KB, patch)
2013-01-04 02:54 EST, Noopur Gupta CLA
no flags Details | Diff
Patch (5.34 KB, patch)
2013-01-09 03:45 EST, Noopur Gupta CLA
no flags Details | Diff
Patch (5.43 KB, patch)
2013-01-10 00:55 EST, Noopur Gupta CLA
daniel_megert: review-
Details | Diff
Patch (6.38 KB, patch)
2013-01-14 04:16 EST, Noopur Gupta CLA
daniel_megert: review-
Details | Diff
Patch (7.02 KB, patch)
2013-01-14 06:21 EST, Noopur Gupta CLA
daniel_megert: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Wim Jongman CLA 2012-10-30 11:35:16 EDT
If the new java class wizard is executed against a project and the project does not have any packages yet then the package field should be prefilled with the named of the project.

Reproduce.

Create an empty project
go to the source folder
select new class

the package field is empty and it requires a lot of work to fill it in with the name of the project, which should, by convention, be the name of the top level package.
Comment 1 Markus Keller CLA 2012-10-30 11:40:25 EDT
Good idea, we should do that if the project name is a valid package name.
Comment 2 Markus Keller CLA 2012-10-30 11:43:18 EDT
This should be done for all type and package creation dialogs (and only if there's no better package context available, of course).
Comment 3 Noopur Gupta CLA 2013-01-04 02:54:32 EST
Created attachment 225201 [details]
Patch
Comment 4 Noopur Gupta CLA 2013-01-04 02:55:52 EST
The patch has the following behavior:
- if the source folder contains no package (excluding the default package) then we use the project name (only if the project name is valid i.e. has no errors or warnings, otherwise the package field is left blank).
- if the source folder contains exactly one package then we use that package name.
- if the source folder contains more than one packages then we leave the package field blank for the user to choose the required package.
Comment 5 Wim Jongman CLA 2013-01-07 05:30:25 EST
(In reply to comment #4)

> - if the source folder contains more than one packages then we leave the
> package field blank for the user to choose the required package.

If you already have all the packages in hand, maybe it is feasible to provide a drop-down dialog that contains all the packages?
Comment 6 Dani Megert CLA 2013-01-07 05:40:27 EST
(In reply to comment #5)
> (In reply to comment #4)
> 
> > - if the source folder contains more than one packages then we leave the
> > package field blank for the user to choose the required package.
> 
> If you already have all the packages in hand, maybe it is feasible to
> provide a drop-down dialog that contains all the packages?

We already have content assist for the packages that exist and the patch covers the case where the project name is pre-filled. I don't plan to invest more here.
Comment 7 Wim Jongman CLA 2013-01-07 06:05:00 EST
(In reply to comment #6)
> covers the case where the project name is pre-filled. I don't plan to invest
> more here.

Fair enough. Trying to get another slice of salami. Thanks for adding this guys.
Comment 8 Dani Megert CLA 2013-01-07 10:58:31 EST
Patch is almost good. Some issues:

- I don't like that the code which guesses the name already messes with the
  status and sets internal state. Instead, simply validate the name and if it
  is valid, set the package. The code will later update the status automatically.
  With your patch the same update happens twice.

- The check whether the source folder contains exactly one package should ignore 
  empty parent packages. Test with "foo.bar" as project name.
Comment 9 Noopur Gupta CLA 2013-01-09 03:45:36 EST
Created attachment 225368 [details]
Patch

Thanks Dani. I have made the changes. Please review.
Comment 10 Dani Megert CLA 2013-01-09 10:34:57 EST
The Package Wizard now suggests the already existing package name even if the project or source folder is selected or there are other packages already.

The behavior of the Type Wizard now looks OK but there's some polish required:
- getPackage(...)
  - says 'elem' can be 'null' but that would cause an NPE
  - "elem.getJavaProject()" is there three times and 'elem' not used otherwise
    ==> change the parameter to be the Java project (don't allow 'null')
  - "if (packages != null)" is not needed
  - 'froot' is a bad name. Sorry that slipped through the first review.
    We use camel-case (e.g. 'packageFragmentRoot' or 
    'pkgFragmentRoot'). Also note that 'fRoot' would also be bad for a local 
    variable since 'f' denotes a filed according to our naming guidelines.
  - if noOfPackages > 1 the loop can be exited
  - "length is 1 -> " I would remove that since it's clear from the 'if' already
- The comment "// evaluate the enclosing type" should be moved down a bit.

Otherwise the patch is OK.
Comment 11 Noopur Gupta CLA 2013-01-10 00:55:54 EST
Created attachment 225410 [details]
Patch

Polished the things suggested by Dani.
Comment 12 Dani Megert CLA 2013-01-10 10:58:27 EST
Comment on attachment 225410 [details]
Patch

(In reply to comment #11)

The first paragraph of comment 10 is still not fixed :-(

getPackages(...)
- "cannot be <code>null</code>" is not needed as this is the default. We only
  have to document if 'null' is allowed
- if noOfPackages > 1 the loop can be exited: better now, but even better
  than breaking out of the loop we can directly return. This makes the code
  easier to read (one can ignore the code that follows)
Comment 13 Noopur Gupta CLA 2013-01-14 04:16:01 EST
Created attachment 225551 [details]
Patch
Comment 14 Noopur Gupta CLA 2013-01-14 04:16:32 EST
Fixed the first paragraph of comment 10.
Now the Package Wizard does not suggest the already existing package name on selecting the project or source folder and no suggestion is made if the source folder already contains other packages.

Also, polished the getPackages(...) based on the comments.

Dani, please review and let me know the comments.
Comment 15 Dani Megert CLA 2013-01-14 05:48:08 EST
Comment on attachment 225551 [details]
Patch

(In reply to comment #14)
> Fixed the first paragraph of comment 10.
> Now the Package Wizard does not suggest the already existing package name on
> selecting the project or source folder and no suggestion is made if the
> source folder already contains other packages.
> 
> Also, polished the getPackages(...) based on the comments.
> 
> Dani, please review and let me know the comments.

Thanks the behavior is now fine. What I now don't like is that we have two methods which almost do the same thing but have different names:
#packageChanged() and #getPackageStatus(packName). I would either remove the former, or also move the following line into #packageChanged:
fPackageStatus= packageChanged() so that it is indeed different.


The @return description in is not well understandable:
the package fragment to be pre-filled in this page or null if it is not applicable

If one just read that sentence, then one does not know what "if it is not applicable" means. Also note, that we always put keywords like 'null', 'true', 'false' into <code>...</code>.
Comment 16 Dani Megert CLA 2013-01-14 05:49:07 EST
(In reply to comment #15)
> The @return description in is not well understandable:

... in #getPackage
Comment 17 Noopur Gupta CLA 2013-01-14 06:21:30 EST
Created attachment 225558 [details]
Patch

Thanks for the review comments Dani.

I have removed the method packageChanged().
Also, updated the @return statement and added @since in getPackage(...) as suggested by you.
Comment 18 Dani Megert CLA 2013-01-14 08:54:52 EST
Comment on attachment 225558 [details]
Patch

Thanks for the final patch Noopur.

Committed your patch with http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=d473e3cfdf27b69a0689587fb0054f40a2643ea7
Comment 19 Dani Megert CLA 2013-01-14 08:55:18 EST
.