]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2005, 2012 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 | * Paul Fullbright <paul.fullbright@oracle.com> - content assist category enablement - http://bugs.eclipse.org/345213 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.jdt.internal.ui.javaeditor; | |
13 | ||
14 | import java.util.Collection; | |
15 | import java.util.Iterator; | |
16 | ||
17 | import org.eclipse.core.runtime.Assert; | |
18 | ||
19 | import org.eclipse.jface.text.ITextOperationTarget; | |
20 | import org.eclipse.jface.text.source.ISourceViewer; | |
21 | ||
22 | import org.eclipse.ui.texteditor.ITextEditor; | |
23 | ||
24 | import org.eclipse.jdt.core.IJavaProject; | |
25 | ||
26 | import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory; | |
27 | import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry; | |
28 | ||
29 | /** | |
30 | * A content assist executor can invoke content assist for a specific proposal category on an editor. | |
31 | * | |
32 | * @since 3.2 | |
33 | */ | |
34 | public final class SpecificContentAssistExecutor { | |
35 | ||
36 | private final CompletionProposalComputerRegistry fRegistry; | |
37 | ||
38 | /** | |
39 | * Creates a new executor. | |
40 | * | |
41 | * @param registry the computer registry to use for the enablement of proposal categories | |
42 | */ | |
43 | public SpecificContentAssistExecutor(CompletionProposalComputerRegistry registry) { | |
44 | Assert.isNotNull(registry); | |
45 | fRegistry= registry; | |
46 | } | |
47 | ||
48 | /** | |
49 | * Invokes content assist on <code>editor</code>, showing only proposals computed by the | |
50 | * <code>CompletionProposalCategory</code> with the given <code>categoryId</code>. | |
51 | * | |
52 | * @param editor the editor to invoke code assist on | |
53 | * @param categoryId the id of the proposal category to show proposals for | |
54 | */ | |
55 | public void invokeContentAssist(final ITextEditor editor, String categoryId) { | |
56 | Collection<CompletionProposalCategory> categories= fRegistry.getProposalCategories(); | |
57 | boolean[] inclusionState= new boolean[categories.size()]; | |
58 | boolean[] separateState= new boolean[categories.size()]; | |
59 | boolean[] enabledState= new boolean[categories.size()]; | |
60 | IJavaProject javaProject = EditorUtility.getJavaProject(editor.getEditorInput()); | |
61 | int i= 0; | |
62 | for (Iterator<CompletionProposalCategory> it= categories.iterator(); it.hasNext(); i++) { | |
63 | CompletionProposalCategory cat= it.next(); | |
64 | inclusionState[i]= cat.isIncluded(); | |
65 | cat.setIncluded(cat.getId().equals(categoryId)); | |
66 | separateState[i]= cat.isSeparateCommand(); | |
67 | cat.setSeparateCommand(false); | |
68 | enabledState[i]= cat.isEnabled(); | |
69 | cat.setEnabled(cat.isEnabled() && cat.matches(javaProject)); | |
70 | } | |
71 | ||
72 | try { | |
73 | ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); | |
74 | if (target != null && target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS)) | |
75 | target.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS); | |
76 | } finally { | |
77 | i= 0; | |
78 | for (Iterator<CompletionProposalCategory> it= categories.iterator(); it.hasNext(); i++) { | |
79 | CompletionProposalCategory cat= it.next(); | |
80 | cat.setIncluded(inclusionState[i]); | |
81 | cat.setSeparateCommand(separateState[i]); | |
82 | cat.setEnabled((enabledState[i])); | |
83 | } | |
84 | } | |
85 | } | |
86 | } |