001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.jdt.internal.ui.javaeditor;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.jface.action.Action;
015: import org.eclipse.jface.viewers.ISelection;
016:
017: import org.eclipse.jface.text.BadLocationException;
018: import org.eclipse.jface.text.IDocument;
019: import org.eclipse.jface.text.ITextOperationTarget;
020: import org.eclipse.jface.text.ITextSelection;
021: import org.eclipse.jface.text.TextUtilities;
022: import org.eclipse.jface.text.source.ISourceViewer;
023:
024: import org.eclipse.ui.IEditorPart;
025: import org.eclipse.ui.texteditor.IDocumentProvider;
026: import org.eclipse.ui.texteditor.ITextEditor;
027: import org.eclipse.ui.texteditor.IUpdate;
028:
029: import org.eclipse.jdt.ui.text.IJavaPartitions;
030:
031: import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
032: import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
033:
034: /**
035: * Action to run content assist on a specific proposal category.
036: *
037: * @since 3.2
038: */
039: final class SpecificContentAssistAction extends Action implements
040: IUpdate {
041: /**
042: * The category represented by this action.
043: */
044: private final CompletionProposalCategory fCategory;
045: /**
046: * The content assist executor.
047: */
048: private final SpecificContentAssistExecutor fExecutor = new SpecificContentAssistExecutor(
049: CompletionProposalComputerRegistry.getDefault());
050: /**
051: * The editor.
052: */
053: private JavaEditor fEditor;
054:
055: /**
056: * Creates a new action for a certain proposal category.
057: *
058: * @param category
059: */
060: public SpecificContentAssistAction(
061: CompletionProposalCategory category) {
062: fCategory = category;
063: setText(category.getName());
064: setImageDescriptor(category.getImageDescriptor());
065: setActionDefinitionId("org.eclipse.jdt.ui.specific_content_assist.command"); //$NON-NLS-1$
066: }
067:
068: /*
069: * @see org.eclipse.jface.action.Action#run()
070: */
071: public void run() {
072: ITextEditor editor = getActiveEditor();
073: if (editor == null)
074: return;
075:
076: fExecutor.invokeContentAssist(editor, fCategory.getId());
077:
078: return;
079: }
080:
081: private ITextEditor getActiveEditor() {
082: return fEditor;
083: }
084:
085: /**
086: * Sets the active editor part.
087: *
088: * @param part the editor, possibly <code>null</code>
089: */
090: public void setActiveEditor(IEditorPart part) {
091: JavaEditor editor;
092: if (part instanceof JavaEditor)
093: editor = (JavaEditor) part;
094: else
095: editor = null;
096: fEditor = editor;
097: setEnabled(computeEnablement(fEditor));
098: }
099:
100: private boolean computeEnablement(ITextEditor editor) {
101: if (editor == null)
102: return false;
103: ITextOperationTarget target = (ITextOperationTarget) editor
104: .getAdapter(ITextOperationTarget.class);
105: boolean hasContentAssist = target != null
106: && target
107: .canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
108: if (!hasContentAssist)
109: return false;
110:
111: ISelection selection = editor.getSelectionProvider()
112: .getSelection();
113: return isValidSelection(selection);
114: }
115:
116: /**
117: * Computes the partition type at the selection start and checks whether the proposal category
118: * has any computers for this partition.
119: *
120: * @param selection the selection
121: * @return <code>true</code> if there are any computers for the selection
122: */
123: private boolean isValidSelection(ISelection selection) {
124: if (!(selection instanceof ITextSelection))
125: return false;
126: int offset = ((ITextSelection) selection).getOffset();
127:
128: IDocument document = getDocument();
129: if (document == null)
130: return false;
131:
132: String contentType;
133: try {
134: contentType = TextUtilities.getContentType(document,
135: IJavaPartitions.JAVA_PARTITIONING, offset, true);
136: } catch (BadLocationException x) {
137: return false;
138: }
139:
140: return fCategory.hasComputers(contentType);
141: }
142:
143: private IDocument getDocument() {
144: Assert.isTrue(fEditor != null);
145: IDocumentProvider provider = fEditor.getDocumentProvider();
146: if (provider == null)
147: return null;
148:
149: IDocument document = provider.getDocument(fEditor
150: .getEditorInput());
151: return document;
152: }
153:
154: /*
155: * @see org.eclipse.ui.texteditor.IUpdate#update()
156: */
157: public void update() {
158: setEnabled(computeEnablement(fEditor));
159: }
160: }
|