Search in sources :

Example 1 with XLFHandler

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

the class OpenReverseConversionDialogHandler method execute.

public Object execute(ExecutionEvent event, List<IFile> list) {
    if (list == null || list.size() == 0) {
        MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle1"), Messages.getString("handler.OpenReverseConversionDialogHandler.msg1"));
        return null;
    }
    // 首先验证是否是合并打开的文件 --robert	2012-10-17
    System.out.println(list.get(0).getFullPath().toOSString());
    if (isEditor) {
        // 针对合并打开
        if (list.get(0).getFullPath().toOSString().endsWith(".xlp")) {
            List<String> multiFiles = new XLFHandler().getMultiFiles(list.get(0));
            if (multiFiles.size() > 0) {
                list = new ArrayList<IFile>();
            }
            for (String filePath : multiFiles) {
                list.add(ResourceUtils.fileToIFile(filePath));
            }
        }
    }
    List<IFile> lstFiles = new ArrayList<IFile>();
    XLFValidator.resetFlag();
    for (IFile iFile : list) {
        if (!XLFValidator.validateXliffFile(iFile)) {
            lstFiles.add(iFile);
        }
    }
    CommonFunction.removeRepeateSelect(list);
    XLFValidator.resetFlag();
    if (!(list instanceof ArrayList)) {
        list = new ArrayList<IFile>(list);
    }
    list.removeAll(lstFiles);
    if (list.size() == 0) {
        return null;
    }
    ArrayList<ConverterViewModel> converterViewModels = new ArrayList<ConverterViewModel>();
    for (int i = 0; i < list.size(); i++) {
        Object adapter = Platform.getAdapterManager().getAdapter(list.get(i), IConversionItem.class);
        IConversionItem sourceItem = null;
        if (adapter instanceof IConversionItem) {
            sourceItem = (IConversionItem) adapter;
        }
        ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_REVERSE);
        // 记住所选择的文件
        converterViewModel.setConversionItem(sourceItem);
        converterViewModels.add(converterViewModel);
    }
    IProject project = list.get(0).getProject();
    ReverseConversionWizard wizard = new ReverseConversionWizard(converterViewModels, project);
    TSWizardDialog dialog = new TSWizardDialog(shell, wizard) {

        @Override
        protected void createButtonsForButtonBar(Composite parent) {
            super.createButtonsForButtonBar(parent);
            getButton(IDialogConstants.FINISH_ID).setText(Messages.getString("handler.OpenConversionDialogHandler.finishLbl"));
        }
    };
    int result = dialog.open();
    if (result == IDialogConstants.OK_ID) {
        final List<ConverterViewModel> models = wizard.getConverterViewModels();
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.setTaskName(Messages.getString("handler.OpenReverseConversionDialogHandler.task1"));
                monitor.beginTask(Messages.getString("handler.OpenReverseConversionDialogHandler.task2"), models.size());
                for (ConverterViewModel converterViewModel : models) {
                    try {
                        IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
                        subMonitor.setTaskName(Messages.getString("handler.OpenReverseConversionDialogHandler.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
                        converterViewModel.convertWithoutJob(subMonitor);
                    } catch (ConverterException e) {
                        //							Bug #2485:转换文件失败时,提示框显示有问题
                        throw new InvocationTargetException(e, e.getMessage());
                    }
                }
                monitor.done();
            }
        };
        try {
            new ProgressMonitorDialog(shell).run(true, true, runnable);
        } catch (InvocationTargetException e) {
            MessageDialog.openInformation(shell, Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle2"), e.getMessage());
            LOGGER.error("", e);
        } catch (InterruptedException e) {
            MessageDialog.openInformation(shell, Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle2"), e.getMessage());
            LOGGER.error("", e);
        }
    }
    return null;
}
Also used : ConverterException(net.heartsome.cat.converter.ConverterException) IFile(org.eclipse.core.resources.IFile) Composite(org.eclipse.swt.widgets.Composite) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ReverseConversionWizard(net.heartsome.cat.convert.ui.wizard.ReverseConversionWizard) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem) TSWizardDialog(net.heartsome.cat.common.ui.wizard.TSWizardDialog) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 2 with XLFHandler

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

the class PreviewTranslationHandler method getConverterViewModel.

/**
	 * 得到 ConverterViewModel 对象
	 * @param file
	 *            需要预览翻译的文件
	 * @return
	 * @throws ExecutionException
	 *             ;
	 */
private ConverterViewModel getConverterViewModel(IFile file) throws ExecutionException {
    Object adapter = Platform.getAdapterManager().getAdapter(file, IConversionItem.class);
    if (adapter instanceof IConversionItem) {
        IConversionItem item = (IConversionItem) adapter;
        ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), // 逆向转换
        Converter.DIRECTION_REVERSE);
        converterViewModel.setConversionItem(item);
        try {
            ConversionResource resource = new ConversionResource(Converter.DIRECTION_REVERSE, item);
            String xliffpath = resource.getXliffPath();
            String targetPath = resource.getPreviewPath();
            ConversionConfigBean configBean = converterViewModel.getConfigBean();
            configBean.setSource(xliffpath);
            configBean.setTarget(targetPath);
            configBean.setTargetEncoding("UTF-8");
            // 设为预览翻译模式
            configBean.setPreviewMode(true);
            final IStatus status = converterViewModel.validateXliffFile(ConverterUtil.toLocalPath(xliffpath), new XLFHandler(), // 注:验证的过程中,会为文件类型和骨架文件的路径赋值。
            null);
            if (status != null && status.isOK()) {
                return converterViewModel;
            } else {
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), status.getMessage());
                    }
                });
                throw new ExecutionException(status.getMessage(), status.getException());
            }
        } catch (CoreException e) {
            throw new ExecutionException(e.getMessage(), e);
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) ConversionResource(net.heartsome.cat.convert.ui.utils.ConversionResource) ExecutionException(org.eclipse.core.commands.ExecutionException) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 3 with XLFHandler

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

the class PreviewTranslationHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor == null) {
        return false;
    }
    IEditorInput input = editor.getEditorInput();
    IFile file = ResourceUtil.getFile(input);
    shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
    if (file == null) {
        MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), Messages.getString("handler.PreviewTranslationHandler.msg1"));
    } else {
        String fileExtension = file.getFileExtension();
        if (fileExtension != null && CommonFunction.validXlfExtension(fileExtension)) {
            ConverterViewModel model = getConverterViewModel(file);
            if (model != null) {
                // model.convert();
                try {
                    previewFiles(new ArrayList<IFile>(Arrays.asList(new IFile[] { file })));
                } catch (Exception e) {
                    // 修改 当异常没有消息,提示信息为空
                    MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), Messages.getString("handler.PreviewTranslationHandler.msg7"));
                    LOGGER.error("", e);
                }
            }
        } else if (fileExtension != null && "xlp".equalsIgnoreCase(fileExtension)) {
            // UNDO 合并打开的预览翻译有问题,是针对合并打开的文件,而不是针对项目所有的文件 robert 2012-07-12
            if (file.exists()) {
                // IFolder xliffFolder = file.getProject().getFolder(Constant.FOLDER_XLIFF);
                // Fixed Bug #2616 预览翻译--合并打开的文件不能进行预览翻译 by Jason
                XLFHandler hander = new XLFHandler();
                List<String> files = hander.getMultiFiles(file);
                List<IFile> ifileList = new ArrayList<IFile>();
                for (String tf : files) {
                    ifileList.add(ResourceUtils.fileToIFile(tf));
                }
                // if (xliffFolder.exists()) {
                // ArrayList<IFile> files = new ArrayList<IFile>();
                // try {
                // ResourceUtils.getXliffs(xliffFolder, files);
                // } catch (CoreException e) {
                // throw new ExecutionException(e.getMessage(), e);
                // }
                previewFiles(ifileList);
            // } else {
            // MessageDialog
            // .openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"),
            // Messages.getString("handler.PreviewTranslationHandler.msg2"));
            // }
            }
        } else {
            MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), Messages.getString("handler.PreviewTranslationHandler.msg3"));
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) ArrayList(java.util.ArrayList) List(java.util.List) IEditorPart(org.eclipse.ui.IEditorPart) IEditorInput(org.eclipse.ui.IEditorInput) CoreException(org.eclipse.core.runtime.CoreException) ConverterException(net.heartsome.cat.converter.ConverterException) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 4 with XLFHandler

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

the class UpdateTMWizard method performFinish.

public boolean performFinish() {
    final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    final boolean isDraft = page.isDraft();
    final boolean isApproved = page.isApproved();
    final boolean isSignedOff = page.isSignedOff();
    final boolean isTranslated = page.isTranslated();
    final boolean isLocked = page.isLocked();
    if (!isDraft && !isApproved && !isSignedOff && !isTranslated && !isLocked) {
        MessageDialog.openInformation(getShell(), Messages.getString("wizard.UpdateTMWizard.msgTitle"), Messages.getString("wizard.UpdateTMWizard.msg"));
        return false;
    }
    IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
    final int contextSize = ps.getInt(TMPreferenceConstants.CONTEXT_MATCH);
    final int tmxImportStrategy = ps.getInt(TMPreferenceConstants.TM_UPDATE);
    IRunnableWithProgress progress = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) {
            monitor.setTaskName(Messages.getString("dialog.UpdateTMDialog.jobTask1"));
            monitor.beginTask(Messages.getString("dialog.UpdateTMDialog.jobTask1"), lstXLIFF.size() * 2);
            for (final IFile xliffFile : lstXLIFF) {
                ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(xliffFile.getProject());
                DatabaseModelBean modelBean = projectConfig.getDefaultTMDb();
                FileEditorInput editorInput = new FileEditorInput(xliffFile);
                IEditorPart editorPart = activePage.findEditor(editorInput);
                // 选择所有语言
                XLFHandler handler = null;
                if (editorPart != null && editorPart instanceof IXliffEditor) {
                    // xliff 文件已用 XLIFF 编辑器打开
                    IXliffEditor xliffEditor = (IXliffEditor) editorPart;
                    handler = xliffEditor.getXLFHandler();
                } else {
                    // xliff 文件未打开
                    handler = new XLFHandler();
                }
                monitor.subTask(Messages.getString("dialog.UpdateTMDialog.jobTask2"));
                // 修改获取系统用户方式/*System.getProperty("user.name");*/
                String systemUser = PlatformUI.getPreferenceStore().getString(IPreferenceConstants.SYSTEM_USER);
                String[] arrTempTMX = handler.generateTMXToUpdateTM(xliffFile, isApproved, isSignedOff, isTranslated, isDraft, isLocked, contextSize, systemUser);
                monitor.worked(1);
                if (arrTempTMX != null) {
                    monitor.subTask(Messages.getString("dialog.UpdateTMDialog.jobTask3"));
                    // }
                    try {
                        DatabaseService.importTmxWithString(modelBean.toDbMetaData(), arrTempTMX[1], new SubProgressMonitor(monitor, 1), tmxImportStrategy, false, arrTempTMX[0]);
                    } catch (ImportException e) {
                        final String msg = e.getMessage();
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openInformation(getShell(), Messages.getString("dialog.UpdateTMDialog.job.msgTitle"), msg);
                            }
                        });
                        canFinish = false;
                        return;
                    }
                }
            }
            monitor.done();
            // 刷新项目
            ResourceUtils.refreshCurentSelectProject();
            canFinish = true;
        }
    };
    try {
        getContainer().run(true, true, progress);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return canFinish;
}
Also used : IFile(org.eclipse.core.resources.IFile) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ImportException(net.heartsome.cat.common.core.exception.ImportException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProjectConfiger(net.heartsome.cat.ts.core.file.ProjectConfiger) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 5 with XLFHandler

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

the class DeleteAllSegmentTranslationsHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (!(editor instanceof XLIFFEditorImplWithNatTable)) {
        return null;
    }
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    boolean res = MessageDialog.openConfirm(window.getShell(), Messages.getString("handler.DeleteSelectionSegmentTranslationsHandler.msgTitle1"), Messages.getString("handler.DeleteSelectionSegmentTranslationsHandler.msg3"));
    if (res) {
        XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
        try {
            XLFHandler handler = xliffEditor.getXLFHandler();
            int count = xliffEditor.getAllRowCount();
            int[] selectedRows = new int[count];
            for (int i = 0; i < count; i++) {
                selectedRows[i] = i;
            }
            List<Integer> rows = new ArrayList<Integer>();
            boolean exitFlag = false;
            for (int i = 0; i < selectedRows.length; i++) {
                String tgt = handler.getCaseTgtContent(handler.getRowId(selectedRows[i]));
                if (null != tgt) {
                    if (tgt.equals("no")) {
                        exitFlag = true;
                        continue;
                    }
                }
                rows.add(selectedRows[i]);
            }
            if (rows.size() != 0) {
                int columnIndex = xliffEditor.getTgtColumnIndex();
                StyledTextCellEditor cellEditor = HsMultiActiveCellEditor.getTargetStyledEditor();
                if (cellEditor != null && cellEditor.getColumnIndex() == columnIndex) {
                    cellEditor.getSegmentViewer().getTextWidget().forceFocus();
                }
                int[] updateRows = new int[rows.size()];
                for (int i = 0; i < rows.size(); i++) {
                    int ri = rows.get(i);
                    updateRows[i] = ri;
                }
                xliffEditor.updateCells(updateRows, columnIndex, "");
                if (exitFlag) {
                    MessageDialog.openInformation(xliffEditor.getSite().getShell(), Messages.getString("handler.DeleteSelectionSegmentTranslationsHandler.msgTitle2"), Messages.getString("handler.DeleteSelectionSegmentTranslationsHandler.msg2"));
                }
            }
        } catch (Exception e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) ExecutionException(org.eclipse.core.commands.ExecutionException) StyledTextCellEditor(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.StyledTextCellEditor) 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