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