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.ContentProposalAdapter;
14 import org.eclipse.jface.fieldassist.IControlContentAdapter;
15 import org.eclipse.jface.fieldassist.IControlContentAdapter2;
16 import org.eclipse.swt.custom.StyledText;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Control;
22 * An {@link IControlContentAdapter} for {@link org.eclipse.swt.custom.StyledText}. This is
23 * a convenience class for easily creating a {@link ContentProposalAdapter} for text fields.
27 public class StyledTextContentAdapter implements IControlContentAdapter, IControlContentAdapter2 {
32 * org.eclipse.jface.fieldassist.IControlContentAdapter#getControlContents(org.eclipse
33 * .swt.widgets.Control)
35 public String getControlContents(Control control) {
36 return ((StyledText)control).getText();
43 * org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse
44 * .swt.widgets.Control)
46 public int getCursorPosition(Control control) {
47 return ((StyledText)control).getCaretOffset();
54 * org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse
55 * .swt.widgets.Control)
57 public Rectangle getInsertionBounds(Control control) {
58 StyledText text= (StyledText)control;
59 Point caretOrigin= text.getLocationAtOffset(text.getCaretOffset());
60 return new Rectangle(caretOrigin.x + text.getClientArea().x, caretOrigin.y + text.getClientArea().y + 3, 1, text.getLineHeight());
67 * org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse
68 * .swt.widgets.Control, java.lang.String, int)
70 public void insertControlContents(Control control, String contents, int cursorPosition) {
71 StyledText text = ((StyledText)control);
72 cursorPosition = Math.min(cursorPosition, contents.length());
74 int caretOffset = text.getCaretOffset();
75 String currentText = text.getText();
76 int replacementOffset = findPrefixMatchOffset(currentText, caretOffset, contents);
78 // System.out.println("text: " + currentText);
79 // System.out.println("proposal to fill: " + contents);
80 // System.out.format("longest match of proposed contents found from text @ offset %d: \"%s[%s]%s\"%n",
82 // currentText.substring(0, replacementOffset),
83 // currentText.substring(replacementOffset, caretOffset),
84 // currentText.substring(caretOffset));
86 // The text between [replaceOffset, caretOffset) will be replaced with `contents`
87 text.setSelection(replacementOffset, caretOffset);
88 text.insert(contents);
89 text.setSelection(replacementOffset + contents.length());
93 * Find offset of longest prefix match of <code>match</code> in
94 * <code>text</code> ending at offset <code>endOffset</code>.
104 * res match! return endOffset - 3
107 * @param text the text from which to find the match
108 * @param endOffset endOffset until which to search for the longest match
109 * @param match the text to prefix-match
112 private static int findPrefixMatchOffset(String text, int endOffset, String match) {
113 for (int i = match.length(); i >= 0; --i) {
114 if (text.regionMatches(true, endOffset - i, match, 0, i))
115 return endOffset - i;
124 * org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse
125 * .swt.widgets.Control, java.lang.String, int)
127 public void setControlContents(Control control, String contents, int cursorPosition) {
128 ((StyledText)control).setText(contents);
129 ((StyledText)control).setCaretOffset(cursorPosition);
136 * org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse
137 * .swt.widgets.Control, int)
139 public void setCursorPosition(Control control, int index) {
140 ((StyledText)control).setCaretOffset(index);
147 * org.eclipse.jface.fieldassist.IControlContentAdapter2#getSelection(org.eclipse.swt
150 public Point getSelection(Control control) {
151 return ((StyledText)control).getSelection();
158 * org.eclipse.jface.fieldassist.IControlContentAdapter2#setSelection(org.eclipse.swt
159 * .widgets.Control, org.eclipse.swt.graphics.Point)
161 public void setSelection(Control control, Point range) {
162 ((StyledText)control).setSelection(range);