use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class GridCopyEnable method handleVerticalScroll.
void handleVerticalScroll(Event event) {
GridVisibleRange visibleR = gridTable.getVisibleRange();
GridItem[] items = visibleR.getItems();
boolean itemFlg = false;
for (GridItem item : items) {
if (focusItem == item) {
itemFlg = true;
}
}
boolean columnFlg = false;
GridColumn[] columns = visibleR.getColumns();
if (columns.length - 1 >= focusColIndex) {
columnFlg = true;
}
if (!itemFlg || !columnFlg) {
defaultCaret.setVisible(false);
return;
}
defaultCaret.setVisible(true);
GridColumn col = gridTable.getColumn(focusColIndex);
GridCellRenderer gcr = col.getCellRenderer();
int colIndex = gcr.getColumn();
if (gcr == null || !(gcr instanceof XGridCellRenderer) || !copyAbleColumnIndexs.contains(colIndex)) {
return;
}
XGridCellRenderer cellRender = (XGridCellRenderer) gcr;
Rectangle cellBounds = focusItem.getBounds(colIndex);
GC gc = new GC(Display.getDefault());
TextLayout layout = null;
try {
layout = cellRender.getTextLayout(gc, focusItem, colIndex, true, false);
if (layout == null) {
gc.dispose();
return;
}
Point point = layout.getLocation(caretOffset, false);
coordinateOffsetX = cellBounds.x + cellRender.leftMargin;
coordinateOffsetY = cellBounds.y + cellRender.topMargin + cellRender.textTopMargin;
defaultCaret.setLocation(point.x + coordinateOffsetX, point.y + coordinateOffsetY);
} finally {
if (layout != null) {
layout.dispose();
}
if (gc != null) {
gc.dispose();
}
}
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class AttributeTextCellRenderer method draw.
public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row, IColumn column, boolean drawFocus, boolean selected, boolean printing) {
super.draw(gc, jaretTable, cellStyle, drawingArea, row, column, drawFocus, selected, printing);
// Color bg = gc.getBackground();
// Color fg = gc.getForeground();
Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
Rectangle rect = applyInsets(drect);
String s = convertValue(row, column);
if (s != null && mapStyle != null && mapStyle.containsKey(row)) {
if (selected && !printing) {
Color color = ColorManager.getColorManager(Display.getCurrent()).getColor(new RGB(218, 218, 218));
gc.setBackground(color);
} else {
gc.setBackground(getBackgroundColor(cellStyle, printing));
}
if (textLayout == null) {
textLayout = new TextLayout(gc.getDevice());
jaretTable.getParent().addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
textLayout.dispose();
}
});
}
textLayout.setOrientation(jaretTable.getOrientation());
textLayout.setText(s);
textLayout.setFont(gc.getFont());
textLayout.setWidth(rect.width);
ArrayList<int[]> lstIndex = mapStyle.get(row);
if (lstIndex != null) {
for (int[] arrIndex : lstIndex) {
if (arrIndex != null && arrIndex.length == 2) {
textLayout.setStyle(style, arrIndex[0], arrIndex[1] - 1);
}
}
gc.fillRectangle(rect);
textLayout.draw(gc, rect.x, rect.y);
gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
}
}
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TextPainterWithPadding method getCellTextLayout.
private TextLayout getCellTextLayout(LayerCell cell) {
int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
TextLayout layout = new TextLayout(editor.getTable().getDisplay());
layout.setOrientation(orientation);
layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
layout.setFont(font);
layout.setAscent(ascent);
// 和 StyledTextEditor 同步
layout.setDescent(descent);
layout.setTabs(new int[] { tabWidth });
Rectangle rectangle = cell.getBounds();
int width = rectangle.width - leftPadding - rightPadding;
width -= 1;
if (wrapText && width > 0) {
layout.setWidth(width);
}
String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue()));
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "");
displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "");
}
layout.setText(displayText);
List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
for (InnerTagBean innerTagBean : innerTagBeans) {
String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
int start = displayText.indexOf(placeHolder);
if (start == -1) {
continue;
}
TextStyle style = new TextStyle();
Point rect = tagRender.calculateTagSize(innerTagBean);
style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
layout.setStyle(style, start, start + placeHolder.length() - 1);
}
return layout;
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TextPainterWithPadding method setFont.
public void setFont(Font font) {
TextLayout layout = new TextLayout(Display.getDefault());
try {
if (font != null) {
this.font = font;
Font boldFont = getFont(SWT.BOLD), italicFont = getFont(SWT.ITALIC), boldItalicFont = getFont(SWT.BOLD | SWT.ITALIC);
layout.setText(" ");
layout.setFont(font);
layout.setStyle(new TextStyle(font, null, null), 0, 0);
layout.setStyle(new TextStyle(boldFont, null, null), 1, 1);
layout.setStyle(new TextStyle(italicFont, null, null), 2, 2);
layout.setStyle(new TextStyle(boldItalicFont, null, null), 3, 3);
FontMetrics metrics = layout.getLineMetrics(0);
ascent = metrics.getAscent() + metrics.getLeading();
descent = metrics.getDescent();
boldFont.dispose();
italicFont.dispose();
boldItalicFont.dispose();
boldFont = italicFont = boldItalicFont = null;
}
layout.dispose();
layout = new TextLayout(Display.getDefault());
layout.setFont(this.font);
StringBuffer tabBuffer = new StringBuffer(tabSize);
for (int i = 0; i < tabSize; i++) {
tabBuffer.append(' ');
}
layout.setText(tabBuffer.toString());
tabWidth = layout.getBounds().width;
layout.dispose();
} finally {
if (layout != null && !layout.isDisposed()) {
layout.dispose();
}
}
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TextPainterWithPadding method getPreferredHeight.
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
if (innerTagFactory == null) {
innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
}
innerTagFactory.reset();
TextLayout layout = getCellTextLayout(cell);
int counts = layout.getLineCount();
int contentHeight = 0;
for (int i = 0; i < counts; i++) {
contentHeight += layout.getLineBounds(i).height;
}
layout.dispose();
contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING;
// 加上编辑模式下,StyledTextCellEditor的边框
contentHeight += 4;
contentHeight += topPadding;
contentHeight += bottomPadding;
return contentHeight;
}
Aggregations