KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > fieldassist > ContentAssistField


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.ui.fieldassist;
13
14 import org.eclipse.jface.fieldassist.DecoratedField;
15 import org.eclipse.jface.fieldassist.FieldDecoration;
16 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
17 import org.eclipse.jface.fieldassist.IContentProposalProvider;
18 import org.eclipse.jface.fieldassist.IControlContentAdapter;
19 import org.eclipse.jface.fieldassist.IControlCreator;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.WorkbenchMessages;
25 import org.eclipse.ui.keys.IBindingService;
26
27 /**
28  * ContentAssistField utilizes the concepts of a {@link DecoratedField} and the
29  * {@link ContentAssistCommandAdapter} to provide a decorated field that shows a
30  * content assist cue when it gets focus and invokes content assist for a
31  * specified command.
32  * <p>
33  * This class is not intended to be subclassed.
34  *
35  * @since 3.2
36  * @deprecated As of 3.3, clients should use
37  * {@link org.eclipse.jface.fieldassist.ControlDecoration} and
38  * {@link ContentAssistCommandAdapter} instead of this class.
39  */

40 public class ContentAssistField extends DecoratedField {
41
42     private ContentAssistCommandAdapter adapter;
43
44     private static final String JavaDoc CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; //$NON-NLS-1$
45

46     /**
47      * Construct a content assist field that shows a content assist cue and can
48      * assist the user with choosing content for the field.
49      *
50      * @param parent
51      * the parent of the decorated field.
52      * @param style
53      * the desired style bits for the field.
54      * @param controlCreator
55      * the IControlCreator used to specify the specific kind of
56      * control that is to be decorated.
57      * @param controlContentAdapter
58      * the <code>IControlContentAdapter</code> used to obtain and
59      * update the control's contents as proposals are accepted. May
60      * not be <code>null</code>.
61      * @param proposalProvider
62      * the <code>IContentProposalProvider</code> used to obtain
63      * content proposals for this control, or <code>null</code> if
64      * no content proposal is available.
65      * @param commandId
66      * the String id of the command that will invoke the content
67      * assistant. If not supplied, the default value will be
68      * "org.eclipse.ui.edit.text.contentAssist.proposals".
69      * @param autoActivationCharacters
70      * An array of characters that trigger auto-activation of content
71      * proposal. If specified, these characters will trigger
72      * auto-activation of the proposal popup, regardless of the
73      * specified command id.
74      */

75     public ContentAssistField(Composite parent, int style,
76             IControlCreator controlCreator,
77             IControlContentAdapter controlContentAdapter,
78             IContentProposalProvider proposalProvider, String JavaDoc commandId,
79             char[] autoActivationCharacters) {
80
81         super(parent, style, controlCreator);
82         adapter = new ContentAssistCommandAdapter(getControl(),
83                 controlContentAdapter, proposalProvider, commandId,
84                 autoActivationCharacters);
85
86         addFieldDecoration(getFieldDecoration(), SWT.LEFT | SWT.TOP, true);
87
88     }
89
90     /**
91      * Set the boolean flag that determines whether the content assist is
92      * enabled.
93      *
94      * @param enabled
95      * <code>true</code> if content assist is enabled and
96      * responding to user input, <code>false</code> if it is
97      * ignoring user input.
98      *
99      */

100     public void setEnabled(boolean enabled) {
101         adapter.setEnabled(enabled);
102         if (enabled) {
103             showDecoration(getFieldDecoration());
104         } else {
105             hideDecoration(getFieldDecoration());
106         }
107     }
108
109     /*
110      * Get a field decoration appropriate for cueing the user, including a
111      * description of the active key binding.
112      *
113      */

114     private FieldDecoration getFieldDecoration() {
115         FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
116         // Look for a decoration installed for this particular command id.
117
String JavaDoc decId = CONTENT_ASSIST_DECORATION_ID + adapter.getCommandId();
118         FieldDecoration dec = registry.getFieldDecoration(decId);
119
120         // If there is not one, base ours on the standard JFace one.
121
if (dec == null) {
122             FieldDecoration originalDec = registry
123                     .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
124
125             registry.registerFieldDecoration(decId, null, originalDec
126                     .getImage());
127             dec = registry.getFieldDecoration(decId);
128         }
129         // Always update the decoration text since the key binding may
130
// have changed since it was last retrieved.
131
IBindingService bindingService = (IBindingService) PlatformUI
132                 .getWorkbench().getService(IBindingService.class);
133         dec.setDescription(NLS.bind(
134                 WorkbenchMessages.ContentAssist_Cue_Description_Key,
135                 bindingService.getBestActiveBindingFormattedFor(adapter
136                         .getCommandId())));
137
138         // Now return the field decoration
139
return dec;
140     }
141
142     /**
143      * Return the ContentAssistCommandAdapter installed on the receiver. This
144      * adapter is provided so that clients can configure the adapter if the
145      * default values are not appropriate.
146      *
147      * @return the ContentAssistCommandAdapter installed on the field.
148      */

149     public ContentAssistCommandAdapter getContentAssistCommandAdapter() {
150         return adapter;
151     }
152 }
153
Popular Tags