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 (-4 / +34 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
					final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot();
169
					if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
170
						try {
171
							IJavaElement[] packages= pkgFragmentRoot.getChildren();
172
							if (packages.length == 1) { // only default package
173
								String prName= jelem.getJavaProject().getElementName();
174
								IStatus status= getPackageStatus(prName);
175
								if (status.getSeverity() == IStatus.OK) {
176
									pName= prName;
177
								}
178
							}
179
						} catch (JavaModelException e) {
180
							// fall through
181
						}
182
					}
183
				}
184
			}
166
		}
185
		}
167
		setPackageText(pName, true);
186
		setPackageText(pName, true);
168
187
Lines 282-289 Link Here
282
	 * Verifies the input for the package field.
301
	 * Verifies the input for the package field.
283
	 */
302
	 */
284
	private IStatus packageChanged() {
303
	private IStatus packageChanged() {
285
		StatusInfo status= new StatusInfo();
286
		String packName= getPackageText();
304
		String packName= getPackageText();
305
		return getPackageStatus(packName);
306
	}
307
308
	/**
309
	 * Validates the package name and returns the status of the validation.
310
	 * 
311
	 * @param packName the package name
312
	 * 
313
	 * @return the status of the validation
314
	 */
315
	private IStatus getPackageStatus(String packName) {
316
		StatusInfo status= new StatusInfo();
287
		if (packName.length() > 0) {
317
		if (packName.length() > 0) {
288
			IStatus val= validatePackageName(packName);
318
			IStatus val= validatePackageName(packName);
289
			if (val.getSeverity() == IStatus.ERROR) {
319
			if (val.getSeverity() == IStatus.ERROR) {
Lines 504-510 Link Here
504
		if (fileComment != null) {
534
		if (fileComment != null) {
505
			content.append(fileComment);
535
			content.append(fileComment);
506
			content.append(lineDelimiter);
536
			content.append(lineDelimiter);
507
		} 
537
		}
508
538
509
		if (typeComment != null) {
539
		if (typeComment != null) {
510
			content.append(typeComment);
540
			content.append(typeComment);
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java (-5 / +57 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 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;

Return to bug 393161