KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21
22 import org.eclipse.jface.bindings.TriggerSequence;
23 import org.eclipse.jface.viewers.ISelection;
24
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.jface.text.contentassist.ICompletionProposal;
30 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
31 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
32 import org.eclipse.jface.text.source.Annotation;
33 import org.eclipse.jface.text.source.IAnnotationModel;
34
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.keys.IBindingService;
37
38 import org.eclipse.jdt.core.ICompilationUnit;
39 import org.eclipse.jdt.core.dom.ASTNode;
40 import org.eclipse.jdt.core.dom.SimpleName;
41
42 import org.eclipse.jdt.ui.JavaUI;
43 import org.eclipse.jdt.ui.text.java.IInvocationContext;
44
45 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
46 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
47
48 /**
49  * Handler to be used to run a quick fix or assist by keyboard shortcut
50  */

51 public class CorrectionCommandHandler extends AbstractHandler {
52         
53     private final JavaEditor fEditor;
54     private final String JavaDoc fId;
55     private final boolean fIsAssist;
56
57     public CorrectionCommandHandler(JavaEditor editor, String JavaDoc id, boolean isAssist) {
58         fEditor= editor;
59         fId= id;
60         fIsAssist= isAssist;
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
65      */

66     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
67         ISelection selection= fEditor.getSelectionProvider().getSelection();
68         ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput());
69         IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
70         if (selection instanceof ITextSelection && cu != null && model != null) {
71             if (! ActionUtil.isEditable(fEditor)) {
72                 return null;
73             }
74             ICompletionProposal proposal= findCorrection(fId, fIsAssist, (ITextSelection) selection, cu, model);
75             if (proposal != null) {
76                 invokeProposal(proposal, ((ITextSelection) selection).getOffset());
77             }
78         }
79         return null;
80     }
81     
82     private ICompletionProposal findCorrection(String JavaDoc id, boolean isAssist, ITextSelection selection, ICompilationUnit cu, IAnnotationModel model) {
83         AssistContext context= new AssistContext(cu, selection.getOffset(), selection.getLength());
84         Collection JavaDoc proposals= new ArrayList JavaDoc(10);
85         if (isAssist) {
86             if (id.equals(LinkedNamesAssistProposal.ASSIST_ID)) {
87                 return getLocalRenameProposal(context); // shortcut for local rename
88
}
89             JavaCorrectionProcessor.collectAssists(context, new ProblemLocation[0], proposals);
90         } else {
91             try {
92                 boolean goToClosest= selection.getLength() == 0;
93                 Annotation[] annotations= getAnnotations(selection.getOffset(), goToClosest);
94                 JavaCorrectionProcessor.collectProposals(context, model, annotations, true, false, proposals);
95             } catch (BadLocationException e) {
96                 return null;
97             }
98         }
99         for (Iterator JavaDoc iter= proposals.iterator(); iter.hasNext();) {
100             Object JavaDoc curr= iter.next();
101             if (curr instanceof ICommandAccess) {
102                 if (id.equals(((ICommandAccess) curr).getCommandId())) {
103                     return (ICompletionProposal) curr;
104                 }
105             }
106         }
107         return null;
108     }
109
110     private Annotation[] getAnnotations(int offset, boolean goToClosest) throws BadLocationException {
111         ArrayList JavaDoc resultingAnnotations= new ArrayList JavaDoc();
112         JavaCorrectionAssistant.collectQuickFixableAnnotations(fEditor, offset, goToClosest, resultingAnnotations);
113         return (Annotation[]) resultingAnnotations.toArray(new Annotation[resultingAnnotations.size()]);
114     }
115     
116     private ICompletionProposal getLocalRenameProposal(IInvocationContext context) {
117         ASTNode node= context.getCoveringNode();
118         if (node instanceof SimpleName) {
119             return new LinkedNamesAssistProposal(context.getCompilationUnit(), (SimpleName) node);
120         }
121         return null;
122     }
123
124     private IDocument getDocument() {
125         return JavaUI.getDocumentProvider().getDocument(fEditor.getEditorInput());
126     }
127     
128     
129     private void invokeProposal(ICompletionProposal proposal, int offset) {
130         if (proposal instanceof ICompletionProposalExtension2) {
131             ITextViewer viewer= fEditor.getViewer();
132             if (viewer != null) {
133                 ((ICompletionProposalExtension2) proposal).apply(viewer, (char) 0, 0, offset);
134                 return;
135             }
136         } else if (proposal instanceof ICompletionProposalExtension) {
137             IDocument document= getDocument();
138             if (document != null) {
139                 ((ICompletionProposalExtension) proposal).apply(document, (char) 0, offset);
140                 return;
141             }
142         }
143         IDocument document= getDocument();
144         if (document != null) {
145             proposal.apply(document);
146         }
147     }
148     
149     public static String JavaDoc getShortCutString(String JavaDoc proposalId) {
150         if (proposalId != null) {
151             IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
152             if (bindingService != null) {
153                 TriggerSequence[] activeBindingsFor= bindingService.getActiveBindingsFor(proposalId);
154                 if (activeBindingsFor.length > 0) {
155                     return activeBindingsFor[0].format();
156                 }
157             }
158         }
159         return null;
160     }
161     
162 }
163
Popular Tags