1 /*******************************************************************************
\r
2 * Copyright (c) 2008 Mateusz Matela and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * Mateusz Matela <mateusz.matela@gmail.com> - Make StyledText work with ContentAssistCommandAdapter - https://bugs.eclipse.org/bugs/show_bug.cgi?id=246388
\r
10 *******************************************************************************/
\r
11 package org.simantics.scl.ui.assist;
\r
13 import org.eclipse.jface.fieldassist.IControlContentAdapter;
\r
14 import org.eclipse.jface.fieldassist.IControlContentAdapter2;
\r
15 import org.eclipse.swt.custom.StyledText;
\r
16 import org.eclipse.swt.graphics.Point;
\r
17 import org.eclipse.swt.graphics.Rectangle;
\r
18 import org.eclipse.swt.widgets.Control;
\r
21 * An {@link IControlContentAdapter} for {@link org.eclipse.swt.custom.StyledText}. This is
\r
22 * a convenience class for easily creating a {@link ContentProposalAdapter} for text fields.
\r
26 public class StyledTextContentAdapter implements IControlContentAdapter, IControlContentAdapter2 {
\r
31 * org.eclipse.jface.fieldassist.IControlContentAdapter#getControlContents(org.eclipse
\r
32 * .swt.widgets.Control)
\r
34 public String getControlContents(Control control) {
\r
35 return ((StyledText)control).getText();
\r
42 * org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse
\r
43 * .swt.widgets.Control)
\r
45 public int getCursorPosition(Control control) {
\r
46 return ((StyledText)control).getCaretOffset();
\r
53 * org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse
\r
54 * .swt.widgets.Control)
\r
56 public Rectangle getInsertionBounds(Control control) {
\r
57 StyledText text= (StyledText)control;
\r
58 Point caretOrigin= text.getLocationAtOffset(text.getCaretOffset());
\r
59 return new Rectangle(caretOrigin.x + text.getClientArea().x, caretOrigin.y + text.getClientArea().y + 3, 1, text.getLineHeight());
\r
66 * org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse
\r
67 * .swt.widgets.Control, java.lang.String, int)
\r
69 public void insertControlContents(Control control, String contents, int cursorPosition) {
\r
70 StyledText text = ((StyledText)control);
\r
71 cursorPosition = Math.min(cursorPosition, contents.length());
\r
72 int caretEndRange = text.getCaretOffset();
\r
73 String currentText = text.getText();
\r
75 int offset = caretEndRange;
\r
76 int length = currentText.length();
\r
77 while (--offset >= 0 && (Character.isJavaIdentifierPart(currentText.charAt(offset)) && !Character.isWhitespace(currentText.charAt(offset))))
\r
80 int nameSpaceBeginRange = currentText.lastIndexOf(".", caretEndRange - 1);
\r
81 if (nameSpaceBeginRange > length)
\r
82 length = nameSpaceBeginRange;
\r
83 int endRange = currentText.length();
\r
84 if (caretEndRange < endRange)
\r
85 endRange = caretEndRange;
\r
86 text.setSelection(length, endRange);
\r
87 text.insert(contents);
\r
88 // calculate the initial count of letters that was typed when the proposal was accepted to insert the caret
\r
89 // at the right position
\r
90 int proposalFirstLettersCount = endRange - (length);
\r
91 text.setCaretOffset(caretEndRange + cursorPosition - proposalFirstLettersCount);
\r
98 * org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse
\r
99 * .swt.widgets.Control, java.lang.String, int)
\r
101 public void setControlContents(Control control, String contents, int cursorPosition) {
\r
102 ((StyledText)control).setText(contents);
\r
103 ((StyledText)control).setCaretOffset(cursorPosition);
\r
110 * org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse
\r
111 * .swt.widgets.Control, int)
\r
113 public void setCursorPosition(Control control, int index) {
\r
114 ((StyledText)control).setCaretOffset(index);
\r
121 * org.eclipse.jface.fieldassist.IControlContentAdapter2#getSelection(org.eclipse.swt
\r
122 * .widgets.Control)
\r
124 public Point getSelection(Control control) {
\r
125 return ((StyledText)control).getSelection();
\r
132 * org.eclipse.jface.fieldassist.IControlContentAdapter2#setSelection(org.eclipse.swt
\r
133 * .widgets.Control, org.eclipse.swt.graphics.Point)
\r
135 public void setSelection(Control control, Point range) {
\r
136 ((StyledText)control).setSelection(range);
\r