Fixed bug 348085 - [rewrite] creating Internal Role pattern at deep
nesting creates bogus base import for role
diff --git a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/RoleCreator.java b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/RoleCreator.java
index 89a4a48..7599cbb 100644
--- a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/RoleCreator.java
+++ b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/RoleCreator.java
@@ -23,10 +23,12 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.dom.ITypeBinding;
 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
 import org.eclipse.jdt.internal.corext.refactoring.TypeContextChecker;
+import org.eclipse.objectteams.otdt.core.OTModelManager;
 import org.eclipse.objectteams.otdt.internal.ui.OTDTUIPluginConstants;
 
 
@@ -77,8 +79,13 @@
 				}
 			}
 			if (binding != null) {
-				buf.append(imports.addImport(binding));
-				imports.setImportBase(binding);
+				if (hasCommonEnclosingTeam(currentType, binding)) {
+					// no importing
+					buf.append(binding.getName());				
+				} else {
+					buf.append(imports.addImport(binding));
+					imports.setImportBase(binding);
+				}
 			} else {
 				buf.append(imports.addImportBase(baseName));
 			}
@@ -87,6 +94,22 @@
 	}
 
 
+	private boolean hasCommonEnclosingTeam(IType type, ITypeBinding binding) {
+		IJavaElement currentElement = type;
+		while ((currentElement = currentElement.getParent()) instanceof IType) {
+			IType currentType = (IType) currentElement;
+			if (OTModelManager.isTeam(currentType)) {
+				ITypeBinding currentBinding = binding;
+				while ((currentBinding = currentBinding.getDeclaringClass()) != null) {
+					if (currentBinding.getQualifiedName().equals(currentType.getFullyQualifiedName('.')))
+						return true;
+				}
+			}
+		}
+		return false;
+	}
+
+
 	protected void validateTypeCreation()
 		throws CoreException
 	{		
diff --git a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
index 71383a3..2852299 100644
--- a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
+++ b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
@@ -16,6 +16,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -679,13 +680,27 @@
 			ICompilationUnit cu= enclosingType.getCompilationUnit();
 			StringBuffer teamString = new StringBuffer();
 			teamString.append("package ").append(packageFragment.getElementName()).append(";\n"); //$NON-NLS-1$ //$NON-NLS-2$
-			teamString.append("public class ").append(enclosingType.getElementName()).append(" {}\n"); //$NON-NLS-1$ //$NON-NLS-2$
+			teamString.append("public"); //$NON-NLS-1$
+			int levels = writeClassHeader(enclosingType, teamString);
+			for (int i = 0; i < levels; i++)
+				teamString.append('}');
+			//class ").append(enclosingType.getElementName()).append(" {}\n"); //$NON-NLS-1$ //$NON-NLS-2$
 			
 			String prolog= "class " + typeName + (isInterface ? " implements " : " extends "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 			String epilog= " {} "; //$NON-NLS-1$
 			return new StubTypeContext(cu, teamString.toString() + prolog, epilog);
 		}
 	}
+	
+	private int writeClassHeader(IType type, StringBuffer buf) {
+		IJavaElement parent = type.getParent();
+		int nestingLevels = 1;
+		if (parent instanceof IType) {
+			nestingLevels += writeClassHeader((IType) parent, buf);
+		}
+		buf.append(" class ").append(type.getElementName()).append(" {"); //$NON-NLS-1$ //$NON-NLS-2$
+		return nestingLevels;
+	}
 
 	private void writeSuperInterfaces(StringBuffer buf, ImportsManager imports) throws CoreException 
 	{
diff --git a/releng/map/otdt.map b/releng/map/otdt.map
index 8b72d99..1f06fa1 100644
--- a/releng/map/otdt.map
+++ b/releng/map/otdt.map
@@ -13,7 +13,7 @@
 

 plugin@org.eclipse.objectteams.otdt.debug,2.0.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.debug,tag=builds/201105161939

 plugin@org.eclipse.objectteams.otdt.debug.ui,2.0.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.debug.ui,tag=builds/201105161939

-plugin@org.eclipse.objectteams.otdt.ui,2.1.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.ui,tag=builds/201109201811

+plugin@org.eclipse.objectteams.otdt.ui,2.1.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.ui

 plugin@org.eclipse.objectteams.otdt.doc,2.1.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.doc,tag=builds/201109201811

 plugin@org.eclipse.objectteams.otdt.metrics,0.7.0=GIT,repo=git://git.eclipse.org/gitroot/objectteams/org.eclipse.objectteams.git,path=plugins/org.eclipse.objectteams.otdt.metrics,tag=builds/201101290806