Search in sources :

Example 1 with ConverterViewModel

use of net.heartsome.cat.convert.ui.model.ConverterViewModel in project translationstudio8 by heartsome.

the class OpenConversionDialogHandler method execute.

@Override
public Object execute(ExecutionEvent event, List<IFile> list) {
    CommonFunction.removeRepeateSelect(list);
    if (list == null || list.size() == 0) {
        MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.OpenConversionDialogHandler.msgTitle1"), Messages.getString("handler.OpenConversionDialogHandler.msg1"));
        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_POSITIVE);
        // 记住所选择的文件
        converterViewModel.setConversionItem(sourceItem);
        converterViewModels.add(converterViewModel);
    }
    IProject project = list.get(0).getProject();
    ConversionWizard wizard = new ConversionWizard(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();
        final List<IFile> targetFiles = new ArrayList<IFile>();
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.setTaskName(Messages.getString("handler.OpenConversionDialogHandler.task1"));
                monitor.beginTask(Messages.getString("handler.OpenConversionDialogHandler.task2"), models.size());
                for (ConverterViewModel converterViewModel : models) {
                    try {
                        IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
                        subMonitor.setTaskName(Messages.getString("handler.OpenConversionDialogHandler.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
                        converterViewModel.convertWithoutJob(subMonitor);
                        List<File> tgtFileList = converterViewModel.getGenerateTgtFileList();
                        for (File f : tgtFileList) {
                            IFile tgtIfile = ConverterUtil.localPath2IFile(f.getAbsolutePath());
                            if (tgtIfile != null) {
                                targetFiles.add(tgtIfile);
                            }
                        }
                        // 若新转换的 xliff 文件重复,那么关闭已经打开的重复文件,防止文件同步冲突	robert	2013-04-01
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                CommonFunction.closePointEditor(targetFiles);
                            }
                        });
                    } catch (ConverterException e) {
                        final String message = e.getMessage();
                        LOGGER.error("", e);
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openInformation(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), message);
                            }
                        });
                    } catch (Exception e) {
                        final String message = e.getMessage();
                        LOGGER.error("", e);
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openInformation(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), message);
                            }
                        });
                    }
                }
                monitor.done();
            }
        };
        try {
            new ProgressMonitorDialog(shell).run(true, true, runnable);
        } catch (InvocationTargetException e) {
            MessageDialog.openError(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), e.getMessage());
            LOGGER.error("", e);
        } catch (InterruptedException e) {
            MessageDialog.openError(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), e.getMessage());
            LOGGER.error(e.getMessage());
        }
        if (wizard.isOpenPreTranslation()) {
            // 加载转换器扩展
            final IExecutePretranslation[] impls = new IExecutePretranslation[1];
            IConfigurationElement[] config2 = Platform.getExtensionRegistry().getConfigurationElementsFor("net.heartsome.converter.extension.pretranslation");
            try {
                for (IConfigurationElement e : config2) {
                    final Object o = e.createExecutableExtension("class");
                    if (o instanceof IExecutePretranslation) {
                        ISafeRunnable runnable1 = new ISafeRunnable() {

                            public void handleException(Throwable exception) {
                                LOGGER.error(Messages.getString("handler.OpenConversionDialogHandler.logger1"), exception);
                            }

                            public void run() throws Exception {
                                impls[0] = (IExecutePretranslation) o;
                            }
                        };
                        SafeRunner.run(runnable1);
                    }
                }
            } catch (CoreException ex) {
                LOGGER.error(Messages.getString("handler.OpenConversionDialogHandler.logger1"), ex);
            }
            if (impls[0] != null) {
                if (targetFiles.size() < 1) {
                    return null;
                }
                impls[0].executePreTranslation(targetFiles);
            }
        }
    }
    return null;
}
Also used : ConverterException(net.heartsome.cat.converter.ConverterException) IFile(org.eclipse.core.resources.IFile) ConversionWizard(net.heartsome.cat.convert.ui.wizard.ConversionWizard) ArrayList(java.util.ArrayList) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IExecutePretranslation(net.heartsome.cat.convert.extenstion.IExecutePretranslation) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) Composite(org.eclipse.swt.widgets.Composite) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) ConverterException(net.heartsome.cat.converter.ConverterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem) TSWizardDialog(net.heartsome.cat.common.ui.wizard.TSWizardDialog) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 2 with ConverterViewModel

use of net.heartsome.cat.convert.ui.model.ConverterViewModel 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 3 with ConverterViewModel

use of net.heartsome.cat.convert.ui.model.ConverterViewModel 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 4 with ConverterViewModel

use of net.heartsome.cat.convert.ui.model.ConverterViewModel 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 5 with ConverterViewModel

use of net.heartsome.cat.convert.ui.model.ConverterViewModel in project translationstudio8 by heartsome.

the class ConversionWizardPage method validate.

private void validate() {
    if (conversionConfigBeans.size() == 0) {
        setPageComplete(false);
        return;
    }
    ConversionConfigBean cb = conversionConfigBeans.get(0);
    if (cb.getHasSelTgtLangList() == null || cb.getHasSelTgtLangList().size() == 0) {
        setPageComplete(false);
        setErrorMessage(Messages.getString("wizard.ConversionWizardPage.msg1"));
        return;
    }
    IStatus result = Status.OK_STATUS;
    int line = 1;
    for (ConverterViewModel converterViewModel : converterViewModels) {
        result = converterViewModel.validate();
        if (!result.isOK()) {
            break;
        }
        line++;
    }
    if (!result.isOK()) {
        filesTable.setSelection(line - 1);
        filesTable.notifyListeners(SWT.Selection, null);
        setPageComplete(false);
        setErrorMessage(MessageFormat.format(Messages.getString("wizard.ConversionWizardPage.msg2"), line) + result.getMessage());
    } else {
        setErrorMessage(null);
        setPageComplete(true);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel)

Aggregations

ConverterViewModel (net.heartsome.cat.convert.ui.model.ConverterViewModel)12 IConversionItem (net.heartsome.cat.convert.ui.model.IConversionItem)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArrayList (java.util.ArrayList)5 ConverterException (net.heartsome.cat.converter.ConverterException)5 IFile (org.eclipse.core.resources.IFile)5 TSWizardDialog (net.heartsome.cat.common.ui.wizard.TSWizardDialog)4 ConversionConfigBean (net.heartsome.cat.convert.ui.model.ConversionConfigBean)4 CoreException (org.eclipse.core.runtime.CoreException)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)4 Composite (org.eclipse.swt.widgets.Composite)4 ConversionWizard (net.heartsome.cat.convert.ui.wizard.ConversionWizard)3 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 IProject (org.eclipse.core.resources.IProject)3 IStatus (org.eclipse.core.runtime.IStatus)3 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)3 List (java.util.List)2 ReverseConversionWizard (net.heartsome.cat.convert.ui.wizard.ReverseConversionWizard)2