001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.swt.custom.BusyIndicator;
015: import org.eclipse.swt.widgets.Display;
016: import org.eclipse.swt.widgets.Shell;
017:
018: import org.eclipse.jface.text.ITextOperationTarget;
019: import org.eclipse.jface.text.ITextOperationTargetExtension;
020: import org.eclipse.jface.text.source.ISourceViewer;
021:
022: import org.eclipse.ui.IWorkbenchPartSite;
023:
024: /**
025: * A content assist action which gets its target from its text editor.
026: * <p>
027: * The action is initially associated with a text editor via the constructor,
028: * but can subsequently be changed using <code>setEditor</code>.</p>
029: * <p>
030: * If this class is used as is, it works by asking the text editor for its text operation target
031: * (using <code>getAdapter(ITextOperationTarget.class)</code> and runs the content assist
032: * operation on this target.
033: * </p>
034: * @since 2.0
035: */
036: public final class ContentAssistAction extends TextEditorAction {
037:
038: /** The text operation target */
039: private ITextOperationTarget fOperationTarget;
040:
041: /**
042: * Creates and initializes the action for the given text editor.
043: * The action configures its visual representation from the given resource
044: * bundle. The action works by asking the text editor at the time for its
045: * text operation target adapter (using
046: * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs the
047: * content assist operation on this target.
048: *
049: * @param bundle the resource bundle
050: * @param prefix a prefix to be prepended to the various resource keys
051: * (described in <code>ResourceAction</code> constructor), or
052: * <code>null</code> if none
053: * @param editor the text editor
054: * @see ResourceAction#ResourceAction(ResourceBundle, String)
055: */
056: public ContentAssistAction(ResourceBundle bundle, String prefix,
057: ITextEditor editor) {
058: super (bundle, prefix, editor);
059: }
060:
061: /**
062: * Runs the content assist operation on the editor's text operation target.
063: */
064: public void run() {
065: if (fOperationTarget != null) {
066:
067: ITextEditor editor = getTextEditor();
068: if (editor != null && validateEditorInputState()) {
069:
070: Display display = null;
071:
072: IWorkbenchPartSite site = editor.getSite();
073: Shell shell = site.getShell();
074: if (shell != null && !shell.isDisposed())
075: display = shell.getDisplay();
076:
077: BusyIndicator.showWhile(display, new Runnable() {
078: public void run() {
079: fOperationTarget
080: .doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
081: }
082: });
083: }
084: }
085: }
086:
087: /**
088: * The <code>ContentAssistAction</code> implementation of this
089: * <code>IUpdate</code> method discovers the operation through the current
090: * editor's <code>ITextOperationTarget</code> adapter, and sets the
091: * enabled state accordingly.
092: */
093: public void update() {
094:
095: ITextEditor editor = getTextEditor();
096:
097: if (fOperationTarget == null && editor != null)
098: fOperationTarget = (ITextOperationTarget) editor
099: .getAdapter(ITextOperationTarget.class);
100:
101: if (fOperationTarget == null) {
102: setEnabled(false);
103: return;
104: }
105:
106: if (fOperationTarget instanceof ITextOperationTargetExtension) {
107: ITextOperationTargetExtension targetExtension = (ITextOperationTargetExtension) fOperationTarget;
108: targetExtension.enableOperation(
109: ISourceViewer.CONTENTASSIST_PROPOSALS,
110: canModifyEditor());
111: }
112:
113: setEnabled(fOperationTarget
114: .canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS));
115: }
116:
117: /**
118: * @see TextEditorAction#setEditor(ITextEditor)
119: */
120: public void setEditor(ITextEditor editor) {
121: super.setEditor(editor);
122: fOperationTarget = null;
123: }
124: }
|