Search in sources :

Example 1 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class TBSearchCellRenderer method paint.

/**
	 * (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.grid.IRenderer#paint(org.eclipse.swt.graphics.GC, java.lang.Object)
	 */
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_LIST_SELECTION_TEXT));
    } 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()));
    }
    int width = getBounds().width - x - rightMargin;
    int height = getBounds().height - bottomMargin;
    int y = getBounds().y + textTopMargin + topMargin;
    if (textLayout == null) {
        textLayout = new TextLayout(gc.getDevice());
        item.getParent().addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                textLayout.dispose();
            }
        });
    }
    textLayout.setOrientation(item.getParent().getOrientation());
    textLayout.setFont(gc.getFont());
    String itemText = item.getText(getColumn());
    textLayout.setText(item.getText(getColumn()));
    textLayout.setAlignment(getAlignment());
    textLayout.setWidth(width < 1 ? 1 : width);
    if (styleColumn != -1 && getColumn() == styleColumn) {
        if (style == null) {
            final Font font = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont().getFontData()[0].getHeight(), SWT.BOLD);
            style = new TextStyle(font, null, null);
            item.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    font.dispose();
                }
            });
        }
        if (strText != null) {
            if (blnIsApplyRegular) {
                Pattern pattern = null;
                if (blnIsCaseSensitive) {
                    pattern = Pattern.compile(strText);
                } else {
                    pattern = Pattern.compile(strText, Pattern.CASE_INSENSITIVE);
                }
                Matcher matcher = pattern.matcher(itemText);
                while (matcher.find()) {
                    textLayout.setStyle(style, matcher.start(), matcher.end() - 1);
                }
            } else {
                int index;
                if (blnIsCaseSensitive) {
                    index = itemText.indexOf(strText);
                } else {
                    index = itemText.toUpperCase().indexOf(strText.toUpperCase());
                }
                if (index != -1) {
                    textLayout.setStyle(null, 0, itemText.length() - 1);
                    for (int i = 1; i < strText.length(); i++) {
                        int j = TextUtil.indexOf(itemText, strText, i, blnIsCaseSensitive);
                        if (j != -1) {
                            textLayout.setStyle(style, j, j + strText.length() - 1);
                        } else {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (item.getParent().isAutoHeight()) {
        int textHeight = topMargin + textTopMargin;
        for (int cnt = 0; cnt < textLayout.getLineCount(); cnt++) textHeight += textLayout.getLineBounds(cnt).height;
        textHeight += textBottomMargin + bottomMargin;
        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);
        }
    }
    y += getVerticalAlignmentAdjustment(textLayout.getBounds().height, height);
    textLayout.draw(gc, getBounds().x + x, y);
    if (item.getParent().getLinesVisible()) {
        if (isCellSelected()) {
            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);
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle)

Example 2 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class StyleTextCellRenderer 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);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);
    String s = convertValue(row, column);
    if (s != null && strStyleText != null) {
        int index = -1;
        if (blnIsCaseSensitive) {
            index = s.toUpperCase().indexOf(strStyleText.toUpperCase());
        } else {
            index = s.indexOf(strStyleText);
        }
        if (index != -1) {
            if (textLayout == null) {
                textLayout = new TextLayout(gc.getDevice());
                jaretTable.getParent().addDisposeListener(new DisposeListener() {

                    public void widgetDisposed(DisposeEvent e) {
                        textLayout.dispose();
                    }
                });
            }
            textLayout.setText(s);
            textLayout.setFont(gc.getFont());
            textLayout.setWidth(rect.width);
            if (style == null) {
                final Color color = new Color(gc.getDevice(), 150, 100, 100);
                final Font font = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont().getFontData()[0].getHeight(), SWT.ITALIC);
                style = new TextStyle(font, color, null);
                jaretTable.getParent().addDisposeListener(new DisposeListener() {

                    public void widgetDisposed(DisposeEvent e) {
                        color.dispose();
                        font.dispose();
                    }
                });
            }
            for (int i = 1; i < strStyleText.length(); i++) {
                int j = indexOf(s, strStyleText, i, blnIsCaseSensitive);
                if (j != -1) {
                    textLayout.setStyle(style, j, j + strStyleText.length() - 1);
                } else {
                    break;
                }
            }
            gc.fillRectangle(rect);
            textLayout.draw(gc, rect.x, rect.y);
            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) TextStyle(org.eclipse.swt.graphics.TextStyle) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 3 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class ConcordanceSearchDialog method createResultsStyle.

private TextStyle createResultsStyle() {
    background = new Color(Display.getCurrent(), 0x19, 0x19, 0x70);
    foreground = new Color(Display.getCurrent(), 0xff, 0xff, 0xff);
    FontData fontData = JFaceResources.getDefaultFont().getFontData()[0];
    fontData.setStyle(fontData.getStyle());
    rsFont = new Font(Display.getDefault(), fontData);
    TextStyle style = new TextStyle(rsFont, foreground, background);
    return style;
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) Color(org.eclipse.swt.graphics.Color) FontData(org.eclipse.swt.graphics.FontData) Font(org.eclipse.swt.graphics.Font)

Example 4 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class TextPainterWithPadding method appendNonprintingStyle.

private void appendNonprintingStyle(TextLayout layout) {
    TextStyle style = new TextStyle(font, GUIHelper.getColor(new RGB(100, 100, 100)), null);
    String s = layout.getText();
    Matcher matcher = Constants.NONPRINTING_PATTERN.matcher(s);
    while (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();
        // style.metrics = new GlyphMetrics(10, 0, 1);
        layout.setStyle(style, start, end - 1);
    }
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) Matcher(java.util.regex.Matcher) RGB(org.eclipse.swt.graphics.RGB) Point(org.eclipse.swt.graphics.Point)

Example 5 with TextStyle

use of org.eclipse.swt.graphics.TextStyle 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;
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) Rectangle(org.eclipse.swt.graphics.Rectangle) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Aggregations

TextStyle (org.eclipse.swt.graphics.TextStyle)33 Point (org.eclipse.swt.graphics.Point)14 TextLayout (org.eclipse.swt.graphics.TextLayout)14 Color (org.eclipse.swt.graphics.Color)9 Font (org.eclipse.swt.graphics.Font)9 Rectangle (org.eclipse.swt.graphics.Rectangle)8 Matcher (java.util.regex.Matcher)6 StyleRange (org.eclipse.swt.custom.StyleRange)6 ArrayList (java.util.ArrayList)5 IToken (org.eclipse.jface.text.rules.IToken)4 Styler (org.eclipse.jface.viewers.StyledString.Styler)4 RGB (org.eclipse.swt.graphics.RGB)4 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)3 List (java.util.List)2 Point (org.eclipse.draw2d.geometry.Point)2 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 ITokenScanner (org.eclipse.jface.text.rules.ITokenScanner)2 StyledString (org.eclipse.jface.viewers.StyledString)2 GridItem (org.eclipse.nebula.widgets.grid.GridItem)2