Search in sources :

Example 1 with NattableWizardDialog

use of net.heartsome.cat.ts.handlexlf.wizard.NattableWizardDialog in project translationstudio8 by heartsome.

the class SplitXliffHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    final String navegatorID = "net.heartsome.cat.common.ui.navigator.view";
    final String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    final Shell shell = window.getShell();
    IFile selectFile = null;
    XLIFFEditorImplWithNatTable xliffEditor = null;
    List<Integer> splitXlfPointsIndex = new LinkedList<Integer>();
    List<String> splitXlfPointsRowId = new LinkedList<String>();
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    // 如果是导航视图,那么就获取导航视图中选中的文件
    if (activePart instanceof IViewPart) {
        if (navegatorID.equals(activePart.getSite().getId())) {
            IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(navegatorID);
            ISelection currentSelection = (StructuredSelection) viewPart.getSite().getSelectionProvider().getSelection();
            if (currentSelection != null && !currentSelection.isEmpty() && currentSelection instanceof IStructuredSelection) {
                IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
                Object object = structuredSelection.getFirstElement();
                if (object instanceof IFile) {
                    selectFile = (IFile) object;
                    String fileExtension = selectFile.getFileExtension();
                    // 如果后缀名不是xlf,那么就退出操作
                    if (fileExtension == null || !CommonFunction.validXlfExtension(fileExtension)) {
                        MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg1"));
                        return null;
                    }
                    FileEditorInput fileInput = new FileEditorInput(selectFile);
                    IEditorReference[] editorRefer = window.getActivePage().findEditors(fileInput, XLIFF_EDITOR_ID, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
                    IEditorPart editorPart = null;
                    if (editorRefer.length >= 1) {
                        editorPart = editorRefer[0].getEditor(true);
                        xliffEditor = (XLIFFEditorImplWithNatTable) editorPart;
                        if (window.getActivePage().getActiveEditor() != editorPart) {
                            window.getActivePage().activate(editorPart);
                        }
                    } else {
                        try {
                            xliffEditor = (XLIFFEditorImplWithNatTable) window.getActivePage().openEditor(fileInput, XLIFF_EDITOR_ID, true, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
                        } catch (PartInitException e) {
                            LOGGER.error("", e);
                            e.printStackTrace();
                        }
                    }
                } else {
                    MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg1"));
                    return null;
                }
            }
        }
    } else if (activePart instanceof IEditorPart) {
        if (XLIFF_EDITOR_ID.equals(activePart.getSite().getId())) {
            xliffEditor = (XLIFFEditorImplWithNatTable) activePart;
            selectFile = ((FileEditorInput) xliffEditor.getEditorInput()).getFile();
        }
    }
    // 根据每个tu节点的rowId获取其具体的位置,才好进行排序
    Map<Integer, String> pointIndexRowIdMap = new HashMap<Integer, String>();
    for (String rowId : xliffEditor.getSplitXliffPoints()) {
        // 获取指定tu节点所处其他结点的序列号
        int tuPostion = xliffEditor.getXLFHandler().getTUPositionByRowId(rowId);
        if (tuPostion >= 1) {
            splitXlfPointsIndex.add(tuPostion);
            pointIndexRowIdMap.put(tuPostion, rowId);
        }
    }
    if (splitXlfPointsIndex.size() <= 0) {
        MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg2"));
        return null;
    }
    // 对切割点集合进行排序
    for (int i = 0; i < splitXlfPointsIndex.size(); i++) {
        int point1 = splitXlfPointsIndex.get(i);
        for (int j = i + 1; j < splitXlfPointsIndex.size(); j++) {
            int point2 = splitXlfPointsIndex.get(j);
            if (point1 > point2) {
                splitXlfPointsIndex.set(i, point2);
                splitXlfPointsIndex.set(j, point1);
                point1 = point2;
            }
        }
    }
    // 向存储rowId的list存放数据,这样的话,所存储的rowId就是经过排序了的。
    for (int i = 0; i < splitXlfPointsIndex.size(); i++) {
        splitXlfPointsRowId.add(pointIndexRowIdMap.get(splitXlfPointsIndex.get(i)));
    }
    SplitOrMergeXlfModel model = new SplitOrMergeXlfModel();
    model.setSplitFile(selectFile);
    model.setSplitXlfPointsIndex(splitXlfPointsIndex);
    model.setSplitXlfPointsRowId(splitXlfPointsRowId);
    model.setXliffEditor(xliffEditor);
    model.setShell(shell);
    SplitXliffWizard wizard = new SplitXliffWizard(model);
    final TSWizardDialog dialog = new NattableWizardDialog(shell, wizard);
    dialog.open();
    return null;
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) IViewPart(org.eclipse.ui.IViewPart) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) SplitOrMergeXlfModel(net.heartsome.cat.ts.handlexlf.split.SplitOrMergeXlfModel) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SplitXliffWizard(net.heartsome.cat.ts.handlexlf.wizard.SplitXliffWizard) Shell(org.eclipse.swt.widgets.Shell) IEditorReference(org.eclipse.ui.IEditorReference) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) PartInitException(org.eclipse.ui.PartInitException) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) NattableWizardDialog(net.heartsome.cat.ts.handlexlf.wizard.NattableWizardDialog) IEditorPart(org.eclipse.ui.IEditorPart) LinkedList(java.util.LinkedList) FileEditorInput(org.eclipse.ui.part.FileEditorInput) TSWizardDialog(net.heartsome.cat.common.ui.wizard.TSWizardDialog)

Example 2 with NattableWizardDialog

use of net.heartsome.cat.ts.handlexlf.wizard.NattableWizardDialog in project translationstudio8 by heartsome.

the class MergeXliffHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    final Shell shell = window.getShell();
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    if (currentSelection != null && !currentSelection.isEmpty() && currentSelection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
        if (structuredSelection.size() < 2) {
            MessageDialog.openInformation(shell, Messages.getString("handler.MergeXliffHandler.msgTitle1"), Messages.getString("handler.MergeXliffHandler.msg1"));
            return null;
        }
        Vector<IFile> seleFiles = new Vector<IFile>();
        String notXlfFile = "";
        @SuppressWarnings("rawtypes") Iterator selectIt = structuredSelection.iterator();
        while (selectIt.hasNext()) {
            Object object = selectIt.next();
            if (object instanceof IFile) {
                IFile selectFile = (IFile) object;
                String fileExtension = selectFile.getFileExtension();
                // 如果后缀名不是xlf,那么就进行提示
                if (fileExtension == null || !CommonFunction.validXlfExtension(fileExtension)) {
                    notXlfFile += selectFile.getFullPath().toOSString() + ",";
                }
                seleFiles.add(selectFile);
            }
        }
        if (notXlfFile.length() > 0) {
            notXlfFile = notXlfFile.substring(0, notXlfFile.length() - 1);
            boolean isSure = MessageDialog.openConfirm(shell, Messages.getString("handler.MergeXliffHandler.msgTitle2"), MessageFormat.format(Messages.getString("handler.MergeXliffHandler.msg2"), new Object[] { notXlfFile }));
            if (!isSure) {
                return null;
            }
        }
        List<IFile> lstFiles = new ArrayList<IFile>();
        XLFValidator.resetFlag();
        for (IFile iFile : seleFiles) {
            if (!XLFValidator.validateXliffFile(iFile)) {
                lstFiles.add(iFile);
            }
        }
        XLFValidator.resetFlag();
        seleFiles.removeAll(lstFiles);
        if (seleFiles.size() == 0) {
            return null;
        }
        if (seleFiles.size() > 0) {
            String projectPath = seleFiles.get(0).getProject().getFullPath().toOSString();
            for (int i = 1; i < seleFiles.size(); i++) {
                if (!projectPath.equals(seleFiles.get(i).getProject().getFullPath().toOSString())) {
                    MessageDialog.openInformation(shell, Messages.getString("handler.MergeXliffHandler.msgTitle1"), Messages.getString("handler.MergeXliffHandler.msg3"));
                    return null;
                }
            }
            SplitOrMergeXlfModel model = new SplitOrMergeXlfModel();
            model.setMergeXliffFile(seleFiles);
            MergeXliffWizard wizard = new MergeXliffWizard(model);
            TSWizardDialog dialog = new NattableWizardDialog(shell, wizard);
            dialog.open();
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) SplitOrMergeXlfModel(net.heartsome.cat.ts.handlexlf.split.SplitOrMergeXlfModel) NattableWizardDialog(net.heartsome.cat.ts.handlexlf.wizard.NattableWizardDialog) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Shell(org.eclipse.swt.widgets.Shell) ISelection(org.eclipse.jface.viewers.ISelection) Iterator(java.util.Iterator) Vector(java.util.Vector) TSWizardDialog(net.heartsome.cat.common.ui.wizard.TSWizardDialog) MergeXliffWizard(net.heartsome.cat.ts.handlexlf.wizard.MergeXliffWizard)

Aggregations

TSWizardDialog (net.heartsome.cat.common.ui.wizard.TSWizardDialog)2 SplitOrMergeXlfModel (net.heartsome.cat.ts.handlexlf.split.SplitOrMergeXlfModel)2 NattableWizardDialog (net.heartsome.cat.ts.handlexlf.wizard.NattableWizardDialog)2 IFile (org.eclipse.core.resources.IFile)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 Shell (org.eclipse.swt.widgets.Shell)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Vector (java.util.Vector)1 MergeXliffWizard (net.heartsome.cat.ts.handlexlf.wizard.MergeXliffWizard)1 SplitXliffWizard (net.heartsome.cat.ts.handlexlf.wizard.SplitXliffWizard)1 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IEditorReference (org.eclipse.ui.IEditorReference)1 IViewPart (org.eclipse.ui.IViewPart)1