use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TypeColunmCellRenderer method paint.
/**
* {@inheritDoc}
*/
public void paint(GC gc, Object value) {
GridItem item = (GridItem) value;
gc.setFont(item.getFont(getColumn()));
boolean drawBackground = true;
boolean drawAsSelected = isSelected();
if (isCellSelected()) {
drawAsSelected = true;
}
gc.setForeground(item.getForeground(getColumn()));
if (drawAsSelected) {
// if (backColor == null || backColor.isDisposed()) {
gc.setBackground((Color) item.getParent().getData("selectedBgColor"));
// } else {
// gc.setBackground(backColor);
// }
} else {
// if (item.getParent().isEnabled()) {
// if (backColor == null || backColor.isDisposed()) {
// drawBackground = false;
// } else {
// gc.setBackground(backColor);
// }
// } else {
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
// }
}
if (drawBackground) {
gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
}
int x = leftMargin;
Image image = (Image) item.getData("typeImage");
if (image != null) {
int y = getBounds().y;
y += (getBounds().height - image.getBounds().height) / 2;
gc.drawImage(image, getBounds().x + x, y);
x += image.getBounds().width + 3;
}
int height = getBounds().height - bottomMargin;
if (textLayout == null) {
textLayout = new TextLayout(gc.getDevice());
item.getParent().addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
textLayout.dispose();
}
});
}
textLayout.setFont(gc.getFont());
String quality = item.getText(getColumn());
textLayout.setText(quality);
textLayout.setAlignment(SWT.LEFT);
int width = getBounds().width - x - rightMargin;
textLayout.setWidth(width < 1 ? 1 : width);
int y = getBounds().y + textTopMargin + topMargin;
y += getVerticalAlignmentAdjustment(textLayout.getBounds().height, height);
// textLayout.draw(gc, getBounds().x + x, y);
String type = (String) item.getData("matchType");
if (type.equals("TM")) {
Color backColor = TmUtils.getMatchTypeColor(type, quality);
if (backColor != null && !backColor.isDisposed()) {
gc.setBackground(backColor);
}
gc.drawText(item.getText(getColumn()), getBounds().x + x, y);
}
if (item.getParent().getLinesVisible()) {
if (isCellSelected()) {
// XXX: should be user definable?
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
} else {
gc.setForeground(item.getParent().getLineColor());
}
gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
}
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class CellRenderer method computeSize.
/**
* {@inheritDoc}
*/
public Point computeSize(GC gc, int wHint, int hHint, Object value) {
GridItem item = (GridItem) value;
gc.setFont(item.getFont(getColumn()));
int x = 0;
x += leftMargin;
if (isTree()) {
x += getToggleIndent(item);
x += toggleRenderer.getBounds().width + insideMargin;
}
if (isCheck()) {
x += checkRenderer.getBounds().width + insideMargin;
}
int y = 0;
Image image = item.getImage(getColumn());
if (image != null) {
y = topMargin + image.getBounds().height + bottomMargin;
x += image.getBounds().width + insideMargin;
}
// MOPR-DND
// MOPR: replaced this code (to get correct preferred height for cells
// in word-wrap columns)
//
// x += gc.stringExtent(item.getText(column)).x + rightMargin;
//
// y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() +
// bottomMargin);
//
// with this code:
int textHeight = 0;
if (!isWordWrap()) {
x += gc.textExtent(item.getText(getColumn())).x + rightMargin;
textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
} else {
int plainTextWidth;
if (wHint == SWT.DEFAULT)
plainTextWidth = getBounds().width - x - rightMargin;
else
plainTextWidth = wHint - x - rightMargin;
TextLayout currTextLayout = new TextLayout(gc.getDevice());
currTextLayout.setFont(gc.getFont());
currTextLayout.setText(item.getText(getColumn()));
currTextLayout.setAlignment(getAlignment());
currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);
x += plainTextWidth + rightMargin;
textHeight += topMargin + textTopMargin;
for (int cnt = 0; cnt < currTextLayout.getLineCount(); cnt++) textHeight += currTextLayout.getLineBounds(cnt).height;
textHeight += textBottomMargin + bottomMargin;
currTextLayout.dispose();
}
y = Math.max(y, textHeight);
return new Point(x, y);
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class CellRenderer method paint.
/**
* {@inheritDoc}
*/
public void paint(GC gc, Object value) {
GridItem item = (GridItem) value;
gc.setFont(item.getFont(getColumn()));
boolean drawAsSelected = isSelected();
boolean drawBackground = true;
if (isCellSelected()) {
// (!isCellFocus());
drawAsSelected = true;
}
if (drawAsSelected) {
// gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
gc.setBackground((Color) item.getParent().getData("selectedBgColor"));
} else {
if (item.getParent().isEnabled()) {
Color back = item.getBackground(getColumn());
if (back != null) {
gc.setBackground(back);
} else {
drawBackground = false;
}
} else {
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
}
// gc.setForeground(item.getForeground(getColumn()));
}
if (drawBackground)
gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
int x = leftMargin;
if (isTree()) {
boolean renderBranches = item.getParent().getTreeLinesVisible();
if (renderBranches) {
branchRenderer.setBranches(getBranches(item));
branchRenderer.setIndent(treeIndent);
branchRenderer.setBounds(getBounds().x + x, getBounds().y, getToggleIndent(item), // Take into account border
getBounds().height + 1);
}
x += getToggleIndent(item);
toggleRenderer.setExpanded(item.isExpanded());
toggleRenderer.setHover(getHoverDetail().equals("toggle"));
toggleRenderer.setLocation(getBounds().x + x, (getBounds().height - toggleRenderer.getBounds().height) / 2 + getBounds().y);
if (item.hasChildren())
toggleRenderer.paint(gc, null);
if (renderBranches) {
branchRenderer.setToggleBounds(toggleRenderer.getBounds());
branchRenderer.paint(gc, null);
}
x += toggleRenderer.getBounds().width + insideMargin;
}
if (isCheck()) {
checkRenderer.setChecked(item.getChecked(getColumn()));
checkRenderer.setGrayed(item.getGrayed(getColumn()));
if (!item.getParent().isEnabled()) {
checkRenderer.setGrayed(true);
}
checkRenderer.setHover(getHoverDetail().equals("check"));
if (isCenteredCheckBoxOnly(item)) {
// Special logic if this column only has a checkbox and is centered
checkRenderer.setBounds(getBounds().x + ((getBounds().width - checkRenderer.getBounds().width) / 2), (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
} else {
checkRenderer.setBounds(getBounds().x + x, (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
x += checkRenderer.getBounds().width + insideMargin;
}
checkRenderer.paint(gc, null);
}
Image image = item.getImage(getColumn());
if (image != null) {
int y = getBounds().y;
y += (getBounds().height - image.getBounds().height) / 2;
gc.drawImage(image, getBounds().x + x, y);
x += image.getBounds().width + insideMargin;
}
if (drawAsSelected) {
// gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
} else {
gc.setForeground(item.getForeground(getColumn()));
}
if (!isWordWrap()) {
int width = getBounds().width - x - rightMargin;
String text = TextUtils.getShortString(gc, item.getText(getColumn()), width);
if (getAlignment() == SWT.RIGHT) {
int len = gc.stringExtent(text).x;
if (len < width) {
x += width - len;
}
} else if (getAlignment() == SWT.CENTER) {
int len = gc.stringExtent(text).x;
if (len < width) {
x += (width - len) / 2;
}
}
gc.drawString(text, getBounds().x + x, getBounds().y + textTopMargin + topMargin, true);
} else {
TextLayout layout = getTextLayout(gc, item, getColumn(), true, true);
try {
int y = getBounds().y + textTopMargin + topMargin;
y += getVerticalAlignmentAdjustment(layout.getBounds().height, getBounds().height);
if (item.getParent().isAutoHeight()) {
int textHeight = topMargin + textTopMargin;
// fix Bug #3116 库匹配面板--显示的匹配有截断 by Jason
// for (int cnt = 0; cnt < layout.getLineCount(); cnt++)
// textHeight += layout.getLineBounds(cnt).height;
textHeight += layout.getBounds().height;
Object obj = item.getData("itemHeight");
if (getColumn() != item.getParent().getColumnCount() - 1) {
if (obj != null) {
int heigth = (Integer) obj;
textHeight = Math.max(textHeight, heigth);
}
item.setData("itemHeight", textHeight);
} else {
int heigth = (Integer) obj;
textHeight = Math.max(textHeight, heigth);
if (textHeight != item.getHeight()) {
item.setHeight(textHeight);
}
item.setData("itemHeight", null);
}
}
x = getBounds().x + leftMargin;
if (copyEnable != null) {
// Point selection = copyEnable.getSelectionRange(getColumn(), getRow() - 1 );
Point selection = copyEnable.getSelectionRange(getColumn(), item);
if (selection == null || selection.x == selection.y) {
layout.draw(gc, x, y);
} else {
int start = Math.max(0, selection.x);
int end = Math.min(layout.getText().length(), selection.y);
layout.draw(gc, x, y, start, end - 1, getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT), getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
attachInnertTagStyle(gc, layout, true);
}
} else {
layout.draw(gc, x, y);
}
} finally {
layout.dispose();
}
}
if (item.getParent().getLinesVisible()) {
if (isCellSelected()) {
// XXX: should be user definable?
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
} else {
gc.setForeground(item.getParent().getLineColor());
}
gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
}
if (isCellFocus()) {
Rectangle focusRect = new Rectangle(getBounds().x, getBounds().y, getBounds().width - 1, getBounds().height);
// gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
gc.drawRectangle(focusRect);
if (isFocus()) {
focusRect.x++;
focusRect.width -= 2;
focusRect.y++;
focusRect.height -= 2;
gc.drawRectangle(focusRect);
}
}
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TargetColunmCellRenderer method computeSize.
/**
* {@inheritDoc}
*/
public Point computeSize(GC gc, int wHint, int hHint, Object value) {
GridItem item = (GridItem) value;
gc.setFont(item.getFont(getColumn()));
int x = 0;
x += leftMargin;
int y = 0;
Image image = item.getImage(getColumn());
if (image != null) {
y = topMargin + image.getBounds().height + bottomMargin;
x += image.getBounds().width + 3;
}
// MOPR-DND
// MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns)
//
// x += gc.stringExtent(item.getText(column)).x + rightMargin;
//
// y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin);
//
// with this code:
int textHeight = 0;
if (!isWordWrap()) {
x += gc.textExtent(item.getText(getColumn())).x + rightMargin;
textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin;
} else {
int plainTextWidth;
if (wHint == SWT.DEFAULT)
plainTextWidth = getBounds().width - x - rightMargin;
else
plainTextWidth = wHint - x - rightMargin;
TextLayout currTextLayout = new TextLayout(gc.getDevice());
currTextLayout.setFont(gc.getFont());
currTextLayout.setText(item.getText(getColumn()));
currTextLayout.setAlignment(getAlignment());
currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth);
x += plainTextWidth + rightMargin;
textHeight += topMargin + textTopMargin;
for (int cnt = 0; cnt < currTextLayout.getLineCount(); cnt++) textHeight += currTextLayout.getLineBounds(cnt).height;
textHeight += textBottomMargin + bottomMargin;
currTextLayout.dispose();
}
y = Math.max(y, textHeight);
return new Point(x, y);
}
use of org.eclipse.swt.graphics.TextLayout in project translationstudio8 by heartsome.
the class TextPainterWithPadding method paintCell.
/**
* (non-Javadoc)
* @see net.sourceforge.nattable.painter.cell.BackgroundPainter#paintCell(net.sourceforge.nattable.layer.cell.LayerCell,
* org.eclipse.swt.graphics.GC, org.eclipse.swt.graphics.Rectangle,
* net.sourceforge.nattable.config.IConfigRegistry)
*/
@Override
public void paintCell(LayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
super.paintCell(cell, gc, rectangle, configRegistry);
IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
setupGCFromConfig(gc, cellStyle);
if (innerTagFactory == null) {
innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
}
innerTagFactory.reset();
int rowIndex = cell.getLayer().getRowIndexByPosition(cell.getRowPosition());
int columnIndex = cell.getLayer().getColumnIndexByPosition(cell.getColumnPosition());
if (!editor.isHorizontalLayout()) {
// 垂直
if (rowIndex % 2 != 0) {
LayerCell srcCell = cell.getLayer().getCellByPosition(cell.getColumnPosition(), cell.getRowPosition() - 1);
if (srcCell != null) {
String sourceVal = (String) srcCell.getDataValue();
innerTagFactory.parseInnerTag(sourceVal);
}
}
} else {
// 水平
if (columnIndex == editor.getTgtColumnIndex()) {
LayerCell srcCell = cell.getLayer().getCellByPosition(1, cell.getRowPosition());
if (srcCell != null) {
String sourceVal = (String) srcCell.getDataValue();
innerTagFactory.parseInnerTag(sourceVal);
}
}
}
TextLayout layout = getCellTextLayout(cell);
int tempIndx = rowIndex;
if (!editor.isHorizontalLayout()) {
tempIndx = tempIndx / 2;
}
if (tempIndx == editor.getSelectedRows()[0] && (editor.isHorizontalLayout() ? columnIndex == editor.getSrcColumnIndex() : rowIndex % 2 == 0)) {
List<String> terms = editor.getTermsCache().get(tempIndx);
if (terms != null && terms.size() > 0) {
List<StyleRange> ranges = new ArrayList<StyleRange>();
TextStyle style = new TextStyle(getFont(), null, ColorConfigBean.getInstance().getHighlightedTermColor());
char[] source = layout.getText().toCharArray();
for (String term : terms) {
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
term = term.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
term = term.replaceAll("\\t", Constants.TAB_CHARACTER + "");
term = term.replaceAll(" ", Constants.SPACE_CHARACTER + "");
}
ranges.addAll(calculateTermsStyleRange(source, term.toCharArray(), style));
}
for (StyleRange range : ranges) {
layout.setStyle(range, range.start, range.start + range.length - 1);
}
}
}
try {
String displayText = layout.getText();
Rectangle bounds = cell.getBounds();
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
appendNonprintingStyle(layout);
}
layout.draw(gc, bounds.x + leftPadding, bounds.y + topPadding);
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;
}
Point p = layout.getLocation(start, false);
int x = bounds.x + p.x + leftPadding;
x += SEGMENT_LINE_SPACING;
Point tagSize = tagRender.calculateTagSize(innerTagBean);
int lineIdx = layout.getLineIndex(start);
Rectangle r = layout.getLineBounds(lineIdx);
// -
int y = bounds.y + p.y + topPadding + r.height / 2 - tagSize.y / 2;
// layout.getLineMetrics(0).getDescent();
// if (y + r.height > tagSize.y) {
// FontMetrics fm = layout.getLineMetrics(lineIdx);
// y = y + r.height - tagSize.y - fm.getDescent();
// }
tagRender.draw(gc, innerTagBean, x, y);
}
} finally {
layout.dispose();
}
}
Aggregations