KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > SpecificContentAssistAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.jdt.internal.ui.javaeditor;
12
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.viewers.ISelection;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextOperationTarget;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.text.TextUtilities;
24 import org.eclipse.jface.text.source.ISourceViewer;
25
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.texteditor.IDocumentProvider;
28 import org.eclipse.ui.texteditor.ITextEditor;
29 import org.eclipse.ui.texteditor.IUpdate;
30
31 import org.eclipse.jdt.ui.text.IJavaPartitions;
32
33 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
34 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
35
36 /**
37  * Action to run content assist on a specific proposal category.
38  *
39  * @since 3.2
40  */

41 final class SpecificContentAssistAction extends Action implements IUpdate {
42     /**
43      * The category represented by this action.
44      */

45     private final CompletionProposalCategory fCategory;
46     /**
47      * The content assist executor.
48      */

49     private final SpecificContentAssistExecutor fExecutor= new SpecificContentAssistExecutor(CompletionProposalComputerRegistry.getDefault());
50     /**
51      * The editor.
52      */

53     private JavaEditor fEditor;
54     
55     /**
56      * Creates a new action for a certain proposal category.
57      *
58      * @param category
59      */

60     public SpecificContentAssistAction(CompletionProposalCategory category) {
61         fCategory= category;
62         setText(category.getName());
63         setImageDescriptor(category.getImageDescriptor());
64         setActionDefinitionId("org.eclipse.jdt.ui.specific_content_assist.command"); //$NON-NLS-1$
65
}
66
67     /*
68      * @see org.eclipse.jface.action.Action#run()
69      */

70     public void run() {
71         ITextEditor editor= getActiveEditor();
72         if (editor == null)
73             return;
74         
75         fExecutor.invokeContentAssist(editor, fCategory.getId());
76         
77         return;
78     }
79
80     private ITextEditor getActiveEditor() {
81         return fEditor;
82     }
83
84     /**
85      * Sets the active editor part.
86      *
87      * @param part the editor, possibly <code>null</code>
88      */

89     public void setActiveEditor(IEditorPart part) {
90         JavaEditor editor;
91         if (part instanceof JavaEditor)
92             editor= (JavaEditor) part;
93         else
94             editor= null;
95         fEditor= editor;
96         setEnabled(computeEnablement(fEditor));
97     }
98     
99     private boolean computeEnablement(ITextEditor editor) {
100         if (editor == null)
101             return false;
102         ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
103         boolean hasContentAssist= target != null && target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
104         if (!hasContentAssist)
105             return false;
106         
107         ISelection selection= editor.getSelectionProvider().getSelection();
108         return isValidSelection(selection);
109     }
110
111     /**
112      * Computes the partition type at the selection start and checks whether the proposal category
113      * has any computers for this partition.
114      *
115      * @param selection the selection
116      * @return <code>true</code> if there are any computers for the selection
117      */

118     private boolean isValidSelection(ISelection selection) {
119         if (!(selection instanceof ITextSelection))
120             return false;
121         int offset= ((ITextSelection) selection).getOffset();
122         
123         IDocument document= getDocument();
124         if (document == null)
125             return false;
126         
127         String JavaDoc contentType;
128         try {
129             contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
130         } catch (BadLocationException x) {
131             return false;
132         }
133         
134         return fCategory.hasComputers(contentType);
135     }
136     
137     private IDocument getDocument() {
138         Assert.isTrue(fEditor != null);
139         IDocumentProvider provider= fEditor.getDocumentProvider();
140         if (provider == null)
141             return null;
142         
143         IDocument document= provider.getDocument(fEditor.getEditorInput());
144         return document;
145     }
146
147     /*
148      * @see org.eclipse.ui.texteditor.IUpdate#update()
149      */

150     public void update() {
151         setEnabled(computeEnablement(fEditor));
152     }
153 }
154
Popular Tags