use of net.heartsome.cat.ts.tm.match.TmMatcher in project translationstudio8 by heartsome.
the class LockRepeatedSegmentHandler method execute.
@Override
public Object execute(final ExecutionEvent event, final List<IFile> iFileList) {
list = iFileList;
tmMatcher = new TmMatcher();
isCancel = false;
continuee = true;
isLocked = false;
if (list == null || list.isEmpty()) {
if (list.size() == 0) {
MessageDialog.openInformation(shell, Messages.getString("translation.LockRepeatedSegmentHandler.msgTitle"), Messages.getString("translation.LockRepeatedSegmentHandler.msg1"));
return null;
}
return null;
}
try {
window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
} catch (ExecutionException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
}
// 首先验证是否是合并打开的文件 --robert
if (isEditor) {
IEditorReference[] editorRefe = window.getActivePage().findEditors(new FileEditorInput(list.get(0)), XLIFF_EDITOR_ID, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
if (editorRefe.length <= 0) {
return null;
}
nattable = (XLIFFEditorImplWithNatTable) editorRefe[0].getEditor(true);
// 针对合并打开
if (nattable.isMultiFile()) {
list = ResourceUtils.filesToIFiles(nattable.getMultiFileList());
}
}
// 添加验证 peason
List<IFile> lstFiles = new ArrayList<IFile>();
XLFValidator.resetFlag();
for (IFile iFile : list) {
if (!XLFValidator.validateXliffFile(iFile)) {
lstFiles.add(iFile);
}
}
XLFValidator.resetFlag();
if (!(list instanceof ArrayList)) {
list = new ArrayList<IFile>(list);
}
list.removeAll(lstFiles);
if (list.size() == 0) {
return null;
}
CommonFunction.removeRepeateSelect(list);
final LockRepeatedSegmentDialog dialog = new LockRepeatedSegmentDialog(shell, list, Messages.getString("translation.LockRepeatedSegmentHandler.dialog"));
if (dialog.open() == LockRepeatedSegmentDialog.OK) {
isLockInnerRepeatedSegment = dialog.isLockInnerRepeatedSegment();
isLockTM100Segment = dialog.isLockTM100Segment();
isLockTM101Segment = dialog.isLockTM101Segment();
if (!dialog.isLockInnerRepeatedSegment() && !dialog.isLockTM100Segment() && !dialog.isLockTM101Segment()) {
// “锁定内部”、“锁定100%”、“锁定101%”都未选中。
return null;
}
if (!isEditor) {
// 如果针对单个文件, 先验证是否有合并打开的
MultiFilesOper oper = new MultiFilesOper(list.get(0).getProject(), (ArrayList<IFile>) list);
// 如果有合并打开的文件,那么将这种转换成针对编辑器的方式处理
if (oper.validExist()) {
final IFile multiTempIFile = oper.getMultiFilesTempIFile(true);
IEditorReference[] editorRefe = window.getActivePage().findEditors(new FileEditorInput(multiTempIFile), XLIFF_EDITOR_ID, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
// 如果这几个文件没有合并打开,
if (editorRefe.length > 0) {
nattable = (XLIFFEditorImplWithNatTable) editorRefe[0].getEditor(true);
continuee = false;
}
}
}
// 开始进行处理
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
int totalWork = dialog.isLockInnerRepeatedSegment() ? list.size() : 0;
totalWork = (dialog.isLockTM100Segment() || dialog.isLockTM101Segment()) ? totalWork * 2 : totalWork;
monitor.beginTask(Messages.getString("translation.LockRepeatedSegmentHandler.task1"), totalWork);
// 修改结果显示为是否成功
final LockRepeatedSegmentResultDialog lrsrd = new LockRepeatedSegmentResultDialog(shell, list);
// 是否进行外部匹配
boolean checkTM = false;
curProject = list.get(0).getProject();
// 锁定外部完全匹配与外部上下文匹配
if ((dialog.isLockTM100Segment() || dialog.isLockTM101Segment())) {
// 如果是针对编辑器,那么将里面的文件进行统一处理
if (isEditor) {
LockTMSegment lts = lockTMSegmentOFEditor(list, dialog.isLockTM100Segment(), dialog.isLockTM101Segment(), monitor);
// 用户指定退出操作
if (lts == null && isCancel) {
return;
}
lrsrd.setLockedFullMatchResult(lts.getLockedFullMatchResult());
lrsrd.setLockedContextResult(lts.getLockedContextResult());
lrsrd.setTuNumResult(lts.getTuNumResult());
checkTM = true;
} else {
if (continuee) {
Map<String, Integer> lockedFullMatchResultMap = new HashMap<String, Integer>();
Map<String, Integer> lockedContextMatchResultMap = new HashMap<String, Integer>();
Map<String, Integer> lockedTuNumResultMap = new HashMap<String, Integer>();
for (int i = 0; i < list.size(); i++) {
IFile iFile = list.get(i);
LockTMSegment lts = lockTMSegment(Arrays.asList(iFile), dialog.isLockTM100Segment(), dialog.isLockTM101Segment(), monitor);
if (lts == null && isCancel) {
return;
}
// 返回的为空,是解析异常的文件被删除了。
if (lts != null) {
lockedFullMatchResultMap.putAll(lts.getLockedFullMatchResult());
lockedContextMatchResultMap.putAll(lts.getLockedContextResult());
lockedTuNumResultMap.putAll(lts.getTuNumResult());
} else {
i--;
}
}
lrsrd.setLockedFullMatchResult(lockedFullMatchResultMap);
lrsrd.setLockedContextResult(lockedContextMatchResultMap);
lrsrd.setTuNumResult(lockedTuNumResultMap);
checkTM = true;
} else {
LockTMSegment lts = lockTMSegmentOFEditor(list, dialog.isLockTM100Segment(), dialog.isLockTM101Segment(), monitor);
if (lts == null && isCancel) {
return;
}
lrsrd.setLockedFullMatchResult(lts.getLockedFullMatchResult());
lrsrd.setLockedContextResult(lts.getLockedContextResult());
lrsrd.setTuNumResult(lts.getTuNumResult());
checkTM = true;
}
}
}
// 锁定内部重复
if (dialog.isLockInnerRepeatedSegment()) {
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, list.size(), SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
HashMap<String, Integer> lockedInnerRepeatedResault = new HashMap<String, Integer>();
HashMap<String, Integer> tuNumResult = null;
if (!checkTM) {
tuNumResult = new HashMap<String, Integer>();
}
Map<String, int[]> resMap = lockInnerRepeatedSegment(list, subMonitor, checkTM);
for (IFile iFile : list) {
String filePath = ResourceUtils.iFileToOSPath(iFile);
int[] res = resMap.get(filePath);
if (!checkTM) {
int countTU = res[0];
tuNumResult.put(filePath, countTU);
}
int countLockedInnerRepeatedSegment = res[1];
lockedInnerRepeatedResault.put(filePath, countLockedInnerRepeatedSegment);
}
lrsrd.setLockedInnerRepeatedResault(lockedInnerRepeatedResault);
if (!checkTM) {
lrsrd.setTuNumResult(tuNumResult);
}
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor != null && editor instanceof XLIFFEditorImplWithNatTable) {
XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
xliffEditor.reloadXliff();
}
// lrsrd.open();
if (hasWrongResults(list, lrsrd)) {
MessageDialog.openInformation(shell, Messages.getString("translation.LockRepeatedSegmentHandler.msgTitle"), Messages.getString("dialog.LockRepeatedSegmentResultDialog.locksuccesful"));
} else {
MessageDialog.openInformation(shell, Messages.getString("translation.LockRepeatedSegmentHandler.msgTitle"), Messages.getString("dialog.LockRepeatedSegmentResultDialog.locksuccesful"));
}
lrsrd.close();
HsMultiActiveCellEditor.refrushCellsEditAbility();
}
});
} finally {
monitor.done();
}
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, runnable);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
Aggregations