KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > contentassist > ContentAssistHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.contentassist;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.commands.IHandler;
18
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.swt.events.FocusEvent;
22 import org.eclipse.swt.events.FocusListener;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Text;
26
27 import org.eclipse.jface.bindings.TriggerSequence;
28 import org.eclipse.jface.contentassist.AbstractControlContentAssistSubjectAdapter;
29 import org.eclipse.jface.contentassist.ComboContentAssistSubjectAdapter;
30 import org.eclipse.jface.contentassist.SubjectControlContentAssistant;
31 import org.eclipse.jface.contentassist.TextContentAssistSubjectAdapter;
32 import org.eclipse.jface.viewers.ILabelProvider;
33 import org.eclipse.jface.viewers.LabelProvider;
34
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.handlers.IHandlerActivation;
37 import org.eclipse.ui.handlers.IHandlerService;
38 import org.eclipse.ui.internal.texteditor.NLSUtility;
39 import org.eclipse.ui.keys.IBindingService;
40 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
41
42 /**
43  * A content assistant handler which handles the key binding and
44  * the cue for a {@link org.eclipse.jface.text.contentassist.ContentAssistant}
45  * and its subject adapter.
46  *
47  * @since 3.0
48  * @deprecated As of 3.2, replaced by Platform UI's field assist support
49  */

50 public class ContentAssistHandler {
51     /**
52      * The target control.
53      */

54     private Control fControl;
55     /**
56      * The content assist subject adapter.
57      */

58     private AbstractControlContentAssistSubjectAdapter fContentAssistSubjectAdapter;
59     /**
60      * The content assistant.
61      */

62     private SubjectControlContentAssistant fContentAssistant;
63     /**
64      * The currently installed FocusListener, or <code>null</code> iff none installed.
65      * This is also used as flag to tell whether content assist is enabled
66      */

67     private FocusListener fFocusListener;
68     /**
69      * The currently installed IHandlerActivation, or <code>null</code> iff none installed.
70      */

71     private IHandlerActivation fHandlerActivation;
72
73     /**
74      * Creates a new {@link ContentAssistHandler} for the given {@link Combo}.
75      * Only a single {@link ContentAssistHandler} may be installed on a {@link Combo} instance.
76      * Content Assist is enabled by default.
77      *
78      * @param combo target combo
79      * @param contentAssistant a configured content assistant
80      * @return a new {@link ContentAssistHandler}
81      */

82     public static ContentAssistHandler createHandlerForCombo(Combo combo, SubjectControlContentAssistant contentAssistant) {
83         return new ContentAssistHandler(combo, new ComboContentAssistSubjectAdapter(combo), contentAssistant);
84     }
85
86     /**
87      * Creates a new {@link ContentAssistHandler} for the given {@link Text}.
88      * Only a single {@link ContentAssistHandler} may be installed on a {@link Text} instance.
89      * Content Assist is enabled by default.
90      *
91      * @param text target text
92      * @param contentAssistant a configured content assistant
93      * @return a new {@link ContentAssistHandler}
94      */

95     public static ContentAssistHandler createHandlerForText(Text text, SubjectControlContentAssistant contentAssistant) {
96         return new ContentAssistHandler(text, new TextContentAssistSubjectAdapter(text), contentAssistant);
97     }
98
99     /**
100      * Internal constructor.
101      *
102      * @param control target control
103      * @param subjectAdapter content assist subject adapter
104      * @param contentAssistant content assistant
105      */

106     private ContentAssistHandler(
107             Control control,
108             AbstractControlContentAssistSubjectAdapter subjectAdapter,
109             SubjectControlContentAssistant contentAssistant) {
110         fControl= control;
111         fContentAssistant= contentAssistant;
112         fContentAssistSubjectAdapter= subjectAdapter;
113         setEnabled(true);
114         fControl.addDisposeListener(new DisposeListener() {
115             public void widgetDisposed(DisposeEvent e) {
116                 setEnabled(false);
117             }
118         });
119     }
120
121     /**
122      * @return <code>true</code> iff content assist is enabled
123      */

124     public boolean isEnabled() {
125         return fFocusListener != null;
126     }
127
128     /**
129      * Controls enablement of content assist.
130      * When enabled, a cue is shown next to the focused field
131      * and the affordance hover shows the shortcut.
132      *
133      * @param enable enable content assist iff true
134      */

135     public void setEnabled(boolean enable) {
136         if (enable == isEnabled())
137             return;
138
139         if (enable)
140             enable();
141         else
142             disable();
143     }
144
145     /**
146      * Enable content assist.
147      */

148     private void enable() {
149         if (! fControl.isDisposed()) {
150             fContentAssistant.install(fContentAssistSubjectAdapter);
151             installCueLabelProvider();
152             installFocusListener();
153             if (fControl.isFocusControl())
154                 activateHandler();
155         }
156     }
157
158     /**
159      * Disable content assist.
160      */

161     private void disable() {
162         if (! fControl.isDisposed()) {
163             fContentAssistant.uninstall();
164             fContentAssistSubjectAdapter.setContentAssistCueProvider(null);
165             fControl.removeFocusListener(fFocusListener);
166             fFocusListener= null;
167             if (fHandlerActivation != null)
168                 deactivateHandler();
169         }
170     }
171
172     /**
173      * Create and install the {@link LabelProvider} for fContentAssistSubjectAdapter.
174      */

175     private void installCueLabelProvider() {
176         ILabelProvider labelProvider= new LabelProvider() {
177             /*
178              * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
179              */

180             public String JavaDoc getText(Object JavaDoc element) {
181                 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
182                 TriggerSequence[] activeBindings= bindingService.getActiveBindingsFor(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
183                 if (activeBindings.length == 0)
184                     return ContentAssistMessages.ContentAssistHandler_contentAssistAvailable;
185                 return NLSUtility.format(ContentAssistMessages.ContentAssistHandler_contentAssistAvailableWithKeyBinding, activeBindings[0].format());
186             }
187         };
188         fContentAssistSubjectAdapter.setContentAssistCueProvider(labelProvider);
189     }
190
191     /**
192      * Create fFocusListener and install it on fControl.
193      */

194     private void installFocusListener() {
195         fFocusListener= new FocusListener() {
196             public void focusGained(final FocusEvent e) {
197                 if (fHandlerActivation == null)
198                     activateHandler();
199             }
200             public void focusLost(FocusEvent e) {
201                 if (fHandlerActivation != null)
202                     deactivateHandler();
203             }
204         };
205         fControl.addFocusListener(fFocusListener);
206     }
207
208     /**
209      * Create and register fHandlerSubmission.
210      */

211     private void activateHandler() {
212         IHandlerService handlerService= (IHandlerService)PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
213         if (handlerService == null)
214             return;
215         
216         IHandler handler= new AbstractHandler() {
217             public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
218                 if (ContentAssistHandler.this.isEnabled()) // don't call AbstractHandler#isEnabled()!
219
fContentAssistant.showPossibleCompletions();
220                 return null;
221             }
222         };
223         fHandlerActivation= handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler);
224     }
225
226     /**
227      * Unregister the {@link IHandlerActivation} from the shell.
228      */

229     private void deactivateHandler() {
230         IHandlerService handlerService= (IHandlerService)PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
231         if (handlerService != null)
232             handlerService.deactivateHandler(fHandlerActivation);
233         fHandlerActivation= null;
234     }
235 }
236
Popular Tags