Search in sources :

Example 6 with KeyValue

use of com.amalto.workbench.models.KeyValue in project tmdm-studio-se by Talend.

the class FKFilterParser method buildLine.

private static List<Line> buildLine(String criteria, String[] keyNames) {
    List<Line> lines = new ArrayList<Line>();
    if (criteria != null && keyNames != null) {
        String[] criterias = criteria.split(endSeparator);
        for (String cria : criterias) {
            // $NON-NLS-1$
            String[] values = cria.split("\\$\\$");
            List<String> list = new ArrayList<String>();
            list.addAll(Arrays.asList(values));
            int num = 4 - list.size();
            for (int i = 0; i < num; i++) {
                // $NON-NLS-1$
                list.add("");
            }
            // filter value
            if (list.get(2) != null && list.get(2).length() > 0) {
                String value = list.get(2);
                // $NON-NLS-1$
                value = value.replaceAll(quot, "\"");
                list.set(2, value);
            }
            List<KeyValue> keyValues = buildKeyValue(keyNames, list.toArray(new String[list.size()]));
            Line line = new Line(keyValues);
            lines.add(line);
        }
    }
    return lines;
}
Also used : Line(com.amalto.workbench.models.Line) KeyValue(com.amalto.workbench.models.KeyValue) ArrayList(java.util.ArrayList)

Example 7 with KeyValue

use of com.amalto.workbench.models.KeyValue in project tmdm-studio-se by Talend.

the class TisTableViewer method createViewer.

@Override
protected void createViewer() {
    super.createTable();
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    table.setLayoutData(gd);
    gd.heightHint = 80;
    // Up Down Delete button group
    Composite stepUpDownComposite = toolkit.createComposite(mainComposite, SWT.NONE);
    stepUpDownComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    GridLayout layout = new GridLayout(8, false);
    layout.marginTop = 0;
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    stepUpDownComposite.setLayout(layout);
    // 
    // $NON-NLS-1$
    addButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    addButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    addButton.setToolTipText(Messages.Add);
    if (isXpath()) {
        addButton.setImage(ImageCache.getCreatedImage(EImage.ADD_NEWXPATH.getPath()));
    } else {
        addButton.setImage(ImageCache.getCreatedImage(EImage.ADD_OBJ.getPath()));
    }
    addButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            // check uniqueness by concatenating all the values
            List<Line> list = (List<Line>) getViewer().getInput();
            // Update the model
            Line line = new Line(columns.toArray(new ComplexTableViewerColumn[columns.size()]), getInitValues());
            list.add(line);
            viewer.setInput(list);
            // update the instances viewer
            viewer.setSelection(null);
            viewer.refresh();
            viewer.getTable().select(viewer.getTable().getItemCount() - 1);
            markDirty();
        }
    });
    // Add Multi
    if (isAddMulti()) {
        // $NON-NLS-1$
        Button selNewPathButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
        selNewPathButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        selNewPathButton.setToolTipText(Messages.AddMultiple);
        selNewPathButton.setImage(ImageCache.getCreatedImage(EImage.SELECT_NEWXPATH.getPath()));
        selNewPathButton.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
            }

            @SuppressWarnings("unchecked")
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                xpathDialog = getNewXpathDlgInstance();
                xpathDialog.setConceptName(conceptName);
                xpathDialog.setBlockOnOpen(true);
                xpathDialog.open();
                if (xpathDialog.getReturnCode() == Window.OK) {
                    datamodelName = xpathDialog.getDataModelName();
                    // $NON-NLS-1$
                    String[] xpaths = xpathDialog.getXpath().split("&");
                    List<Line> list = new LinkedList<Line>();
                    for (String xpath : xpaths) {
                        // check uniqueness by concatenating all the values
                        list = (List<Line>) getViewer().getInput();
                        // Update the model
                        Line line = new Line(columns.toArray(new ComplexTableViewerColumn[columns.size()]), getLineValues(xpath, 0));
                        list.add(line);
                    }
                    viewer.setInput(list);
                    // update the instances viewer
                    viewer.setSelection(null);
                    viewer.refresh();
                    viewer.getTable().select(viewer.getTable().getItemCount() - 1);
                    markDirty();
                }
            }
        });
    }
    // $NON-NLS-1$
    deleteButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    deleteButton.setToolTipText(Messages.DeleteSelectedItem);
    deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            List<Line> input = (List<Line>) viewer.getInput();
            TableItem[] items = viewer.getTable().getSelection();
            for (int i = 0; i < items.length; i++) {
                input.remove(items[i].getData());
            }
            viewer.setInput(input);
            markDirty();
        }
    });
    deleteButton.setImage(ImageCache.getCreatedImage(EImage.DELETE_OBJ.getPath()));
    // delete all
    // $NON-NLS-1$
    Button deleteAllButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    deleteAllButton.setToolTipText(Messages.DeleteAllItems);
    deleteAllButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteAllButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            List<Line> items = (List<Line>) viewer.getInput();
            items.clear();
            viewer.setInput(items);
            viewer.refresh();
            markDirty();
        }
    });
    deleteAllButton.setImage(ImageCache.getCreatedImage(EImage.PROGRESS_REMALL.getPath()));
    // $NON-NLS-1$
    upButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    upButton.setToolTipText(Messages.MoveUpSelectedItem);
    upButton.setImage(ImageCache.getCreatedImage(EImage.PREV_NAV.getPath()));
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    upButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index > 0 && index < viewer.getTable().getItemCount()) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index - 1, line);
                viewer.refresh();
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        }
    });
    // $NON-NLS-1$
    downButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    downButton.setToolTipText(Messages.MovedownSelectedItem);
    downButton.setImage(ImageCache.getCreatedImage(EImage.NEXT_NAV.getPath()));
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    downButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount() - 1) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index + 1, line);
                // viewer.setInput(items);
                viewer.refresh();
                // TODO
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        }
    });
    // $NON-NLS-1$
    copyButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    copyButton.setToolTipText(Messages.CopySelectedItems);
    copyButton.setImage(ImageCache.getCreatedImage(EImage.COPY.getPath()));
    copyButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    copyButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            int[] indexs = viewer.getTable().getSelectionIndices();
            List<Line> copyLines = new ArrayList<Line>();
            for (int index : indexs) {
                if (index >= 0 && index <= viewer.getTable().getItemCount() - 1) {
                    List<Line> items = (List<Line>) viewer.getInput();
                    Line line = items.get(index);
                    Line copyLine = line.clone();
                    copyLines.add(copyLine);
                }
            }
            if (indexs.length > 0) {
                // enable all paste buttons
                HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
                if (btns != null) {
                    for (Button btn : btns) {
                        if (btn != null) {
                            btn.setEnabled(true);
                        }
                    }
                }
                // add to workbenchclipboard
                WorkbenchClipboard.getWorkbenchClipboard().setLines(String.valueOf(columns.size()), copyLines);
            }
        }
    });
    // $NON-NLS-1$
    pastButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    pastButton.setToolTipText(Messages.PasteSelectedItem);
    pastButton.setImage(ImageCache.getCreatedImage(EImage.PASTE.getPath()));
    pastButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    pastButton.setEnabled(false);
    pastButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            if (mainPage != null) {
                mainPage.setComitting(true);
            }
            boolean dirty = false;
            List<Line> items = (List<Line>) viewer.getInput();
            List<Line> cacheLine = WorkbenchClipboard.getWorkbenchClipboard().getLines(String.valueOf(columns.size()));
            List<Line> coloneLine = new ArrayList<Line>();
            for (Line l : cacheLine) {
                coloneLine.add(l.clone());
            }
            if (cacheLine.size() > 0) {
                items.addAll(coloneLine);
                dirty = true;
            }
            viewer.refresh();
            // TODO
            if (mainPage != null) {
                mainPage.setComitting(false);
            }
            if (dirty) {
                markDirty();
            }
        }
    });
    HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
    if (btns == null) {
        btns = new HashSet<Button>();
        pastBtns.put(String.valueOf(columns.size()), btns);
    }
    btns.add(pastButton);
    // Create the cell editors --> We actually discard those later: not natural for an user
    CellEditor[] editors = new CellEditor[columns.size()];
    for (int i = 0; i < columns.size(); i++) {
        if (columns.get(i).isText()) {
            editors[i] = new TextCellEditor(table);
        } else if (columns.get(i).isCombo()) {
            editors[i] = new ComboBoxCellEditor(table, columns.get(i).getComboValues(), SWT.READ_ONLY);
        } else if (columns.get(i).isXPATH()) {
            editors[i] = new XpathCellEditor(table, validators.get(columns.get(i)));
        } else if (columns.get(i).isMultiMessage()) {
            editors[i] = new MultiMessageEditor(table);
            multiMsg.setColumn(table.getColumn(i));
        } else if (columns.get(i).isValidationRule()) {
            editors[i] = createValidationRuleEditor();
            validationRule.setColumn(table.getColumn(i));
        }
    }
    viewer.setCellEditors(editors);
    // set the content provider
    viewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @SuppressWarnings("unchecked")
        public Object[] getElements(Object inputElement) {
            ArrayList<Line> lines = (ArrayList<Line>) inputElement;
            return lines.toArray(new Line[lines.size()]);
        }
    });
    // set the label provider
    viewer.setLabelProvider(new ITableLabelProvider() {

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void removeListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            Line line = (Line) element;
            if (columnIndex >= 0 && columnIndex < columns.size()) {
                for (KeyValue keyvalue : line.keyValues) {
                    if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                        String val = keyvalue.value;
                        if (columns.get(columnIndex).isNillable()) {
                            if (columns.get(columnIndex).getNillValue().equals(val)) {
                                val = columns.get(columnIndex).getNillDisplay();
                            }
                        }
                        return val;
                    }
                }
            }
            // $NON-NLS-1$
            return "";
        }

        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    });
    // Set the column properties
    ArrayList<String> columnNames = new ArrayList<String>();
    for (ComplexTableViewerColumn column : columns) {
        columnNames.add(column.getName());
    }
    viewer.setColumnProperties(columnNames.toArray(new String[columnNames.size()]));
    // set the Cell Modifier
    viewer.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            return editable;
        }

        @SuppressWarnings("unchecked")
        public void modify(Object element, String property, Object value) {
            if (value instanceof Integer) {
                if (Integer.valueOf(value.toString()) == -1) {
                    return;
                }
            }
            // modify the text and combo cell value
            TableItem item = (TableItem) element;
            Line line = (Line) item.getData();
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            if (columnIndex >= 0 && columnIndex < viewer.getColumnProperties().length) {
                if (isAColumnWithCombo(columnIndex)) {
                    String[] attrs = columns.get(columnIndex).getComboValues();
                    value = attrs[Integer.parseInt(value.toString())];
                }
                KeyValue kv = line.keyValues.get(columnIndex);
                boolean noChange = kv.value.equals(value.toString());
                kv.value = value.toString();
                viewer.refresh();
                if (!noChange) {
                    markDirty();
                }
            }
        }

        public Object getValue(Object element, String property) {
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            Line line = (Line) element;
            // add getting value from combo
            if (isAColumnWithCombo(columnIndex)) {
                String value = line.keyValues.get(columnIndex).value;
                String[] attrs = columns.get(columnIndex).getComboValues();
                return Arrays.asList(attrs).indexOf(value);
            }
            for (KeyValue keyvalue : line.keyValues) {
                if (property.equals(keyvalue.key)) {
                    if (keyvalue.value.equals("")) {
                        // $NON-NLS-1$
                        return columns.get(columnIndex).getNillDisplay();
                    }
                    return keyvalue.value;
                }
            }
            return null;
        }

        private boolean isAColumnWithCombo(int columnIdx) {
            return columns.get(columnIdx).isCombo();
        }
    });
    // display for Delete Key events to delete an instance
    viewer.getTable().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void keyReleased(KeyEvent e) {
            if ((e.stateMask == 0) && (e.character == SWT.DEL) && (viewer.getSelection() != null)) {
                Line line = (Line) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
                // update the underlying role and refresh the table
                // update the underlying model
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(line);
                // refresh
                viewer.refresh();
                // mark for update
                markDirty();
            }
        }
    });
    // add dispose listener
    viewer.getTable().addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
            btns.remove(pastButton);
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) KeyValue(com.amalto.workbench.models.KeyValue) CellEditor(org.eclipse.jface.viewers.CellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) Viewer(org.eclipse.jface.viewers.Viewer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Image(org.eclipse.swt.graphics.Image) EImage(com.amalto.workbench.image.EImage) DisposeEvent(org.eclipse.swt.events.DisposeEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ITableLabelProvider(org.eclipse.jface.viewers.ITableLabelProvider) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ICellModifier(org.eclipse.jface.viewers.ICellModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) Composite(org.eclipse.swt.widgets.Composite) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) Line(com.amalto.workbench.models.Line) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) GridData(org.eclipse.swt.layout.GridData) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) KeyListener(org.eclipse.swt.events.KeyListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 8 with KeyValue

use of com.amalto.workbench.models.KeyValue in project tmdm-studio-se by Talend.

the class ComplexTableViewer method createViewer.

protected void createViewer() {
    createTable();
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, columns.size(), 1);
    table.setLayoutData(gd);
    gd.heightHint = 80;
    // Up Down Delete button group
    Composite stepUpDownComposite = toolkit.createComposite(mainComposite, SWT.NONE);
    stepUpDownComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
    stepUpDownComposite.setLayout(new GridLayout(1, false));
    // $NON-NLS-1$
    upButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    upButton.setImage(ImageCache.getCreatedImage(EImage.PREV_NAV.getPath()));
    upButton.setToolTipText(Messages.ComplexTableViewer_UpBtnTip);
    upButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index > 0 && index < viewer.getTable().getItemCount()) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index - 1, line);
                viewer.refresh();
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        }
    });
    // $NON-NLS-1$
    downButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    downButton.setImage(ImageCache.getCreatedImage(EImage.NEXT_NAV.getPath()));
    downButton.setToolTipText(Messages.ComplexTableViewer_DownBtnTip);
    downButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount() - 1) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index + 1, line);
                // viewer.setInput(items);
                viewer.refresh();
                // TODO
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        }
    });
    // $NON-NLS-1$
    deleteButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);
    deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteButton.setToolTipText(Messages.ComplexTableViewer_DelBtnLabel);
    deleteButton.setImage(ImageCache.getCreatedImage(EImage.DELETE_OBJ.getPath()));
    deleteButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount()) {
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(index);
                viewer.refresh();
                int pos = index - 1;
                if (pos >= 0) {
                    viewer.getTable().select(pos);
                }
                markDirty();
            }
        }
    });
    // Create the cell editors --> We actually discard those later: not natural for an user
    CellEditor[] editors = new CellEditor[columns.size()];
    for (int i = 0; i < columns.size(); i++) {
        if (columns.get(i).isText()) {
            editors[i] = new TextCellEditor(table);
        } else if (columns.get(i).isCombo()) {
            editors[i] = new ComboBoxCellEditor(table, columns.get(i).getComboValues(), SWT.READ_ONLY);
        } else if (columns.get(i).isXPATH()) {
            editors[i] = new XpathCellEditor(table, validators.get(columns.get(i)));
        } else if (columns.get(i).isMultiMessage()) {
            editors[i] = new MultiMessageEditor(table);
            multiMsg.setColumn(table.getColumn(i));
        } else if (columns.get(i).isValidationRule()) {
            editors[i] = new ValidationRuleEditor(table);
            validationRule.setColumn(table.getColumn(i));
        }
    }
    viewer.setCellEditors(editors);
    // set the content provider
    viewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @SuppressWarnings("unchecked")
        public Object[] getElements(Object inputElement) {
            List<Line> lines = (List<Line>) inputElement;
            return lines.toArray(new Line[lines.size()]);
        }
    });
    // set the label provider
    viewer.setLabelProvider(new ITableLabelProvider() {

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void removeListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            Line line = (Line) element;
            if (columnIndex >= 0 && columnIndex < columns.size()) {
                for (KeyValue keyvalue : line.keyValues) {
                    if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                        String val = keyvalue.value;
                        if (columns.get(columnIndex).isNillable()) {
                            if (columns.get(columnIndex).getNillValue().equals(val)) {
                                val = columns.get(columnIndex).getNillDisplay();
                            }
                        }
                        return val;
                    }
                }
            }
            // $NON-NLS-1$
            return "";
        }

        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    });
    // Set the column properties
    ArrayList<String> columnNames = new ArrayList<String>();
    for (ComplexTableViewerColumn column : columns) {
        columnNames.add(column.getName());
    }
    viewer.setColumnProperties(columnNames.toArray(new String[columnNames.size()]));
    // set the Cell Modifier
    viewer.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            // if (INSTANCE_ACCESS.equals(property)) return true; Deactivated
            return editable;
        }

        @SuppressWarnings("unchecked")
        public void modify(Object element, String property, Object value) {
            if (value instanceof Integer) {
                if (Integer.valueOf(value.toString()) == -1) {
                    return;
                }
            }
            // modify the text and combo cell value
            TableItem item = (TableItem) element;
            Line line = (Line) item.getData();
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            if (!isForceTextCellEditor(columnIndex) && isAColumnWithCombo(columnIndex)) {
                String[] attrs = columns.get(columnIndex).getComboValues();
                value = attrs[Integer.parseInt(value.toString())];
            }
            KeyValue kv = line.keyValues.get(columnIndex);
            boolean noChange = kv.value.equals(value.toString());
            kv.value = value.toString();
            viewer.refresh();
            if (!noChange) {
                markDirty();
            }
        }

        public Object getValue(Object element, String property) {
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            Line line = (Line) element;
            // add getting value from combo
            if (isAColumnWithCombo(columnIndex)) {
                String value = line.keyValues.get(columnIndex).value;
                String[] attrs = columns.get(columnIndex).getComboValues();
                return Arrays.asList(attrs).indexOf(value);
            }
            for (KeyValue keyvalue : line.keyValues) {
                if (property.equals(keyvalue.key)) {
                    if (keyvalue.value.equals("")) {
                        // $NON-NLS-1$
                        return columns.get(columnIndex).getNillDisplay();
                    }
                    return keyvalue.value;
                }
            }
            return null;
        }

        private boolean isAColumnWithCombo(int columnIdx) {
            return columns.get(columnIdx).isCombo();
        }

        private boolean isForceTextCellEditor(int columnIdx) {
            return columns.get(columnIdx).isText();
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            Line line = (Line) ((IStructuredSelection) event.getSelection()).getFirstElement();
            for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
                if (line == null) {
                    Control control = columns.get(columnIndex).getControl();
                    if (control instanceof Text) {
                        // $NON-NLS-1$
                        ((Text) control).setText("");
                    }
                    if (control instanceof CCombo) {
                        ((CCombo) control).select(0);
                    }
                    if (control instanceof Combo) {
                        ((CCombo) control).select(0);
                    }
                } else {
                    for (KeyValue keyvalue : line.keyValues) {
                        if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                            String val = keyvalue.value;
                            Control control = columns.get(columnIndex).getControl();
                            if (control instanceof Text) {
                                ((Text) control).setText(val);
                            }
                            if (control instanceof CCombo) {
                                ((CCombo) control).setText(val);
                            }
                            if (control instanceof Combo) {
                                ((CCombo) control).setText(val);
                            }
                        }
                    }
                }
            }
        }
    });
    // display for Delete Key events to delete an instance
    viewer.getTable().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void keyReleased(KeyEvent e) {
            if ((e.stateMask == 0) && (e.character == SWT.DEL) && (viewer.getSelection() != null)) {
                Line line = (Line) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
                // update the underlying role and refresh the table
                // update the underlying model
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(line);
                // refresh
                viewer.refresh();
                // mark for update
                markDirty();
            }
        }
    });
}
Also used : KeyValue(com.amalto.workbench.models.KeyValue) CellEditor(org.eclipse.jface.viewers.CellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) CCombo(org.eclipse.swt.custom.CCombo) Combo(org.eclipse.swt.widgets.Combo) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) EImage(com.amalto.workbench.image.EImage) Image(org.eclipse.swt.graphics.Image) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ITableLabelProvider(org.eclipse.jface.viewers.ITableLabelProvider) Control(org.eclipse.swt.widgets.Control) ICellModifier(org.eclipse.jface.viewers.ICellModifier) List(java.util.List) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) Text(org.eclipse.swt.widgets.Text) Line(com.amalto.workbench.models.Line) CCombo(org.eclipse.swt.custom.CCombo) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) GridData(org.eclipse.swt.layout.GridData) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) KeyListener(org.eclipse.swt.events.KeyListener) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 9 with KeyValue

use of com.amalto.workbench.models.KeyValue in project tmdm-studio-se by Talend.

the class SchematronExpressBuilder method create.

private void create() {
    com = new Composite(parent, SWT.NONE);
    com.setLayout(getLayout(3));
    // expression
    Group expressG = new Group(com, SWT.NONE);
    expressG.setText(Messages.SchematronExpressBuilder_Expression);
    expressG.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 2));
    expressG.setLayout(getLayout(1));
    // top button group
    Composite topCom = new Composite(expressG, 0);
    Button clearBtn = new Button(topCom, SWT.PUSH);
    topCom.setLayout(getLayout(4));
    clearBtn.setText(Messages.SchematronExpressBuilder_Clear);
    clearBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
    clearBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // $NON-NLS-1$
            sourceViewer.setDocument(new Document(""));
        }
    });
    org.eclipse.jface.text.Document doc = new org.eclipse.jface.text.Document(value);
    sourceViewer = new SourceViewer(expressG, new VerticalRuler(5), SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 6);
    gd.heightHint = 150;
    sourceViewer.getControl().setLayoutData(gd);
    WidgetUtils.initRedoUndo(sourceViewer);
    sourceViewer.setDocument(doc);
    // bottom button group
    Composite bottomCom = new Composite(expressG, 0);
    GridLayout ly = getLayout(4);
    bottomCom.setLayout(ly);
    Composite com1 = new Composite(bottomCom, 0);
    com1.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
    String[] strs1 = { "+", "-", "*", "div" };
    for (String str : strs1) {
        final Button btn = new Button(com1, SWT.PUSH);
        btn.setText(str);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com2 = new Composite(bottomCom, 0);
    com2.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
    String[] strs2 = { "=", "!=", ">", "<" };
    for (String str : strs2) {
        final Button btn = new Button(com2, SWT.PUSH);
        btn.setText(str);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com3 = new Composite(bottomCom, 0);
    com3.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$
    String[] strs3 = { "and", "or" };
    for (int i = 0; i < strs3.length; i++) {
        final Button btn = new Button(com3, SWT.PUSH);
        btn.setText(strs3[i]);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com4 = new Composite(bottomCom, 0);
    com4.setLayout(getLayout(3));
    // $NON-NLS-1$//$NON-NLS-2$
    String[] strs4 = { ")", "(" };
    for (int i = 0; i < strs4.length; i++) {
        final Button btn = new Button(com4, SWT.PUSH);
        btn.setText(strs4[i]);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Button xpathButton = new Button(com4, SWT.PUSH);
    // $NON-NLS-1$
    xpathButton.setText("xpath");
    xpathButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            XpathSelectDialog dlg = getXPathSelectDialog();
            dlg.setConceptName(conceptName);
            // dlg.setContext(context);
            dlg.setBlockOnOpen(true);
            dlg.open();
            if (dlg.getReturnCode() == Window.OK) {
                if (getTextWidget().getSelectionText().length() > 0) {
                    // getTextWidget().setText(getText().replace(getTextWidget().getSelectionText(),
                    // dlg.getXpath()));
                    getTextWidget().replaceTextRange(getTextWidget().getSelectionRanges()[0], getTextWidget().getSelectionRanges()[1], dlg.getXpath());
                } else {
                    // getTextWidget().setText(getText()+dlg.getXpath());
                    insertText(dlg.getXpath());
                }
            }
        }
    });
    // //test
    // Group testG=new Group(com,SWT.NONE);
    // testG.setText("Test");
    // GridData gd2=new GridData(SWT.FILL,SWT.FILL,true,true,1,2);
    // gd2.widthHint=190;
    // testG.setLayoutData(gd2);
    // testG.setLayout(getLayout(2));
    // Button btnStartTest=new Button(testG,SWT.PUSH);
    // btnStartTest.setText("Start Test");
    // btnStartTest.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
    // 
    // Button btnCleartTest=new Button(testG,SWT.PUSH);
    // btnCleartTest.setText("Clear");
    // btnCleartTest.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
    // 
    // testResult=new StyledText(testG,SWT.V_SCROLL|SWT.BORDER);
    // testResult.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,3));
    // btnCleartTest.addSelectionListener(new SelectionAdapter(){
    // @Override
    // public void widgetSelected(SelectionEvent e) {
    // testResult.setText("");
    // }
    // });
    // categories
    Group categoryG = new Group(com, SWT.NONE);
    categoryG.setText(Messages.SchematronExpressBuilder_Categories);
    GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd1.heightHint = 200;
    gd1.widthHint = 140;
    categoryG.setLayoutData(gd1);
    categoryG.setLayout(getLayout(1));
    categoryList = new List(categoryG, SWT.BORDER | SWT.V_SCROLL);
    categoryList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    for (XPathFunc c : categories) {
        categoryList.add(c.getCategory());
    }
    categoryList.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            funcList.removeAll();
            String c = categoryList.getSelection()[0];
            for (XPathFunc func : categories) {
                if (func.getCategory().equals(c)) {
                    curfc = func;
                    for (KeyValue kv : func.getFuncs()) {
                        funcList.add(kv.key);
                    }
                    break;
                }
            }
            if (funcList.getItems().length > 0) {
                funcList.select(0);
                helpTxt.setText(curfc.getFuncs().get(0).value);
            }
        }
    });
    // funcations
    Group functionG = new Group(com, SWT.NONE);
    functionG.setText(Messages.SchematronExpressBuilder_Functions);
    functionG.setLayout(getLayout(1));
    GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd3.widthHint = 220;
    functionG.setLayoutData(gd3);
    funcList = new List(functionG, SWT.BORDER | SWT.V_SCROLL);
    funcList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    funcList.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            getTextWidget().setText(getText() + funcList.getItem(funcList.getSelectionIndex()));
        }

        public void widgetSelected(SelectionEvent e) {
            String c = funcList.getSelection()[0];
            for (KeyValue kv : curfc.getFuncs()) {
                if (kv.key.equals(c)) {
                    helpTxt.setText(kv.value);
                    break;
                }
            }
        }
    });
    // help
    Group helpG = new Group(com, SWT.NONE);
    helpG.setText(Messages.SchematronExpressBuilder_Help);
    helpG.setLayout(getLayout(1));
    helpG.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    helpTxt = new StyledText(helpG, SWT.BORDER | SWT.WRAP);
    GridData gd4 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd4.widthHint = 210;
    helpTxt.setLayoutData(gd4);
}
Also used : Group(org.eclipse.swt.widgets.Group) SourceViewer(org.eclipse.jface.text.source.SourceViewer) KeyValue(com.amalto.workbench.models.KeyValue) XpathSelectDialog(com.amalto.workbench.dialogs.XpathSelectDialog) Document(org.eclipse.jface.text.Document) Document(org.eclipse.jface.text.Document) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(org.eclipse.swt.widgets.List) VerticalRuler(org.eclipse.jface.text.source.VerticalRuler) StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) XPathFunc(com.amalto.workbench.models.XPathFunc) Point(org.eclipse.swt.graphics.Point) GridData(org.eclipse.swt.layout.GridData) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 10 with KeyValue

use of com.amalto.workbench.models.KeyValue in project tmdm-studio-se by Talend.

the class FKFilterParserTest method testBuildLine.

@Test
public void testBuildLine() {
    // $NON-NLS-1$
    String criteria = "Store/Long$$=$$111$$And#Store/Address$$Contains$$shanghai$$Or#";
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    String[] keyNames = { "XPath", "Operator", "Value", "Predicate" };
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    String[] values1 = { "Store/Long", "=", "111", "And" };
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    String[] values2 = { "Store/Address", "Contains", "shanghai", "Or" };
    List<KeyValue> keyValues1 = new ArrayList<KeyValue>();
    List<KeyValue> keyValues2 = new ArrayList<KeyValue>();
    for (int i = 0; i < keyNames.length && i < values1.length; i++) {
        keyValues1.add(new KeyValue(keyNames[i], values1[i]));
        keyValues2.add(new KeyValue(keyNames[i], values2[i]));
    }
    Line line1 = new Line(keyValues1);
    Line line2 = new Line(keyValues2);
    try {
        PowerMockito.mockStatic(FKFilterParser.class);
        // $NON-NLS-1$
        PowerMockito.when(FKFilterParser.class, "buildLine", anyString(), any(String[].class)).thenCallRealMethod();
        // $NON-NLS-1$
        PowerMockito.when(FKFilterParser.class, "buildKeyValue", any(String[].class), any(String[].class)).thenCallRealMethod();
        // $NON-NLS-1$
        List<Line> lines = Whitebox.invokeMethod(FKFilterParser.class, "buildLine", criteria, keyNames);
        assertNotNull(lines);
        assertEquals(2, lines.size());
        assertTrue(lines.contains(line1));
        assertTrue(lines.contains(line2));
        // $NON-NLS-1$
        lines = Whitebox.invokeMethod(FKFilterParser.class, "buildLine", (String) null, keyNames);
        assertTrue(lines.size() == 0);
        // $NON-NLS-1$
        lines = Whitebox.invokeMethod(FKFilterParser.class, "buildLine", criteria, null);
        assertTrue(lines.size() == 0);
        // $NON-NLS-1$
        lines = Whitebox.invokeMethod(FKFilterParser.class, "buildLine", (String) null, null);
        assertTrue(lines.size() == 0);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Line(com.amalto.workbench.models.Line) KeyValue(com.amalto.workbench.models.KeyValue) ArrayList(java.util.ArrayList) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

KeyValue (com.amalto.workbench.models.KeyValue)12 Line (com.amalto.workbench.models.Line)10 ArrayList (java.util.ArrayList)9 SelectionListener (org.eclipse.swt.events.SelectionListener)4 GridData (org.eclipse.swt.layout.GridData)4 Composite (org.eclipse.swt.widgets.Composite)4 List (java.util.List)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 TableItem (org.eclipse.swt.widgets.TableItem)3 EImage (com.amalto.workbench.image.EImage)2 XPathFunc (com.amalto.workbench.models.XPathFunc)2 ListenerList (org.eclipse.core.runtime.ListenerList)2 CellEditor (org.eclipse.jface.viewers.CellEditor)2 ComboBoxCellEditor (org.eclipse.jface.viewers.ComboBoxCellEditor)2 ICellModifier (org.eclipse.jface.viewers.ICellModifier)2 ILabelProviderListener (org.eclipse.jface.viewers.ILabelProviderListener)2 IStructuredContentProvider (org.eclipse.jface.viewers.IStructuredContentProvider)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ITableLabelProvider (org.eclipse.jface.viewers.ITableLabelProvider)2 TextCellEditor (org.eclipse.jface.viewers.TextCellEditor)2