View | Details | Raw Unified | Return to bug 393161 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java (-3 / +12 lines)
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 161-168 Link Here
161
		String pName= ""; //$NON-NLS-1$
161
		String pName= ""; //$NON-NLS-1$
162
		if (jelem != null) {
162
		if (jelem != null) {
163
			IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
163
			IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
164
			if (pf != null && !pf.isDefaultPackage())
164
			if (pf != null && !pf.isDefaultPackage()) {
165
				pName= pf.getElementName();
165
				pName= pf.getElementName();
166
			} else {
167
				if (jelem.getJavaProject() != null) {
168
					String prName= jelem.getJavaProject().getElementName();
169
					IStatus status= validatePackageName(prName);
170
					if (status.getSeverity() == IStatus.OK) {
171
						pName= prName;
172
					}
173
				}
174
			}
166
		}
175
		}
167
		setPackageText(pName, true);
176
		setPackageText(pName, true);
168
177
Lines 504-510 Link Here
504
		if (fileComment != null) {
513
		if (fileComment != null) {
505
			content.append(fileComment);
514
			content.append(fileComment);
506
			content.append(lineDelimiter);
515
			content.append(lineDelimiter);
507
		} 
516
		}
508
517
509
		if (typeComment != null) {
518
		if (typeComment != null) {
510
			content.append(typeComment);
519
			content.append(typeComment);
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java (-4 / +58 lines)
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 571-576 Link Here
571
			// evaluate the enclosing type
571
			// evaluate the enclosing type
572
			project= elem.getJavaProject();
572
			project= elem.getJavaProject();
573
			pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
573
			pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
574
			if (pack == null) {
575
				pack= getPackage(elem);
576
			}
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 elem the selection used to initialize this page or <code>
637
	 * null</code> if no selection was available
638
	 * 
639
	 * @return the package fragment to be pre-filled in this page or <code>null</code> if it is not
640
	 *         applicable
641
	 */
642
	private IPackageFragment getPackage(IJavaElement elem) {
643
		String packName= null;
644
		final IPackageFragmentRoot froot= getPackageFragmentRoot();
645
		IJavaElement[] packages= null;
646
		try {
647
			if (froot != null && froot.exists()) {
648
				packages= froot.getChildren();
649
				if (packages != null) {
650
					if (packages.length == 1) { // length is 1 -> only default package -> use Project name
651
						if (elem.getJavaProject() != null) {
652
							packName= elem.getJavaProject().getElementName();
653
							// validate package name
654
							IStatus status= validatePackageName(packName, elem.getJavaProject());
655
							if (status.getSeverity() == IStatus.OK) {
656
								return froot.getPackageFragment(packName);
657
							}
658
						}
659
					} else {
660
						int noOfPackages= 0;
661
						IPackageFragment thePackage= null;
662
						for (final IJavaElement pack : packages) {
663
							IPackageFragment pkg= (IPackageFragment) pack;
664
							// ignoring empty parent packages and default package
665
							if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) {
666
								noOfPackages++;
667
								thePackage= pkg;
668
							}
669
						}
670
						if (noOfPackages == 1) { // exactly one package -> use package name
671
							packName= thePackage.getElementName();
672
							return froot.getPackageFragment(packName);
673
						}
674
					}
675
				}
676
			}
677
		} catch (JavaModelException e) {
678
			// fall through
679
		}
680
		return null;
681
	}
628
682
629
	private static IStatus validateJavaTypeName(String text, IJavaProject project) {
683
	private static IStatus validateJavaTypeName(String text, IJavaProject project) {
630
		if (project == null || !project.exists()) {
684
		if (project == null || !project.exists()) {
Lines 1509-1515 Link Here
1509
				if (resource.isVirtual()){
1563
				if (resource.isVirtual()){
1510
					status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageIsVirtual);
1564
					status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageIsVirtual);
1511
					return status;
1565
					return status;
1512
				}			
1566
				}
1513
				if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
1567
				if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
1514
					status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageNameFiltered);
1568
					status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageNameFiltered);
1515
					return status;
1569
					return status;

Return to bug 393161