KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > SpecificContentAssistExecutor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.javaeditor;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jface.text.ITextOperationTarget;
19 import org.eclipse.jface.text.source.ISourceViewer;
20
21 import org.eclipse.ui.texteditor.ITextEditor;
22
23
24 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
25 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
26
27 /**
28  * A content assist executor can invoke content assist for a specific proposal category on an editor.
29  *
30  * @since 3.2
31  */

32 public final class SpecificContentAssistExecutor {
33
34     private final CompletionProposalComputerRegistry fRegistry;
35
36     /**
37      * Creates a new executor.
38      *
39      * @param registry the computer registry to use for the enablement of proposal categories
40      */

41     public SpecificContentAssistExecutor(CompletionProposalComputerRegistry registry) {
42         Assert.isNotNull(registry);
43         fRegistry= registry;
44     }
45
46     /**
47      * Invokes content assist on <code>editor</code>, showing only proposals computed by the
48      * <code>CompletionProposalCategory</code> with the given <code>categoryId</code>.
49      *
50      * @param editor the editor to invoke code assist on
51      * @param categoryId the id of the proposal category to show proposals for
52      */

53     public void invokeContentAssist(final ITextEditor editor, String JavaDoc categoryId) {
54         Collection JavaDoc categories= fRegistry.getProposalCategories();
55         boolean[] inclusionState= new boolean[categories.size()];
56         boolean[] separateState= new boolean[categories.size()];
57         int i= 0;
58         for (Iterator JavaDoc it= categories.iterator(); it.hasNext(); i++) {
59             CompletionProposalCategory cat= (CompletionProposalCategory) it.next();
60             inclusionState[i]= cat.isIncluded();
61             cat.setIncluded(cat.getId().equals(categoryId));
62             separateState[i]= cat.isSeparateCommand();
63             cat.setSeparateCommand(false);
64         }
65         
66         try {
67             ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
68             if (target != null && target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS))
69                 target.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
70         } finally {
71             i= 0;
72             for (Iterator JavaDoc it= categories.iterator(); it.hasNext(); i++) {
73                 CompletionProposalCategory cat= (CompletionProposalCategory) it.next();
74                 cat.setIncluded(inclusionState[i]);
75                 cat.setSeparateCommand(separateState[i]);
76             }
77         }
78     }
79 }
80
Popular Tags