KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ContentAssistAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.texteditor;
13
14
15 import java.util.ResourceBundle JavaDoc;
16
17 import org.eclipse.swt.custom.BusyIndicator;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.text.ITextOperationTarget;
22 import org.eclipse.jface.text.ITextOperationTargetExtension;
23 import org.eclipse.jface.text.source.ISourceViewer;
24
25 import org.eclipse.ui.IWorkbenchPartSite;
26
27
28 /**
29  * A content assist action which gets its target from its text editor.
30  * <p>
31  * The action is initially associated with a text editor via the constructor,
32  * but can subsequently be changed using <code>setEditor</code>.</p>
33  * <p>
34  * If this class is used as is, it works by asking the text editor for its text operation target
35  * (using <code>getAdapter(ITextOperationTarget.class)</code> and runs the content assist
36  * operation on this target.
37  * </p>
38  * @since 2.0
39  */

40 public final class ContentAssistAction extends TextEditorAction {
41
42     /** The text operation target */
43     private ITextOperationTarget fOperationTarget;
44
45     /**
46      * Creates and initializes the action for the given text editor.
47      * The action configures its visual representation from the given resource
48      * bundle. The action works by asking the text editor at the time for its
49      * text operation target adapter (using
50      * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs the
51      * content assist operation on this target.
52      *
53      * @param bundle the resource bundle
54      * @param prefix a prefix to be prepended to the various resource keys
55      * (described in <code>ResourceAction</code> constructor), or
56      * <code>null</code> if none
57      * @param editor the text editor
58      * @see ResourceAction#ResourceAction(ResourceBundle, String)
59      */

60     public ContentAssistAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
61         super(bundle, prefix, editor);
62     }
63
64     /**
65      * Runs the content assist operation on the editor's text operation target.
66      */

67     public void run() {
68         if (fOperationTarget != null) {
69
70             ITextEditor editor= getTextEditor();
71             if (editor != null && validateEditorInputState()) {
72
73                 Display display= null;
74
75                 IWorkbenchPartSite site= editor.getSite();
76                 Shell shell= site.getShell();
77                 if (shell != null && !shell.isDisposed())
78                     display= shell.getDisplay();
79
80                 BusyIndicator.showWhile(display, new Runnable JavaDoc() {
81                     public void run() {
82                         fOperationTarget.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
83                     }
84                 });
85             }
86         }
87     }
88
89     /**
90      * The <code>ContentAssistAction</code> implementation of this
91      * <code>IUpdate</code> method discovers the operation through the current
92      * editor's <code>ITextOperationTarget</code> adapter, and sets the
93      * enabled state accordingly.
94      */

95     public void update() {
96
97         ITextEditor editor= getTextEditor();
98
99         if (fOperationTarget == null && editor!= null)
100             fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
101
102         if (fOperationTarget == null) {
103             setEnabled(false);
104             return;
105         }
106
107         if (fOperationTarget instanceof ITextOperationTargetExtension) {
108             ITextOperationTargetExtension targetExtension= (ITextOperationTargetExtension) fOperationTarget;
109             targetExtension.enableOperation(ISourceViewer.CONTENTASSIST_PROPOSALS, canModifyEditor());
110         }
111
112         setEnabled(fOperationTarget.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS));
113     }
114
115     /**
116      * @see TextEditorAction#setEditor(ITextEditor)
117      */

118     public void setEditor(ITextEditor editor) {
119         super.setEditor(editor);
120         fOperationTarget= null;
121     }
122 }
123
Popular Tags