Search in sources :

Example 1 with InnerTagBean

use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.

the class PlaceHolderNormalModeBuilder method getPlaceHolder.

/**
	 * 得到内部标记的占位符。
	 * @param innerTagBeans
	 *            内部标记集合(不能为null)
	 * @param index
	 *            当前标记所在索引
	 * @return 内部标记占位符;
	 */
public String getPlaceHolder(List<InnerTagBean> innerTagBeans, int index) {
    Assert.isNotNull(innerTagBeans);
    InnerTagBean innerTagBean = innerTagBeans.get(index);
    int tagIndex = innerTagBean.getIndex();
    StringBuffer tagContent = new StringBuffer();
    StringBuffer innerTag = new StringBuffer();
    if (innerTagBean.getType() == TagType.START) {
        StringBuffer id = createStyledTagStartIndex(tagIndex);
        if (style == TagStyle.SIMPLE || style == TagStyle.INDEX) {
            innerTag.append(id);
            if (style == TagStyle.SIMPLE) {
                tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getName()).append(INVISIBLE_CHAR);
                // 形如“|_1| g_|” (“_”代指不可见字符)
                innerTag.append(tagContent);
            }
        } else if (style == TagStyle.FULL) {
            tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getContent()).append(INVISIBLE_CHAR);
            // 形如“|_1| g_|” (“_”代指不可见字符)
            innerTag.append(id).append(tagContent);
        }
    } else if (innerTagBean.getType() == TagType.END) {
        StringBuffer id = createStyledTagEndIndex(tagIndex);
        if (style == TagStyle.SIMPLE || style == TagStyle.INDEX) {
            if (style == TagStyle.SIMPLE) {
                tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getName()).append(INVISIBLE_CHAR);
                // 形如“|_1| g_|” (“_”代指不可见字符)
                innerTag.append(tagContent);
            }
            innerTag.append(id);
        } else if (style == TagStyle.FULL) {
            tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getContent()).append(INVISIBLE_CHAR);
            innerTag.append(tagContent).append(id);
        }
    } else if (innerTagBean.getType() == TagType.STANDALONE) {
        StringBuffer id = createStyledTagStartIndex(tagIndex);
        StringBuffer endId = createStyledTagEndIndex(tagIndex);
        if (style == TagStyle.SIMPLE || style == TagStyle.INDEX) {
            innerTag.append(id);
            if (style == TagStyle.SIMPLE) {
                tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getName()).append(INVISIBLE_CHAR);
                // 形如“|_1| ph |1_|”;(“_”代指不可见字符)
                innerTag.append(tagContent).append(endId);
            }
        } else if (style == TagStyle.FULL) {
            tagContent.append(INVISIBLE_CHAR).append(innerTagBean.getContent()).append(INVISIBLE_CHAR);
            innerTag.append(id).append(tagContent).append(endId);
        }
    }
    return innerTag.insert(0, BLANK_CHARACTER).append(BLANK_CHARACTER).toString();
}
Also used : InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean)

Example 2 with InnerTagBean

use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.

the class InnerTagUtil method parseXmlToDisplayValue.

/**
	 * 将带内部标记的文本由XML格式转换为显示格式的文本
	 * @param originalValue
	 *            原始的带内部标记的XML格式的文本
	 * @return ;
	 */
public static TreeMap<String, InnerTagBean> parseXmlToDisplayValue(StringBuffer originalValue, TagStyle style) {
    // 得到标签映射map(key: 内部标记;value: 内部标记实体)
    TreeMap<String, InnerTagBean> tags = new TreeMap<String, InnerTagBean>(new Comparator<String>() {

        public int compare(String str1, String str2) {
            int num1 = InnerTagUtil.getStyledTagNum(str1);
            int num2 = InnerTagUtil.getStyledTagNum(str2);
            if (num1 == num2) {
                return str1.indexOf(String.valueOf(num1)) - str2.indexOf(String.valueOf(num1));
            }
            return num1 - num2;
        }
    });
    if (originalValue == null || originalValue.length() == 0) {
        return tags;
    }
    placeHolderCreater.setStyle(style);
    IInnerTagFactory innerTagFactory = new XliffInnerTagFactory(originalValue.toString(), placeHolderCreater);
    // 提取标记之后的文本。
    originalValue.replace(0, originalValue.length(), innerTagFactory.getText());
    List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
    if (innerTagBeans != null && innerTagBeans.size() > 0) {
        for (int i = 0; i < innerTagBeans.size(); i++) {
            String placeHolder = placeHolderCreater.getPlaceHolder(innerTagBeans, i);
            tags.put(placeHolder, innerTagBeans.get(i));
        // originalValue.replace(innerTagBean.getOffset(), innerTagBean.getOffset(), innerTag.toString());
        }
    }
    return tags;
}
Also used : XliffInnerTagFactory(net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) IInnerTagFactory(net.heartsome.cat.common.innertag.factory.IInnerTagFactory) TreeMap(java.util.TreeMap)

Example 3 with InnerTagBean

use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.

the class SegmentViewer method insertInnerTag.

/**
	 * @see net.heartsome.cat.ts.ui.innertag.ISegmentViewer#insertInnerTag(int, int)
	 */
public void insertInnerTag(int tagIndex, int offset) {
    StringBuffer placeHolders = new StringBuffer();
    int caretOffset = offset;
    out: for (int i = 0; i < errorTagStart; i++) {
        InnerTagBean innerTagBean = innerTagCacheList.get(i).getInnerTagBean();
        if (innerTagBean.getIndex() == tagIndex) {
            String placeHolder = placeHolderBuilder.getPlaceHolder(null, i);
            int index = -1;
            if ((index = getTextWidget().getText().indexOf(placeHolder)) > -1) {
                // 使用 StyledTextContent 的 replaceTextRange 方法,不会触发成对删除标记的处理
                getTextWidget().getContent().replaceTextRange(index, placeHolder.length(), "");
                if (index < offset) {
                    offset -= placeHolder.length();
                    caretOffset -= placeHolder.length();
                }
            }
            boolean isEmpty = placeHolders.length() <= 0;
            switch(innerTagBean.getType()) {
                case START:
                    placeHolders.insert(0, placeHolder);
                    caretOffset += placeHolder.length();
                    if (isEmpty) {
                        break;
                    } else {
                        break out;
                    }
                case END:
                    placeHolders.append(placeHolder);
                    if (isEmpty) {
                        break;
                    } else {
                        break out;
                    }
                case STANDALONE:
                    placeHolders.append(placeHolder);
                    caretOffset += placeHolder.length();
                    break out;
                default:
                    break;
            }
        }
    }
    if (placeHolders.length() > 0) {
        // 撤销/重做控制。
        getUndoManager().beginCompoundChange();
        if (placeHolders.length() == 2) {
            // 成对标记包裹选中内容
            placeHolders.insert(1, getSelectedPureText());
            Point p = getSelectedRange();
            getTextWidget().replaceTextRange(p.x, p.y, placeHolders.toString());
        } else {
            getTextWidget().replaceTextRange(offset, 0, placeHolders.toString());
        }
        getTextWidget().setCaretOffset(caretOffset);
        getUndoManager().endCompoundChange();
    }
}
Also used : InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 4 with InnerTagBean

use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.

the class XliffInnerTagFactory method addInnerTagBean.

/**
	 * @param tagType
	 * @param text
	 * @param tagContent
	 * @param tagName
	 *            ;
	 */
private void addInnerTagBean(TagType tagType, StringBuffer text, String tagContent, String tagName) {
    /* 在文本中插入索引 */
    int index = -1;
    if (tagType == START) {
        HasStartTag = true;
        if (targetFlg) {
            for (int i = 0; i < beans.size(); ++i) {
                InnerTagBean bean = beans.get(i);
                if (bean.getType() != TagType.START) {
                    continue;
                }
                if (bean.getContent().equals(tagContent)) {
                    String placeHolder = placeHolderCreater.getPlaceHolder(beans, i);
                    if (text.indexOf(placeHolder) != -1) {
                        // 已经存在,继续寻找。
                        continue;
                    }
                    text.replace(start, start + tagContent.length(), placeHolder);
                    indexStack.push(bean.getIndex());
                    exist = true;
                    return;
                }
            }
        }
        maxIndex++;
        indexStack.push(maxIndex);
        index = maxIndex;
    } else if (tagType == END) {
        if (!HasStartTag) {
            maxIndex++;
            indexStack.push(maxIndex);
        }
        HasStartTag = false;
        if (!indexStack.empty()) {
            index = indexStack.pop();
            if (exist && targetFlg) {
                for (int i = 0; i < beans.size(); ++i) {
                    InnerTagBean bean = beans.get(i);
                    if (bean.getIndex() == index && bean.getType() == TagType.END) {
                        String placeHolder = placeHolderCreater.getPlaceHolder(beans, i);
                        text.replace(start, start + tagContent.length(), placeHolder);
                        if (!indexStack.isEmpty()) {
                            HasStartTag = true;
                        }
                        return;
                    }
                }
            }
        }
        if (!indexStack.isEmpty()) {
            HasStartTag = true;
        }
    } else if (tagType == STANDALONE) {
        maxIndex++;
        index = maxIndex;
    }
    if (index > -1) {
        InnerTagBean bean = new InnerTagBean(index, tagName, tagContent, tagType);
        beans.add(bean);
        String placeHolder = placeHolderCreater.getPlaceHolder(beans, beans.size() - 1);
        text.replace(start, start + tagContent.length(), placeHolder);
        // 显示完整标记时,start 计算错误,因此添加下行语句
        start += placeHolder.length() - 1;
    }
}
Also used : InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean)

Example 5 with InnerTagBean

use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.

the class ConcordanceSearchDialog method createItem.

/**
	 * 向表格中添加记录
	 * @param metaData
	 * @param map
	 *            ;
	 */
private void createItem(MetaData metaData, ConcordanceBean bean, int rowNum, HashMap<IRow, ArrayList<int[]>> mapStyle) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("id", String.valueOf(bean.getId()));
    map.put("changeDate", bean.getChangeDate());
    map.put("changeId", bean.getChangeId());
    map.put("creationDate", bean.getCreationDate());
    map.put("creationId", bean.getCreationId());
    map.put("flag", bean.isBlnIsFlag() ? "1" : "0");
    for (LanguageTMX lang : bean.getLanguageList()) {
        map.put(lang.getLanguageCode(), lang.getText());
    }
    map.put("dbName", metaData.getDatabaseName());
    map.put("dbType", metaData.getDbType());
    map.put("severName", metaData.getServerName());
    List<TmxProp> lstAttr = bean.getAttributeList();
    StringBuffer sbAttr = new StringBuffer();
    ArrayList<int[]> lstAttrIndex = new ArrayList<int[]>();
    for (TmxProp attr : lstAttr) {
        if (attr.getName() != null && !attr.getName().trim().equals("") && !attr.getName().equals("x-flag")) {
            String attrName = TextUtil.xmlToString(attr.getName());
            int index = attrName.indexOf("::");
            if (index != -1) {
                attrName = attrName.substring(index + 2, attrName.length());
            }
            sbAttr.append("   ").append(attrName).append(Messages.getString("dialog.ConcordanceSearchDialog.colon"));
            int startIndex = sbAttr.length();
            sbAttr.append(TextUtil.xmlToString(attr.getValue()));
            int endIndex = sbAttr.length();
            lstAttrIndex.add(new int[] { startIndex, endIndex });
        }
    }
    String creationDate = checkString(bean.getCreationDate());
    String creationId = checkString(bean.getCreationId());
    String changeDate = checkString(bean.getChangeDate());
    String changeId = checkString(bean.getChangeId());
    String attribute = MessageFormat.format(strMsg, creationDate, creationId, changeDate, changeId, sbAttr.toString());
    ArrayList<int[]> lstFieldIndex = new ArrayList<int[]>();
    int creationStart = attribute.indexOf(creationDate);
    lstFieldIndex.add(new int[] { creationStart, creationStart + creationDate.length() });
    int creationIdStart = attribute.indexOf(creationId, creationStart);
    lstFieldIndex.add(new int[] { creationIdStart, creationIdStart + creationId.length() });
    int changeDateStart = attribute.indexOf(changeDate, creationIdStart);
    lstFieldIndex.add(new int[] { changeDateStart, changeDateStart + changeDate.length() });
    int changeIdStart = attribute.indexOf(changeId, changeDateStart);
    lstFieldIndex.add(new int[] { changeIdStart, changeIdStart + changeId.length() });
    if (lstAttrIndex.size() > 0) {
        int index = attribute.indexOf(sbAttr.toString());
        for (int[] attrIndex : lstAttrIndex) {
            lstFieldIndex.add(new int[] { attrIndex[0] + index, attrIndex[1] + index });
        }
    }
    String source = map.get(strSrcLang) == null ? "" : map.get(strSrcLang);
    StringBuffer sourceBf = new StringBuffer(source);
    TreeMap<String, InnerTagBean> sourceTags = InnerTagUtil.parseXmlToDisplayValue(sourceBf, TagStyle.getDefault(false));
    source = InnerTagUtil.resolveTag(sourceBf.toString());
    source = InnerTagUtil.parseDisplayToXmlValue(sourceTags, source);
    String target = map.get(strTgtLang) == null ? "" : map.get(strTgtLang);
    StringBuffer tgtBf = new StringBuffer(target);
    TreeMap<String, InnerTagBean> tgtTags = InnerTagUtil.parseXmlToDisplayValue(tgtBf, TagStyle.getDefault(false));
    target = InnerTagUtil.resolveTag(tgtBf.toString());
    target = InnerTagUtil.parseDisplayToXmlValue(tgtTags, target);
    ArrayList<String> lstTarget = new ArrayList<String>();
    for (String lang : lstLangs) {
        String _tgt = map.get(lang);
        _tgt = _tgt == null ? "" : _tgt;
        StringBuffer _tgtBf = new StringBuffer(_tgt);
        TreeMap<String, InnerTagBean> _tgtTags = InnerTagUtil.parseXmlToDisplayValue(_tgtBf, TagStyle.getDefault(false));
        _tgt = InnerTagUtil.resolveTag(_tgtBf.toString());
        _tgt = InnerTagUtil.parseDisplayToXmlValue(_tgtTags, _tgt);
        lstTarget.add(_tgt);
    }
    try {
        if (tableModel.getRow(rowNum) == null) {
            XPropRow row = new XPropRow(bean.isBlnIsFlag(), source, lstTarget, target, attribute);
            row.setDataMap(map);
            if (metaData != null) {
                row.setData("metaData", metaData);
            }
            tableModel.addRow(row);
            mapStyle.put(row, lstFieldIndex);
        } else {
            tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(0), bean.isBlnIsFlag());
            tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(1), source);
            tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(2), target);
            int i = 0;
            for (; i < lstTarget.size(); i++) {
                tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(3 + i), lstTarget.get(i));
            }
            tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(3 + i), attribute);
            XPropRow row = (XPropRow) tableModel.getRow(rowNum);
            row.setDataMap(map);
            if (metaData != null) {
                row.setData("metaData", metaData);
            }
            mapStyle.put(row, lstFieldIndex);
        }
    } catch (Exception e) {
        XPropRow row = new XPropRow(bean.isBlnIsFlag(), source, lstTarget, target, attribute);
        row.setDataMap(map);
        if (metaData != null) {
            row.setData("metaData", metaData);
        }
        tableModel.addRow(row);
        mapStyle.put(row, lstFieldIndex);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) TmxProp(net.heartsome.cat.common.bean.TmxProp) Point(org.eclipse.swt.graphics.Point) SQLException(java.sql.SQLException) LanguageTMX(net.heartsome.cat.database.tmx.LanguageTMX)

Aggregations

InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)19 Point (org.eclipse.swt.graphics.Point)7 XliffInnerTagFactory (net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory)5 ArrayList (java.util.ArrayList)4 Rectangle (org.eclipse.swt.graphics.Rectangle)4 SQLException (java.sql.SQLException)3 IInnerTagFactory (net.heartsome.cat.common.innertag.factory.IInnerTagFactory)3 TextStyle (org.eclipse.swt.graphics.TextStyle)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 MetaData (net.heartsome.cat.common.bean.MetaData)2 InnerTag (net.heartsome.cat.common.ui.innertag.InnerTag)2 DBOperator (net.heartsome.cat.database.DBOperator)2 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)2 TextLayout (org.eclipse.swt.graphics.TextLayout)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2 IRow (de.jaret.util.ui.table.model.IRow)1 List (java.util.List)1 Entry (java.util.Map.Entry)1