Search in sources :

Example 1 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project dbeaver by serge-rider.

the class HexEditControl method doModifyKeyPressed.

void doModifyKeyPressed(KeyEvent event) {
    char aChar = event.character;
    if (aChar == '\0' || aChar == '\b' || aChar == '' || event.stateMask == SWT.CTRL || event.widget == hexText && ((event.stateMask & SWT.MODIFIER_MASK) != 0 || aChar < '0' || aChar > '9' && aChar < 'A' || aChar > 'F' && aChar < 'a' || aChar > 'f')) {
        return;
    }
    boolean origInserting = isInserting;
    if (getCaretPos() == content.length() && !isInserting) {
        //            ensureCaretIsVisible();
        //            redrawTextAreas(false);
        //            return;
        isInserting = true;
    }
    try {
        handleSelectedPreModify();
        try {
            if (isInserting) {
                if (event.widget == previewText) {
                    content.insert((byte) aChar, getCaretPos());
                } else if (upANibble == 0) {
                    content.insert((byte) (hexToNibble[aChar - '0'] << 4), getCaretPos());
                } else {
                    content.overwrite(hexToNibble[aChar - '0'], 4, 4, getCaretPos());
                }
            } else {
                if (event.widget == previewText) {
                    content.overwrite((byte) aChar, getCaretPos());
                } else {
                    content.overwrite(hexToNibble[aChar - '0'], upANibble * 4, 4, getCaretPos());
                }
                content.get(ByteBuffer.wrap(tmpRawBuffer, 0, 1), null, getCaretPos());
                int offset = (int) (getCaretPos() - textAreasStart);
                hexText.replaceTextRange(offset * 3, 2, GeneralUtils.byteToHex[tmpRawBuffer[0] & 0x0ff]);
                hexText.setStyleRange(new StyleRange(offset * 3, 2, COLOR_BLUE, null));
                previewText.replaceTextRange(offset, 1, Character.toString(byteToChar[tmpRawBuffer[0] & 0x0ff]));
                previewText.setStyleRange(new StyleRange(offset, 1, COLOR_BLUE, null));
            }
        } catch (IOException e) {
            log.warn(e);
        }
        startPosition = endPosition = incrementPosWithinLimits(getCaretPos(), event.widget == hexText);
        Runnable delayed = new Runnable() {

            @Override
            public void run() {
                ensureCaretIsVisible();
                redrawTextAreas(false);
                if (isInserting) {
                    updateScrollBar();
                    redrawTextAreas(true);
                }
                refreshSelections();
                runnableEnd();
            }
        };
        runnableAdd(delayed);
        notifyListeners(SWT.Modify, null);
        notifyLongSelectionListeners();
    } finally {
        isInserting = origInserting;
    }
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) IOException(java.io.IOException)

Example 2 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project dbeaver by serge-rider.

the class HexEditControl method redrawTextAreas.

void redrawTextAreas(int mode, StringBuilder newText, StringBuilder resultHex, StringBuilder resultChar, List<StyleRange> viewRanges) {
    hexText.getCaret().setVisible(false);
    previewText.getCaret().setVisible(false);
    if (mode == SET_TEXT) {
        linesText.getContent().setText(newText.toString());
        hexText.getContent().setText(resultHex.toString());
        previewText.getContent().setText(resultChar.toString());
        previousLine = -1;
    } else {
        boolean forward = mode == SHIFT_FORWARD;
        linesText.setRedraw(false);
        hexText.setRedraw(false);
        previewText.setRedraw(false);
        ((DisplayedContent) linesText.getContent()).shiftLines(newText.toString(), forward);
        ((DisplayedContent) hexText.getContent()).shiftLines(resultHex.toString(), forward);
        ((DisplayedContent) previewText.getContent()).shiftLines(resultChar.toString(), forward);
        linesText.setRedraw(true);
        hexText.setRedraw(true);
        previewText.setRedraw(true);
        if (previousLine >= 0 && previousLine < numberOfLines)
            previousLine += newText.length() / charsForAddress * (forward ? 1 : -1);
        if (previousLine < -1 || previousLine >= numberOfLines)
            previousLine = -1;
    }
    if (viewRanges != null) {
        for (StyleRange styleRange : viewRanges) {
            previewText.setStyleRange(styleRange);
            styleRange = (StyleRange) styleRange.clone();
            styleRange.start *= 3;
            styleRange.length *= 3;
            hexText.setStyleRange(styleRange);
        }
    }
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange)

Example 3 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project translationstudio8 by heartsome.

the class TMXValidatorDialog method cleanCharacters.

private void cleanCharacters() {
    FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
    //$NON-NLS-1$ //$NON-NLS-2$
    String[] extensions = { "*.tmx", "*.*" };
    fd.setFilterExtensions(extensions);
    String[] names = { Messages.getString("dialog.TMXValidatorDialog.names1"), Messages.getString("dialog.TMXValidatorDialog.names2") };
    fd.setFilterNames(names);
    String name = fd.open();
    if (name != null) {
        red = Display.getDefault().getSystemColor(SWT.COLOR_RED);
        styledText.setText("");
        styledText.append(Messages.getString("dialog.TMXValidatorDialog.styledText1"));
        getShell().setCursor(cursorWait);
        try {
        } catch (Exception e) {
            LOGGER.error("", e);
            String errorTip = e.getMessage();
            if (errorTip == null) {
                errorTip = MessageFormat.format(Messages.getString("dialog.TMXValidatorDialog.msg1"), name);
            }
            StyleRange range = new StyleRange(styledText.getText().length(), errorTip.length(), red, null);
            styledText.append(errorTip);
            styledText.setStyleRange(range);
        }
        styledText.append(Messages.getString("dialog.TMXValidatorDialog.styledText2"));
        getShell().setCursor(cursorArrow);
    }
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 4 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project translationstudio8 by heartsome.

the class SourceColunmCellRenderer 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) {
        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));
        }
    }
    if (drawBackground) {
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    }
    String text = segmentViewer.getTextWidget().getText();
    // 创建 TextLayout
    TextLayout layout = getTextLayout(gc, item, getColumn(), false, false);
    String displayStr = layout.getText();
    // 附加内部标记样式前,先和源文比较,不一样的地方着色实现
    if (!tuSrcText.equals(displayStr)) {
        // TextStyle style = new TextStyle(layout.getFont(), colorConfigBean.getSrcDiffFgColor(),
        // colorConfigBean.getSrcDiffBgColor());
        // List<Position> diff = Comparator.Compare(tuSrcText, displayStr);
        // for (Iterator<Position> iterator = diff.iterator(); iterator.hasNext();) {
        // Position position = iterator.next();
        // layout.setStyle(style, position.offset, position.length);
        // }
        TextStyle diffStyle = new TextStyle(layout.getFont(), null, diffColor);
        // Increase or
        TextStyle InOrDecreaseStyle = new TextStyle(layout.getFont(), null, inOrDecreaseColor);
        // decrease
        TextStyle positionStyle = new TextStyle(layout.getFont(), null, positionColor);
        TextDiffMatcher dmp = new TextDiffMatcher();
        LinkedList<Diff> diffs = dmp.diff_main(text, displayStr);
        dmp.diff_cleanupEfficiency(diffs);
        boolean pFlg = positionDiff(diffs, text, displayStr);
        if (pFlg) {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE && drawAsSelected) {
                    String delText = f.text;
                    Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                    TextStyle style = positionStyle;
                    boolean flg = m.find();
                    if (flg && delText.length() != 1) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        int mark = 0;
                        List<StyleRange> rangeList = new ArrayList<StyleRange>();
                        do {
                            int off = m.start();
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = off - mark;
                            mark = m.end();
                            rangeList.add(range);
                        } while (m.find());
                        if (mark < delText.length()) {
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = delText.length() - mark;
                            rangeList.add(range);
                        }
                        for (StyleRange range : rangeList) {
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    } else if (!flg) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        StyleRange range = new StyleRange(style);
                        range.start = start;
                        range.length = delText.length();
                        segmentViewer.getTextWidget().setStyleRange(range);
                    }
                } else if (f.operation == Operation.INSERT) {
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(positionStyle, start, start + insText.length() - 1);
                }
            }
        } else {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE) {
                    String delText = f.text;
                    boolean isDiff = false;
                    if (i + 1 < diffs.size() && diffs.get(i + 1).operation == Operation.INSERT) {
                        // 库和当前句子存在不一致部分
                        i += 1;
                        // 处理库
                        f = diffs.get(i);
                        String insText = f.text;
                        int start = displayStr.indexOf(insText, insTextStart);
                        layout.setStyle(diffStyle, start, start + insText.length() - 1);
                        insTextStart = start + insText.length();
                        isDiff = true;
                    }
                    // 处理当前句子
                    if (drawAsSelected) {
                        Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                        TextStyle style = isDiff ? diffStyle : InOrDecreaseStyle;
                        boolean flg = m.find();
                        if (flg && delText.length() != 1) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            int mark = 0;
                            List<StyleRange> rangeList = new ArrayList<StyleRange>();
                            do {
                                int off = m.start();
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = off - mark;
                                mark = m.end();
                                rangeList.add(range);
                            } while (m.find());
                            if (mark < delText.length()) {
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = delText.length() - mark;
                                rangeList.add(range);
                            }
                            for (StyleRange range : rangeList) {
                                segmentViewer.getTextWidget().setStyleRange(range);
                            }
                        } else if (!flg) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            StyleRange range = new StyleRange(style);
                            range.start = start;
                            range.length = delText.length();
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    }
                } else if (f.operation == Operation.INSERT) {
                    // 库多出来的
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(InOrDecreaseStyle, start, start + insText.length() - 1);
                }
            }
        }
    }
    // 添加标记样式,并创建标记
    attachInnertTagStyle(gc, layout, false);
    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;
            textHeight += textBottomMargin + bottomMargin;
            item.setData("itemHeight", textHeight);
        }
        Point selection = copyEnable.getSelectionRange(getColumn(), item);
        if (selection == null || selection.x == selection.y) {
            layout.draw(gc, getBounds().x + leftMargin, y);
        } else {
            int x = getBounds().x + leftMargin;
            int start = Math.max(0, selection.x);
            int end = Math.min(displayStr.length(), selection.y);
            layout.draw(gc, x, y, start, end - 1, getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT), getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        }
        drawInnerTag(gc, layout);
        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);
        }
    } finally {
        if (layout != null) {
            layout.dispose();
        }
    }
}
Also used : Diff(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff) Matcher(java.util.regex.Matcher) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher)

Example 5 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project translationstudio8 by heartsome.

the class Comparator method Compare.

/**
	 * 用于比较的参照文本(不含内部标记)
	 * @param referenceText
	 * @param text
	 *            ;
	 */
public static void Compare(String referenceText, StyledText text) {
    ColorConfigBean colorCfgBean = ColorConfigBean.getInstance();
    final Color differencefg = colorCfgBean.getSrcDiffFgColor();
    final Color differencebg = colorCfgBean.getSrcDiffBgColor();
    Position[] tagRanges = InnerTagUtil.getStyledTagRanges(text.getText());
    TokenComparator left = new TokenComparator(InnerTagUtil.getDisplayValueWithoutTags(text.getText()));
    TokenComparator right = new TokenComparator(referenceText);
    ArrayList<StyleRange> styleRanges = new ArrayList<StyleRange>();
    RangeDifference[] e = RangeDifferencer.findRanges(left, right);
    for (int i = 0; i < e.length; i++) {
        RangeDifference es = e[i];
        int leftStart = es.leftStart();
        int leftEnd = es.leftEnd();
        int lStart = left.getTokenStart(leftStart);
        int lEnd = left.getTokenStart(leftEnd);
        if (es.kind() == RangeDifference.CHANGE) {
            int start = lStart;
            int end = lEnd;
            if (tagRanges.length > 0) {
                for (Position tagRange : tagRanges) {
                    int tagStart = tagRange.getOffset();
                    int tagEnd = tagRange.getOffset() + tagRange.getLength();
                    if (tagStart <= start) {
                        start += tagRange.getLength();
                        end += tagRange.getLength();
                    } else if (start < tagStart && tagStart < end) {
                        StyleRange range = new StyleRange(start, tagStart - start, differencefg, differencebg);
                        styleRanges.add(range);
                        start = tagEnd;
                        end += tagRange.getLength();
                    } else {
                        break;
                    }
                }
            }
            StyleRange range = new StyleRange(start, end - start, differencefg, differencebg);
            styleRanges.add(range);
        }
    }
    for (int i = 0; i < styleRanges.size(); i++) {
        text.setStyleRange(styleRanges.get(i));
        text.addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                differencefg.dispose();
                differencebg.dispose();
            }
        });
    }
}
Also used : ColorConfigBean(net.heartsome.cat.common.bean.ColorConfigBean) DisposeListener(org.eclipse.swt.events.DisposeListener) Position(org.eclipse.jface.text.Position) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) ArrayList(java.util.ArrayList) DisposeEvent(org.eclipse.swt.events.DisposeEvent) RangeDifference(org.eclipse.compare.rangedifferencer.RangeDifference)

Aggregations

StyleRange (org.eclipse.swt.custom.StyleRange)51 Point (org.eclipse.swt.graphics.Point)24 ArrayList (java.util.ArrayList)13 Color (org.eclipse.swt.graphics.Color)9 StyledText (org.eclipse.swt.custom.StyledText)7 Matcher (java.util.regex.Matcher)6 TextStyle (org.eclipse.swt.graphics.TextStyle)5 LinkedList (java.util.LinkedList)2 List (java.util.List)2 InnerTag (net.heartsome.cat.common.ui.innertag.InnerTag)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 KeyEvent (org.eclipse.swt.events.KeyEvent)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 MouseMoveListener (org.eclipse.swt.events.MouseMoveListener)2 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)2 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)1 ServerInfo (com.cubrid.cubridmanager.core.common.model.ServerInfo)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1