]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / assist / StyledTextContentAdapter.java
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
7  *\r
8  * Contributors:\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
12 \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
19 \r
20 /**\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
23  * \r
24  * @since 3.5\r
25  */\r
26 public class StyledTextContentAdapter implements IControlContentAdapter, IControlContentAdapter2 {\r
27         /*\r
28          * (non-Javadoc)\r
29          * \r
30          * @see\r
31          * org.eclipse.jface.fieldassist.IControlContentAdapter#getControlContents(org.eclipse\r
32          * .swt.widgets.Control)\r
33          */\r
34         public String getControlContents(Control control) {\r
35                 return ((StyledText)control).getText();\r
36         }\r
37 \r
38         /*\r
39          * (non-Javadoc)\r
40          * \r
41          * @see\r
42          * org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse\r
43          * .swt.widgets.Control)\r
44          */\r
45         public int getCursorPosition(Control control) {\r
46                 return ((StyledText)control).getCaretOffset();\r
47         }\r
48 \r
49         /*\r
50          * (non-Javadoc)\r
51          * \r
52          * @see\r
53          * org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse\r
54          * .swt.widgets.Control)\r
55          */\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
60         }\r
61 \r
62         /*\r
63          * (non-Javadoc)\r
64          * \r
65          * @see\r
66          * org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse\r
67          * .swt.widgets.Control, java.lang.String, int)\r
68          */\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
74                 \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
78             length--;\r
79                 \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
92         }\r
93 \r
94         /*\r
95          * (non-Javadoc)\r
96          * \r
97          * @see\r
98          * org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse\r
99          * .swt.widgets.Control, java.lang.String, int)\r
100          */\r
101         public void setControlContents(Control control, String contents, int cursorPosition) {\r
102                 ((StyledText)control).setText(contents);\r
103                 ((StyledText)control).setCaretOffset(cursorPosition);\r
104         }\r
105 \r
106         /*\r
107          * (non-Javadoc)\r
108          * \r
109          * @see\r
110          * org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse\r
111          * .swt.widgets.Control, int)\r
112          */\r
113         public void setCursorPosition(Control control, int index) {\r
114                 ((StyledText)control).setCaretOffset(index);\r
115         }\r
116 \r
117         /*\r
118          * (non-Javadoc)\r
119          * \r
120          * @see\r
121          * org.eclipse.jface.fieldassist.IControlContentAdapter2#getSelection(org.eclipse.swt\r
122          * .widgets.Control)\r
123          */\r
124         public Point getSelection(Control control) {\r
125                 return ((StyledText)control).getSelection();\r
126         }\r
127 \r
128         /*\r
129          * (non-Javadoc)\r
130          * \r
131          * @see\r
132          * org.eclipse.jface.fieldassist.IControlContentAdapter2#setSelection(org.eclipse.swt\r
133          * .widgets.Control, org.eclipse.swt.graphics.Point)\r
134          */\r
135         public void setSelection(Control control, Point range) {\r
136                 ((StyledText)control).setSelection(range);\r
137         }\r
138 \r
139 }\r