KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.IHandler;
17 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
18 import org.eclipse.jface.fieldassist.ControlDecoration;
19 import org.eclipse.jface.fieldassist.FieldDecoration;
20 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
21 import org.eclipse.jface.fieldassist.IContentProposalProvider;
22 import org.eclipse.jface.fieldassist.IControlContentAdapter;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.FocusEvent;
28 import org.eclipse.swt.events.FocusListener;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.handlers.IHandlerActivation;
32 import org.eclipse.ui.handlers.IHandlerService;
33 import org.eclipse.ui.internal.WorkbenchMessages;
34 import org.eclipse.ui.keys.IBindingService;
35
36 /**
37  * ContentAssistCommandAdapter extends {@link ContentProposalAdapter} to invoke
38  * content proposals using a specified {@link org.eclipse.ui.commands.ICommand}.
39  * The ability to specify a {@link org.eclipse.jface.bindings.keys.KeyStroke}
40  * that explicitly invokes content proposals is hidden by this class, and
41  * instead the String id of a command is used. If no command id is specified by
42  * the client, then the default workbench content assist command is used.
43  * <p>
44  * As of 3.3, ContentAssistCommandAdapter can be optionally configured to
45  * install the content assist decoration on its control.
46  * <p>
47  * This class is not intended to be subclassed.
48  *
49  * @since 3.2
50  */

51 public class ContentAssistCommandAdapter extends ContentProposalAdapter {
52
53     private static final String JavaDoc CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; //$NON-NLS-1$
54
private String JavaDoc commandId;
55
56     /**
57      * The command id used for content assist. (value
58      * <code>"org.eclipse.ui.edit.text.contentAssist.proposals"</code>)
59      */

60     public static final String JavaDoc CONTENT_PROPOSAL_COMMAND = "org.eclipse.ui.edit.text.contentAssist.proposals"; //$NON-NLS-1$
61

62     // Default autoactivation delay in milliseconds
63
// TODO: This should eventually be controlled by
64
// a platform UI preference.
65
private static final int DEFAULT_AUTO_ACTIVATION_DELAY = 500;
66
67     private IHandlerService handlerService;
68
69     private IHandlerActivation activeHandler;
70
71     private IHandler proposalHandler = new AbstractHandler() {
72         public Object JavaDoc execute(ExecutionEvent event) {
73             openProposalPopup();
74             return null;
75         }
76
77     };
78     private ControlDecoration decoration;
79
80     /**
81      * Construct a content proposal adapter that can assist the user with
82      * choosing content for the field. No visual indicator of content assist is
83      * shown.
84      *
85      * @param control
86      * the control for which the adapter is providing content assist.
87      * May not be <code>null</code>.
88      * @param controlContentAdapter
89      * the <code>IControlContentAdapter</code> used to obtain and
90      * update the control's contents as proposals are accepted. May
91      * not be <code>null</code>.
92      * @param proposalProvider
93      * the <code>IContentProposalProvider</code> used to obtain
94      * content proposals for this control, or <code>null</code> if
95      * no content proposal is available.
96      * @param commandId
97      * the String id of the command that will invoke the content
98      * assistant. If not supplied, the default value will be
99      * "org.eclipse.ui.edit.text.contentAssist.proposals".
100      * @param autoActivationCharacters
101      * An array of characters that trigger auto-activation of content
102      * proposal. If specified, these characters will trigger
103      * auto-activation of the proposal popup, regardless of the
104      * specified command id.
105      */

106     public ContentAssistCommandAdapter(Control control,
107             IControlContentAdapter controlContentAdapter,
108             IContentProposalProvider proposalProvider, String JavaDoc commandId,
109             char[] autoActivationCharacters) {
110         this(control, controlContentAdapter, proposalProvider, commandId,
111                 autoActivationCharacters, false);
112     }
113
114     /**
115      * Construct a content proposal adapter that can assist the user with
116      * choosing content for the field.
117      *
118      * @param control
119      * the control for which the adapter is providing content assist.
120      * May not be <code>null</code>.
121      * @param controlContentAdapter
122      * the <code>IControlContentAdapter</code> used to obtain and
123      * update the control's contents as proposals are accepted. May
124      * not be <code>null</code>.
125      * @param proposalProvider
126      * the <code>IContentProposalProvider</code> used to obtain
127      * content proposals for this control, or <code>null</code> if
128      * no content proposal is available.
129      * @param commandId
130      * the String id of the command that will invoke the content
131      * assistant. If not supplied, the default value will be
132      * "org.eclipse.ui.edit.text.contentAssist.proposals".
133      * @param autoActivationCharacters
134      * An array of characters that trigger auto-activation of content
135      * proposal. If specified, these characters will trigger
136      * auto-activation of the proposal popup, regardless of the
137      * specified command id.
138      * @param installDecoration
139      * A boolean that specifies whether a content assist control
140      * decoration should be installed. The client is responsible for
141      * ensuring that adequate space is reserved for the decoration.
142      * Clients that want more fine-grained control of the
143      * decoration's location or appearance should use
144      * <code>false</code> for this parameter, creating their own
145      * {@link ControlDecoration} and managing it directly.
146      * @since 3.3
147      */

148     public ContentAssistCommandAdapter(Control control,
149             IControlContentAdapter controlContentAdapter,
150             IContentProposalProvider proposalProvider, String JavaDoc commandId,
151             char[] autoActivationCharacters, boolean installDecoration) {
152         super(control, controlContentAdapter, proposalProvider, null,
153                 autoActivationCharacters);
154         this.commandId = commandId;
155         if (commandId == null) {
156             this.commandId = CONTENT_PROPOSAL_COMMAND;
157         }
158
159         // If no autoactivation characters were specified, set them to the empty
160
// array so that we don't get the alphanumeric auto-trigger of our
161
// superclass.
162
if (autoActivationCharacters == null) {
163             this.setAutoActivationCharacters(new char[] {});
164         }
165         // Set a default autoactivation delay.
166
setAutoActivationDelay(DEFAULT_AUTO_ACTIVATION_DELAY);
167
168         // Add listeners to the control to manage activation of the handler
169
addListeners(control);
170
171         // Cache the handler service so we don't have to retrieve it each time
172
this.handlerService = (IHandlerService) PlatformUI.getWorkbench()
173                 .getService(IHandlerService.class);
174         if (installDecoration) {
175             // Note top left is used for compatibility with 3.2, although
176
// this may change to center alignment in the future.
177
decoration = new ControlDecoration(control, SWT.TOP | SWT.LEFT);
178             decoration.setShowOnlyOnFocus(true);
179             FieldDecoration dec = getContentAssistFieldDecoration();
180             decoration.setImage(dec.getImage());
181             decoration.setDescriptionText(dec.getDescription());
182         }
183
184     }
185
186     /*
187      * Add the listeners needed in order to activate the content assist command
188      * on the control.
189      */

190     private void addListeners(Control control) {
191         control.addFocusListener(new FocusListener() {
192             public void focusLost(FocusEvent e) {
193                 if (activeHandler != null) {
194                     handlerService.deactivateHandler(activeHandler);
195                     activeHandler = null;
196                 }
197             }
198
199             public void focusGained(FocusEvent e) {
200                 if (isEnabled()) {
201                     if (activeHandler == null) {
202                         activeHandler = handlerService.activateHandler(
203                                 commandId, proposalHandler);
204                     }
205                 } else {
206                     if (activeHandler != null) {
207                         handlerService.deactivateHandler(activeHandler);
208                     }
209                     activeHandler = null;
210                 }
211             }
212         });
213         control.addDisposeListener(new DisposeListener() {
214             public void widgetDisposed(DisposeEvent e) {
215                 if (activeHandler != null) {
216                     handlerService.deactivateHandler(activeHandler);
217                     activeHandler = null;
218                 }
219
220             }
221         });
222     }
223
224     /**
225      * Return the string command ID of the command used to invoke content
226      * assist.
227      *
228      * @return the command ID of the command that invokes content assist.
229      */

230     public String JavaDoc getCommandId() {
231         return commandId;
232     }
233
234     /*
235      * Return the field decoration that should be used to indicate that content
236      * assist is available for a field. Ensure that the decoration text includes
237      * the correct key binding.
238      *
239      * @return the {@link FieldDecoration} that should be used to show content
240      * assist.
241      *
242      * @since 3.3
243      */

244     private FieldDecoration getContentAssistFieldDecoration() {
245         FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
246         // Look for a decoration installed for this particular command id.
247
String JavaDoc decId = CONTENT_ASSIST_DECORATION_ID + getCommandId();
248         FieldDecoration dec = registry.getFieldDecoration(decId);
249
250         // If there is not one, base ours on the standard JFace one.
251
if (dec == null) {
252             FieldDecoration originalDec = registry
253                     .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
254
255             registry.registerFieldDecoration(decId, null, originalDec
256                     .getImage());
257             dec = registry.getFieldDecoration(decId);
258         }
259         // Always update the decoration text since the key binding may
260
// have changed since it was last retrieved.
261
IBindingService bindingService = (IBindingService) PlatformUI
262                 .getWorkbench().getService(IBindingService.class);
263         dec
264                 .setDescription(NLS
265                         .bind(
266                                 WorkbenchMessages.ContentAssist_Cue_Description_Key,
267                                 bindingService
268                                         .getBestActiveBindingFormattedFor(getCommandId())));
269
270         // Now return the field decoration
271
return dec;
272     }
273
274     /*
275      * (non-Javadoc)
276      *
277      * Overridden to hide and show the content assist decoration
278      *
279      * @see org.eclipse.jface.fieldassist.ContentProposalAdapter#setEnabled(boolean)
280      * @since 3.3
281      */

282     public void setEnabled(boolean enabled) {
283         super.setEnabled(enabled);
284         if (decoration == null) {
285             return;
286         }
287         if (enabled) {
288             decoration.show();
289         } else {
290             decoration.hide();
291         }
292     }
293 }
294
Popular Tags