summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDeepak Azad2012-03-12 15:13:30 (EDT)
committer Markus Keller2012-03-12 15:13:30 (EDT)
commit97f91896bd1a409a8d219bc43d88f5c330faecf5 (patch)
tree870d81db22ee89b2abba56f24c3dd56227680ebd
parent638b8cff16948311402105b301d004001a20c80d (diff)
downloadeclipse.jdt.ui-97f91896bd1a409a8d219bc43d88f5c330faecf5.zip
eclipse.jdt.ui-97f91896bd1a409a8d219bc43d88f5c330faecf5.tar.gz
eclipse.jdt.ui-97f91896bd1a409a8d219bc43d88f5c330faecf5.tar.bz2
Bug 287136: [quick fix][api] Make ASTRewriteCorrectionProposal and
parents public API preparation
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java6
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java52
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ASTRewriteCorrectionProposal.java20
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/CUCorrectionProposal.java82
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ChangeCorrectionProposal.java15
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/FixCorrectionProposal.java4
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/LinkedCorrectionProposal.java62
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/RefactoringCorrectionProposal.java73
8 files changed, 203 insertions, 111 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java
index 98f9cc9..8815d4a 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java
@@ -101,7 +101,6 @@ import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
-import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
@@ -161,6 +160,7 @@ import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionPro
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal;
+import org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal;
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
@@ -207,7 +207,7 @@ public class LocalCorrectionsSubProcessor {
if (refactoring.checkActivationBasics(astRoot).isOK()) {
String label= CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trycatch_description;
Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
- CUCorrectionProposal proposal= new CUCorrectionProposal(label, cu, (CompilationUnitChange) refactoring.createChange(null), 6, image);
+ RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, cu, refactoring, 6, image);
proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel());
proposals.add(proposal);
}
@@ -221,7 +221,7 @@ public class LocalCorrectionsSubProcessor {
if (refactoring.checkActivationBasics(astRoot).isOK()) {
String label= CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description;
Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
- CUCorrectionProposal proposal= new CUCorrectionProposal(label, cu, (CompilationUnitChange) refactoring.createChange(null), 7, image);
+ RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, cu, refactoring, 7, image);
proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel());
proposals.add(proposal);
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
index 64ac735..5feb583 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
@@ -25,21 +25,15 @@ import java.util.Map;
import org.eclipse.swt.graphics.Image;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.text.edits.InsertEdit;
import org.eclipse.jface.text.link.LinkedPositionGroup;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ltk.core.refactoring.Refactoring;
-import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.ltk.core.refactoring.TextChange;
-import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IBuffer;
@@ -156,12 +150,12 @@ import org.eclipse.jdt.internal.ui.fix.VariableDeclarationCleanUp;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ASTRewriteCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.AssignToVariableAssistProposal;
-import org.eclipse.jdt.internal.ui.text.correction.proposals.CUCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewDefiningMethodProposal;
+import org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.RenameRefactoringProposal;
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
@@ -2679,48 +2673,4 @@ public class QuickAssistProcessor implements IQuickAssistProcessor {
}
return true;
}
-
- private static class RefactoringCorrectionProposal extends CUCorrectionProposal {
- private final Refactoring fRefactoring;
- private RefactoringStatus fRefactoringStatus;
-
- public RefactoringCorrectionProposal(String name, ICompilationUnit cu, Refactoring refactoring, int relevance, Image image) {
- super(name, cu, null, relevance, image);
- fRefactoring= refactoring;
- }
-
- /**
- * Can be overridden by clients to perform expensive initializations of the refactoring
- *
- * @param refactoring the refactoring
- * @throws CoreException if something goes wrong during init
- */
- protected void init(Refactoring refactoring) throws CoreException {
- // empty default implementation
- }
-
- @Override
- protected TextChange createTextChange() throws CoreException {
- init(fRefactoring);
- fRefactoringStatus= fRefactoring.checkFinalConditions(new NullProgressMonitor());
- if (fRefactoringStatus.hasFatalError()) {
- TextFileChange dummyChange= new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$
- dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$
- return dummyChange;
- }
- return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
- }
-
- /*
- * @see org.eclipse.jdt.internal.ui.text.correction.proposals.CUCorrectionProposal#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
- * @since 3.6
- */
- @Override
- public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
- if (fRefactoringStatus != null && fRefactoringStatus.hasFatalError()) {
- return fRefactoringStatus.getEntryWithHighestSeverity().getMessage();
- }
- return super.getAdditionalProposalInfo(monitor);
- }
- }
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ASTRewriteCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ASTRewriteCorrectionProposal.java
index 968bbb6..fde85ff 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ASTRewriteCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ASTRewriteCorrectionProposal.java
@@ -27,6 +27,7 @@ import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
+import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.JavaUIStatus;
/**
@@ -59,8 +60,23 @@ public class ASTRewriteCorrectionProposal extends CUCorrectionProposal {
}
/**
+ * Constructs a AST rewrite correction proposal. Uses the default image for this proposal.
+ *
+ * @param name the display name of the proposal.
+ * @param cu the compilation unit that is modified.
+ * @param rewrite the AST rewrite that is invoked when the proposal is applied or
+ * <code>null</code> if {@link #getRewrite()} is overridden.
+ * @param relevance The relevance of this proposal.
+ */
+ public ASTRewriteCorrectionProposal(String name, ICompilationUnit cu, ASTRewrite rewrite, int relevance) {
+ this(name, cu, rewrite, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
+ }
+
+ /**
* Returns the import rewriter used for this compilation unit. <code>
+ *
* @return the import rewriter or <code>null</code> if no import rewriter is set
+ * @nooverride This method is not intended to be re-implemented or extended by clients.
*/
public ImportRewrite getImportRewrite() {
return fImportRewrite;
@@ -68,7 +84,9 @@ public class ASTRewriteCorrectionProposal extends CUCorrectionProposal {
/**
* Sets the import rewriter used for this compilation unit.
+ *
* @param rewrite the import rewriter
+ * @nooverride This method is not intended to be re-implemented or extended by clients.
*/
public void setImportRewrite(ImportRewrite rewrite) {
fImportRewrite= rewrite;
@@ -76,8 +94,10 @@ public class ASTRewriteCorrectionProposal extends CUCorrectionProposal {
/**
* Sets the import rewriter used for this compilation unit.
+ *
* @param astRoot the AST for the current CU
* @return returns the create import rewriter
+ * @nooverride This method is not intended to be re-implemented or extended by clients.
*/
public ImportRewrite createImportRewrite(CompilationUnit astRoot) {
fImportRewrite= StubUtility.createImportRewrite(astRoot, true);
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/CUCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/CUCorrectionProposal.java
index 0d669a6..48db139 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/CUCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/CUCorrectionProposal.java
@@ -23,16 +23,12 @@ import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.dialogs.ErrorDialog;
-import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.ITextViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.texteditor.ITextEditor;
-
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.ltk.core.refactoring.TextChange;
@@ -43,19 +39,15 @@ import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
-import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel;
-import org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup;
import org.eclipse.jdt.internal.corext.util.Resources;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.internal.ui.JavaPlugin;
-import org.eclipse.jdt.internal.ui.JavaUIStatus;
+import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
-import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
-import org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter;
/**
* A proposal for quick fixes and quick assist that work on a single compilation unit.
@@ -70,10 +62,8 @@ import org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter;
public class CUCorrectionProposal extends ChangeCorrectionProposal {
private ICompilationUnit fCompilationUnit;
- private LinkedProposalModel fLinkedProposalModel;
private boolean fSwitchedEditor;
-
/**
* Constructs a correction proposal working on a compilation unit with a given text change
*
@@ -92,7 +82,21 @@ public class CUCorrectionProposal extends ChangeCorrectionProposal {
throw new IllegalArgumentException("Compilation unit must not be null"); //$NON-NLS-1$
}
fCompilationUnit= cu;
- fLinkedProposalModel= null;
+ }
+
+ /**
+ * Constructs a correction proposal working on a compilation unit with a given text change. Uses
+ * the default image for this proposal.
+ *
+ * @param name the name that is displayed in the proposal selection dialog.
+ * @param cu the compilation unit on that the change works.
+ * @param change the change that is executed when the proposal is applied or <code>null</code>
+ * if implementors override {@link #addEdits(IDocument, TextEdit)} to provide the
+ * text edits or {@link #createTextChange()} to provide a text change.
+ * @param relevance the relevance of this proposal.
+ */
+ public CUCorrectionProposal(String name, ICompilationUnit cu, TextChange change, int relevance) {
+ this(name, cu, change, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
}
/**
@@ -124,17 +128,6 @@ public class CUCorrectionProposal extends ChangeCorrectionProposal {
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
}
- protected LinkedProposalModel getLinkedProposalModel() {
- if (fLinkedProposalModel == null) {
- fLinkedProposalModel= new LinkedProposalModel();
- }
- return fLinkedProposalModel;
- }
-
- public void setLinkedProposalModel(LinkedProposalModel model) {
- fLinkedProposalModel= model;
- }
-
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
StringBuffer buf= new StringBuffer();
@@ -199,36 +192,6 @@ public class CUCorrectionProposal extends ChangeCorrectionProposal {
return true;
}
- /* (non-Javadoc)
- * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#performChange(org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorPart)
- */
- @Override
- protected void performChange(IEditorPart part, IDocument document) throws CoreException {
- try {
- super.performChange(part, document);
- if (part == null) {
- return;
- }
-
- if (fLinkedProposalModel != null) {
- if (fLinkedProposalModel.hasLinkedPositions() && part instanceof JavaEditor) {
- // enter linked mode
- ITextViewer viewer= ((JavaEditor) part).getViewer();
- new LinkedProposalModelPresenter().enterLinkedMode(viewer, part, fSwitchedEditor, fLinkedProposalModel);
- } else if (part instanceof ITextEditor) {
- LinkedProposalPositionGroup.PositionInformation endPosition= fLinkedProposalModel.getEndPosition();
- if (endPosition != null) {
- // select a result
- int pos= endPosition.getOffset() + endPosition.getLength();
- ((ITextEditor) part).selectAndReveal(pos, 0);
- }
- }
- }
- } catch (BadLocationException e) {
- throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
- }
- }
-
/**
* Creates the text change for this proposal.
* This method is only called once and only when no text change has been passed in
@@ -315,4 +278,15 @@ public class CUCorrectionProposal extends ChangeCorrectionProposal {
}
return super.toString();
}
-} \ No newline at end of file
+
+ /**
+ * Returns whether the changed compilation unit was previously open in an editor or not.
+ *
+ * @return <code>true</code> if the changed compilation unit was not previously open in an
+ * editor, <code>false</code> if the changed compilation unit was already open in an
+ * editor
+ */
+ protected boolean isSwitchedEditor() {
+ return fSwitchedEditor;
+ }
+}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ChangeCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ChangeCorrectionProposal.java
index 3c9a32f..fdfc973 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ChangeCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/ChangeCorrectionProposal.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2011 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
@@ -49,6 +49,7 @@ import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.JavaPlugin;
+import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionCommandHandler;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
import org.eclipse.jdt.internal.ui.text.correction.ICommandAccess;
@@ -93,6 +94,18 @@ public class ChangeCorrectionProposal implements IJavaCompletionProposal, IComma
fCommandId= null;
}
+ /**
+ * Constructs a change correction proposal. Uses the default image for this proposal.
+ *
+ * @param name The name that is displayed in the proposal selection dialog.
+ * @param change The change that is executed when the proposal is applied or <code>null</code>
+ * if the change will be created by implementors of {@link #createChange()}.
+ * @param relevance The relevance of this proposal.
+ */
+ public ChangeCorrectionProposal(String name, Change change, int relevance) {
+ this(name, change, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
+ }
+
/*
* @see ICompletionProposal#apply(IDocument)
*/
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/FixCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/FixCorrectionProposal.java
index cd5ae05..c4a1e6f 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/FixCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/FixCorrectionProposal.java
@@ -42,9 +42,9 @@ import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
+import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.MultiFixTarget;
import org.eclipse.jdt.internal.corext.fix.ILinkedFix;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
-import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.MultiFixTarget;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.JavaElementImageDescriptor;
@@ -66,7 +66,7 @@ import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor;
* fix a problem. A fix correction proposal may have an {@link ICleanUp}
* attached which can be executed instead of the provided IFix.
*/
-public class FixCorrectionProposal extends CUCorrectionProposal implements ICompletionProposalExtension2, IStatusLineProposal {
+public class FixCorrectionProposal extends LinkedCorrectionProposal implements ICompletionProposalExtension2, IStatusLineProposal {
private final IProposableFix fFix;
private final ICleanUp fCleanUp;
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/LinkedCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/LinkedCorrectionProposal.java
index a2fa8f2..bf62633 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/LinkedCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/LinkedCorrectionProposal.java
@@ -12,11 +12,29 @@ package org.eclipse.jdt.internal.ui.text.correction.proposals;
import org.eclipse.swt.graphics.Image;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+
+import org.eclipse.ui.IEditorPart;
+
+import org.eclipse.ui.texteditor.ITextEditor;
+
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition;
+import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel;
+import org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup;
+
+import org.eclipse.jdt.internal.ui.JavaUIStatus;
+import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
+import org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter;
+
/**
* A proposal for quick fixes and quick assists that works on a AST rewriter and enters the
@@ -28,6 +46,8 @@ import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition;
*/
public class LinkedCorrectionProposal extends ASTRewriteCorrectionProposal {
+ private LinkedProposalModel fLinkedProposalModel;
+
/**
* Constructs a linked correction proposal.
* @param name The display name of the proposal.
@@ -40,6 +60,18 @@ public class LinkedCorrectionProposal extends ASTRewriteCorrectionProposal {
*/
public LinkedCorrectionProposal(String name, ICompilationUnit cu, ASTRewrite rewrite, int relevance, Image image) {
super(name, cu, rewrite, relevance, image);
+ fLinkedProposalModel= null;
+ }
+
+ protected LinkedProposalModel getLinkedProposalModel() {
+ if (fLinkedProposalModel == null) {
+ fLinkedProposalModel= new LinkedProposalModel();
+ }
+ return fLinkedProposalModel;
+ }
+
+ public void setLinkedProposalModel(LinkedProposalModel model) {
+ fLinkedProposalModel= model;
}
/**
@@ -106,4 +138,34 @@ public class LinkedCorrectionProposal extends ASTRewriteCorrectionProposal {
public void addLinkedPositionProposal(String groupID, ITypeBinding type) {
getLinkedProposalModel().getPositionGroup(groupID, true).addProposal(type, getCompilationUnit(), 10);
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#performChange(org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorPart)
+ */
+ @Override
+ protected void performChange(IEditorPart part, IDocument document) throws CoreException {
+ try {
+ super.performChange(part, document);
+ if (part == null) {
+ return;
+ }
+
+ if (fLinkedProposalModel != null) {
+ if (fLinkedProposalModel.hasLinkedPositions() && part instanceof JavaEditor) {
+ // enter linked mode
+ ITextViewer viewer= ((JavaEditor) part).getViewer();
+ new LinkedProposalModelPresenter().enterLinkedMode(viewer, part, isSwitchedEditor(), fLinkedProposalModel);
+ } else if (part instanceof ITextEditor) {
+ LinkedProposalPositionGroup.PositionInformation endPosition= fLinkedProposalModel.getEndPosition();
+ if (endPosition != null) {
+ // select a result
+ int pos= endPosition.getOffset() + endPosition.getLength();
+ ((ITextEditor) part).selectAndReveal(pos, 0);
+ }
+ }
+ }
+ } catch (BadLocationException e) {
+ throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
+ }
+ }
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/RefactoringCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/RefactoringCorrectionProposal.java
new file mode 100644
index 0000000..1f02926
--- /dev/null
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/RefactoringCorrectionProposal.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.internal.ui.text.correction.proposals;
+
+import org.eclipse.swt.graphics.Image;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+
+import org.eclipse.core.resources.IFile;
+
+import org.eclipse.text.edits.InsertEdit;
+
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.TextChange;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+
+
+public class RefactoringCorrectionProposal extends LinkedCorrectionProposal {
+ private final Refactoring fRefactoring;
+ private RefactoringStatus fRefactoringStatus;
+
+ public RefactoringCorrectionProposal(String name, ICompilationUnit cu, Refactoring refactoring, int relevance, Image image) {
+ super(name, cu, null, relevance, image);
+ fRefactoring= refactoring;
+ }
+
+ /**
+ * Can be overridden by clients to perform expensive initializations of the refactoring
+ *
+ * @param refactoring the refactoring
+ * @throws CoreException if something goes wrong during init
+ */
+ protected void init(Refactoring refactoring) throws CoreException {
+ // empty default implementation
+ }
+
+ @Override
+ protected TextChange createTextChange() throws CoreException {
+ init(fRefactoring);
+ fRefactoringStatus= fRefactoring.checkFinalConditions(new NullProgressMonitor());
+ if (fRefactoringStatus.hasFatalError()) {
+ TextFileChange dummyChange= new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$
+ dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$
+ return dummyChange;
+ }
+ return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
+ }
+
+ /*
+ * @see org.eclipse.jdt.internal.ui.text.correction.proposals.CUCorrectionProposal#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
+ * @since 3.6
+ */
+ @Override
+ public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
+ if (fRefactoringStatus != null && fRefactoringStatus.hasFatalError()) {
+ return fRefactoringStatus.getEntryWithHighestSeverity().getMessage();
+ }
+ return super.getAdditionalProposalInfo(monitor);
+ }
+} \ No newline at end of file