summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoopur Gupta2013-01-14 08:54:08 (EST)
committerDani Megert2013-01-14 08:54:08 (EST)
commitd473e3cfdf27b69a0689587fb0054f40a2643ea7 (patch)
tree97884ea617b82c57c159677e9b25d6b987327095
parent54032a9e7765889b0276b390626a9fbb2877336c (diff)
downloadeclipse.jdt.ui-d473e3cfdf27b69a0689587fb0054f40a2643ea7.zip
eclipse.jdt.ui-d473e3cfdf27b69a0689587fb0054f40a2643ea7.tar.gz
eclipse.jdt.ui-d473e3cfdf27b69a0689587fb0054f40a2643ea7.tar.bz2
Fixed bug 393161: [type wizards] Pre-fill package name in the new classv20130114-135408I20130115-1300I20130115-0800
dialog
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java40
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java64
2 files changed, 90 insertions, 14 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java
index 537df59..1cf7a06 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewPackageWizardPage.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -161,8 +161,27 @@ public class NewPackageWizardPage extends NewContainerWizardPage {
String pName= ""; //$NON-NLS-1$
if (jelem != null) {
IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
- if (pf != null && !pf.isDefaultPackage())
+ if (pf != null && !pf.isDefaultPackage()) {
pName= pf.getElementName();
+ } else {
+ if (jelem.getJavaProject() != null) {
+ final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot();
+ if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
+ try {
+ IJavaElement[] packages= pkgFragmentRoot.getChildren();
+ if (packages.length == 1) { // only default package
+ String prName= jelem.getJavaProject().getElementName();
+ IStatus status= getPackageStatus(prName);
+ if (status.getSeverity() == IStatus.OK) {
+ pName= prName;
+ }
+ }
+ } catch (JavaModelException e) {
+ // fall through
+ }
+ }
+ }
+ }
}
setPackageText(pName, true);
@@ -247,7 +266,7 @@ public class NewPackageWizardPage extends NewContainerWizardPage {
// --------- IDialogFieldListener
public void dialogFieldChanged(DialogField field) {
- fPackageStatus= packageChanged();
+ fPackageStatus= getPackageStatus(getPackageText());
// tell all others
handleFieldChanged(PACKAGE);
}
@@ -262,7 +281,7 @@ public class NewPackageWizardPage extends NewContainerWizardPage {
protected void handleFieldChanged(String fieldName) {
super.handleFieldChanged(fieldName);
if (fieldName == CONTAINER) {
- fPackageStatus= packageChanged();
+ fPackageStatus= getPackageStatus(getPackageText());
}
// do status line update
updateStatus(new IStatus[] { fContainerStatus, fPackageStatus });
@@ -278,12 +297,15 @@ public class NewPackageWizardPage extends NewContainerWizardPage {
return JavaConventionsUtil.validatePackageName(text, project);
}
- /*
- * Verifies the input for the package field.
+ /**
+ * Validates the package name and returns the status of the validation.
+ *
+ * @param packName the package name
+ *
+ * @return the status of the validation
*/
- private IStatus packageChanged() {
+ private IStatus getPackageStatus(String packName) {
StatusInfo status= new StatusInfo();
- String packName= getPackageText();
if (packName.length() > 0) {
IStatus val= validatePackageName(packName);
if (val.getSeverity() == IStatus.ERROR) {
@@ -504,7 +526,7 @@ public class NewPackageWizardPage extends NewContainerWizardPage {
if (fileComment != null) {
content.append(fileComment);
content.append(lineDelimiter);
- }
+ }
if (typeComment != null) {
content.append(typeComment);
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java
index df804dc..0c437d5 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -568,9 +568,12 @@ public abstract class NewTypeWizardPage extends NewContainerWizardPage {
IType enclosingType= null;
if (elem != null) {
- // evaluate the enclosing type
project= elem.getJavaProject();
pack= (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
+ if (pack == null && project != null) {
+ pack= getPackage(project);
+ }
+ // evaluate the enclosing type
IType typeInCU= (IType) elem.getAncestor(IJavaElement.TYPE);
if (typeInCU != null) {
if (typeInCU.getCompilationUnit() != null) {
@@ -623,8 +626,59 @@ public abstract class NewTypeWizardPage extends NewContainerWizardPage {
setAddComments(StubUtility.doAddComments(project), true); // from project or workspace
}
-
-
+ /**
+ * Checks if the package field has to be pre-filled in this page and returns the package
+ * fragment to be used for that. The package fragment has the name of the project if the source
+ * folder does not contain any package and if the project name is a valid package name. If the
+ * source folder contains exactly one package then the name of that package is used as the
+ * package fragment's name. <code>null</code> is returned if none of the above is applicable.
+ *
+ * @param javaProject the containing Java project of the selection used to initialize this page
+ *
+ * @return the package fragment to be pre-filled in this page or <code>null</code> if no
+ * suitable package can be suggested for the given project
+ *
+ * @since 3.9
+ */
+ private IPackageFragment getPackage(IJavaProject javaProject) {
+ String packName= null;
+ final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot();
+ IJavaElement[] packages= null;
+ try {
+ if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
+ packages= pkgFragmentRoot.getChildren();
+ if (packages.length == 1) { // only default package -> use Project name
+ packName= javaProject.getElementName();
+ // validate package name
+ IStatus status= validatePackageName(packName, javaProject);
+ if (status.getSeverity() == IStatus.OK) {
+ return pkgFragmentRoot.getPackageFragment(packName);
+ }
+ } else {
+ int noOfPackages= 0;
+ IPackageFragment thePackage= null;
+ for (final IJavaElement pack : packages) {
+ IPackageFragment pkg= (IPackageFragment) pack;
+ // ignoring empty parent packages and default package
+ if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) {
+ noOfPackages++;
+ thePackage= pkg;
+ if (noOfPackages > 1) {
+ return null;
+ }
+ }
+ }
+ if (noOfPackages == 1) { // use package name
+ packName= thePackage.getElementName();
+ return pkgFragmentRoot.getPackageFragment(packName);
+ }
+ }
+ }
+ } catch (JavaModelException e) {
+ // fall through
+ }
+ return null;
+ }
private static IStatus validateJavaTypeName(String text, IJavaProject project) {
if (project == null || !project.exists()) {
@@ -1509,7 +1563,7 @@ public abstract class NewTypeWizardPage extends NewContainerWizardPage {
if (resource.isVirtual()){
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageIsVirtual);
return status;
- }
+ }
if (!ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_PackageNameFiltered);
return status;