]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java
Fixed StyledtextContentAdapter to overwrite existing content properly
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / assist / StyledTextContentAdapter.java
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
7  *
8  * Contributors:
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;
12
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;
20
21 /**
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.
24  * 
25  * @since 3.5
26  */
27 public class StyledTextContentAdapter implements IControlContentAdapter, IControlContentAdapter2 {
28         /*
29          * (non-Javadoc)
30          * 
31          * @see
32          * org.eclipse.jface.fieldassist.IControlContentAdapter#getControlContents(org.eclipse
33          * .swt.widgets.Control)
34          */
35         public String getControlContents(Control control) {
36                 return ((StyledText)control).getText();
37         }
38
39         /*
40          * (non-Javadoc)
41          * 
42          * @see
43          * org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse
44          * .swt.widgets.Control)
45          */
46         public int getCursorPosition(Control control) {
47                 return ((StyledText)control).getCaretOffset();
48         }
49
50         /*
51          * (non-Javadoc)
52          * 
53          * @see
54          * org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse
55          * .swt.widgets.Control)
56          */
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());
61         }
62
63         /*
64          * (non-Javadoc)
65          * 
66          * @see
67          * org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse
68          * .swt.widgets.Control, java.lang.String, int)
69          */
70         public void insertControlContents(Control control, String contents, int cursorPosition) {
71                 StyledText text = ((StyledText)control);
72                 cursorPosition = Math.min(cursorPosition, contents.length());
73
74                 int caretOffset = text.getCaretOffset();
75                 String currentText = text.getText();
76                 int replacementOffset = findPrefixMatchOffset(currentText, caretOffset, contents);
77
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",
81 //                              replacementOffset,
82 //                              currentText.substring(0, replacementOffset),
83 //                              currentText.substring(replacementOffset, caretOffset),
84 //                              currentText.substring(caretOffset));
85
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());
90         }
91
92         /**
93          * Find offset of longest prefix match of <code>match</code> in
94          * <code>text</code> ending at offset <code>endOffset</code>.
95          * 
96          * Example:
97          * <pre>
98          * ...  res (=text)
99          * resource (=match)
100          *  resourc
101          *   resour
102          *    resou
103          *     reso
104          *      res match! return endOffset - 3
105          * </pre>
106          * 
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
110          * @return
111          */
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;
116                 }
117                 return 0;
118         }
119
120         /*
121          * (non-Javadoc)
122          * 
123          * @see
124          * org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse
125          * .swt.widgets.Control, java.lang.String, int)
126          */
127         public void setControlContents(Control control, String contents, int cursorPosition) {
128                 ((StyledText)control).setText(contents);
129                 ((StyledText)control).setCaretOffset(cursorPosition);
130         }
131
132         /*
133          * (non-Javadoc)
134          * 
135          * @see
136          * org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse
137          * .swt.widgets.Control, int)
138          */
139         public void setCursorPosition(Control control, int index) {
140                 ((StyledText)control).setCaretOffset(index);
141         }
142
143         /*
144          * (non-Javadoc)
145          * 
146          * @see
147          * org.eclipse.jface.fieldassist.IControlContentAdapter2#getSelection(org.eclipse.swt
148          * .widgets.Control)
149          */
150         public Point getSelection(Control control) {
151                 return ((StyledText)control).getSelection();
152         }
153
154         /*
155          * (non-Javadoc)
156          * 
157          * @see
158          * org.eclipse.jface.fieldassist.IControlContentAdapter2#setSelection(org.eclipse.swt
159          * .widgets.Control, org.eclipse.swt.graphics.Point)
160          */
161         public void setSelection(Control control, Point range) {
162                 ((StyledText)control).setSelection(range);
163         }
164
165 }