1 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 49 public class ChangeCorrectionProposal implements IJavaCompletionProposal, ICommandAccess { 50 51 private Change fChange; 52 private String fName; 53 private int fRelevance; 54 private Image fImage; 55 private String fCommandId; 56 57 67 public ChangeCorrectionProposal(String name, Change change, int relevance, Image image) { 68 if (name == null) { 69 throw new IllegalArgumentException ("Name must not be null"); } 71 fName= name; 72 fChange= change; 73 fRelevance= relevance; 74 fImage= image; 75 fCommandId= null; 76 } 77 78 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 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 145 public String getAdditionalProposalInfo() { 146 StringBuffer buf= new StringBuffer (); 147 buf.append("<p>"); try { 149 Change change= getChange(); 150 if (change != null) { 151 String 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>"); buf.append(e.getLocalizedMessage()); 162 buf.append("</pre>"); } 164 buf.append("</p>"); return buf.toString(); 166 } 167 168 171 public IContextInformation getContextInformation() { 172 return null; 173 } 174 175 178 public String getDisplayString() { 179 String shortCutString= CorrectionCommandHandler.getShortCutString(getCommandId()); 180 if (shortCutString != null) { 181 return Messages.format(CorrectionMessages.ChangeCorrectionProposal_name_with_shortcut, new String [] { getName(), shortCutString }); 182 } 183 return getName(); 184 } 185 186 191 public String getName() { 192 return fName; 193 } 194 195 198 public Image getImage() { 199 return fImage; 200 } 201 202 205 public Point getSelection(IDocument document) { 206 return null; 207 } 208 209 214 public void setImage(Image image) { 215 fImage= image; 216 } 217 218 224 public final Change getChange() throws CoreException { 225 if (fChange == null) { 226 fChange= createChange(); 227 } 228 return fChange; 229 } 230 231 239 protected Change createChange() throws CoreException { 240 return new NullChange(); 241 } 242 243 248 public void setDisplayName(String name) { 249 if (name == null) { 250 throw new IllegalArgumentException ("Name must not be null"); } 252 fName= name; 253 } 254 255 258 public int getRelevance() { 259 return fRelevance; 260 } 261 262 266 public void setRelevance(int relevance) { 267 fRelevance= relevance; 268 } 269 270 273 public String getCommandId() { 274 return fCommandId; 275 } 276 277 283 public void setCommandId(String commandId) { 284 fCommandId= commandId; 285 } 286 287 } 288 | Popular Tags |