1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IStatus; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 24 import org.eclipse.jface.resource.ImageDescriptor; 25 26 import org.eclipse.jface.text.DocumentEvent; 27 import org.eclipse.jface.text.IDocument; 28 import org.eclipse.jface.text.ITextViewer; 29 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 30 31 import org.eclipse.ltk.core.refactoring.RefactoringCore; 32 import org.eclipse.ltk.core.refactoring.TextChange; 33 import org.eclipse.ltk.core.refactoring.TextFileChange; 34 35 import org.eclipse.jdt.core.dom.CompilationUnit; 36 37 import org.eclipse.jdt.internal.corext.fix.AbstractFix; 38 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring; 39 import org.eclipse.jdt.internal.corext.fix.IFix; 40 import org.eclipse.jdt.internal.corext.fix.LinkedFix; 41 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 44 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 45 import org.eclipse.jdt.ui.text.java.IInvocationContext; 46 47 import org.eclipse.jdt.internal.ui.JavaPlugin; 48 import org.eclipse.jdt.internal.ui.fix.ICleanUp; 49 import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper; 50 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper; 51 import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor; 52 53 58 public class FixCorrectionProposal extends LinkedCorrectionProposal implements ICompletionProposalExtension2, IStatusLineProposal { 59 60 private final IFix fFix; 61 private final ICleanUp fCleanUp; 62 private CompilationUnit fCompilationUnit; 63 64 public FixCorrectionProposal(IFix fix, ICleanUp cleanUp, int relevance, Image image, IInvocationContext context) { 65 super(fix.getDescription(), fix.getCompilationUnit(), null, relevance, image); 66 fFix= fix; 67 fCleanUp= cleanUp; 68 fCompilationUnit= context.getASTRoot(); 69 } 70 71 public ICleanUp getCleanUp() { 72 return fCleanUp; 73 } 74 75 78 public Image getImage() { 79 IStatus status= fFix.getStatus(); 80 if (!status.isOK()) { 81 ImageImageDescriptor image= new ImageImageDescriptor(super.getImage()); 82 83 int flag= JavaElementImageDescriptor.WARNING; 84 if (status.getSeverity() == IStatus.ERROR) { 85 flag= JavaElementImageDescriptor.ERROR; 86 } 87 88 ImageDescriptor composite= new JavaElementImageDescriptor(image, flag, new Point(image.getImageData().width, image.getImageData().height)); 89 return composite.createImage(); 90 } else { 91 return super.getImage(); 92 } 93 } 94 95 98 public String getAdditionalProposalInfo() { 99 String result= super.getAdditionalProposalInfo(); 100 IStatus status= fFix.getStatus(); 101 if (!status.isOK()) { 102 StringBuffer buf= new StringBuffer (); 103 buf.append("<b>"); buf.append(CorrectionMessages.FixCorrectionProposal_WarningAdditionalProposalInfo); 105 buf.append("</b>"); buf.append(status.getMessage()); 107 buf.append("<br><br>"); buf.append(result); 109 return buf.toString(); 110 } else { 111 if (fFix instanceof AbstractFix) { 112 AbstractFix af = (AbstractFix) fFix; 113 String info = af.getAdditionalInfo(); 114 if (info != null) { 115 StringBuffer sb=new StringBuffer (); 116 sb.append(info); 117 sb.append("<br>"); sb.append(result); 119 return sb.toString(); 120 } 121 } 122 return result; 123 } 124 } 125 126 129 public int getRelevance() { 130 IStatus status= fFix.getStatus(); 131 if (status.getSeverity() == IStatus.WARNING) { 132 return super.getRelevance() - 100; 133 } else { 134 return super.getRelevance(); 135 } 136 } 137 138 141 protected TextChange createTextChange() throws CoreException { 142 IFix fix= fFix; 143 TextChange createChange= fix.createChange(); 144 if (createChange instanceof TextFileChange) 145 ((TextFileChange)createChange).setSaveMode(TextFileChange.LEAVE_DIRTY); 146 147 if (fix instanceof LinkedFix) { 148 setLinkedProposalModel(((LinkedFix) fix).getLinkedPositions()); 149 } 150 151 if (createChange == null) 152 return new CompilationUnitChange("", getCompilationUnit()); 154 return createChange; 155 } 156 157 160 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 161 if (stateMask == SWT.CONTROL && fCleanUp != null){ 162 CleanUpRefactoring refactoring= new CleanUpRefactoring(); 163 refactoring.addCompilationUnit(getCompilationUnit()); 164 refactoring.addCleanUp(fCleanUp); 165 refactoring.setLeaveFilesDirty(true); 166 167 int stopSeverity= RefactoringCore.getConditionCheckingFailedSeverity(); 168 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 169 ProgressMonitorDialog context= new ProgressMonitorDialog(shell); 170 RefactoringExecutionHelper executer= new RefactoringExecutionHelper(refactoring, stopSeverity, RefactoringSaveHelper.SAVE_NOTHING, shell, context); 171 try { 172 executer.perform(true, true); 173 } catch (InterruptedException e) { 174 } catch (InvocationTargetException e) { 175 JavaPlugin.log(e); 176 } 177 return; 178 } 179 apply(viewer.getDocument()); 180 } 181 182 public void selected(ITextViewer viewer, boolean smartToggle) { 183 } 184 185 public void unselected(ITextViewer viewer) { 186 } 187 188 public boolean validate(IDocument document, int offset, DocumentEvent event) { 189 return false; 190 } 191 192 195 public String getStatusMessage() { 196 if (fCleanUp == null) 197 return null; 198 199 int count= fCleanUp.maximalNumberOfFixes(fCompilationUnit); 200 if (count == -1) { 201 return CorrectionMessages.FixCorrectionProposal_HitCtrlEnter_description; 202 } else if (count < 2) { 203 return ""; } else { 205 return Messages.format(CorrectionMessages.FixCorrectionProposal_hitCtrlEnter_variable_description, new Integer (count)); 206 } 207 } 208 209 } 210 | Popular Tags |