KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ChangeCorrectionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Point;
21
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRewriteTarget;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.jface.text.link.LinkedModeModel;
26
27 import org.eclipse.ui.IEditorPart;
28
29 import org.eclipse.ltk.core.refactoring.Change;
30 import org.eclipse.ltk.core.refactoring.IUndoManager;
31 import org.eclipse.ltk.core.refactoring.NullChange;
32 import org.eclipse.ltk.core.refactoring.RefactoringCore;
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34
35 import org.eclipse.jdt.internal.corext.util.Messages;
36
37 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
38
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41
42 /**
43  * Implementation of a Java completion proposal to be used for quick fix and quick assist
44  * proposals that invoke a {@link Change}. The proposal offers a proposal information but no context
45  * information.
46  *
47  * @since 3.2
48  */

49 public class ChangeCorrectionProposal implements IJavaCompletionProposal, ICommandAccess {
50
51     private Change fChange;
52     private String JavaDoc fName;
53     private int fRelevance;
54     private Image fImage;
55     private String JavaDoc fCommandId;
56
57     /**
58      * Constructs a change correction proposal.
59      *
60      * @param name The name that is displayed in the proposal selection dialog.
61      * @param change The change that is executed when the proposal is applied or <code>null</code>
62      * if the change will be created by implementors of {@link #createChange()}.
63      * @param relevance The relevance of this proposal.
64      * @param image The image that is displayed for this proposal or <code>null</code> if no
65      * image is desired.
66      */

67     public ChangeCorrectionProposal(String JavaDoc name, Change change, int relevance, Image image) {
68         if (name == null) {
69             throw new IllegalArgumentException JavaDoc("Name must not be null"); //$NON-NLS-1$
70
}
71         fName= name;
72         fChange= change;
73         fRelevance= relevance;
74         fImage= image;
75         fCommandId= null;
76     }
77
78     /*
79      * @see ICompletionProposal#apply(IDocument)
80      */

81     public void apply(IDocument document) {
82         try {
83             performChange(JavaPlugin.getActivePage().getActiveEditor(), document);
84         } catch (CoreException e) {
85             ExceptionHandler.handle(e, CorrectionMessages.ChangeCorrectionProposal_error_title, CorrectionMessages.ChangeCorrectionProposal_error_message);
86         }
87     }
88
89     /**
90      * Performs the change associated with this proposal.
91      *
92      * @param activeEditor The editor currently active or <code>null</code> if no
93      * editor is active.
94      * @param document The document of the editor currently active or <code>null</code> if
95      * no editor is visible.
96      * @throws CoreException Thrown when the invocation of the change failed.
97      */

98     protected void performChange(IEditorPart activeEditor, IDocument document) throws CoreException {
99         Change change= null;
100         IRewriteTarget rewriteTarget= null;
101         try {
102             change= getChange();
103             if (change != null) {
104                 if (document != null) {
105                     LinkedModeModel.closeAllModels(document);
106                 }
107                 if (activeEditor != null) {
108                     rewriteTarget= (IRewriteTarget) activeEditor.getAdapter(IRewriteTarget.class);
109                     if (rewriteTarget != null) {
110                         rewriteTarget.beginCompoundChange();
111                     }
112                 }
113
114                 change.initializeValidationData(new NullProgressMonitor());
115                 RefactoringStatus valid= change.isValid(new NullProgressMonitor());
116                 if (valid.hasFatalError()) {
117                     IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
118                         valid.getMessageMatchingSeverity(RefactoringStatus.FATAL), null);
119                     throw new CoreException(status);
120                 } else {
121                     IUndoManager manager= RefactoringCore.getUndoManager();
122                     manager.aboutToPerformChange(change);
123                     Change undoChange= change.perform(new NullProgressMonitor());
124                     manager.changePerformed(change, true);
125                     if (undoChange != null) {
126                         undoChange.initializeValidationData(new NullProgressMonitor());
127                         manager.addUndo(getName(), undoChange);
128                     }
129                 }
130             }
131         } finally {
132             if (rewriteTarget != null) {
133                 rewriteTarget.endCompoundChange();
134             }
135
136             if (change != null) {
137                 change.dispose();
138             }
139         }
140     }
141
142     /*
143      * @see ICompletionProposal#getAdditionalProposalInfo()
144      */

145     public String JavaDoc getAdditionalProposalInfo() {
146         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
147         buf.append("<p>"); //$NON-NLS-1$
148
try {
149             Change change= getChange();
150             if (change != null) {
151                 String JavaDoc name= change.getName();
152                 if (name.length() == 0) {
153                     return null;
154                 }
155                 buf.append(name);
156             } else {
157                 return null;
158             }
159         } catch (CoreException e) {
160             buf.append("Unexpected error when accessing this proposal:<p><pre>"); //$NON-NLS-1$
161
buf.append(e.getLocalizedMessage());
162             buf.append("</pre>"); //$NON-NLS-1$
163
}
164         buf.append("</p>"); //$NON-NLS-1$
165
return buf.toString();
166     }
167
168     /*
169      * @see ICompletionProposal#getContextInformation()
170      */

171     public IContextInformation getContextInformation() {
172         return null;
173     }
174
175     /*
176      * @see ICompletionProposal#getDisplayString()
177      */

178     public String JavaDoc getDisplayString() {
179         String JavaDoc shortCutString= CorrectionCommandHandler.getShortCutString(getCommandId());
180         if (shortCutString != null) {
181             return Messages.format(CorrectionMessages.ChangeCorrectionProposal_name_with_shortcut, new String JavaDoc[] { getName(), shortCutString });
182         }
183         return getName();
184     }
185     
186     /**
187      * Returns the name of the proposal.
188      *
189      * @return return the name of the proposal
190      */

191     public String JavaDoc getName() {
192         return fName;
193     }
194     
195     /*
196      * @see ICompletionProposal#getImage()
197      */

198     public Image getImage() {
199         return fImage;
200     }
201
202     /*
203      * @see ICompletionProposal#getSelection(IDocument)
204      */

205     public Point getSelection(IDocument document) {
206         return null;
207     }
208
209     /**
210      * Sets the proposal's image or <code>null</code> if no image is desired.
211      *
212      * @param image the desired image.
213      */

214     public void setImage(Image image) {
215         fImage= image;
216     }
217
218     /**
219      * Returns the change that will be executed when the proposal is applied.
220      *
221      * @return returns the change for this proposal.
222      * @throws CoreException thrown when the change could not be created
223      */

224     public final Change getChange() throws CoreException {
225         if (fChange == null) {
226             fChange= createChange();
227         }
228         return fChange;
229     }
230
231     /**
232      * Creates the text change for this proposal.
233      * This method is only called once and only when no text change has been passed in
234      * {@link #ChangeCorrectionProposal(String, Change, int, Image)}.
235      *
236      * @return returns the created change.
237      * @throws CoreException thrown if the creation of the change failed.
238      */

239     protected Change createChange() throws CoreException {
240         return new NullChange();
241     }
242     
243     /**
244      * Sets the display name.
245      *
246      * @param name the name to set
247      */

248     public void setDisplayName(String JavaDoc name) {
249         if (name == null) {
250             throw new IllegalArgumentException JavaDoc("Name must not be null"); //$NON-NLS-1$
251
}
252         fName= name;
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
257      */

258     public int getRelevance() {
259         return fRelevance;
260     }
261
262     /**
263      * Sets the relevance.
264      * @param relevance the relevance to set
265      */

266     public void setRelevance(int relevance) {
267         fRelevance= relevance;
268     }
269
270     /* (non-Javadoc)
271      * @see org.eclipse.jdt.internal.ui.text.correction.IShortcutProposal#getProposalId()
272      */

273     public String JavaDoc getCommandId() {
274         return fCommandId;
275     }
276     
277     /**
278      * Set the proposal id to allow assigning a shortcut to the correction proposal.
279      *
280      * @param commandId The proposal id for this proposal or <code>null</code> if no command
281      * should be assigned to this proposal.
282      */

283     public void setCommandId(String JavaDoc commandId) {
284         fCommandId= commandId;
285     }
286
287 }
288
Popular Tags