]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2012 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.text.correction.proposals; | |
12 | ||
13 | import org.eclipse.swt.graphics.Image; | |
14 | ||
15 | import org.eclipse.core.runtime.CoreException; | |
16 | import org.eclipse.core.runtime.IStatus; | |
17 | ||
18 | import org.eclipse.jface.text.BadLocationException; | |
19 | import org.eclipse.jface.text.IDocument; | |
20 | import org.eclipse.jface.text.ITextViewer; | |
21 | ||
22 | import org.eclipse.ui.IEditorPart; | |
23 | ||
24 | import org.eclipse.ui.texteditor.ITextEditor; | |
25 | ||
26 | import org.eclipse.jdt.core.ICompilationUnit; | |
27 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
28 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
29 | import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition; | |
30 | ||
31 | import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel; | |
32 | import org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup; | |
33 | ||
34 | import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal; | |
35 | ||
36 | import org.eclipse.jdt.internal.ui.JavaUIStatus; | |
37 | import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | |
38 | import org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter; | |
39 | ||
40 | ||
41 | /** | |
42 | * A proposal for quick fixes and quick assists that works on a AST rewriter and enters the | |
43 | * linked mode when the proposal is set up. | |
44 | * Either a rewriter is directly passed in the constructor or method {@link #getRewrite()} is overridden | |
45 | * to provide the AST rewriter that is evaluated to the document when the proposal is | |
46 | * applied. | |
47 | * @since 3.0 | |
48 | */ | |
49 | public class LinkedCorrectionProposal extends ASTRewriteCorrectionProposal { | |
50 | ||
51 | private LinkedProposalModel fLinkedProposalModel; | |
52 | ||
53 | /** | |
54 | * Constructs a linked correction proposal. | |
55 | * @param name The display name of the proposal. | |
56 | * @param cu The compilation unit that is modified. | |
57 | * @param rewrite The AST rewrite that is invoked when the proposal is applied | |
58 | * <code>null</code> can be passed if {@link #getRewrite()} is overridden. | |
59 | * @param relevance The relevance of this proposal. | |
60 | * @param image The image that is displayed for this proposal or <code>null</code> if no | |
61 | * image is desired. | |
62 | */ | |
63 | public LinkedCorrectionProposal(String name, ICompilationUnit cu, ASTRewrite rewrite, int relevance, Image image) { | |
64 | super(name, cu, rewrite, relevance, image); | |
65 | fLinkedProposalModel= null; | |
66 | } | |
67 | ||
68 | protected LinkedProposalModel getLinkedProposalModel() { | |
69 | if (fLinkedProposalModel == null) { | |
70 | fLinkedProposalModel= new LinkedProposalModel(); | |
71 | } | |
72 | return fLinkedProposalModel; | |
73 | } | |
74 | ||
75 | public void setLinkedProposalModel(LinkedProposalModel model) { | |
76 | fLinkedProposalModel= model; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Adds a linked position to be shown when the proposal is applied. All positions with the | |
81 | * same group id are linked. | |
82 | * @param position The position to add. | |
83 | * @param isFirst If set, the proposal is jumped to first. | |
84 | * @param groupID The id of the group the proposal belongs to. All proposals in the same group | |
85 | * are linked. | |
86 | */ | |
87 | public void addLinkedPosition(ITrackedNodePosition position, boolean isFirst, String groupID) { | |
88 | getLinkedProposalModel().getPositionGroup(groupID, true).addPosition(position, isFirst); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Adds a linked position to be shown when the proposal is applied. All positions with the | |
93 | * same group id are linked. | |
94 | * @param position The position to add. | |
95 | * @param sequenceRank The sequence rank, see TODO. | |
96 | * @param groupID The id of the group the proposal belongs to. All proposals in the same group | |
97 | * are linked. | |
98 | */ | |
99 | public void addLinkedPosition(ITrackedNodePosition position, int sequenceRank, String groupID) { | |
100 | getLinkedProposalModel().getPositionGroup(groupID, true).addPosition(position, sequenceRank); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Sets the end position of the linked mode to the end of the passed range. | |
105 | * @param position The position that describes the end position of the linked mode. | |
106 | */ | |
107 | public void setEndPosition(ITrackedNodePosition position) { | |
108 | getLinkedProposalModel().setEndPosition(position); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Adds a linked position proposal to the group with the given id. | |
113 | * @param groupID The id of the group that should present the proposal | |
114 | * @param proposal The string to propose. | |
115 | * @param image The image to show for the position proposal or <code>null</code> if | |
116 | * no image is desired. | |
117 | */ | |
118 | public void addLinkedPositionProposal(String groupID, String proposal, Image image) { | |
119 | getLinkedProposalModel().getPositionGroup(groupID, true).addProposal(proposal, image, 10); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Adds a linked position proposal to the group with the given id. | |
124 | * @param groupID The id of the group that should present the proposal | |
125 | * @param displayString The name of the proposal | |
126 | * @param proposal The string to insert. | |
127 | * @param image The image to show for the position proposal or <code>null</code> if | |
128 | * no image is desired. | |
129 | * @deprecated use {@link #addLinkedPositionProposal(String, String, Image)} instead | |
130 | */ | |
131 | public void addLinkedPositionProposal(String groupID, String displayString, String proposal, Image image) { | |
132 | addLinkedPositionProposal(groupID, proposal, image); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Adds a linked position proposal to the group with the given id. | |
137 | * @param groupID The id of the group that should present the proposal | |
138 | * @param type The binding to use as type name proposal. | |
139 | */ | |
140 | public void addLinkedPositionProposal(String groupID, ITypeBinding type) { | |
141 | getLinkedProposalModel().getPositionGroup(groupID, true).addProposal(type, getCompilationUnit(), 10); | |
142 | } | |
143 | ||
144 | /* (non-Javadoc) | |
145 | * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#performChange(org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorPart) | |
146 | */ | |
147 | @Override | |
148 | protected void performChange(IEditorPart part, IDocument document) throws CoreException { | |
149 | try { | |
150 | super.performChange(part, document); | |
151 | if (part == null) { | |
152 | return; | |
153 | } | |
154 | ||
155 | if (fLinkedProposalModel != null) { | |
156 | if (fLinkedProposalModel.hasLinkedPositions() && part instanceof JavaEditor) { | |
157 | // enter linked mode | |
158 | ITextViewer viewer= ((JavaEditor) part).getViewer(); | |
159 | new LinkedProposalModelPresenter().enterLinkedMode(viewer, part, didOpenEditor(), fLinkedProposalModel); | |
160 | } else if (part instanceof ITextEditor) { | |
161 | LinkedProposalPositionGroup.PositionInformation endPosition= fLinkedProposalModel.getEndPosition(); | |
162 | if (endPosition != null) { | |
163 | // select a result | |
164 | int pos= endPosition.getOffset() + endPosition.getLength(); | |
165 | ((ITextEditor) part).selectAndReveal(pos, 0); | |
166 | } | |
167 | } | |
168 | } | |
169 | } catch (BadLocationException e) { | |
170 | throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); | |
171 | } | |
172 | } | |
173 | } |