1 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 41 final class SpecificContentAssistAction extends Action implements IUpdate { 42 45 private final CompletionProposalCategory fCategory; 46 49 private final SpecificContentAssistExecutor fExecutor= new SpecificContentAssistExecutor(CompletionProposalComputerRegistry.getDefault()); 50 53 private JavaEditor fEditor; 54 55 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"); } 66 67 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 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 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 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 150 public void update() { 151 setEnabled(computeEnablement(fEditor)); 152 } 153 } 154 | Popular Tags |