Search in sources :

Example 1 with SegPointBean

use of net.heartsome.cat.ts.core.bean.SegPointBean in project translationstudio8 by heartsome.

the class SetSplitPointByWordNumDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite tparent = (Composite) super.createDialogArea(parent);
    GridData parentData = new GridData(SWT.FILL, SWT.FILL, true, true);
    parentData.widthHint = 400;
    parentData.heightHint = 350;
    tparent.setLayoutData(parentData);
    GridLayoutFactory.swtDefaults().numColumns(1).applyTo(tparent);
    Group typeGroup = new Group(tparent, SWT.NONE);
    typeGroup.setText(Messages.getString("SetSplitPointByWordNumDialog.splitType"));
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(typeGroup);
    GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(true).applyTo(typeGroup);
    averageBtn = new Button(typeGroup, SWT.RADIO);
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(averageBtn);
    averageBtn.setText(Messages.getString("SetSplitPointByWordNumDialog.splitByAverWordNum"));
    averageBtn.setSelection(true);
    wordNumberBtn = new Button(typeGroup, SWT.RADIO);
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(wordNumberBtn);
    wordNumberBtn.setText(Messages.getString("SetSplitPointByWordNumDialog.splitByFixWordNum"));
    // >显示总字数的面板
    Composite allNumCmp = new Composite(tparent, SWT.NONE);
    GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(allNumCmp);
    GridLayoutFactory.swtDefaults().numColumns(2).applyTo(allNumCmp);
    Label countLbl = new Label(allNumCmp, SWT.NONE);
    countLbl.setText(Messages.getString("SetSplitPointByWordNumDialog.curFileTotalWordNum"));
    countLbl.setFont(totalLblFont);
    Label countValueLbl = new Label(allNumCmp, SWT.NONE);
    countValueLbl.setText(totalWordNum + "");
    countValueLbl.setFont(totalLblFont);
    // >两个面板的父面板
    Composite contentCmp = new Composite(tparent, SWT.NONE);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(contentCmp);
    contentCmp.setLayout(stack);
    // >>平均字数分割方式的面板
    averageContentCmp = new Composite(contentCmp, SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(averageContentCmp);
    GridLayoutFactory.swtDefaults().numColumns(2).applyTo(averageContentCmp);
    Label segLbl = new Label(averageContentCmp, SWT.NONE);
    segLbl.setText(Messages.getString("SetSplitPointByWordNumDialog.segNumLbl"));
    segSpiner = new Spinner(averageContentCmp, SWT.BORDER);
    segSpiner.setIncrement(1);
    segSpiner.setMinimum(2);
    segSpiner.setMaximum(totalTuNum - 1);
    segSpiner.setSelection(segList.size());
    segSpiner.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent arg0) {
            String text = segSpiner.getText();
            if (!(text.matches("\\d{1,}") && Integer.parseInt(text) <= (totalTuNum - 1))) {
                segSpiner.setSelection(2);
            }
        }
    });
    GridDataFactory.swtDefaults().hint(50, SWT.DEFAULT).applyTo(segSpiner);
    // >>按指定字数分割的面板
    wordNumberScroll = new ScrolledComposite(contentCmp, SWT.NONE | SWT.V_SCROLL | SWT.BORDER);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(wordNumberScroll);
    GridLayoutFactory.swtDefaults().numColumns(1).applyTo(wordNumberScroll);
    wordNumberScroll.setExpandHorizontal(true);
    wordNumberScroll.setExpandVertical(true);
    segParentCmp = new Composite(wordNumberScroll, SWT.NONE);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(segParentCmp);
    GridLayoutFactory.swtDefaults().spacing(SWT.DEFAULT, 0).numColumns(1).applyTo(segParentCmp);
    wordNumberScroll.setContent(segParentCmp);
    averageBtn.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            viewerTopControl();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            viewerTopControl();
        }
    });
    wordNumberBtn.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            viewerTopControl();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            viewerTopControl();
        }
    });
    stack.topControl = averageContentCmp;
    // 开始创建片断列表
    for (int i = 0; i < segList.size(); i++) {
        SegPointBean bean = segList.get(i);
        createSegCmp(bean);
    }
    wordNumberScroll.setMinSize(segParentCmp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    segParentCmp.layout();
    wordNumberScroll.layout();
    return tparent;
}
Also used : Group(org.eclipse.swt.widgets.Group) SegPointBean(net.heartsome.cat.ts.core.bean.SegPointBean) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ModifyListener(org.eclipse.swt.events.ModifyListener) Spinner(org.eclipse.swt.widgets.Spinner) Label(org.eclipse.swt.widgets.Label) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 2 with SegPointBean

use of net.heartsome.cat.ts.core.bean.SegPointBean in project translationstudio8 by heartsome.

the class SetSplitPointByWordNumDialog method okPressed.

@Override
protected void okPressed() {
    segList.clear();
    if (averageBtn.getSelection()) {
        // 分配字数时,将多余的字往后分
        int averageWordNum = totalWordNum / segSpiner.getSelection();
        int segWordNum = 0;
        for (Entry<String, Integer> entry : rowWordNumMap.entrySet()) {
            if (segWordNum < averageWordNum) {
                segWordNum += entry.getValue();
                if (segWordNum >= averageWordNum) {
                    segList.add(new SegPointBean(entry.getKey()));
                    segWordNum = 0;
                }
            }
        }
    } else {
        // UNDO 首先验证总字数是否超标,这里以后会改动,比如用户输入以后,强制进行提示
        int addedWordNum = 0;
        int curWordNum = 0;
        List<Integer> segWordNumList = new ArrayList<Integer>();
        for (Control control : segParentCmp.getChildren()) {
            if (control instanceof Composite) {
                Control childControl = ((Composite) control).getChildren()[2];
                if (childControl instanceof Text) {
                    curWordNum = Integer.parseInt(((Text) childControl).getText());
                    if (curWordNum > 0) {
                        addedWordNum += curWordNum;
                        segWordNumList.add(curWordNum);
                    }
                }
            }
        }
        if (addedWordNum > totalWordNum) {
            MessageDialog.openInformation(getShell(), Messages.getString("all.dialog.info"), Messages.getString("SetSplitPointByWordNumDialog.msg.wordNumError"));
            return;
        }
        // 将剩下的字数算到最后一个文本段中
        segWordNumList.add(totalWordNum - addedWordNum);
        int i = 0;
        curWordNum = segWordNumList.get(i);
        addedWordNum = 0;
        for (Entry<String, Integer> entry : rowWordNumMap.entrySet()) {
            if (addedWordNum < curWordNum) {
                addedWordNum += entry.getValue();
                if (addedWordNum >= curWordNum) {
                    segList.add(new SegPointBean(entry.getKey()));
                    i++;
                    curWordNum = segWordNumList.get(i);
                    addedWordNum = 0;
                }
            }
        }
    }
    for (int i = 0; i < segList.size(); i++) {
        SegPointBean bean = segList.get(i);
        if (bean.getRowId() != null && !bean.getRowId().isEmpty()) {
            // 最后一个文本段不允许添加分割点
            String lastTURowid = xlfHandler.getRowIdByXpath(xlfPath, "/xliff/file[last()]/body/descendant::trans-unit[last()]");
            if (lastTURowid.equals(bean.getRowId())) {
                segList.remove(i);
                i--;
            }
        }
    }
    if (segList.size() <= 0) {
        MessageDialog.openInformation(getShell(), Messages.getString("all.dialog.info"), Messages.getString("SetSplitPointHandler.cantSetPoint"));
        return;
    }
    super.okPressed();
}
Also used : SegPointBean(net.heartsome.cat.ts.core.bean.SegPointBean) Control(org.eclipse.swt.widgets.Control) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ArrayList(java.util.ArrayList) Text(org.eclipse.swt.widgets.Text)

Example 3 with SegPointBean

use of net.heartsome.cat.ts.core.bean.SegPointBean in project translationstudio8 by heartsome.

the class SetSplitPointByWordNumDialog method createSegCmp.

/**
	 * 创建片段
	 * @param bean
	 */
private void createSegCmp(SegPointBean bean) {
    final Composite curSegCmp = new Composite(segParentCmp, SWT.NONE);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(curSegCmp);
    GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(5).applyTo(curSegCmp);
    // 第一列,一个label
    Label segLbl = new Label(curSegCmp, SWT.NONE);
    segLbl.setText(Messages.getString("SetSplitPointByWordNumDialog.segLbl"));
    Label segIndexLbl = new Label(curSegCmp, SWT.LEFT);
    segIndexLbl.setText("");
    GridDataFactory.swtDefaults().align(SWT.LEFT, SWT.CENTER).hint(20, SWT.DEFAULT).applyTo(segIndexLbl);
    final Text wordNumTxt = new Text(curSegCmp, SWT.BORDER);
    GridDataFactory.swtDefaults().hint(60, SWT.DEFAULT).applyTo(wordNumTxt);
    wordNumTxt.setText(bean.getWordNumber() + "");
    wordNumTxt.addVerifyListener(new VerifyListener() {

        public void verifyText(VerifyEvent e) {
            String addText = e.text;
            if (addText.length() > 0) {
                e.doit = addText.matches("\\d+");
            }
        }
    });
    Button addBtn = new Button(curSegCmp, SWT.NONE);
    addBtn.setImage(addImg);
    Button deleteBtn = new Button(curSegCmp, SWT.NONE);
    deleteBtn.setImage(deleteImg);
    addBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            int addedWordNum = 0;
            for (Control control : segParentCmp.getChildren()) {
                if (control instanceof Composite) {
                    Control childControl = ((Composite) control).getChildren()[2];
                    if (childControl instanceof Text) {
                        addedWordNum += Integer.parseInt(((Text) childControl).getText());
                    }
                }
            }
            SegPointBean addBean = new SegPointBean((totalWordNum - addedWordNum) > 0 ? (totalWordNum - addedWordNum) : 0);
            // 开始创建片断列表
            createSegCmp(addBean);
            wordNumberScroll.setMinSize(segParentCmp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            segParentCmp.layout();
            wordNumberScroll.layout();
        }
    });
    deleteBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (e.getSource() instanceof Button) {
                Composite parentCmp = ((Button) e.getSource()).getParent();
                parentCmp.dispose();
                wordNumberScroll.setMinSize(segParentCmp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                segParentCmp.layout();
                wordNumberScroll.layout();
                refreshSegListArea();
            }
        }
    });
    refreshSegListArea();
}
Also used : SegPointBean(net.heartsome.cat.ts.core.bean.SegPointBean) Control(org.eclipse.swt.widgets.Control) VerifyListener(org.eclipse.swt.events.VerifyListener) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) Button(org.eclipse.swt.widgets.Button) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Text(org.eclipse.swt.widgets.Text) VerifyEvent(org.eclipse.swt.events.VerifyEvent)

Example 4 with SegPointBean

use of net.heartsome.cat.ts.core.bean.SegPointBean in project translationstudio8 by heartsome.

the class SetSplitPointByWordNumHandler 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;
    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("all.dialog.info"), Messages.getString("SetSplitPointHandler.cantSetPointToNotXlf"));
                    return null;
                }
            }
        }
    } else if (activePart instanceof IEditorPart) {
        if (XLIFF_EDITOR_ID.equals(activePart.getSite().getId())) {
            xliffEditor = (XLIFFEditorImplWithNatTable) activePart;
            if (xliffEditor.isMultiFile()) {
                MessageDialog.openInformation(shell, Messages.getString("all.dialog.info"), Messages.getString("SetSplitPointByWOrdNumHandler.cantSetForMulty"));
                return null;
            }
            selectFile = ((FileEditorInput) xliffEditor.getEditorInput()).getFile();
        }
    }
    if (selectFile == null) {
        return null;
    }
    XLIFFEditorImplWithNatTable nattable = XLIFFEditorImplWithNatTable.getCurrent();
    final XLFHandler xlfHander = nattable.getXLFHandler();
    Map<String, Integer> rowWordNumMap = xlfHander.getSplitFileInfoForPointSetting(selectFile.getLocation().toOSString());
    if (rowWordNumMap.size() <= 1) {
        MessageDialog.openInformation(shell, Messages.getString("all.dialog.info"), Messages.getString("SetSplitPointHandler.cantSetPointForTuNum"));
        return null;
    }
    List<String> splitPointList = nattable.getSplitXliffPoints();
    // 排序之前先过滤重复
    HashSet<String> set = new HashSet<String>(splitPointList);
    splitPointList.clear();
    splitPointList.addAll(set);
    // 先对 splitPointList 进行排序
    Collections.sort(splitPointList, new Comparator<String>() {

        public int compare(String rowId1, String rowId2) {
            int rowIndex1 = xlfHander.getRowIndex(rowId1);
            int rowIndex2 = xlfHander.getRowIndex(rowId2);
            return rowIndex1 > rowIndex2 ? 1 : -1;
        }
    });
    List<SegPointBean> segPointList = new ArrayList<SegPointBean>();
    if (splitPointList.size() > 0) {
        int segWordNum = 0;
        int i = 0;
        String pointRowId = splitPointList.get(i);
        for (Entry<String, Integer> entry : rowWordNumMap.entrySet()) {
            segWordNum += entry.getValue();
            if (pointRowId.equals(entry.getKey())) {
                segPointList.add(new SegPointBean(segWordNum));
                segWordNum = 0;
                i++;
                if (i < splitPointList.size()) {
                    pointRowId = splitPointList.get(i);
                }
            }
        }
        if (splitPointList.size() + 1 > segPointList.size()) {
            segPointList.add(new SegPointBean(segWordNum));
        }
    }
    SetSplitPointByWordNumDialog dialog = new SetSplitPointByWordNumDialog(shell, xlfHander, selectFile.getLocation().toOSString(), rowWordNumMap, segPointList);
    int openResult = dialog.open();
    if (openResult == IDialogConstants.OK_ID) {
        // 如果选择确定,那么在界面上生成分割点
        nattable.getSplitXliffPoints().clear();
        for (SegPointBean bean : segPointList) {
            if (bean.getRowId() != null && !bean.getRowId().isEmpty()) {
                nattable.getSplitXliffPoints().add(bean.getRowId());
            }
        }
        nattable.refresh();
    }
    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) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) 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) SetSplitPointByWordNumDialog(net.heartsome.cat.ts.handlexlf.dialog.SetSplitPointByWordNumDialog) HashSet(java.util.HashSet) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SegPointBean(net.heartsome.cat.ts.core.bean.SegPointBean) IEditorPart(org.eclipse.ui.IEditorPart) FileEditorInput(org.eclipse.ui.part.FileEditorInput) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Aggregations

SegPointBean (net.heartsome.cat.ts.core.bean.SegPointBean)4 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)3 Composite (org.eclipse.swt.widgets.Composite)3 ArrayList (java.util.ArrayList)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Button (org.eclipse.swt.widgets.Button)2 Control (org.eclipse.swt.widgets.Control)2 Label (org.eclipse.swt.widgets.Label)2 Text (org.eclipse.swt.widgets.Text)2 HashSet (java.util.HashSet)1 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)1 SetSplitPointByWordNumDialog (net.heartsome.cat.ts.handlexlf.dialog.SetSplitPointByWordNumDialog)1 XLIFFEditorImplWithNatTable (net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable)1 IFile (org.eclipse.core.resources.IFile)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1