Search in sources :

Example 6 with ConverterViewModel

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

the class ReverseConversionWizardPage method initData.

/**
	 * 初始化本页面需要的数据 ;
	 */
private void initData() {
    ReverseConversionWizard wizard = (ReverseConversionWizard) getWizard();
    converterViewModels = wizard.getConverterViewModels();
    conversionConfigBeans = new ArrayList<ConversionConfigBean>();
    for (ConverterViewModel converterViewModel : converterViewModels) {
        IConversionItem conversionItem = converterViewModel.getConversionItem();
        String xliff = ResourceUtils.toWorkspacePath(conversionItem.getLocation());
        ConversionConfigBean bean = converterViewModel.getConfigBean();
        bean.setSource(xliff);
        conversionConfigBeans.add(bean);
    }
}
Also used : ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem)

Example 7 with ConverterViewModel

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

the class ReverseConversionWizardPage method validate.

/**
	 * 验证方法 ;
	 */
private void validate() {
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            monitor.setTaskName(Messages.getString("wizard.ReverseConversionWizardPage.task1"));
            monitor.beginTask(Messages.getString("wizard.ReverseConversionWizardPage.task2"), converterViewModels.size());
            IStatus result = Status.OK_STATUS;
            int line = 1;
            for (ConverterViewModel converterViewModel : converterViewModels) {
                String xliff = converterViewModel.getConversionItem().getLocation().toOSString();
                IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
                result = converterViewModel.validateXliffFile(xliff, xlfHandler, subMonitor);
                if (!result.isOK()) {
                    break;
                }
                result = converterViewModel.validate();
                if (!result.isOK()) {
                    break;
                }
                line++;
            }
            monitor.done();
            final IStatus validateResult = result;
            final int i = line;
            if (validateResult.isOK()) {
                complete = true;
                errorMessage = null;
            } else {
                complete = false;
                errorMessage = MessageFormat.format(Messages.getString("wizard.ReverseConversionWizardPage.msg1"), i) + validateResult.getMessage();
            }
        }
    };
    try {
        // 验证 xliff 文件比较耗时,需把他放到后台线程进行处理。
        // new ProgressMonitorDialog(shell).run(true, true, runnable);
        getContainer().run(true, true, runnable);
        setErrorMessage(errorMessage);
        setPageComplete(complete);
    } catch (InvocationTargetException e) {
        LOGGER.error("", e);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) Point(org.eclipse.swt.graphics.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 8 with ConverterViewModel

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

the class View method createPartControl.

public void createPartControl(Composite parent) {
    positiveConverterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_POSITIVE);
    reverseConverterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_REVERSE);
    GridLayout layout = new GridLayout(2, true);
    parent.setLayout(layout);
    Composite left = new Composite(parent, SWT.NONE);
    left.setLayout(new GridLayout(2, false));
    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.grabExcessHorizontalSpace = true;
    left.setLayoutData(gridData);
    positiveSupportList = createConvertControl("Convert from Suport format to Xliff", left, true);
    Composite right = new Composite(parent, SWT.NONE);
    right.setLayout(new GridLayout(2, false));
    gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.grabExcessHorizontalSpace = true;
    right.setLayoutData(gridData);
    reverseSupportList = createConvertControl("Convert from Xliff to Support format", right, false);
    bindValue();
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) GridData(org.eclipse.swt.layout.GridData)

Example 9 with ConverterViewModel

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

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

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