blob: b9adb0ec8d96e79e1640480b10e29bcb94c62e63 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 KPIT Technologies.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jan Mauersberger- initial API and implementation
* Sascha Baumgart- initial API and implementation
*******************************************************************************/
package org.eclipse.opencert.userguidance.autocomplete;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.jface.fieldassist.IControlContentAdapter2;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
public class StyledTextContentAdapter implements IControlContentAdapter,
IControlContentAdapter2 {
@Override
public String getControlContents(Control control) {
return ((StyledText) control).getText();
}
@Override
public void setControlContents(Control control, String text,
int cursorPosition) {
((StyledText) control).setText(text);
((StyledText) control).setSelection(cursorPosition, cursorPosition);
}
@Override
public void insertControlContents(Control control, String text,
int cursorPosition) {
Point selection = ((StyledText) control).getSelection();
((StyledText) control).insert(text);
// Insert will leave the cursor at the end of the inserted text. If this
// is not what we wanted, reset the selection.
if (cursorPosition < text.length()) {
((StyledText) control).setSelection(selection.x + cursorPosition,
selection.x + cursorPosition);
}
}
@Override
public int getCursorPosition(Control control) {
return ((StyledText) control).getCaretOffset();
}
@Override
public Rectangle getInsertionBounds(Control control) {
StyledText text = (StyledText) control;
Point caretOrigin = text.getCaret().getLocation();
// We fudge the y pixels due to problems with getCaretLocation
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=52520
return new Rectangle(caretOrigin.x + text.getClientArea().x,
caretOrigin.y + text.getClientArea().y + 3, 1,
text.getLineHeight());
}
@Override
public void setCursorPosition(Control control, int position) {
((StyledText) control).setSelection(new Point(position, position));
}
@Override
public Point getSelection(Control control) {
return ((StyledText) control).getSelection();
}
@Override
public void setSelection(Control control, Point range) {
((StyledText) control).setSelection(range);
}
}