use of net.heartsome.cat.common.ui.innertag.InnerTag in project translationstudio8 by heartsome.
the class InnerTagUtil method createInnerTagControl.
/** Display 对象 */
// private static Display display = Display.getDefault();
// /** 错误标记边框颜色 */
// private static Color wrongTagBorderColor = ColorConfigBean.getInstance().getWrongTagColor();
// /** 边框颜色 */
// private static Color borderColor = new Color(display, 0, 255, 255);
// /** 文本前景色 */
// private static Color textFgColor = new Color(display, 0, 104, 139);
// /** 文本背景色 */
// private static Color textBgColor = new Color(display, 0, 205, 205);
// /** 索引前景色 */
// private static Color inxFgColor = borderColor;
// /** 索引背景色 */
// private static Color inxBgColor = new Color(display, 0, 139, 139);
/**
* 创建内部标记控件
* @param parent
* 父容器
* @param innerTagBean
* 内部标记实体
* @return 内部标记控件;
*/
public static InnerTag createInnerTagControl(Composite parent, InnerTagBean innerTagBean, TagStyle tagStyle) {
final InnerTag innerTag = new InnerTag(parent, SWT.NONE, innerTagBean, tagStyle);
//--- 此部分配置已经移动到 InnertagRender 中
// /** 错误标记边框颜色 */
// ColorConfigBean cbean = ColorConfigBean.getInstance();
// Color wrongTagBorderColor = cbean.getWrongTagColor();
// Color tmpBorderColor = innerTagBean.isWrongTag() ? wrongTagBorderColor : cbean.getTagBgColor();
// Color bgColor = innerTagBean.isWrongTag() ? wrongTagBorderColor : cbean.getTagBgColor();
// innerTag.initColor(cbean.getTagBgColor(), cbean.getTagFgColor(), cbean.getTagFgColor(), bgColor,
// tmpBorderColor);
// innerTag.setFont(net.heartsome.cat.common.ui.utils.InnerTagUtil.tagFont);
innerTag.pack();
return innerTag;
}
use of net.heartsome.cat.common.ui.innertag.InnerTag in project translationstudio8 by heartsome.
the class SegmentViewer method filterInnerTag.
/**
* 过滤掉系统剪切板中的标记
*/
private String filterInnerTag(String contents) {
if (contents == null) {
return contents;
}
List<InnerTag> cacheTags = getInnerTagCacheList();
String fullText = getTextWidget().getText();
Matcher matcher = PATTERN.matcher(contents);
Stack<InnerTag> stack = new Stack<InnerTag>();
Stack<String> phStack = new Stack<String>();
List<String> needRemove = new ArrayList<String>();
while (matcher.find()) {
String placeHolder = matcher.group();
InnerTag tag = InnerTagUtil.getInnerTag(cacheTags, placeHolder);
if (tag == null) {
needRemove.add(placeHolder);
continue;
}
if (tag.getInnerTagBean().getType() == TagType.START) {
stack.push(tag);
phStack.push(placeHolder);
continue;
} else if (tag.getInnerTagBean().getType() == TagType.END) {
if (stack.isEmpty()) {
// 只有结束 没有开始
needRemove.add(placeHolder);
continue;
}
InnerTag _tag = stack.pop();
String _placeHolder = phStack.pop();
if (tag.getInnerTagBean().getIndex() != _tag.getInnerTagBean().getIndex()) {
needRemove.add(placeHolder);
needRemove.add(_placeHolder);
continue;
}
int start = -1;
if ((start = fullText.indexOf(_placeHolder)) != -1) {
getTextWidget().replaceTextRange(start, _placeHolder.length(), "");
fullText = getTextWidget().getText();
}
if ((start = fullText.indexOf(placeHolder)) != -1) {
getTextWidget().replaceTextRange(start, placeHolder.length(), "");
fullText = getTextWidget().getText();
}
} else {
int start = -1;
if ((start = fullText.indexOf(placeHolder)) != -1) {
getTextWidget().replaceTextRange(start, placeHolder.length(), "");
fullText = getTextWidget().getText();
}
}
}
while (!stack.isEmpty()) {
needRemove.add(InnerTagUtil.getPlaceHolder(getInnerTagCacheList(), stack.pop().getInnerTagBean()));
}
for (String r : needRemove) {
contents = contents.replaceAll(r, "");
}
return contents;
}
use of net.heartsome.cat.common.ui.innertag.InnerTag in project translationstudio8 by heartsome.
the class InsertNextTagHandler method getNextTagIndex.
private int getNextTagIndex() {
List<InnerTag> currentInnerTag = cellEditor.getSegmentViewer().getCurrentInnerTags();
// 按照标记索引从小到大将当前显示的内部标记排序。
Collections.sort(currentInnerTag, new Comparator<InnerTag>() {
public int compare(InnerTag o1, InnerTag o2) {
InnerTagBean bean1 = o1.getInnerTagBean();
InnerTagBean bean2 = o2.getInnerTagBean();
if (bean1.getIndex() != bean2.getIndex()) {
return bean1.getIndex() - bean2.getIndex();
} else {
return bean1.getType().compareTo(bean2.getType());
}
}
});
int index = 1;
for (InnerTag innerTag : currentInnerTag) {
if (innerTag.getInnerTagBean().getIndex() != index) {
break;
} else {
switch(innerTag.getInnerTagBean().getType()) {
case END:
case STANDALONE:
index++;
break;
default:
break;
}
}
}
// 如果超过了源文本内部标记最大索引,则重置为 -1。
return index <= sourceMaxTagIndex ? index : -1;
}
use of net.heartsome.cat.common.ui.innertag.InnerTag in project translationstudio8 by heartsome.
the class SegmentViewer method getRealSplitOffset.
/**
* 根据传入的光标位置得到实际的光标位置(StyledText中的文本有一部分是已经被转换成内部标记的,与XML文本的分割位置有差异,因此需要此方法得到在XML中实际的分割位置)
* @return 实际光标所在的位置;
*/
public int getRealSplitOffset(int caretOffset) {
StyledText styledText = getTextWidget();
if (styledText == null) {
return 0;
}
String text = styledText.getText();
// 当 caretOffset 之前有转义字符时,caretOffset 要转换成实际位置
String subText = text.substring(0, caretOffset);
subText = cleanRegularString(subText);
// 下面两行不能颠倒
int offset = caretOffset;
caretOffset = subText.length();
Matcher matcher = PATTERN.matcher(styledText.getText());
while (matcher.find()) {
String placeHolder = matcher.group();
int start = matcher.start();
// 当光标在标记前或后时,获取实际光标位置会有问题,此处添加判断
if (start >= offset) {
return caretOffset;
}
InnerTag innerTag = InnerTagUtil.getInnerTag(innerTagCacheList, placeHolder);
if (innerTag != null) {
String tagContent = innerTag.getInnerTagBean().getContent();
int length = tagContent.length() - placeHolder.length();
caretOffset += length;
}
}
return caretOffset;
}
use of net.heartsome.cat.common.ui.innertag.InnerTag in project translationstudio8 by heartsome.
the class SegmentViewer method convertDisplayTextToOriginalText.
/**
* 转换显示文本为原始文本。
* @param displayText
* 显示文本
* @return XML 中的原始文本;
*/
public String convertDisplayTextToOriginalText(String displayText) {
if (displayText == null || displayText.length() == 0) {
return "";
}
Matcher matcher = PATTERN.matcher(displayText);
int offset = 0;
StringBuffer sb = new StringBuffer(displayText);
while (matcher.find()) {
String placeHolder = matcher.group();
InnerTag innerTag = InnerTagUtil.getInnerTag(innerTagCacheList, placeHolder);
if (innerTag != null) {
String tagContent = innerTag.getInnerTagBean().getContent();
// tagContent = tagContent.replaceAll("&", "&");
int start = matcher.start() + offset;
int end = matcher.end() + offset;
sb.replace(start, end, tagContent);
offset += tagContent.length() - 1;
}
}
return sb.toString();
}
Aggregations