Search in sources :

Example 1 with ConversionWizard

use of net.heartsome.cat.convert.ui.wizard.ConversionWizard 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 ConversionWizard

use of net.heartsome.cat.convert.ui.wizard.ConversionWizard in project translationstudio8 by heartsome.

the class IConverterCallerImpl method openConverter.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.ts.ui.extensionpoint.IConverterCaller#openConverter(java.util.List)
	 */
public void openConverter(List<IFile> files) {
    if (files == null || files.size() == 0) {
        return;
    }
    final Shell shell = Display.getCurrent().getActiveShell();
    ArrayList<IFile> list = new ArrayList<IFile>();
    ArrayList<IFile> wrongFiles = new ArrayList<IFile>();
    for (IFile file : files) {
        String fileExtension = file.getFileExtension();
        if (getLegalFileExtensions() == null || getLegalFileExtensions().length == 0) {
            // 未限制后缀名的情况
            list.add(file);
        } else {
            // 限制了后缀名的情况
            if (fileExtension == null) {
                // 无后缀名的文件
                fileExtension = "";
            }
            if (CommonFunction.containsIgnoreCase(getLegalFileExtensions(), fileExtension)) {
                list.add(file);
            } else {
                wrongFiles.add(file);
            }
        }
    }
    if (!wrongFiles.isEmpty()) {
        StringBuffer msg = new StringBuffer(Messages.getString("extension.IConverterCallerImpl.msg1"));
        for (IFile iFile : wrongFiles) {
            msg.append("\n").append(iFile.getFullPath().toOSString());
        }
        if (!MessageDialog.openConfirm(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle1"), msg.toString())) {
            return;
        }
    }
    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);
    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("extension.IConverterCallerImpl.task1"));
                monitor.beginTask(Messages.getString("extension.IConverterCallerImpl.task2"), models.size());
                for (ConverterViewModel converterViewModel : models) {
                    try {
                        IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
                        subMonitor.setTaskName(Messages.getString("extension.IConverterCallerImpl.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
                        converterViewModel.convertWithoutJob(subMonitor);
                    } catch (ConverterException e) {
                        final String message = e.getMessage();
                        LOGGER.error("", e);
                        Display.getDefault().syncExec(new Runnable() {

                            public void run() {
                                MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.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("extension.IConverterCallerImpl.msgTitle2"), message);
                            }
                        });
                    }
                }
                monitor.done();
            }
        };
        try {
            new ProgressMonitorDialog(shell).run(true, true, runnable);
        } catch (InvocationTargetException e) {
            MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), e.getMessage());
            LOGGER.error("", e);
        } catch (InterruptedException e) {
            MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), e.getMessage());
            LOGGER.error(e.getMessage());
        }
    }
}
Also used : ConverterException(net.heartsome.cat.converter.ConverterException) IFile(org.eclipse.core.resources.IFile) ConversionWizard(net.heartsome.cat.convert.ui.wizard.ConversionWizard) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) ConverterException(net.heartsome.cat.converter.ConverterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Shell(org.eclipse.swt.widgets.Shell) 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)

Example 3 with ConversionWizard

use of net.heartsome.cat.convert.ui.wizard.ConversionWizard in project translationstudio8 by heartsome.

the class ConversionHandler method execute.

/**
	 * the command has been executed, so extract extract the needed information from the application context.
	 */
public Object execute(ExecutionEvent event) throws ExecutionException {
    String commandId = event.getCommand().getId();
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    if (currentSelection.isEmpty()) {
        return null;
    }
    if (currentSelection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
        Object object = structuredSelection.getFirstElement();
        // 通过 adapter manager 获得 conversion item
        Object adapter = Platform.getAdapterManager().getAdapter(object, IConversionItem.class);
        if (adapter instanceof IConversionItem) {
            // open the config conversion dialog
            ConverterViewModel converterViewModel = null;
            IConversionItem sourceItem = (IConversionItem) adapter;
            if (commandId.equals("net.heartsome.cat.convert.ui.commands.convertCommand")) {
                converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_POSITIVE);
                // 记住所选择的文件
                converterViewModel.setConversionItem(sourceItem);
                IWizard wizard = new ConversionWizard(Arrays.asList(converterViewModel), null);
                TSWizardDialog dialog = new ConversionWizardDialog(window.getShell(), wizard);
                int result = dialog.open();
                if (result == IDialogConstants.OK_ID) {
                    converterViewModel.convert();
                }
            } else {
                converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_REVERSE);
                converterViewModel.setConversionItem(sourceItem);
                IWizard wizard = new ReverseConversionWizard(Arrays.asList(converterViewModel), null);
                TSWizardDialog dialog = new ConversionWizardDialog(window.getShell(), wizard);
                int result = dialog.open();
                if (result == IDialogConstants.OK_ID) {
                    converterViewModel.convert();
                }
            }
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ReverseConversionWizard(net.heartsome.cat.convert.ui.wizard.ReverseConversionWizard) ConversionWizard(net.heartsome.cat.convert.ui.wizard.ConversionWizard) IWizard(org.eclipse.jface.wizard.IWizard) ConversionWizardDialog(net.heartsome.cat.convert.ui.wizard.ConversionWizardDialog) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ReverseConversionWizard(net.heartsome.cat.convert.ui.wizard.ReverseConversionWizard) ISelection(org.eclipse.jface.viewers.ISelection) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem) TSWizardDialog(net.heartsome.cat.common.ui.wizard.TSWizardDialog)

Aggregations

TSWizardDialog (net.heartsome.cat.common.ui.wizard.TSWizardDialog)3 ConverterViewModel (net.heartsome.cat.convert.ui.model.ConverterViewModel)3 IConversionItem (net.heartsome.cat.convert.ui.model.IConversionItem)3 ConversionWizard (net.heartsome.cat.convert.ui.wizard.ConversionWizard)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 ConverterException (net.heartsome.cat.converter.ConverterException)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 File (java.io.File)1 IExecutePretranslation (net.heartsome.cat.convert.extenstion.IExecutePretranslation)1 ConversionWizardDialog (net.heartsome.cat.convert.ui.wizard.ConversionWizardDialog)1 ReverseConversionWizard (net.heartsome.cat.convert.ui.wizard.ReverseConversionWizard)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 ISelection (org.eclipse.jface.viewers.ISelection)1