Search in sources :

Example 56 with XLFHandler

use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.

the class XlfEditor method getRowIds.

/**
	 * @return List<String> 当前所有可见文本段的 RowID,合并、分割文本段后应重新获取
	 */
public ArrayList<String> getRowIds() {
    WorkbenchContentsFinder finder = new WorkbenchContentsFinder();
    IEditorPart activateEditor = finder.activeWorkbenchWindow().getActivePage().getActiveEditor();
    IXliffEditor xliffEditor = (IXliffEditor) activateEditor;
    XLFHandler handler = xliffEditor.getXLFHandler();
    return handler.getRowIds();
}
Also used : IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler) WorkbenchContentsFinder(org.eclipse.swtbot.eclipse.finder.finders.WorkbenchContentsFinder)

Example 57 with XLFHandler

use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.

the class PreTransUitls method executeTranslation.

public static void executeTranslation(List<IFile> list, final Shell shell) {
    HsMultiActiveCellEditor.commit(true);
    try {
        if (list.size() == 0) {
            MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg1"));
            return;
        }
        List<IFile> lstFiles = new ArrayList<IFile>();
        XLFValidator.resetFlag();
        for (IFile iFile : list) {
            if (!XLFValidator.validateXliffFile(iFile)) {
                lstFiles.add(iFile);
            }
        }
        XLFValidator.resetFlag();
        list = new ArrayList<IFile>(list);
        list.removeAll(lstFiles);
        if (list.size() == 0) {
            return;
        }
        final IProject project = list.get(0).getProject();
        final List<String> filesWithOsPath = ResourceUtils.IFilesToOsPath(list);
        final XLFHandler xlfHandler = new XLFHandler();
        Map<String, Object> resultMap = xlfHandler.openFiles(filesWithOsPath);
        if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
            // 打开文件失败。
            MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg2"));
            return;
        }
        Map<String, List<XliffBean>> map = xlfHandler.getXliffInfo();
        final PreTransParameters parameters = new PreTransParameters();
        PreTranslationDialog dialog = new PreTranslationDialog(shell, map, parameters);
        if (dialog.open() == Window.OK) {
            if (project == null) {
                MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg3"));
                return;
            }
            if (filesWithOsPath == null || filesWithOsPath.size() == 0) {
                MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg4"));
                return;
            }
            final List<IFile> lstFile = list;
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    PreTranslation pt = new PreTranslation(xlfHandler, filesWithOsPath, project, parameters);
                    try {
                        final List<PreTranslationCounter> result = pt.executeTranslation(monitor);
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                PreTranslationResultDialog dialog = new PreTranslationResultDialog(shell, result);
                                dialog.open();
                            }
                        });
                        project.refreshLocal(IResource.DEPTH_INFINITE, null);
                        result.clear();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (CoreException e) {
                        logger.error("", e);
                        e.printStackTrace();
                    } finally {
                        pt.clearResources();
                    }
                    Display.getDefault().syncExec(new Runnable() {

                        public void run() {
                            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                            for (IFile file : lstFile) {
                                FileEditorInput editorInput = new FileEditorInput(file);
                                IEditorPart editorPart = page.findEditor(editorInput);
                                // 选择所有语言
                                XLFHandler handler = null;
                                if (editorPart != null && editorPart instanceof IXliffEditor) {
                                    // xliff 文件已用 XLIFF 编辑器打开
                                    IXliffEditor xliffEditor = (IXliffEditor) editorPart;
                                    handler = xliffEditor.getXLFHandler();
                                    handler.resetCache();
                                    VTDGen vg = new VTDGen();
                                    String path = ResourceUtils.iFileToOSPath(file);
                                    if (vg.parseFile(path, true)) {
                                        handler.getVnMap().put(path, vg.getNav());
                                        xliffEditor.refresh();
                                    }
                                }
                            }
                        }
                    });
                }
            };
            try {
                new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).run(true, true, runnable);
            } catch (InvocationTargetException e) {
                logger.error(Messages.getString("pretranslation.PreTransUitls.logger1"), e);
            } catch (InterruptedException e) {
                logger.error(Messages.getString("pretranslation.PreTransUitls.logger1"), e);
            }
        }
    } finally {
        HsMultiCellEditorControl.activeSourceAndTargetCell(XLIFFEditorImplWithNatTable.getCurrent());
    }
}
Also used : PreTranslationResultDialog(net.heartsome.cat.ts.pretranslation.dialog.PreTranslationResultDialog) IFile(org.eclipse.core.resources.IFile) PreTransParameters(net.heartsome.cat.ts.pretranslation.bean.PreTransParameters) ArrayList(java.util.ArrayList) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ArrayList(java.util.ArrayList) List(java.util.List) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) PreTranslationDialog(net.heartsome.cat.ts.pretranslation.dialog.PreTranslationDialog) VTDGen(com.ximpleware.VTDGen) IEditorPart(org.eclipse.ui.IEditorPart) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) PreTranslationCounter(net.heartsome.cat.ts.pretranslation.bean.PreTranslationCounter) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 58 with XLFHandler

use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.

the class JumpToNextHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof XLIFFEditorImplWithNatTable) {
        String jumpToNext = event.getParameter("JumpNextSegment");
        if (jumpToNext == null) {
            return null;
        }
        XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
        int[] selectedRows = xliffEditor.getSelectedRows();
        if (selectedRows.length < 1) {
            return null;
        }
        Arrays.sort(selectedRows);
        int lastSelectRow = selectedRows[selectedRows.length - 1];
        XLFHandler handler = xliffEditor.getXLFHandler();
        int nextRow = -1;
        Shell shell = HandlerUtil.getActiveShell(event);
        if (jumpToNext.equalsIgnoreCase("JumpNextNonTranslation")) {
            // 下一未翻译文本段
            nextRow = handler.getNextUntranslatedSegmentIndex(lastSelectRow);
            if (nextRow == -1) {
                MessageDialog.openInformation(shell, Messages.getString("handler.JumpToNextHandler.msgTitle"), Messages.getString("handler.JumpToNextHandler.msg1"));
                return null;
            }
        } else if (jumpToNext.equalsIgnoreCase("JumpNextQuestion")) {
            // 下一带疑问文本段
            nextRow = handler.getNextQuestionSegmentIndex(lastSelectRow);
            if (nextRow == -1) {
                MessageDialog.openInformation(shell, Messages.getString("handler.JumpToNextHandler.msgTitle"), Messages.getString("handler.JumpToNextHandler.msg2"));
                return null;
            }
        } else if (jumpToNext.equalsIgnoreCase("JumpNextNote")) {
            // 下一带标注文本段
            nextRow = handler.getNextNoteSegmentIndex(lastSelectRow);
            if (nextRow == -1) {
                MessageDialog.openInformation(shell, Messages.getString("handler.JumpToNextHandler.msgTitle"), Messages.getString("handler.JumpToNextHandler.msg3"));
                return null;
            }
        }
        xliffEditor.jumpToRow(nextRow);
    }
    return null;
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) Shell(org.eclipse.swt.widgets.Shell) IEditorPart(org.eclipse.ui.IEditorPart) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 59 with XLFHandler

use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.

the class LockRepeatedSegmentHandler method lockInnerRepeatedSegment.

/**
	 * 锁定内部重复文本段
	 * @param iFile
	 * @param monitor
	 * @return ;
	 */
private Map<String, int[]> lockInnerRepeatedSegment(List<IFile> iFileList, IProgressMonitor monitor, boolean checkTM) {
    Map<String, int[]> repeatedMap = new HashMap<String, int[]>();
    final XLFHandler handler = new XLFHandler();
    Map<String, Integer> lockedSizeMap = new HashMap<String, Integer>();
    monitor.beginTask(Messages.getString("translation.LockRepeatedSegmentHandler.task3"), iFileList.size() * 3);
    List<IFile> removeiFileList = new ArrayList<IFile>();
    for (final IFile iFile : iFileList) {
        File file = iFile.getLocation().toFile();
        try {
            Map<String, Object> resultMap = handler.openFile(file);
            if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
                // 打开文件失败。
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openInformation(shell, Messages.getString("translation.LockRepeatedSegmentHandler.msgTitle"), MessageFormat.format(Messages.getString("translation.LockRepeatedSegmentHandler.msg2"), iFile.getLocation().toOSString()));
                    }
                });
                removeiFileList.add(iFile);
                repeatedMap.put(file.getPath(), new int[] { 0, 0 });
            }
        } catch (Exception e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        lockedSizeMap.put(iFile.getLocation().toOSString(), 0);
        if (!monitorWork(monitor, 1)) {
            return null;
        }
    }
    Map<String, ArrayList<String>> languages = handler.getLanguages();
    for (Entry<String, ArrayList<String>> entry : languages.entrySet()) {
        String srcLanguage = entry.getKey();
        for (String tgtLanguage : entry.getValue()) {
            ArrayList<String> rowIds = handler.getRepeatedSegmentExceptFirstOne(srcLanguage, tgtLanguage);
            // 将所有的RowId进行按文件名排序
            Map<String, List<String>> lockedRowidsMap = RowIdUtil.groupRowIdByFileName(rowIds);
            // 将锁定的文件本个数添加到map中
            for (String rowId : rowIds) {
                String fileLC = RowIdUtil.getFileNameByRowId(rowId);
                lockedSizeMap.put(fileLC, lockedSizeMap.get(fileLC) + 1);
            }
            if (!monitorWork(monitor, iFileList.size())) {
                isCancel = true;
                return null;
            }
            lockTU(handler, lockedRowidsMap);
        }
    }
    // 如果没有进行外部匹配,那么就必须获取每个文件的TU节点数量
    for (IFile iFile : iFileList) {
        if (removeiFileList.indexOf(iFile) >= 0) {
            continue;
        }
        String iFileLc = iFile.getLocation().toOSString();
        repeatedMap.put(iFileLc, new int[] { checkTM ? -1 : handler.countTransUnit(iFileLc), lockedSizeMap.get(iFileLc) });
        if (!monitorWork(monitor, 1)) {
            isCancel = true;
            return null;
        }
    }
    monitor.done();
    return repeatedMap;
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) IFile(org.eclipse.core.resources.IFile) File(java.io.File) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 60 with XLFHandler

use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.

the class MatchViewPart method reLoadMatches.

public void reLoadMatches(IXliffEditor editor, int rowIndex) {
    // 修复 Bug #3064 编辑匹配--更换记忆库后再编辑原记忆库匹配,出现异常.刷新问题
    // handler.getTransUnit(rowId);
    TransUnitBean transUnit = editor.getRowTransUnitBean(rowIndex);
    if (transUnit == null) {
        return;
    }
    XLFHandler handler = editor.getXLFHandler();
    if (handler == null) {
        return;
    }
    IProject prj = null;
    if (editor instanceof IEditorPart) {
        IEditorPart p = (IEditorPart) editor;
        FileEditorInput input = (FileEditorInput) p.getEditorInput();
        prj = input.getFile().getProject();
    }
    if (prj == null) {
        return;
    }
    String rowId = handler.getRowId(rowIndex);
    TransUnitInfo2TranslationBean tuInfoBean = getTuInfoBean(transUnit, handler, rowId);
    executeMatch(editor, rowId, transUnit, tuInfoBean, prj);
}
Also used : TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler) IProject(org.eclipse.core.resources.IProject) TransUnitInfo2TranslationBean(net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean)

Aggregations

XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)63 IEditorPart (org.eclipse.ui.IEditorPart)45 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)36 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)24 ArrayList (java.util.ArrayList)21 IFile (org.eclipse.core.resources.IFile)16 FileEditorInput (org.eclipse.ui.part.FileEditorInput)16 List (java.util.List)14 IProject (org.eclipse.core.resources.IProject)13 ExecutionException (org.eclipse.core.commands.ExecutionException)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 CoreException (org.eclipse.core.runtime.CoreException)9 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)8 Shell (org.eclipse.swt.widgets.Shell)8 IViewPart (org.eclipse.ui.IViewPart)8 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)8 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)7 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)6 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)6