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
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;
14 import java.util.Collection;
15 import java.util.Iterator;
17 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.text.ITextOperationTarget;
20 import org.eclipse.jface.text.source.ISourceViewer;
22 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
27 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
30 * A content assist executor can invoke content assist for a specific proposal category on an editor.
34 public final class SpecificContentAssistExecutor {
36 private final CompletionProposalComputerRegistry fRegistry;
39 * Creates a new executor.
41 * @param registry the computer registry to use for the enablement of proposal categories
43 public SpecificContentAssistExecutor(CompletionProposalComputerRegistry registry) {
44 Assert.isNotNull(registry);
49 * Invokes content assist on <code>editor</code>, showing only proposals computed by the
50 * <code>CompletionProposalCategory</code> with the given <code>categoryId</code>.
52 * @param editor the editor to invoke code assist on
53 * @param categoryId the id of the proposal category to show proposals for
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());
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));
73 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
74 if (target != null && target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS))
75 target.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
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]));