|
Lines 7-12
Link Here
|
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* David Green - fix for bug 301196: [FieldAssist] ContentAssistCommandAdapter should provide an activation expression when activating the command handler |
| 10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
| 11 |
|
12 |
|
| 12 |
package org.eclipse.ui.fieldassist; |
13 |
package org.eclipse.ui.fieldassist; |
|
Lines 14-19
Link Here
|
| 14 |
import org.eclipse.core.commands.AbstractHandler; |
15 |
import org.eclipse.core.commands.AbstractHandler; |
| 15 |
import org.eclipse.core.commands.ExecutionEvent; |
16 |
import org.eclipse.core.commands.ExecutionEvent; |
| 16 |
import org.eclipse.core.commands.IHandler; |
17 |
import org.eclipse.core.commands.IHandler; |
|
|
18 |
import org.eclipse.core.expressions.EvaluationResult; |
| 19 |
import org.eclipse.core.expressions.Expression; |
| 20 |
import org.eclipse.core.expressions.ExpressionInfo; |
| 21 |
import org.eclipse.core.expressions.IEvaluationContext; |
| 17 |
import org.eclipse.jface.fieldassist.ContentProposalAdapter; |
22 |
import org.eclipse.jface.fieldassist.ContentProposalAdapter; |
| 18 |
import org.eclipse.jface.fieldassist.ControlDecoration; |
23 |
import org.eclipse.jface.fieldassist.ControlDecoration; |
| 19 |
import org.eclipse.jface.fieldassist.FieldDecoration; |
24 |
import org.eclipse.jface.fieldassist.FieldDecoration; |
|
Lines 24-38
Link Here
|
| 24 |
import org.eclipse.swt.SWT; |
29 |
import org.eclipse.swt.SWT; |
| 25 |
import org.eclipse.swt.events.DisposeEvent; |
30 |
import org.eclipse.swt.events.DisposeEvent; |
| 26 |
import org.eclipse.swt.events.DisposeListener; |
31 |
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; |
32 |
import org.eclipse.swt.widgets.Control; |
|
|
33 |
import org.eclipse.ui.ISources; |
| 30 |
import org.eclipse.ui.IWorkbenchCommandConstants; |
34 |
import org.eclipse.ui.IWorkbenchCommandConstants; |
| 31 |
import org.eclipse.ui.PlatformUI; |
35 |
import org.eclipse.ui.PlatformUI; |
| 32 |
import org.eclipse.ui.handlers.IHandlerActivation; |
36 |
import org.eclipse.ui.handlers.IHandlerActivation; |
| 33 |
import org.eclipse.ui.handlers.IHandlerService; |
37 |
import org.eclipse.ui.handlers.IHandlerService; |
| 34 |
import org.eclipse.ui.internal.WorkbenchMessages; |
38 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 35 |
import org.eclipse.ui.keys.IBindingService; |
39 |
import org.eclipse.ui.keys.IBindingService; |
|
|
40 |
import org.eclipse.ui.swt.IFocusService; |
| 36 |
|
41 |
|
| 37 |
/** |
42 |
/** |
| 38 |
* ContentAssistCommandAdapter extends {@link ContentProposalAdapter} to invoke |
43 |
* ContentAssistCommandAdapter extends {@link ContentProposalAdapter} to invoke |
|
Lines 51-57
Link Here
|
| 51 |
*/ |
56 |
*/ |
| 52 |
public class ContentAssistCommandAdapter extends ContentProposalAdapter { |
57 |
public class ContentAssistCommandAdapter extends ContentProposalAdapter { |
| 53 |
|
58 |
|
|
|
59 |
// ID used in the decoration registry. |
| 54 |
private static final String CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; //$NON-NLS-1$ |
60 |
private static final String CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; //$NON-NLS-1$ |
|
|
61 |
|
| 62 |
// ID used when registering our control with the focus service. Since we |
| 63 |
// never test the control ID in our activation expression, we can safely use |
| 64 |
// the same ID for all controls |
| 65 |
private static final String CONTROL_ID = "org.eclipse.ui.fieldAssist.ContentAssistField.ControlID"; //$NON-NLS-1$ |
| 66 |
|
| 55 |
private String commandId; |
67 |
private String commandId; |
| 56 |
|
68 |
|
| 57 |
/** |
69 |
/** |
|
Lines 67-76
Link Here
|
| 67 |
// a platform UI preference. |
79 |
// a platform UI preference. |
| 68 |
private static final int DEFAULT_AUTO_ACTIVATION_DELAY = 500; |
80 |
private static final int DEFAULT_AUTO_ACTIVATION_DELAY = 500; |
| 69 |
|
81 |
|
| 70 |
private IHandlerService handlerService; |
|
|
| 71 |
|
| 72 |
private IHandlerActivation activeHandler; |
| 73 |
|
| 74 |
private IHandler proposalHandler = new AbstractHandler() { |
82 |
private IHandler proposalHandler = new AbstractHandler() { |
| 75 |
public Object execute(ExecutionEvent event) { |
83 |
public Object execute(ExecutionEvent event) { |
| 76 |
openProposalPopup(); |
84 |
openProposalPopup(); |
|
Lines 168-183
Link Here
|
| 168 |
// Set a default autoactivation delay. |
176 |
// Set a default autoactivation delay. |
| 169 |
setAutoActivationDelay(DEFAULT_AUTO_ACTIVATION_DELAY); |
177 |
setAutoActivationDelay(DEFAULT_AUTO_ACTIVATION_DELAY); |
| 170 |
|
178 |
|
| 171 |
// Cache the handler service so we don't have to retrieve it each time |
179 |
// Activate the handler |
| 172 |
this.handlerService = (IHandlerService) PlatformUI.getWorkbench() |
180 |
activateHandler(control); |
| 173 |
.getService(IHandlerService.class); |
|
|
| 174 |
|
| 175 |
// Add listeners to the control to manage activation of the handler |
| 176 |
addListeners(control); |
| 177 |
|
| 178 |
if (control.isFocusControl()) { |
| 179 |
activateHandler(); |
| 180 |
} |
| 181 |
|
181 |
|
| 182 |
if (installDecoration) { |
182 |
if (installDecoration) { |
| 183 |
// Note top left is used for compatibility with 3.2, although |
183 |
// Note top left is used for compatibility with 3.2, although |
|
Lines 191-221
Link Here
|
| 191 |
|
191 |
|
| 192 |
} |
192 |
} |
| 193 |
|
193 |
|
| 194 |
/* |
|
|
| 195 |
* Add the listeners needed in order to activate the content assist command |
| 196 |
* on the control. |
| 197 |
*/ |
| 198 |
private void addListeners(Control control) { |
| 199 |
control.addFocusListener(new FocusListener() { |
| 200 |
public void focusLost(FocusEvent e) { |
| 201 |
deactivateHandler(); |
| 202 |
} |
| 203 |
|
| 204 |
public void focusGained(FocusEvent e) { |
| 205 |
if (isEnabled()) { |
| 206 |
activateHandler(); |
| 207 |
} else { |
| 208 |
deactivateHandler(); |
| 209 |
} |
| 210 |
} |
| 211 |
}); |
| 212 |
control.addDisposeListener(new DisposeListener() { |
| 213 |
public void widgetDisposed(DisposeEvent e) { |
| 214 |
deactivateHandler(); |
| 215 |
} |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
194 |
/** |
| 220 |
* Return the string command ID of the command used to invoke content |
195 |
* Return the string command ID of the command used to invoke content |
| 221 |
* assist. |
196 |
* assist. |
|
Lines 283-308
Link Here
|
| 283 |
decoration.hide(); |
258 |
decoration.hide(); |
| 284 |
} |
259 |
} |
| 285 |
} |
260 |
} |
| 286 |
if (getControl().isFocusControl()) { |
|
|
| 287 |
if (enabled) { |
| 288 |
activateHandler(); |
| 289 |
} else { |
| 290 |
deactivateHandler(); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
private void activateHandler() { |
| 296 |
if (activeHandler == null) { |
| 297 |
activeHandler = handlerService.activateHandler(commandId, |
| 298 |
proposalHandler); |
| 299 |
} |
| 300 |
} |
261 |
} |
| 301 |
|
262 |
|
| 302 |
private void deactivateHandler() { |
263 |
private void activateHandler(final Control control) { |
| 303 |
if (activeHandler != null) { |
264 |
IFocusService fs = (IFocusService) PlatformUI.getWorkbench() |
| 304 |
handlerService.deactivateHandler(activeHandler); |
265 |
.getService(IFocusService.class); |
| 305 |
activeHandler = null; |
266 |
final IHandlerService hs = (IHandlerService) PlatformUI.getWorkbench().getService( |
|
|
267 |
IHandlerService.class); |
| 268 |
if (fs != null && hs != null) { |
| 269 |
fs.addFocusTracker(control, CONTROL_ID); |
| 270 |
final IHandlerActivation handlerActivation = hs.activateHandler(commandId, |
| 271 |
proposalHandler, new Expression() { |
| 272 |
public EvaluationResult evaluate(IEvaluationContext context) { |
| 273 |
return context.getVariable(ISources.ACTIVE_FOCUS_CONTROL_NAME) == control |
| 274 |
&& ContentAssistCommandAdapter.this.isEnabled() ? EvaluationResult.TRUE |
| 275 |
: EvaluationResult.FALSE; |
| 276 |
} |
| 277 |
|
| 278 |
public void collectExpressionInfo(final ExpressionInfo info) { |
| 279 |
info.addVariableNameAccess(ISources.ACTIVE_FOCUS_CONTROL_NAME); |
| 280 |
} |
| 281 |
|
| 282 |
}); |
| 283 |
control.addDisposeListener(new DisposeListener() { |
| 284 |
public void widgetDisposed(DisposeEvent e) { |
| 285 |
hs.deactivateHandler(handlerActivation); |
| 286 |
} |
| 287 |
}); |
| 306 |
} |
288 |
} |
| 307 |
} |
289 |
} |
| 308 |
} |
290 |
} |