001: /*******************************************************************************
002: * Copyright (c) 2000, 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.contentassist;
011:
012: import org.eclipse.swt.events.DisposeEvent;
013: import org.eclipse.swt.events.DisposeListener;
014: import org.eclipse.swt.events.FocusEvent;
015: import org.eclipse.swt.events.FocusListener;
016: import org.eclipse.swt.widgets.Combo;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.swt.widgets.Text;
019:
020: import org.eclipse.core.commands.AbstractHandler;
021: import org.eclipse.core.commands.ExecutionEvent;
022: import org.eclipse.core.commands.ExecutionException;
023: import org.eclipse.core.commands.IHandler;
024:
025: import org.eclipse.jface.bindings.TriggerSequence;
026: import org.eclipse.jface.contentassist.AbstractControlContentAssistSubjectAdapter;
027: import org.eclipse.jface.contentassist.ComboContentAssistSubjectAdapter;
028: import org.eclipse.jface.contentassist.SubjectControlContentAssistant;
029: import org.eclipse.jface.contentassist.TextContentAssistSubjectAdapter;
030: import org.eclipse.jface.viewers.ILabelProvider;
031: import org.eclipse.jface.viewers.LabelProvider;
032:
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.handlers.IHandlerActivation;
035: import org.eclipse.ui.handlers.IHandlerService;
036: import org.eclipse.ui.internal.texteditor.NLSUtility;
037: import org.eclipse.ui.keys.IBindingService;
038: import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
039:
040: /**
041: * A content assistant handler which handles the key binding and
042: * the cue for a {@link org.eclipse.jface.text.contentassist.ContentAssistant}
043: * and its subject adapter.
044: *
045: * @since 3.0
046: * @deprecated As of 3.2, replaced by JFace field assist support
047: */
048: public class ContentAssistHandler {
049: /**
050: * The target control.
051: */
052: private Control fControl;
053: /**
054: * The content assist subject adapter.
055: */
056: private AbstractControlContentAssistSubjectAdapter fContentAssistSubjectAdapter;
057: /**
058: * The content assistant.
059: */
060: private SubjectControlContentAssistant fContentAssistant;
061: /**
062: * The currently installed FocusListener, or <code>null</code> iff none installed.
063: * This is also used as flag to tell whether content assist is enabled
064: */
065: private FocusListener fFocusListener;
066: /**
067: * The currently installed IHandlerActivation, or <code>null</code> iff none installed.
068: */
069: private IHandlerActivation fHandlerActivation;
070:
071: /**
072: * Creates a new {@link ContentAssistHandler} for the given {@link Combo}.
073: * Only a single {@link ContentAssistHandler} may be installed on a {@link Combo} instance.
074: * Content Assist is enabled by default.
075: *
076: * @param combo target combo
077: * @param contentAssistant a configured content assistant
078: * @return a new {@link ContentAssistHandler}
079: */
080: public static ContentAssistHandler createHandlerForCombo(
081: Combo combo, SubjectControlContentAssistant contentAssistant) {
082: return new ContentAssistHandler(combo,
083: new ComboContentAssistSubjectAdapter(combo),
084: contentAssistant);
085: }
086:
087: /**
088: * Creates a new {@link ContentAssistHandler} for the given {@link Text}.
089: * Only a single {@link ContentAssistHandler} may be installed on a {@link Text} instance.
090: * Content Assist is enabled by default.
091: *
092: * @param text target text
093: * @param contentAssistant a configured content assistant
094: * @return a new {@link ContentAssistHandler}
095: */
096: public static ContentAssistHandler createHandlerForText(Text text,
097: SubjectControlContentAssistant contentAssistant) {
098: return new ContentAssistHandler(text,
099: new TextContentAssistSubjectAdapter(text),
100: contentAssistant);
101: }
102:
103: /**
104: * Internal constructor.
105: *
106: * @param control target control
107: * @param subjectAdapter content assist subject adapter
108: * @param contentAssistant content assistant
109: */
110: private ContentAssistHandler(Control control,
111: AbstractControlContentAssistSubjectAdapter subjectAdapter,
112: SubjectControlContentAssistant contentAssistant) {
113: fControl = control;
114: fContentAssistant = contentAssistant;
115: fContentAssistSubjectAdapter = subjectAdapter;
116: setEnabled(true);
117: fControl.addDisposeListener(new DisposeListener() {
118: public void widgetDisposed(DisposeEvent e) {
119: setEnabled(false);
120: }
121: });
122: }
123:
124: /**
125: * @return <code>true</code> iff content assist is enabled
126: */
127: public boolean isEnabled() {
128: return fFocusListener != null;
129: }
130:
131: /**
132: * Controls enablement of content assist.
133: * When enabled, a cue is shown next to the focused field
134: * and the affordance hover shows the shortcut.
135: *
136: * @param enable enable content assist iff true
137: */
138: public void setEnabled(boolean enable) {
139: if (enable == isEnabled())
140: return;
141:
142: if (enable)
143: enable();
144: else
145: disable();
146: }
147:
148: /**
149: * Enable content assist.
150: */
151: private void enable() {
152: if (!fControl.isDisposed()) {
153: fContentAssistant.install(fContentAssistSubjectAdapter);
154: installCueLabelProvider();
155: installFocusListener();
156: if (fControl.isFocusControl())
157: activateHandler();
158: }
159: }
160:
161: /**
162: * Disable content assist.
163: */
164: private void disable() {
165: if (!fControl.isDisposed()) {
166: fContentAssistant.uninstall();
167: fContentAssistSubjectAdapter
168: .setContentAssistCueProvider(null);
169: fControl.removeFocusListener(fFocusListener);
170: fFocusListener = null;
171: if (fHandlerActivation != null)
172: deactivateHandler();
173: }
174: }
175:
176: /**
177: * Create and install the {@link LabelProvider} for fContentAssistSubjectAdapter.
178: */
179: private void installCueLabelProvider() {
180: ILabelProvider labelProvider = new LabelProvider() {
181: /*
182: * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
183: */
184: public String getText(Object element) {
185: IBindingService bindingService = (IBindingService) PlatformUI
186: .getWorkbench().getAdapter(
187: IBindingService.class);
188: TriggerSequence[] activeBindings = bindingService
189: .getActiveBindingsFor(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
190: if (activeBindings.length == 0)
191: return ContentAssistMessages.ContentAssistHandler_contentAssistAvailable;
192: return NLSUtility
193: .format(
194: ContentAssistMessages.ContentAssistHandler_contentAssistAvailableWithKeyBinding,
195: activeBindings[0].format());
196: }
197: };
198: fContentAssistSubjectAdapter
199: .setContentAssistCueProvider(labelProvider);
200: }
201:
202: /**
203: * Create fFocusListener and install it on fControl.
204: */
205: private void installFocusListener() {
206: fFocusListener = new FocusListener() {
207: public void focusGained(final FocusEvent e) {
208: if (fHandlerActivation == null)
209: activateHandler();
210: }
211:
212: public void focusLost(FocusEvent e) {
213: if (fHandlerActivation != null)
214: deactivateHandler();
215: }
216: };
217: fControl.addFocusListener(fFocusListener);
218: }
219:
220: /**
221: * Create and register fHandlerSubmission.
222: */
223: private void activateHandler() {
224: IHandlerService handlerService = (IHandlerService) PlatformUI
225: .getWorkbench().getAdapter(IHandlerService.class);
226: if (handlerService == null)
227: return;
228:
229: IHandler handler = new AbstractHandler() {
230: public Object execute(ExecutionEvent event)
231: throws ExecutionException {
232: if (ContentAssistHandler.this .isEnabled()) // don't call AbstractHandler#isEnabled()!
233: fContentAssistant.showPossibleCompletions();
234: return null;
235: }
236: };
237: fHandlerActivation = handlerService
238: .activateHandler(
239: ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS,
240: handler);
241: }
242:
243: /**
244: * Unregister the {@link IHandlerActivation} from the shell.
245: */
246: private void deactivateHandler() {
247: IHandlerService handlerService = (IHandlerService) PlatformUI
248: .getWorkbench().getAdapter(IHandlerService.class);
249: if (handlerService != null)
250: handlerService.deactivateHandler(fHandlerActivation);
251: fHandlerActivation = null;
252: }
253: }
|