use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class MatchViewPart method selectionChanged.
/**
* 监听XLFEditor的选择事件
*/
public void selectionChanged(final IWorkbenchPart part, final ISelection selection) {
if (part == null || selection == null) {
return;
}
if (!(part instanceof IEditorPart)) {
updateActionState();
return;
}
if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
updateActionState();
return;
}
IXliffEditor editor = (IXliffEditor) part;
IStructuredSelection structuredSelecion = (IStructuredSelection) selection;
final Object object = structuredSelecion.getFirstElement();
if (object instanceof Integer) {
int rowIndex = -1;
int selRowIndex = (Integer) object;
if (rowIndex == selRowIndex) {
if (gridTable.getItemCount() != 0) {
updateActionState();
}
return;
} else {
rowIndex = selRowIndex;
}
XLFHandler handler = editor.getXLFHandler();
String rowId = handler.getRowId(rowIndex);
// handler.getTransUnit(rowId);
TransUnitBean transUnit = editor.getRowTransUnitBean(rowIndex);
if (transUnit == null) {
updateActionState();
return;
}
TransUnitInfo2TranslationBean tuInfoBean = getTuInfoBean(transUnit, handler, rowId);
FileEditorInput input = (FileEditorInput) getSite().getPage().getActiveEditor().getEditorInput();
IProject currentProject = input.getFile().getProject();
copyEnable.resetSelection();
gridTable.removeAll();
altTransCacheList.clear();
menuMgr.setEditor(editor);
menuMgr.setRowIndex(rowIndex);
executeMatch(editor, rowId, transUnit, tuInfoBean, currentProject);
}
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class NattableUtil method mergeSegment.
/**
* 合并文本段 ;
*/
public void mergeSegment() {
XLFHandler handler = xliffEditor.getXLFHandler();
List<String> lstRowId = xliffEditor.getSelectedRowIds();
List<String> lstAllRowId = xliffEditor.getXLFHandler().getAllRowIds();
Shell shell = xliffEditor.getSite().getShell();
if (lstRowId.size() < 2) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg1"));
return;
}
Collections.sort(lstRowId, new SortRowIdComparator());
Collections.sort(lstAllRowId, new SortRowIdComparator());
String rowId1 = lstRowId.get(0);
String fileName = RowIdUtil.getFileNameByRowId(rowId1);
if (fileName == null) {
return;
}
if (handler.isLocked(rowId1)) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg3"));
return;
}
for (int i = 1; i < lstRowId.size(); i++) {
String rowId = lstRowId.get(i);
if (handler.isLocked(rowId)) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg3"));
return;
}
String fileName2 = RowIdUtil.getFileNameByRowId(rowId);
// 数组集合必须在一个文件中才能合并
if (fileName2 == null || !fileName.equals(fileName2)) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg4"));
return;
}
// 判断所选文本段是否连续
String strCurTuId = RowIdUtil.getTUIdByRowId(rowId);
String strPreTuId = RowIdUtil.getTUIdByRowId(lstRowId.get(i - 1));
if (strCurTuId == null || strPreTuId == null) {
return;
}
if ((lstAllRowId.indexOf(rowId) - lstAllRowId.indexOf(lstRowId.get(i - 1))) != 1) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg5"));
return;
} else {
String curOriginal = RowIdUtil.getOriginalByRowId(rowId);
String preOriginal = RowIdUtil.getOriginalByRowId(lstRowId.get(i - 1));
if (!curOriginal.equals(preOriginal)) {
MessageDialog.openInformation(shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg5"));
return;
}
}
}
// Bug #2373:选择全部文本段合并后,无显示内容
if (lstRowId.size() == xliffEditor.getXLFHandler().getRowIds().size()) {
xliffEditor.jumpToRow(0);
}
MergeSegmentOperation mergeOper = new MergeSegmentOperation("merge segment", xliffEditor, handler, lstRowId);
IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
try {
operationHistory.execute(mergeOper, null, null);
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class NattableUtil method propagateTranslations.
// /**
// * 获得记忆库更新策略
// * @return ;
// */
// public int getTmxImportStrategy() {
// IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
// return ps.getInt(PreferenceConstants.TM_UPDATE);
// }
/**
* 繁殖翻译 robert
* @param rowIdsMap
* @param monitor
* @return
*/
public IStatus propagateTranslations(Map<String, List<String>> rowIdsMap, IProgressMonitor monitor) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 9, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
subMonitor.beginTask(Messages.getString("utils.NattableUtil.task7"), rowIdsMap.keySet().size());
Iterator<Entry<String, List<String>>> it = rowIdsMap.entrySet().iterator();
final XLFHandler handler = xliffEditor.getXLFHandler();
while (it.hasNext()) {
Entry<String, List<String>> entry = it.next();
// 这是源文本,也就是繁殖翻译中的父
String rootRowId = entry.getKey();
// 这是要被繁殖的所有rowIds,其源文与rootRowId的源文一致
final List<String> rowIds = entry.getValue();
// TransUnitBean tu = handler.getTransUnit(rootRowId);
// String tgtContent = tu.getTgtContent();
final String rootTgtPureText = handler.getTUPureTextByRowId(rootRowId, false);
String rootSrcFullText = handler.getTUFullTextByRowId(rootRowId, true);
String rootTgtFullText = handler.getTUFullTextByRowId(rootRowId, false);
for (String rowId : rowIds) {
String temp = rootTgtPureText;
String srcFullText = handler.getTUFullTextByRowId(rowId, true);
if (srcFullText.trim().equals(rootSrcFullText.trim())) {
temp = rootTgtFullText;
} else {
temp = IntelligentTagPrcessor.intelligentAppendTag(srcFullText, rootTgtFullText);
}
handler.changeTgtTextValue(rowId, temp, null, null);
}
// 下面这是处理处于获得焦点状态的文本段。无法繁殖翻译的情况
Display.getDefault().syncExec(new Runnable() {
public void run() {
int focusRowIndex = HsMultiActiveCellEditor.sourceRowIndex;
if (focusRowIndex == -1) {
return;
}
if (!XLIFFEditorImplWithNatTable.getCurrent().isHorizontalLayout()) {
focusRowIndex = VerticalNatTableConfig.getRealRowIndex(focusRowIndex);
}
String focusRowId = handler.getRowId(focusRowIndex);
if (rowIds.contains(focusRowId)) {
HsMultiActiveCellEditor.getTargetStyledEditor().setCanonicalValue(new UpdateDataBean(rootTgtPureText, null, null));
}
}
});
if (subMonitor.isCanceled()) {
return Status.OK_STATUS;
}
subMonitor.worked(1);
}
subMonitor.done();
return Status.OK_STATUS;
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class NattableUtil method getRowIdsNoEmptyTranslate.
/**
* 对选中的文本段进行过滤
* @param isSignedOff
* @return ;
*/
public List<String> getRowIdsNoEmptyTranslate(boolean isSignedOff) {
List<String> selRowIds = xliffEditor.getSelectedRowIds();
int oldSize = selRowIds.size();
XLFHandler handler = xliffEditor.getXLFHandler();
handler.removeNullTgtContentRowId(selRowIds);
boolean hasEmpty = false;
boolean hasDraft = false;
if (oldSize != selRowIds.size()) {
hasEmpty = true;
}
if (isSignedOff) {
// 判断执行签发时是否有草稿状态的文本段
for (int i = 0; i < selRowIds.size(); i++) {
String rowId = selRowIds.get(i);
if (handler.isDraft(rowId)) {
selRowIds.remove(i);
i--;
hasDraft = true;
}
}
}
String message = null;
if (hasEmpty && hasDraft) {
message = Messages.getString("utils.NattableUtil.msg8");
} else if (hasDraft) {
message = Messages.getString("utils.NattableUtil.msg9");
} else if (hasEmpty) {
message = Messages.getString("utils.NattableUtil.msg1");
}
if (message != null) {
if (!MessageDialog.openConfirm(xliffEditor.getTable().getShell(), Messages.getString("utils.NattableUtil.msgTitle"), message)) {
selRowIds.clear();
}
}
return selRowIds;
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class OpenMultiXliffHandler method execute.
@SuppressWarnings("unchecked")
public Object execute(ExecutionEvent event) throws ExecutionException {
IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("net.heartsome.cat.common.ui.navigator.view");
ISelection currentSelection = (StructuredSelection) viewPart.getSite().getSelectionProvider().getSelection();
if (currentSelection.isEmpty() || !(currentSelection instanceof IStructuredSelection)) {
return null;
}
IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
Iterator<Object> selectIt = structuredSelection.iterator();
final ArrayList<IFile> selectIFiles = new ArrayList<IFile>();
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
shell = window.getShell();
// 先验证是否跨项目
IProject selectedProject = null;
while (selectIt.hasNext()) {
Object object = selectIt.next();
if (object instanceof IFile) {
IFile iFile = (IFile) object;
if (!CommonFunction.validXlfExtension(iFile.getFileExtension())) {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), MessageFormat.format(Messages.getString("handlers.OpenMultiXliffHandler.msg1"), iFile.getFullPath().toOSString()));
continue;
}
selectIFiles.add(iFile);
if (selectedProject == null) {
selectedProject = iFile.getProject();
} else {
if (selectedProject != iFile.getProject()) {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg2"));
return null;
}
}
} else if (object instanceof IContainer) {
IContainer selectContainer = (IContainer) object;
if (selectedProject == null) {
selectedProject = selectContainer.getProject();
}
// 判断当前文件夹是否处于 XLIFF 文件夹下
try {
ResourceUtils.getXliffs(selectContainer, selectIFiles);
} catch (CoreException e) {
logger.error(Messages.getString("handlers.OpenMultiXliffHandler.logger1"), selectContainer.getFullPath().toOSString(), e);
}
}
}
// 过滤重复选择文件
CommonFunction.removeRepeateSelect(selectIFiles);
if (selectIFiles.size() < 2) {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg3"));
return null;
}
MultiFilesOper oper = new MultiFilesOper(selectedProject, selectIFiles);
// 先验证这些文件是否已经合并打开,如果是,则退出
if (oper.validExist()) {
return null;
}
// 判断是否有重复打开的文件,并删除缓存中要合并打开的文件。
if (oper.hasOpenedIFile()) {
if (oper.getSelectIFiles().size() <= 0) {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg4"));
return null;
} else {
boolean isResponse = MessageDialog.openConfirm(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle2"), Messages.getString("handlers.OpenMultiXliffHandler.msg5"));
if (isResponse) {
if (oper.getSelectIFiles().size() < 2) {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg6"));
return null;
}
} else {
return null;
}
}
}
final IFile multiIFile = oper.createMultiTempFile();
if (multiIFile != null && multiIFile.exists()) {
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask(Messages.getString("handler.OpenMultiXliffHandler.tip1"), 10);
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 7, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
// 进行判断所选择文件的语言对是否符合标准,先解析文件
final XLFHandler xlfHander = new XLFHandler();
final Map<String, Object> newResultMap = xlfHander.openFiles(ResourceUtils.iFilesToFiles(selectIFiles), subMonitor);
// 针对解析失败
if (newResultMap == null || QAConstant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) newResultMap.get(QAConstant.RETURNVALUE_RESULT)) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), (String) newResultMap.get(Constant.RETURNVALUE_MSG));
}
});
return;
}
// 验证是否有多个语言
boolean hasDiffrentLangPair = false;
Map<String, ArrayList<String>> langMap = xlfHander.getLanguages();
if (langMap.size() > 1) {
hasDiffrentLangPair = true;
} else {
for (Entry<String, ArrayList<String>> entry : langMap.entrySet()) {
if (entry.getValue().size() > 1) {
hasDiffrentLangPair = true;
}
}
}
if (monitor.isCanceled()) {
return;
}
monitor.worked(1);
if (hasDiffrentLangPair) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg7"));
}
});
// 先删除临时文件,再退出
try {
multiIFile.delete(true, monitor);
} catch (CoreException e) {
logger.error(Messages.getString("handlers.OpenMultiXliffHandler.logger2"), e);
}
return;
}
final boolean[] validateResult = new boolean[] { false };
Display.getDefault().syncExec(new Runnable() {
public void run() {
validateResult[0] = XLFValidator.validateXlifIFiles(selectIFiles);
}
});
if (!validateResult[0]) {
try {
multiIFile.delete(true, monitor);
} catch (CoreException e) {
logger.error(Messages.getString("handlers.OpenMultiXliffHandler.logger2"), e);
}
return;
}
final FileEditorInput input = new FileEditorInput(multiIFile);
if (monitor.isCanceled()) {
return;
}
monitor.worked(1);
Display.getDefault().syncExec(new Runnable() {
public void run() {
try {
// UNDO 这里合并打开时,要考虑传入参数xlfHandler,以防多次解析文件带来的消耗。
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, XLIFF_EDITOR_ID, true);
} catch (PartInitException e) {
e.printStackTrace();
}
}
});
if (monitor.isCanceled()) {
return;
}
monitor.worked(1);
monitor.done();
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, runnable);
} catch (Exception e) {
logger.error(Messages.getString("handlers.OpenMultiXliffHandler.logger3"), e);
}
} else {
MessageDialog.openInformation(shell, Messages.getString("handlers.OpenMultiXliffHandler.msgTitle"), Messages.getString("handlers.OpenMultiXliffHandler.msg8"));
return null;
}
return null;
}
Aggregations