Search in sources :

Example 16 with GridItem

use of org.eclipse.nebula.widgets.grid.GridItem 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 17 with GridItem

use of org.eclipse.nebula.widgets.grid.GridItem in project translationstudio8 by heartsome.

the class TargetColunmCellRenderer 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);
    }
    TextLayout layout = getTextLayout(gc, item, getColumn(), true, false);
    String displayStr = layout.getText();
    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;
            int heigth = (Integer) item.getData("itemHeight");
            textHeight = Math.max(textHeight, heigth);
            if (textHeight != item.getHeight()) {
                item.setHeight(textHeight);
                item.setData("itemHeight", textHeight);
            }
        }
        // Point selection = copyEnable.getSelectionRange(getColumn(), getRow() - 1); // row based zero ,get row
        // first row is 1
        Point selection = copyEnable.getSelectionRange(getColumn(), item);
        if (selection == null || selection.x == selection.y) {
            layout.draw(gc, getBounds().x + leftMargin, y);
        } else {
            // textLayout.draw(gc, getBounds().x + leftMargin, y);
            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()) {
                // 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);
        }
    } finally {
        if (layout != null) {
            layout.dispose();
        }
    }
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) Color(org.eclipse.swt.graphics.Color) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 18 with GridItem

use of org.eclipse.nebula.widgets.grid.GridItem in project translationstudio8 by heartsome.

the class CellRenderer method getBranches.

/**
	 * Calculates the sequence of branch lines which should be rendered for the provided item
	 * @param item
	 * @return an array of integers composed using the constants in {@link BranchRenderer}
	 */
private int[] getBranches(GridItem item) {
    int[] branches = new int[item.getLevel() + 1];
    GridItem[] roots = item.getParent().getRootItems();
    // Is this a node or a leaf?
    if (item.getParentItem() == null) {
        // Add descender if not last item
        if (!item.isExpanded() && roots[roots.length - 1].equals(item)) {
            if (item.hasChildren())
                branches[item.getLevel()] = BranchRenderer.LAST_ROOT;
            else
                branches[item.getLevel()] = BranchRenderer.SMALL_L;
        } else {
            if (item.hasChildren())
                branches[item.getLevel()] = BranchRenderer.ROOT;
            else
                branches[item.getLevel()] = BranchRenderer.SMALL_T;
        }
    } else if (item.hasChildren())
        if (item.isExpanded())
            branches[item.getLevel()] = BranchRenderer.NODE;
        else
            branches[item.getLevel()] = BranchRenderer.NONE;
    else
        branches[item.getLevel()] = BranchRenderer.LEAF;
    // Branch for current item
    GridItem parent = item.getParentItem();
    if (parent == null)
        return branches;
    // Are there siblings below this item?
    if (parent.indexOf(item) < parent.getItemCount() - 1)
        branches[item.getLevel() - 1] = BranchRenderer.T;
    else // Is the next node a root?
    if (parent.getParentItem() == null && !parent.equals(roots[roots.length - 1]))
        branches[item.getLevel() - 1] = BranchRenderer.T;
    else
        // This must be the last element at this level
        branches[item.getLevel() - 1] = BranchRenderer.L;
    Grid grid = item.getParent();
    item = parent;
    parent = item.getParentItem();
    // Branches for parent items
    while (item.getLevel() > 0) {
        if (parent.indexOf(item) == parent.getItemCount() - 1) {
            if (parent.getParentItem() == null && !grid.getRootItem(grid.getRootItemCount() - 1).equals(parent))
                branches[item.getLevel() - 1] = BranchRenderer.I;
            else
                branches[item.getLevel() - 1] = BranchRenderer.NONE;
        } else
            branches[item.getLevel() - 1] = BranchRenderer.I;
        item = parent;
        parent = item.getParentItem();
    }
    // item should be null at this point
    return branches;
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) Grid(org.eclipse.nebula.widgets.grid.Grid)

Example 19 with GridItem

use of org.eclipse.nebula.widgets.grid.GridItem in project translationstudio8 by heartsome.

the class TermViewerBodyMenu method editTerm.

void editTerm() {
    IProject curentProject = getCurentProject();
    DBOperator dbOperator = getDbOperator(curentProject);
    try {
        if (dbOperator.getConnection() == null) {
            dbOperator.start();
        }
        GridItem selectItem = getSelectItem();
        String srcTerm = selectItem.getText(1);
        String tgtTerm = selectItem.getText(2);
        String properValue = selectItem.getText(3);
        AddTermToTBDialog dialog = AddTermToTBDialog.getInstance(this.termView.getSite().getShell(), srcTerm.toString().trim(), tgtTerm.toString().trim(), AddTermToTBDialog.EDIT_TYPE);
        dialog.setProject(curentProject);
        dialog.setSrcLang(getSrcLang());
        dialog.setTgtLang(getTgtLang());
        dialog.setPropertyValue(properValue);
        dialog.setDbOperator(dbOperator);
        Object id = selectItem.getData("DBID");
        int open = dialog.open();
        if (Dialog.OK == open) {
            if (id instanceof String) {
                String termId = (String) id;
                dbOperator.beginTransaction();
                dbOperator.deleteTermEntry(termId, getSrcLang(), getTgtLang());
            }
            dbOperator.commit();
        }
    } catch (Exception e) {
        MessageDialog.openInformation(this.termView.getSite().getShell(), Messages.getString("view.TerminologyViewPart.action.msg"), Messages.getString("view.TerminologyViewPart.action.editFailed"));
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("RollBack error", e);
        }
        logger.error("", e);
    } finally {
        try {
            dbOperator.end();
        } catch (SQLException e) {
            logger.error("", e);
        }
    }
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) AddTermToTBDialog(net.heartsome.cat.ts.ui.term.dialog.AddTermToTBDialog) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) SQLException(java.sql.SQLException)

Example 20 with GridItem

use of org.eclipse.nebula.widgets.grid.GridItem in project translationstudio8 by heartsome.

the class TypeColunmCellRenderer 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);
}
Also used : GridItem(org.eclipse.nebula.widgets.grid.GridItem) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Aggregations

GridItem (org.eclipse.nebula.widgets.grid.GridItem)69 Point (org.eclipse.swt.graphics.Point)46 Image (org.eclipse.swt.graphics.Image)20 TextLayout (org.eclipse.swt.graphics.TextLayout)17 GridColumn (org.eclipse.nebula.widgets.grid.GridColumn)16 Color (org.eclipse.swt.graphics.Color)12 Grid (org.eclipse.nebula.widgets.grid.Grid)9 TalendGridItem (org.eclipse.nebula.widgets.grid.TalendGridItem)8 Rectangle (org.eclipse.swt.graphics.Rectangle)8 DisposeEvent (org.eclipse.swt.events.DisposeEvent)6 DisposeListener (org.eclipse.swt.events.DisposeListener)6 ArrayList (java.util.ArrayList)5 TalendGridColumn (org.eclipse.nebula.widgets.grid.TalendGridColumn)5 Event (org.eclipse.swt.widgets.Event)5 Listener (org.eclipse.swt.widgets.Listener)5 SQLException (java.sql.SQLException)3 List (java.util.List)3 DBOperator (net.heartsome.cat.database.DBOperator)3 GridCellRenderer (org.eclipse.nebula.widgets.grid.GridCellRenderer)3 GC (org.eclipse.swt.graphics.GC)3