Fixed bug 308217: [content assist] Override method proposal not working
if '(' is typed while content assist is open

Change-Id: I105451c4e8ab953cd4615b7fca356cd5f3381568
Signed-off-by: Noopur Gupta <noopur_gupta@in.ibm.com>
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitEditor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitEditor.java
index a70bdde..a0dd3b8 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitEditor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitEditor.java
@@ -66,6 +66,8 @@
 import org.eclipse.jface.text.TabsToSpacesConverter;
 import org.eclipse.jface.text.TextUtilities;
 import org.eclipse.jface.text.contentassist.ContentAssistant;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
 import org.eclipse.jface.text.contentassist.IContentAssistant;
 import org.eclipse.jface.text.formatter.FormattingContextProperties;
 import org.eclipse.jface.text.formatter.IFormattingContext;
@@ -137,8 +139,10 @@
 import org.eclipse.jdt.internal.ui.text.SmartBackspaceManager;
 import org.eclipse.jdt.internal.ui.text.Symbols;
 import org.eclipse.jdt.internal.ui.text.correction.CorrectionCommandInstaller;
+import org.eclipse.jdt.internal.ui.text.java.ContentAssistProcessor;
 import org.eclipse.jdt.internal.ui.text.java.IJavaReconcilingListener;
 import org.eclipse.jdt.internal.ui.text.java.JavaFormattingContext;
+import org.eclipse.jdt.internal.ui.text.java.OverrideCompletionProposal;
 
 
 /**
@@ -273,10 +277,24 @@
 				// when entering an anonymous class between the parenthesis', we don't want
 				// to jump after the closing parenthesis when return is pressed
 				if (event.character == SWT.CR && offset > 0) {
-					IDocument document= getSourceViewer().getDocument();
+					ISourceViewer sourceViewer= getSourceViewer();
+					IDocument document= sourceViewer.getDocument();
 					try {
 						if (document.getChar(offset - 1) == '{')
 							return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
+
+						// see bug 308217: while overriding a method and using '(' followed by parameter type to filter the content assist proposals, if ')' is added 
+						// automatically on typing '(', pressing return key should not result in jumping after ')' instead of applying the selected proposal.
+						if (document.getChar(offset) == ')' && sourceViewer instanceof AdaptedSourceViewer && ((AdaptedSourceViewer) sourceViewer).getContentAssistant() instanceof ContentAssistant) {
+							ContentAssistant contentAssistant= (ContentAssistant) ((AdaptedSourceViewer) sourceViewer).getContentAssistant();
+							IContentAssistProcessor processor= contentAssistant.getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE);
+							if (processor instanceof ContentAssistProcessor) {
+								ICompletionProposal proposal= ((ContentAssistProcessor) processor).getSelectedProposal();
+								if (proposal instanceof OverrideCompletionProposal) {
+									return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
+								}
+							}
+						}
 					} catch (BadLocationException e) {
 					}
 				}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java
index ed326c8..fe65607 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2014 IBM Corporation and others.
+ * Copyright (c) 2005, 2016 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
@@ -177,6 +177,7 @@
 				cat.sessionEnded();
 			}
 
+			fSelectedProposal= null;
 			fCategoryIteration= null;
 			fRepetition= -1;
 			fIterationGesture= null;
@@ -197,6 +198,7 @@
 		 */
 		@Override
 		public void selectionChanged(ICompletionProposal proposal, boolean smartToggle) {
+			fSelectedProposal= proposal;
 		}
 
 		/*
@@ -229,6 +231,7 @@
 	private final List<CompletionProposalCategory> fCategories;
 	private final String fPartition;
 	private final ContentAssistant fAssistant;
+	private ICompletionProposal fSelectedProposal;
 
 	private char[] fCompletionAutoActivationCharacters;
 
@@ -698,4 +701,14 @@
 		}
 	}
 
+	/**
+	 * Returns the proposal selected in the proposal selector.
+	 *
+	 * @return the selected proposal or <code>null</code> if none
+	 * @since 3.12
+	 */
+	public ICompletionProposal getSelectedProposal() {
+		return fSelectedProposal;
+	}
+
 }
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/OverrideCompletionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/OverrideCompletionProposal.java
index f9e7a2c..b57849d 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/OverrideCompletionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/OverrideCompletionProposal.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 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
@@ -56,6 +56,7 @@
 import org.eclipse.jdt.internal.ui.JavaPlugin;
 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
+import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;
 
 public class OverrideCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 {
 
@@ -140,6 +141,7 @@
 		ITypeBinding declaringType= null;
 		ChildListPropertyDescriptor descriptor= null;
 		ASTNode node= NodeFinder.perform(unit, offset, 1);
+		node= ASTResolving.findParentType(node);
 		if (node instanceof AnonymousClassDeclaration) {
 			declaringType= ((AnonymousClassDeclaration) node).resolveBinding();
 			descriptor= AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
@@ -170,6 +172,11 @@
 					String indent= getIndentAt(document, getReplacementOffset(), settings);
 					setReplacementString(IndentManipulation.changeIndent(generatedCode, generatedIndent, settings.tabWidth, settings.indentWidth, indent, TextUtilities.getDefaultLineDelimiter(document)));
 
+					int replacementLength= getReplacementLength();
+					if (document.get(getReplacementOffset() + replacementLength, 1).equals(")")) { //$NON-NLS-1$
+						setReplacementLength(replacementLength + 1);
+					}
+
 				} catch (MalformedTreeException exception) {
 					JavaPlugin.log(exception);
 				} catch (BadLocationException exception) {