Search in sources :

Example 91 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class AddTermToTBHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof IXliffEditor) {
        IXliffEditor xliffEditor = (IXliffEditor) editor;
        IFile file = ((FileEditorInput) editor.getEditorInput()).getFile();
        //			ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(file.getProject());
        //			List<DatabaseModelBean> lstDatabase = projectConfig.getTermBaseDbs(true);
        TbImporter.getInstance().setProject(file.getProject());
        if (!TbImporter.getInstance().checkImporter()) {
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.AddTermToTBHandler.msgTitle"), Messages.getString("handler.AddTermToTBHandler.msg"));
            return null;
        }
        StringBuffer srcTerm = new StringBuffer();
        StringBuffer tgtTerm = new StringBuffer();
        String srcAllText = xliffEditor.getRowTransUnitBean(xliffEditor.getSelectedRows()[0]).getSrcText();
        xliffEditor.getSelectSrcOrTgtPureText(srcTerm, tgtTerm);
        AddTermToTBDialog dialog = AddTermToTBDialog.getInstance(editor.getSite().getShell(), srcTerm.toString().trim(), tgtTerm.toString().trim(), AddTermToTBDialog.ADD_TYPE);
        dialog.setProject(file.getProject());
        dialog.setSrcLang(xliffEditor.getSrcColumnName());
        dialog.setTgtLang(xliffEditor.getTgtColumnName());
        dialog.setSrcAllText(srcAllText);
        dialog.open();
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) AddTermToTBDialog(net.heartsome.cat.ts.ui.term.dialog.AddTermToTBDialog) IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor)

Example 92 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class TermViewerBodyMenu method addTerm.

void addTerm() {
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (editor instanceof IXliffEditor) {
        IXliffEditor xliffEditor = (IXliffEditor) editor;
        IFile file = ((FileEditorInput) editor.getEditorInput()).getFile();
        TbImporter.getInstance().setProject(file.getProject());
        if (!TbImporter.getInstance().checkImporter()) {
            MessageDialog.openInformation(editor.getSite().getShell(), Messages.getString("handler.AddTermToTBHandler.msgTitle"), Messages.getString("handler.AddTermToTBHandler.msg"));
            return;
        }
        StringBuffer srcTerm = new StringBuffer();
        StringBuffer tgtTerm = new StringBuffer();
        String srcAllText = xliffEditor.getRowTransUnitBean(xliffEditor.getSelectedRows()[0]).getSrcText();
        xliffEditor.getSelectSrcOrTgtPureText(srcTerm, tgtTerm);
        AddTermToTBDialog dialog = AddTermToTBDialog.getInstance(editor.getSite().getShell(), srcTerm.toString().trim(), tgtTerm.toString().trim(), AddTermToTBDialog.ADD_TYPE);
        dialog.setProject(file.getProject());
        dialog.setSrcLang(xliffEditor.getSrcColumnName());
        dialog.setTgtLang(xliffEditor.getTgtColumnName());
        dialog.setSrcAllText(srcAllText);
        dialog.open();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) AddTermToTBDialog(net.heartsome.cat.ts.ui.term.dialog.AddTermToTBDialog) IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor)

Example 93 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput 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)

Example 94 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class SplitPointSettingHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof XLIFFEditorImplWithNatTable) {
        XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
        if (xliffEditor.isMultiFile()) {
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("menu.BodyMenuConfiguration.msgTitle"), Messages.getString("menu.BodyMenuConfiguration.msg7"));
            return null;
        }
        List<String> selectedRowIds = xliffEditor.getSelectedRowIds();
        boolean isSplitPointExist = false;
        if (selectedRowIds != null && selectedRowIds.size() > 0) {
            for (String rowId : selectedRowIds) {
                if (xliffEditor.getSplitXliffPoints().contains(rowId)) {
                    isSplitPointExist = true;
                } else {
                    isSplitPointExist = false;
                    break;
                }
            }
            if (!isSplitPointExist) {
                String xlfPath = ((FileEditorInput) xliffEditor.getEditorInput()).getFile().getLocation().toOSString();
                for (String rowId : selectedRowIds) {
                    //						String firstTURowId = xliffEditor.getXLFHandler().getRowIdByXpath(xlfPath,
                    //								"/xliff/file[1]/body/descendant::trans-unit[1]");
                    //						if (firstTURowId != null && firstTURowId.equals(rowId)) {
                    //							MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
                    //									Messages.getString("menu.BodyMenuConfiguration.msgTitle"),
                    //									Messages.getString("menu.BodyMenuConfiguration.msg8"));
                    //							continue;
                    //						}
                    String lastTURowid = xliffEditor.getXLFHandler().getRowIdByXpath(xlfPath, "/xliff/file[last()]/body/descendant::trans-unit[last()]");
                    if (lastTURowid != null && lastTURowid.equals(rowId)) {
                        MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("menu.BodyMenuConfiguration.msgTitle"), Messages.getString("menu.BodyMenuConfiguration.msg9"));
                        continue;
                    }
                    if (!xliffEditor.getSplitXliffPoints().contains(rowId)) {
                        // 将切割点的序列号添加到List中
                        xliffEditor.getSplitXliffPoints().add(rowId);
                        xliffEditor.getTable().redraw();
                    }
                }
            } else {
                for (String rowId : selectedRowIds) {
                    if (xliffEditor.getSplitXliffPoints().contains(rowId)) {
                        // 删除切割点
                        int index = xliffEditor.getSplitXliffPoints().indexOf(rowId);
                        xliffEditor.getSplitXliffPoints().remove(index);
                        xliffEditor.getTable().redraw();
                    }
                }
            }
        }
    }
    return null;
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IEditorPart(org.eclipse.ui.IEditorPart)

Example 95 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class PreMachineTransUitls 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 PreMachineTransParameters parameters = new PreMachineTransParameters();
        PreMachineTranslationDialog dialog = new PreMachineTranslationDialog(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 {
                    PreMachineTranslation pt = new PreMachineTranslation(xlfHandler, filesWithOsPath, project, parameters);
                    try {
                        final List<PreMachineTranslationCounter> result = pt.executeTranslation(monitor);
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                            //PreMachineTranslationResultDialog dialog = new PreMachineTranslationResultDialog(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);
                MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.result.msg"));
            } catch (InvocationTargetException e) {
                logger.error("", e);
            } catch (InterruptedException e) {
                logger.error("", e);
            }
        }
    } finally {
        HsMultiCellEditorControl.activeSourceAndTargetCell(XLIFFEditorImplWithNatTable.getCurrent());
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) PreMachineTransParameters(net.heartsome.cat.ts.machinetranslation.bean.PreMachineTransParameters) PreMachineTranslationDialog(net.heartsome.cat.ts.machinetranslation.dialog.PreMachineTranslationDialog) ArrayList(java.util.ArrayList) List(java.util.List) PreMachineTranslationCounter(net.heartsome.cat.ts.machinetranslation.bean.PreMachineTranslationCounter) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) 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) 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)

Aggregations

FileEditorInput (org.eclipse.ui.part.FileEditorInput)484 IFile (org.eclipse.core.resources.IFile)320 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)160 PartInitException (org.eclipse.ui.PartInitException)138 IEditorPart (org.eclipse.ui.IEditorPart)135 IEditorInput (org.eclipse.ui.IEditorInput)113 CoreException (org.eclipse.core.runtime.CoreException)95 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)84 IPath (org.eclipse.core.runtime.IPath)76 IResource (org.eclipse.core.resources.IResource)55 IProject (org.eclipse.core.resources.IProject)52 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)51 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)49 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)48 IFileEditorInput (org.eclipse.ui.IFileEditorInput)48 IEditorReference (org.eclipse.ui.IEditorReference)47 SaveAsDialog (org.eclipse.ui.dialogs.SaveAsDialog)46 ISelection (org.eclipse.jface.viewers.ISelection)44 IEditorDescriptor (org.eclipse.ui.IEditorDescriptor)42 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)41