Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2012 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2013 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 568-576
Link Here
|
568 |
IType enclosingType= null; |
568 |
IType enclosingType= null; |
569 |
|
569 |
|
570 |
if (elem != null) { |
570 |
if (elem != null) { |
571 |
// evaluate the enclosing type |
|
|
572 |
project= elem.getJavaProject(); |
571 |
project= elem.getJavaProject(); |
573 |
pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); |
572 |
pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); |
|
|
573 |
if (pack == null && project != null) { |
574 |
pack= getPackage(project); |
575 |
} |
576 |
// evaluate the enclosing type |
574 |
IType typeInCU= (IType) elem.getAncestor(IJavaElement.TYPE); |
577 |
IType typeInCU= (IType) elem.getAncestor(IJavaElement.TYPE); |
575 |
if (typeInCU != null) { |
578 |
if (typeInCU != null) { |
576 |
if (typeInCU.getCompilationUnit() != null) { |
579 |
if (typeInCU.getCompilationUnit() != null) { |
Lines 623-630
Link Here
|
623 |
setAddComments(StubUtility.doAddComments(project), true); // from project or workspace |
626 |
setAddComments(StubUtility.doAddComments(project), true); // from project or workspace |
624 |
} |
627 |
} |
625 |
|
628 |
|
626 |
|
629 |
/** |
627 |
|
630 |
* Checks if the package field has to be pre-filled in this page and returns the package |
|
|
631 |
* fragment to be used for that. The package fragment has the name of the project if the source |
632 |
* folder does not contain any package and if the project name is a valid package name. If the |
633 |
* source folder contains exactly one package then the name of that package is used as the |
634 |
* package fragment's name. <code>null</code> is returned if none of the above is applicable. |
635 |
* |
636 |
* @param javaProject the containing Java project of the selection used to initialize this page |
637 |
* |
638 |
* @return the package fragment to be pre-filled in this page or <code>null</code> if it is not |
639 |
* applicable |
640 |
*/ |
641 |
private IPackageFragment getPackage(IJavaProject javaProject) { |
642 |
String packName= null; |
643 |
final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot(); |
644 |
IJavaElement[] packages= null; |
645 |
try { |
646 |
if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) { |
647 |
packages= pkgFragmentRoot.getChildren(); |
648 |
if (packages.length == 1) { // only default package -> use Project name |
649 |
packName= javaProject.getElementName(); |
650 |
// validate package name |
651 |
IStatus status= validatePackageName(packName, javaProject); |
652 |
if (status.getSeverity() == IStatus.OK) { |
653 |
return pkgFragmentRoot.getPackageFragment(packName); |
654 |
} |
655 |
} else { |
656 |
int noOfPackages= 0; |
657 |
IPackageFragment thePackage= null; |
658 |
for (final IJavaElement pack : packages) { |
659 |
IPackageFragment pkg= (IPackageFragment) pack; |
660 |
// ignoring empty parent packages and default package |
661 |
if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) { |
662 |
noOfPackages++; |
663 |
thePackage= pkg; |
664 |
if (noOfPackages > 1) { |
665 |
return null; |
666 |
} |
667 |
} |
668 |
} |
669 |
if (noOfPackages == 1) { // use package name |
670 |
packName= thePackage.getElementName(); |
671 |
return pkgFragmentRoot.getPackageFragment(packName); |
672 |
} |
673 |
} |
674 |
} |
675 |
} catch (JavaModelException e) { |
676 |
// fall through |
677 |
} |
678 |
return null; |
679 |
} |
628 |
|
680 |
|
629 |
private static IStatus validateJavaTypeName(String text, IJavaProject project) { |
681 |
private static IStatus validateJavaTypeName(String text, IJavaProject project) { |
630 |
if (project == null || !project.exists()) { |
682 |
if (project == null || !project.exists()) { |
Lines 1509-1515
Link Here
|
1509 |
if (resource.isVirtual()){ |
1561 |
if (resource.isVirtual()){ |
1510 |
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageIsVirtual); |
1562 |
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageIsVirtual); |
1511 |
return status; |
1563 |
return status; |
1512 |
} |
1564 |
} |
1513 |
if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) { |
1565 |
if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) { |
1514 |
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageNameFiltered); |
1566 |
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageNameFiltered); |
1515 |
return status; |
1567 |
return status; |