Search in sources :

Example 26 with ISelection

use of org.eclipse.jface.viewers.ISelection in project translationstudio8 by heartsome.

the class MergeXliffWizardPage method createMergeXlfGroup.

public void createMergeXlfGroup(Composite tparent) {
    final Group xliffDataGroup = new Group(tparent, SWT.NONE);
    GridLayoutFactory.fillDefaults().numColumns(2).margins(8, 8).applyTo(xliffDataGroup);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(xliffDataGroup);
    xliffDataGroup.setText(Messages.getString("wizard.MergeXliffWizardPage.xliffDataGroup"));
    tableViewer = new TableViewer(xliffDataGroup, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
    final Table table = tableViewer.getTable();
    GridData tableData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
    tableData.heightHint = 50;
    table.setLayoutData(tableData);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    String[] columnNames = new String[] { Messages.getString("wizard.MergeXliffWizardPage.columnNames1"), Messages.getString("wizard.MergeXliffWizardPage.columnNames2") };
    int[] columnAlignments = new int[] { SWT.LEFT, SWT.LEFT };
    for (int i = 0; i < columnNames.length; i++) {
        TableColumn tableColumn = new TableColumn(table, columnAlignments[i]);
        tableColumn.setText(columnNames[i]);
    }
    tableViewer.setLabelProvider(new TableViewerLabelProvider());
    tableViewer.setContentProvider(new ArrayContentProvider());
    tableViewer.setInput(getSplitTableInfos());
    validXlf();
    // 让列表列宽动态变化
    table.addListener(SWT.Resize, new Listener() {

        public void handleEvent(Event event) {
            final Table table = ((Table) event.widget);
            final TableColumn[] columns = table.getColumns();
            event.widget.getDisplay().syncExec(new Runnable() {

                public void run() {
                    double[] columnWidths = new double[] { 0.15, 0.75 };
                    for (int i = 0; i < columns.length; i++) columns[i].setWidth((int) (table.getBounds().width * columnWidths[i]));
                }
            });
        }
    });
    Composite buttonComp = new Composite(xliffDataGroup, SWT.None);
    GridLayoutFactory.fillDefaults().numColumns(1).margins(8, 8).applyTo(buttonComp);
    GridDataFactory.fillDefaults().grab(false, true).hint(100, SWT.DEFAULT).applyTo(buttonComp);
    Button addbutton = new Button(buttonComp, SWT.NONE);
    addbutton.setText(Messages.getString("wizard.MergeXliffWizardPage.addbutton"));
    addbutton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    addbutton.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            FileFolderSelectionDialog dialog = new FileFolderSelectionDialog(xliffDataGroup.getShell(), true, IResource.FILE);
            dialog.setMessage(Messages.getString("wizard.MergeXliffWizardPage.dialogMsg"));
            dialog.setTitle(Messages.getString("wizard.MergeXliffWizardPage.dialogTitle"));
            dialog.setDoubleClickSelects(true);
            try {
                dialog.setInput(EFS.getStore(root.getLocationURI()));
            } catch (CoreException e1) {
                LOGGER.error("", e1);
                e1.printStackTrace();
            }
            dialog.addFilter(new ViewerFilter() {

                @Override
                public boolean select(Viewer viewer, Object parentElement, Object element) {
                    if (element instanceof LocalFile) {
                        LocalFile folder = (LocalFile) element;
                        if (folder.getName().equalsIgnoreCase(".hsConfig") || folder.getName().equalsIgnoreCase(".metadata") || folder.getName().equalsIgnoreCase(".config") || folder.getName().equalsIgnoreCase(".nonTransElement")) {
                            return false;
                        }
                        if (projectPath.equals(folder.toString())) {
                            return true;
                        }
                        String xliffFolderPath = folder.toString();
                        String tempPath = projectPath + System.getProperty("file.separator") + ".TEMP";
                        String configPath = projectPath + System.getProperty("file.separator") + ".config";
                        String projectFilePath = projectPath + System.getProperty("file.separator") + ".project";
                        if (xliffFolderPath.startsWith(tempPath) || xliffFolderPath.startsWith(configPath) || xliffFolderPath.startsWith(projectFilePath)) {
                            return false;
                        } else if (xliffFolderPath.startsWith(projectPath)) {
                            return xliffFolderPath.substring(projectPath.length()).startsWith(System.getProperty("file.separator"));
                        }
                    }
                    return false;
                }
            });
            dialog.create();
            dialog.open();
            if (dialog.getResult() != null) {
                Object[] selectFiles = dialog.getResult();
                XLFValidator.resetFlag();
                for (int i = 0; i < selectFiles.length; i++) {
                    IFile iFile = root.getFileForLocation(Path.fromOSString(selectFiles[i].toString()));
                    if (XLFValidator.validateXliffFile(iFile)) {
                        // 如果该文件已经存在于列表中,就向添加到重复集合中
                        if (model.getMergeXliffFile().indexOf(iFile) >= 0) {
                            exsistFileList.add(iFile);
                        }
                        model.getMergeXliffFile().add(iFile);
                    }
                }
                XLFValidator.resetFlag();
                tableViewer.setInput(getSplitTableInfos());
                if (!validIsRepeate()) {
                    validXlf();
                }
            //					for (int i = 0; i < selectFiles.length; i++) {
            //						IFile file = root.getFileForLocation(Path.fromOSString(selectFiles[i].toString()));
            //						// 如果该文件已经存在于列表中,就向添加到重复集合中
            //						if (model.getMergeXliffFile().indexOf(file) >= 0) {
            //							exsistFileList.add(file);
            //						}
            //						model.getMergeXliffFile().add(file);
            //					}
            //					tableViewer.setInput(getSplitTableInfos());
            //					if (!validIsRepeate()) {
            //						validXlf();
            //					}
            }
        }
    });
    Button deleteButton = new Button(buttonComp, SWT.NONE);
    deleteButton.setText(Messages.getString("wizard.MergeXliffWizardPage.deleteButton"));
    deleteButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    deleteButton.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            ISelection selection = tableViewer.getSelection();
            Table table = tableViewer.getTable();
            if (selection != null && !selection.isEmpty()) {
                int[] indices = table.getSelectionIndices();
                for (int index : indices) {
                    String fileFullPath = table.getItem(index).getText(1);
                    for (int i = 0; i < model.getMergeXliffFile().size(); i++) {
                        if (model.getMergeXliffFile().get(i).getFullPath().toOSString().equals(fileFullPath)) {
                            model.getMergeXliffFile().remove(i);
                            break;
                        }
                    }
                    // 如果该文件存在于重复集合中,则从该集合中删除
                    for (int j = 0; j < exsistFileList.size(); j++) {
                        if (exsistFileList.get(j).getFullPath().toOSString().equals(fileFullPath)) {
                            exsistFileList.remove(j);
                            break;
                        }
                    }
                }
                tableViewer.setInput(getSplitTableInfos());
            }
            if (!validIsRepeate()) {
                validXlf();
            }
        }
    });
}
Also used : Group(org.eclipse.swt.widgets.Group) Listener(org.eclipse.swt.widgets.Listener) IFile(org.eclipse.core.resources.IFile) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ISelection(org.eclipse.jface.viewers.ISelection) FileFolderSelectionDialog(net.heartsome.cat.common.ui.dialog.FileFolderSelectionDialog) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableColumn(org.eclipse.swt.widgets.TableColumn) LocalFile(org.eclipse.core.internal.filesystem.local.LocalFile) CoreException(org.eclipse.core.runtime.CoreException) TableViewerLabelProvider(net.heartsome.cat.ts.handlexlf.split.TableViewerLabelProvider) GridData(org.eclipse.swt.layout.GridData) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 27 with ISelection

use of org.eclipse.jface.viewers.ISelection 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 28 with ISelection

use of org.eclipse.jface.viewers.ISelection in project translationstudio8 by heartsome.

the class AddOrEditSrxConfigDialog method editLangRules.

/**
	 * 修改语言规则,备注,在修改语言规则名称时,也会同步修改映射中语言规则的名称 ;
	 */
private void editLangRules() {
    ISelection selection = langTableViewer.getSelection();
    if (!selection.isEmpty() && selection != null && selection instanceof StructuredSelection) {
        StructuredSelection struSelection = (StructuredSelection) selection;
        @SuppressWarnings("unchecked") Iterator<String[]> it = struSelection.iterator();
        if (it.hasNext()) {
            // 获取所选中的语言规则的名称
            String langRuleName = it.next()[1];
            SrxLanguageRulesManageDialog dialog = new SrxLanguageRulesManageDialog(getShell(), false, handler, srxLocation);
            dialog.create();
            dialog.setEditInitData(langRuleName);
            int result = dialog.open();
            if (result == IDialogConstants.OK_ID) {
                refreshLangTable(dialog.getCurLanguageRuleName());
            }
        }
    } else {
        MessageDialog.openInformation(getShell(), Messages.getString("srx.AddOrEditSrxConfigDialog.msgTitle2"), Messages.getString("srx.AddOrEditSrxConfigDialog.msg6"));
    }
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection)

Example 29 with ISelection

use of org.eclipse.jface.viewers.ISelection in project translationstudio8 by heartsome.

the class SrxConfigurationDialog method editSrx.

/**
	 * 编辑SRX文件 ;
	 */
private void editSrx() {
    ISelection selection = tableViewer.getSelection();
    if (!selection.isEmpty() && selection != null && selection instanceof StructuredSelection) {
        StructuredSelection structSelection = (StructuredSelection) selection;
        @SuppressWarnings("unchecked") Iterator<String[]> it = structSelection.iterator();
        if (it.hasNext()) {
            String srxName = it.next()[1];
            if (isSystemSrx(srxName)) {
                MessageDialog.openInformation(getShell(), Messages.getString("dialogs.CatalogManagerDialog.msgTitle2"), Messages.getString("srx.SrxConfigurationDialog.msg4"));
                return;
            }
            CreateOrUpdataSRXDialog editDialog = new CreateOrUpdataSRXDialog(getShell(), false);
            editDialog.create();
            editDialog.setEditInitData(srxName);
            int result = editDialog.open();
            if (result == IDialogConstants.OK_ID) {
                String editedSrxName = editDialog.getCurSrxName();
                if (!openSrx(ADConstants.configLocation + ADConstants.AD_SRXConfigFolder + File.separator + editedSrxName)) {
                    return;
                }
                AddOrEditSrxConfigDialog addDialog = new AddOrEditSrxConfigDialog(getShell(), editedSrxName, handler);
                addDialog.open();
                refreshTable(editedSrxName);
            }
        }
    }
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection)

Example 30 with ISelection

use of org.eclipse.jface.viewers.ISelection in project translationstudio8 by heartsome.

the class SrxConfigurationDialog method initListener.

/**
	 * 给增删改三个按钮提示添加事件 ;
	 */
private void initListener() {
    addBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            CreateOrUpdataSRXDialog createDialog = new CreateOrUpdataSRXDialog(getShell(), true);
            int createResult = createDialog.open();
            if (IDialogConstants.OK_ID == createResult) {
                String addedSrxName = createDialog.getCurSrxName();
                // 添加完成该文件后,解析该文件
                boolean openResult = openSrx(ADConstants.configLocation + ADConstants.AD_SRXConfigFolder + File.separator + addedSrxName);
                if (!openResult) {
                    return;
                }
                AddOrEditSrxConfigDialog addDialog = new AddOrEditSrxConfigDialog(getShell(), addedSrxName, handler);
                addDialog.open();
                refreshTable(addedSrxName);
            }
        }
    });
    editBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            editSrx();
        }
    });
    deleteBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = tableViewer.getSelection();
            if (!selection.isEmpty() && selection != null && selection instanceof StructuredSelection) {
                boolean response = MessageDialog.openConfirm(getShell(), Messages.getString("srx.SrxConfigurationDialog.msgTitle1"), Messages.getString("srx.SrxConfigurationDialog.msg1"));
                if (!response) {
                    return;
                }
                StructuredSelection structSelection = (StructuredSelection) selection;
                @SuppressWarnings("unchecked") Iterator<String[]> it = structSelection.iterator();
                File deleteSrx;
                boolean isTiped = false;
                while (it.hasNext()) {
                    String srxName = it.next()[1];
                    if (isSystemSrx(srxName)) {
                        if (!isTiped) {
                            MessageDialog.openInformation(getShell(), Messages.getString("dialogs.CatalogManagerDialog.msgTitle2"), Messages.getString("srx.SrxConfigurationDialog.msg4"));
                            isTiped = true;
                        }
                        continue;
                    }
                    deleteSrx = new File(ADConstants.configLocation + ADConstants.AD_SRXConfigFolder + File.separator + srxName);
                    if (!deleteSrx.delete()) {
                        MessageDialog.openInformation(getShell(), Messages.getString("srx.SrxConfigurationDialog.msgTitle2"), MessageFormat.format(Messages.getString("srx.SrxConfigurationDialog.msg2"), srxName));
                    }
                }
                refreshTable(null);
            } else {
                MessageDialog.openInformation(getShell(), Messages.getString("srx.SrxConfigurationDialog.msgTitle2"), Messages.getString("srx.SrxConfigurationDialog.msg3"));
            }
        }
    });
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ISelection(org.eclipse.jface.viewers.ISelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Iterator(java.util.Iterator) File(java.io.File)

Aggregations

ISelection (org.eclipse.jface.viewers.ISelection)214 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)141 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)37 SelectionEvent (org.eclipse.swt.events.SelectionEvent)34 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)32 GridData (org.eclipse.swt.layout.GridData)31 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)30 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)30 ArrayList (java.util.ArrayList)27 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)25 WizardDialog (org.eclipse.jface.wizard.WizardDialog)22 GridLayout (org.eclipse.swt.layout.GridLayout)22 Composite (org.eclipse.swt.widgets.Composite)21 TableViewer (org.eclipse.jface.viewers.TableViewer)20 RepositoryNode (org.talend.repository.model.RepositoryNode)20 IFile (org.eclipse.core.resources.IFile)19 IProject (org.eclipse.core.resources.IProject)19 Iterator (java.util.Iterator)17 PartInitException (org.eclipse.ui.PartInitException)17 TreeViewer (org.eclipse.jface.viewers.TreeViewer)16