001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.fieldassist;
011:
012: import org.eclipse.jface.fieldassist.DecoratedField;
013: import org.eclipse.jface.fieldassist.FieldDecoration;
014: import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
015: import org.eclipse.jface.fieldassist.IContentProposalProvider;
016: import org.eclipse.jface.fieldassist.IControlContentAdapter;
017: import org.eclipse.jface.fieldassist.IControlCreator;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023: import org.eclipse.ui.keys.IBindingService;
024:
025: /**
026: * ContentAssistField utilizes the concepts of a {@link DecoratedField} and the
027: * {@link ContentAssistCommandAdapter} to provide a decorated field that shows a
028: * content assist cue when it gets focus and invokes content assist for a
029: * specified command.
030: * <p>
031: * This class is not intended to be subclassed.
032: *
033: * @since 3.2
034: * @deprecated As of 3.3, clients should use
035: * {@link org.eclipse.jface.fieldassist.ControlDecoration} and
036: * {@link ContentAssistCommandAdapter} instead of this class.
037: */
038: public class ContentAssistField extends DecoratedField {
039:
040: private ContentAssistCommandAdapter adapter;
041:
042: private static final String CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; //$NON-NLS-1$
043:
044: /**
045: * Construct a content assist field that shows a content assist cue and can
046: * assist the user with choosing content for the field.
047: *
048: * @param parent
049: * the parent of the decorated field.
050: * @param style
051: * the desired style bits for the field.
052: * @param controlCreator
053: * the IControlCreator used to specify the specific kind of
054: * control that is to be decorated.
055: * @param controlContentAdapter
056: * the <code>IControlContentAdapter</code> used to obtain and
057: * update the control's contents as proposals are accepted. May
058: * not be <code>null</code>.
059: * @param proposalProvider
060: * the <code>IContentProposalProvider</code> used to obtain
061: * content proposals for this control, or <code>null</code> if
062: * no content proposal is available.
063: * @param commandId
064: * the String id of the command that will invoke the content
065: * assistant. If not supplied, the default value will be
066: * "org.eclipse.ui.edit.text.contentAssist.proposals".
067: * @param autoActivationCharacters
068: * An array of characters that trigger auto-activation of content
069: * proposal. If specified, these characters will trigger
070: * auto-activation of the proposal popup, regardless of the
071: * specified command id.
072: */
073: public ContentAssistField(Composite parent, int style,
074: IControlCreator controlCreator,
075: IControlContentAdapter controlContentAdapter,
076: IContentProposalProvider proposalProvider,
077: String commandId, char[] autoActivationCharacters) {
078:
079: super (parent, style, controlCreator);
080: adapter = new ContentAssistCommandAdapter(getControl(),
081: controlContentAdapter, proposalProvider, commandId,
082: autoActivationCharacters);
083:
084: addFieldDecoration(getFieldDecoration(), SWT.LEFT | SWT.TOP,
085: true);
086:
087: }
088:
089: /**
090: * Set the boolean flag that determines whether the content assist is
091: * enabled.
092: *
093: * @param enabled
094: * <code>true</code> if content assist is enabled and
095: * responding to user input, <code>false</code> if it is
096: * ignoring user input.
097: *
098: */
099: public void setEnabled(boolean enabled) {
100: adapter.setEnabled(enabled);
101: if (enabled) {
102: showDecoration(getFieldDecoration());
103: } else {
104: hideDecoration(getFieldDecoration());
105: }
106: }
107:
108: /*
109: * Get a field decoration appropriate for cueing the user, including a
110: * description of the active key binding.
111: *
112: */
113: private FieldDecoration getFieldDecoration() {
114: FieldDecorationRegistry registry = FieldDecorationRegistry
115: .getDefault();
116: // Look for a decoration installed for this particular command id.
117: String decId = CONTENT_ASSIST_DECORATION_ID
118: + adapter.getCommandId();
119: FieldDecoration dec = registry.getFieldDecoration(decId);
120:
121: // If there is not one, base ours on the standard JFace one.
122: if (dec == null) {
123: FieldDecoration originalDec = registry
124: .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
125:
126: registry.registerFieldDecoration(decId, null, originalDec
127: .getImage());
128: dec = registry.getFieldDecoration(decId);
129: }
130: // Always update the decoration text since the key binding may
131: // have changed since it was last retrieved.
132: IBindingService bindingService = (IBindingService) PlatformUI
133: .getWorkbench().getService(IBindingService.class);
134: dec.setDescription(NLS.bind(
135: WorkbenchMessages.ContentAssist_Cue_Description_Key,
136: bindingService.getBestActiveBindingFormattedFor(adapter
137: .getCommandId())));
138:
139: // Now return the field decoration
140: return dec;
141: }
142:
143: /**
144: * Return the ContentAssistCommandAdapter installed on the receiver. This
145: * adapter is provided so that clients can configure the adapter if the
146: * default values are not appropriate.
147: *
148: * @return the ContentAssistCommandAdapter installed on the field.
149: */
150: public ContentAssistCommandAdapter getContentAssistCommandAdapter() {
151: return adapter;
152: }
153: }
|