Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d864c159af9ed4023efcb65602900bd943bc88c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
 * Copyright (c) 2005, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.dialogs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.internal.ui.text.CWordIterator;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.commands.contexts.ContextManager;
import org.eclipse.jface.bindings.BindingManager;
import org.eclipse.jface.bindings.Scheme;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.bindings.keys.KeySequence;
import org.eclipse.jface.bindings.keys.SWTKeySupport;
import org.eclipse.jface.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;

import com.ibm.icu.text.BreakIterator;

/**
 * Support for camelCase-aware sub-word navigation in dialog fields.
 */
public class TextFieldNavigationHandler {

	public static void install(Text text) {
		new FocusHandler(new TextNavigable(text));
	}

	public static void install(StyledText styledText) {
		new FocusHandler(new StyledTextNavigable(styledText));
	}

	public static void install(Combo combo) {
		new FocusHandler(new ComboNavigable(combo));
	}

	private abstract static class WorkaroundNavigable extends Navigable {
		/* workarounds for:
		 * - bug 103630: Add API: Combo#getCaretPosition()
		 * - bug 106024: Text#setSelection(int, int) does not handle start > end with SWT.SINGLE
		 */
		Point fLastSelection;
		int fCaretPosition;

		void selectionChanged() {
			Point selection = getSelection();
			if (selection.equals(fLastSelection)) {
				// leave caret position
			} else if (selection.x == selection.y) { //empty range
				fCaretPosition = selection.x;
			} else if (fLastSelection.y == selection.y) {
				fCaretPosition = selection.x; //same end -> assume caret at start
			} else {
				fCaretPosition = selection.y;
			}
			fLastSelection = selection;
		}
	}

	private abstract static class Navigable {
		public abstract Control getControl();

		public abstract String getText();

		public abstract void setText(String text);

		public abstract Point getSelection();

		public abstract void setSelection(int start, int end);

		public abstract int getCaretPosition();
	}

	private static class TextNavigable extends WorkaroundNavigable {
		static final boolean BUG_106024_TEXT_SELECTION = "win32".equals(SWT.getPlatform()) //$NON-NLS-1$
				// on carbon, getCaretPosition() always returns getSelection().x
				|| Util.isMac();

		private final Text fText;

		public TextNavigable(Text text) {
			fText = text;
			// workaround for bug 106024:
			if (BUG_106024_TEXT_SELECTION) {
				fLastSelection = getSelection();
				fCaretPosition = fLastSelection.y;
				fText.addKeyListener(new KeyAdapter() {
					@Override
					public void keyReleased(KeyEvent e) {
						selectionChanged();
					}
				});
				fText.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent e) {
						selectionChanged();
					}
				});
			}
		}

		@Override
		public Control getControl() {
			return fText;
		}

		@Override
		public String getText() {
			return fText.getText();
		}

		@Override
		public void setText(String text) {
			fText.setText(text);
		}

		@Override
		public Point getSelection() {
			return fText.getSelection();
		}

		@Override
		public int getCaretPosition() {
			if (BUG_106024_TEXT_SELECTION) {
				selectionChanged();
				return fCaretPosition;
			} else {
				return fText.getCaretPosition();
			}
		}

		@Override
		public void setSelection(int start, int end) {
			fText.setSelection(start, end);
		}
	}

	private static class StyledTextNavigable extends Navigable {
		private final StyledText fStyledText;

		public StyledTextNavigable(StyledText styledText) {
			fStyledText = styledText;
		}

		@Override
		public Control getControl() {
			return fStyledText;
		}

		@Override
		public String getText() {
			return fStyledText.getText();
		}

		@Override
		public void setText(String text) {
			fStyledText.setText(text);
		}

		@Override
		public Point getSelection() {
			return fStyledText.getSelection();
		}

		@Override
		public int getCaretPosition() {
			return fStyledText.getCaretOffset();
		}

		@Override
		public void setSelection(int start, int end) {
			fStyledText.setSelection(start, end);
		}
	}

	private static class ComboNavigable extends WorkaroundNavigable {
		private final Combo fCombo;

		public ComboNavigable(Combo combo) {
			fCombo = combo;
			// workaround for bug 103630:
			fLastSelection = getSelection();
			fCaretPosition = fLastSelection.y;
			fCombo.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					selectionChanged();
				}
			});
			fCombo.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseUp(MouseEvent e) {
					selectionChanged();
				}
			});
		}

		@Override
		public Control getControl() {
			return fCombo;
		}

		@Override
		public String getText() {
			return fCombo.getText();
		}

		@Override
		public void setText(String text) {
			fCombo.setText(text);
		}

		@Override
		public Point getSelection() {
			return fCombo.getSelection();
		}

		@Override
		public int getCaretPosition() {
			selectionChanged();
			return fCaretPosition;
			//			return fCombo.getCaretPosition(); // not available: bug 103630
		}

		@Override
		public void setSelection(int start, int end) {
			fCombo.setSelection(new Point(start, end));
		}
	}

	private static class FocusHandler implements FocusListener {
		private static final String EMPTY_TEXT = ""; //$NON-NLS-1$

		private final CWordIterator fIterator;
		private final Navigable fNavigable;
		private KeyAdapter fKeyListener;

		private FocusHandler(Navigable navigable) {
			fIterator = new CWordIterator();
			fNavigable = navigable;

			Control control = navigable.getControl();
			control.addFocusListener(this);
			if (control.isFocusControl())
				activate();
			control.addDisposeListener(new DisposeListener() {
				@Override
				public void widgetDisposed(DisposeEvent e) {
					deactivate();
				}
			});
		}

		@Override
		public void focusGained(FocusEvent e) {
			activate();
		}

		@Override
		public void focusLost(FocusEvent e) {
			deactivate();
		}

		private void activate() {
			fNavigable.getControl().addKeyListener(getKeyListener());
		}

		private void deactivate() {
			if (fKeyListener != null) {
				Control control = fNavigable.getControl();
				if (!control.isDisposed())
					control.removeKeyListener(fKeyListener);
				fKeyListener = null;
			}
		}

		private KeyAdapter getKeyListener() {
			if (fKeyListener == null) {
				fKeyListener = new KeyAdapter() {
					private final boolean IS_WORKAROUND = (fNavigable instanceof ComboNavigable)
							|| (fNavigable instanceof TextNavigable && TextNavigable.BUG_106024_TEXT_SELECTION);
					private List<Submission> fSubmissions;

					@Override
					public void keyPressed(KeyEvent e) {
						if (IS_WORKAROUND) {
							if (e.keyCode == SWT.ARROW_LEFT && e.stateMask == SWT.MOD2) {
								int caretPosition = fNavigable.getCaretPosition();
								if (caretPosition != 0) {
									Point selection = fNavigable.getSelection();
									if (caretPosition == selection.x)
										fNavigable.setSelection(selection.y, caretPosition - 1);
									else
										fNavigable.setSelection(selection.x, caretPosition - 1);
								}
								e.doit = false;
								return;

							} else if (e.keyCode == SWT.ARROW_RIGHT && e.stateMask == SWT.MOD2) {
								String text = fNavigable.getText();
								int caretPosition = fNavigable.getCaretPosition();
								if (caretPosition != text.length()) {
									Point selection = fNavigable.getSelection();
									if (caretPosition == selection.y)
										fNavigable.setSelection(selection.x, caretPosition + 1);
									else
										fNavigable.setSelection(selection.y, caretPosition + 1);
								}
								e.doit = false;
								return;
							}
						}
						int accelerator = SWTKeySupport.convertEventToUnmodifiedAccelerator(e);
						KeySequence keySequence = KeySequence
								.getInstance(SWTKeySupport.convertAcceleratorToKeyStroke(accelerator));
						for (Submission submission : getSubmissions()) {
							TriggerSequence[] triggerSequences = submission.getTriggerSequences();
							for (int i = 0; i < triggerSequences.length; i++) {
								if (triggerSequences[i].equals(keySequence)) { // XXX does not work for multi-stroke bindings
									e.doit = false;
									submission.execute();
									return;
								}
							}
						}
					}

					private List<Submission> getSubmissions() {
						if (fSubmissions != null)
							return fSubmissions;

						fSubmissions = new ArrayList<>();

						ICommandService commandService = PlatformUI.getWorkbench().getAdapter(ICommandService.class);
						IBindingService bindingService = PlatformUI.getWorkbench().getAdapter(IBindingService.class);
						if (commandService == null || bindingService == null)
							return fSubmissions;

						// Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=184502 ,
						// similar to CodeAssistAdvancedConfigurationBlock.getKeyboardShortcut(..):
						BindingManager localBindingManager = new BindingManager(new ContextManager(),
								new CommandManager());
						final Scheme[] definedSchemes = bindingService.getDefinedSchemes();
						if (definedSchemes != null) {
							try {
								for (int i = 0; i < definedSchemes.length; i++) {
									Scheme scheme = definedSchemes[i];
									Scheme localSchemeCopy = localBindingManager.getScheme(scheme.getId());
									localSchemeCopy.define(scheme.getName(), scheme.getDescription(),
											scheme.getParentId());
								}
							} catch (final NotDefinedException e) {
								CUIPlugin.log(e);
							}
						}
						localBindingManager.setLocale(bindingService.getLocale());
						localBindingManager.setPlatform(bindingService.getPlatform());

						localBindingManager.setBindings(bindingService.getBindings());
						try {
							Scheme activeScheme = bindingService.getActiveScheme();
							if (activeScheme != null)
								localBindingManager.setActiveScheme(activeScheme);
						} catch (NotDefinedException e) {
							CUIPlugin.log(e);
						}

						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.SELECT_WORD_NEXT)) {
							@Override
							public void execute() {
								fIterator.setText(fNavigable.getText());
								int caretPosition = fNavigable.getCaretPosition();
								int newCaret = fIterator.following(caretPosition);
								if (newCaret != BreakIterator.DONE) {
									Point selection = fNavigable.getSelection();
									if (caretPosition == selection.y)
										fNavigable.setSelection(selection.x, newCaret);
									else
										fNavigable.setSelection(selection.y, newCaret);
								}
								fIterator.setText(EMPTY_TEXT);
							}
						});
						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS)) {
							@Override
							public void execute() {
								fIterator.setText(fNavigable.getText());
								int caretPosition = fNavigable.getCaretPosition();
								int newCaret = fIterator.preceding(caretPosition);
								if (newCaret != BreakIterator.DONE) {
									Point selection = fNavigable.getSelection();
									if (caretPosition == selection.x)
										fNavigable.setSelection(selection.y, newCaret);
									else
										fNavigable.setSelection(selection.x, newCaret);
								}
								fIterator.setText(EMPTY_TEXT);
							}
						});
						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.WORD_NEXT)) {
							@Override
							public void execute() {
								fIterator.setText(fNavigable.getText());
								int caretPosition = fNavigable.getCaretPosition();
								int newCaret = fIterator.following(caretPosition);
								if (newCaret != BreakIterator.DONE)
									fNavigable.setSelection(newCaret, newCaret);
								fIterator.setText(EMPTY_TEXT);
							}
						});
						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.WORD_PREVIOUS)) {
							@Override
							public void execute() {
								fIterator.setText(fNavigable.getText());
								int caretPosition = fNavigable.getCaretPosition();
								int newCaret = fIterator.preceding(caretPosition);
								if (newCaret != BreakIterator.DONE)
									fNavigable.setSelection(newCaret, newCaret);
								fIterator.setText(EMPTY_TEXT);
							}
						});
						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.DELETE_NEXT_WORD)) {
							@Override
							public void execute() {
								Point selection = fNavigable.getSelection();
								String text = fNavigable.getText();
								int start;
								int end;
								if (selection.x != selection.y) {
									start = selection.x;
									end = selection.y;
								} else {
									fIterator.setText(text);
									start = fNavigable.getCaretPosition();
									end = fIterator.following(start);
									fIterator.setText(EMPTY_TEXT);
									if (end == BreakIterator.DONE)
										return;
								}
								fNavigable.setText(text.substring(0, start) + text.substring(end));
								fNavigable.setSelection(start, start);
							}
						});
						fSubmissions.add(new Submission(getKeyBindings(localBindingManager, commandService,
								ITextEditorActionDefinitionIds.DELETE_PREVIOUS_WORD)) {
							@Override
							public void execute() {
								Point selection = fNavigable.getSelection();
								String text = fNavigable.getText();
								int start;
								int end;
								if (selection.x != selection.y) {
									start = selection.x;
									end = selection.y;
								} else {
									fIterator.setText(text);
									end = fNavigable.getCaretPosition();
									start = fIterator.preceding(end);
									fIterator.setText(EMPTY_TEXT);
									if (start == BreakIterator.DONE)
										return;
								}
								fNavigable.setText(text.substring(0, start) + text.substring(end));
								fNavigable.setSelection(start, start);
							}
						});

						return fSubmissions;
					}

					private TriggerSequence[] getKeyBindings(BindingManager localBindingManager,
							ICommandService commandService, String commandID) {
						Command command = commandService.getCommand(commandID);
						ParameterizedCommand pCmd = new ParameterizedCommand(command, null);
						return localBindingManager.getActiveBindingsDisregardingContextFor(pCmd);
					}

				};
			}
			return fKeyListener;
		}
	}

	private abstract static class Submission {
		private TriggerSequence[] fTriggerSequences;

		public Submission(TriggerSequence[] triggerSequences) {
			fTriggerSequences = triggerSequences;
		}

		public TriggerSequence[] getTriggerSequences() {
			return fTriggerSequences;
		}

		public abstract void execute();
	}

}

Back to the top