KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
54  * A correction proposal which uses an {@link IFix} to
55  * fix a problem. A fix correction proposal may have an {@link ICleanUp}
56  * attached which can be executed instead of the provided IFix.
57  */

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     /* (non-Javadoc)
76      * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#getImage()
77      */

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     /* (non-Javadoc)
96      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#getAdditionalProposalInfo()
97      */

98     public String JavaDoc getAdditionalProposalInfo() {
99         String JavaDoc result= super.getAdditionalProposalInfo();
100         IStatus status= fFix.getStatus();
101         if (!status.isOK()) {
102             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
103             buf.append("<b>"); //$NON-NLS-1$
104
buf.append(CorrectionMessages.FixCorrectionProposal_WarningAdditionalProposalInfo);
105             buf.append("</b>"); //$NON-NLS-1$
106
buf.append(status.getMessage());
107             buf.append("<br><br>"); //$NON-NLS-1$
108
buf.append(result);
109             return buf.toString();
110         } else {
111             if (fFix instanceof AbstractFix) {
112                 AbstractFix af = (AbstractFix) fFix;
113                 String JavaDoc info = af.getAdditionalInfo();
114                 if (info != null) {
115                     StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
116                     sb.append(info);
117                     sb.append("<br>"); //$NON-NLS-1$
118
sb.append(result);
119                     return sb.toString();
120                 }
121             }
122             return result;
123         }
124     }
125     
126     /* (non-Javadoc)
127      * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#getRelevance()
128      */

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     /* (non-Javadoc)
139      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#createTextChange()
140      */

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()); //$NON-NLS-1$
153

154         return createChange;
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
159      */

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 JavaDoc e) {
174             } catch (InvocationTargetException JavaDoc 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     /**
193      * {@inheritDoc}
194      */

195     public String JavaDoc 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 ""; //$NON-NLS-1$
204
} else {
205             return Messages.format(CorrectionMessages.FixCorrectionProposal_hitCtrlEnter_variable_description, new Integer JavaDoc(count));
206         }
207     }
208
209 }
210
Popular Tags