Search in sources :

Example 21 with XLFHandler

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);
    }
}
Also used : TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) Point(org.eclipse.swt.graphics.Point) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler) TransUnitInfo2TranslationBean(net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean) IProject(org.eclipse.core.resources.IProject)

Example 22 with XLFHandler

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);
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler) MergeSegmentOperation(net.heartsome.cat.ts.ui.xliffeditor.nattable.undoable.MergeSegmentOperation) ImportException(net.heartsome.cat.common.core.exception.ImportException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 23 with XLFHandler

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Entry(java.util.Map.Entry) List(java.util.List) ArrayList(java.util.ArrayList) UpdateDataBean(net.heartsome.cat.ts.ui.xliffeditor.nattable.UpdateDataBean) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 24 with XLFHandler

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;
}
Also used : XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 25 with XLFHandler

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;
}
Also used : IViewPart(org.eclipse.ui.IViewPart) IFile(org.eclipse.core.resources.IFile) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ISelection(org.eclipse.jface.viewers.ISelection) PartInitException(org.eclipse.ui.PartInitException) IContainer(org.eclipse.core.resources.IContainer) MultiFilesOper(net.heartsome.cat.ts.ui.util.MultiFilesOper) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IProject(org.eclipse.core.resources.IProject) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

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