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