Search in sources :

Example 11 with IElement

use of org.talend.core.model.process.IElement in project tdi-studio-se by Talend.

the class Expression method evaluateSimpleExpression.

private static boolean evaluateSimpleExpression(String simpleExpression, List<? extends IElementParameter> listParam, ElementParameter currentParam) {
    boolean showParameter = false;
    String test = null;
    if (simpleExpression.contains(EQUALS)) {
        test = EQUALS;
    } else if (simpleExpression.contains(NOT_EQUALS)) {
        test = NOT_EQUALS;
    } else if (simpleExpression.contains(GREAT_THAN)) {
        test = GREAT_THAN;
    } else if (simpleExpression.contains(LESS_THAN)) {
        test = LESS_THAN;
    }
    if ((//$NON-NLS-1$
    simpleExpression.contains(" IN [") || simpleExpression.contains(" IN[")) && simpleExpression.endsWith("]")) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return evaluateInExpression(simpleExpression, listParam);
    }
    if ((simpleExpression.contains("DISTRIB["))) {
        //$NON-NLS-1$
        return evaluateDistrib(simpleExpression, listParam, currentParam);
    }
    if ((simpleExpression.contains("SPARK_VERSION["))) {
        //$NON-NLS-1$
        return evaluateSparkVersion(simpleExpression, listParam, currentParam);
    }
    List<String> paraNames = getParaNamesFromIsShowFunc(simpleExpression);
    if (paraNames.size() > 0) {
        // Here only be one isShow() function since it has been already split.
        String paraName = paraNames.get(0);
        try {
            checkIsShowLoop(paraName, simpleExpression, listParam, currentParam, null);
        } catch (Exception e) {
            ExceptionHandler.process(e);
            return false;
        }
        for (IElementParameter param : listParam) {
            if (paraName != null && paraName.equals(param.getName())) {
                if (simpleExpression.startsWith("!")) {
                    //$NON-NLS-1$
                    return !param.isShow(param.getShowIf(), param.getNotShowIf(), listParam);
                } else {
                    return param.isShow(param.getShowIf(), param.getNotShowIf(), listParam);
                }
            }
        }
    }
    if (test == null) {
        throwUnsupportedExpression(simpleExpression, currentParam);
        return false;
    }
    String[] strings = simpleExpression.split(test);
    String variableName = null, variableValue = null;
    for (String string2 : strings) {
        String string = string2.trim();
        if (string.contains("'")) {
            // value //$NON-NLS-1$
            variableValue = string;
            //$NON-NLS-1$
            variableValue = variableValue.substring(1, string.lastIndexOf("'"));
        } else {
            variableName = string;
        }
    }
    /*
         * this is only for Current OS condition.
         */
    if (variableName != null && EParameterName.CURRENT_OS.getName().equals(variableName)) {
        if (variableValue != null) {
            if (EQUALS.endsWith(test)) {
                return checkCurrentOS(variableValue);
            } else if (NOT_EQUALS.equals(test)) {
                return !checkCurrentOS(variableValue);
            }
        }
    }
    if (listParam == null) {
        return false;
    }
    /*
         * this only used to check is EE version or not
         */
    if ("IS_STUDIO_EE_VERSION".equals(variableName)) {
        //$NON-NLS-1$
        boolean isTIS = PluginChecker.isTIS();
        if ("true".equals(variableValue)) {
            //$NON-NLS-1$
            return isTIS;
        } else {
            return !isTIS;
        }
    }
    /*
         * this only used to check is IPAAS Components are Loaded or not
         */
    if ("IS_STUDIO_IPAAS_VERSION".equals(variableName)) {
        //$NON-NLS-1$
        boolean isIPaas = PluginChecker.isIPaasPluginLoaded();
        if ("true".equals(variableValue)) {
            //$NON-NLS-1$
            return isIPaas;
        } else {
            return !isIPaas;
        }
    }
    // 3 levels of variable name accepted maximum (ex: MY_VAR.TABLE.FIELD == 'test')
    String[] varNames;
    varNames = StringUtils.split(variableName, '.');
    if ((variableName != null) && (variableValue != null)) {
        if (varNames[0].equals("#LINK@NODE")) {
            //$NON-NLS-1$
            INode node = null;
            if (currentParam != null && currentParam.getElement() instanceof INode) {
                node = (INode) currentParam.getElement();
            } else if (currentParam == null) {
                if (listParam != null && listParam.size() > 0) {
                    IElement element = listParam.get(0).getElement();
                    if (element instanceof INode) {
                        node = (INode) element;
                    }
                }
            }
            if (node != null) {
                //$NON-NLS-1$ //$NON-NLS-2$
                String relatedNodeName = ElementParameterParser.getValue(node, "__" + varNames[1] + "__");
                // if relatedNodeName is empty, maybe means this property have not been setted
                if (relatedNodeName != null && !relatedNodeName.trim().isEmpty()) {
                    List<? extends INode> generatingNodes = node.getProcess().getGeneratingNodes();
                    for (INode aNode : generatingNodes) {
                        if (aNode.getUniqueName().equals(relatedNodeName)) {
                            //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
                            simpleExpression = simpleExpression.replace(varNames[0] + "." + varNames[1] + ".", "");
                            List<? extends IElementParameter> elementParameters = aNode.getElementParameters();
                            // only deal with LIST/CHECKBOX
                            return evaluate(simpleExpression, elementParameters);
                        }
                    }
                }
            }
        } else /*
             * TESB-6240 GangLiu Test the connection configuration.
             */
        if ("#LINK@CONNECTOR".equals(varNames[0])) {
            //$NON-NLS-1$
            if (listParam != null && listParam.size() > 0) {
                IElement element = listParam.get(0).getElement();
                if (element != null && element instanceof INode) {
                    INode node = (INode) element;
                    if (varNames.length > 2 && varNames[1] != null && varNames[2] != null) {
                        // read in/out connection type accounts
                        List<? extends IConnection> connections = new ArrayList<IConnection>();
                        if ("IN".equals(varNames[1]) || "OUT".equals(varNames[1])) {
                            //$NON-NLS-1$ //$NON-NLS-2$
                            if ("IN".equals(varNames[1])) {
                                //$NON-NLS-1$
                                if ("ANY".equals(varNames[2])) {
                                    //$NON-NLS-1$
                                    connections = node.getIncomingConnections();
                                } else {
                                    connections = node.getIncomingConnections(EConnectionType.valueOf(varNames[2]));
                                }
                            } else {
                                if ("ANY".equals(varNames[2])) {
                                    //$NON-NLS-1$
                                    connections = node.getOutgoingConnections();
                                } else {
                                    connections = node.getOutgoingConnections(EConnectionType.valueOf(varNames[2]));
                                }
                            }
                            try {
                                int connSize = connections.size();
                                int targetNumber = Integer.parseInt(variableValue);
                                if (GREAT_THAN.equals(test)) {
                                    return connSize > targetNumber;
                                } else if (LESS_THAN.equals(test)) {
                                    return connSize < targetNumber;
                                } else if (EQUALS.equals(test)) {
                                    return connSize == targetNumber;
                                } else if (NOT_EQUALS.equals(test)) {
                                    return connSize != targetNumber;
                                }
                            } catch (Exception e) {
                            }
                        } else {
                            // read specific connection parameter
                            connections = node.getOutgoingConnections(EConnectionType.valueOf(varNames[1]));
                            for (IConnection c : connections) {
                                IElementParameter elementParameter = c.getElementParameter(varNames[2]);
                                if (variableValue.equals(elementParameter.getValue())) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        } else // End of TESB-6240
        if ("#NODE@IN".equals(varNames[0])) {
            //$NON-NLS-1$
            if (listParam != null && listParam.size() > 0) {
                IElement element = listParam.get(0).getElement();
                if (element != null && element instanceof IConnection) {
                    INode sourceNode = ((IConnection) element).getSource();
                    // change from: #NODE@IN.SUBTREE_START == 'false'
                    // to: SUBTREE_START == 'false'
                    //$NON-NLS-1$ //$NON-NLS-2$
                    simpleExpression = simpleExpression.replace(varNames[0] + ".", "");
                    return evaluate(simpleExpression, sourceNode.getElementParameters());
                }
            }
        } else if ("#NODE@OUT".equals(varNames[0])) {
            //$NON-NLS-1$
            if (listParam != null && listParam.size() > 0) {
                IElement element = listParam.get(0).getElement();
                if (element != null && element instanceof IConnection) {
                    INode sourceNode = ((IConnection) element).getTarget();
                    // change from: #NODE@OUT.END_OF_FLOW == 'false'
                    // to: END_OF_FLOW == 'false'
                    //$NON-NLS-1$ //$NON-NLS-2$
                    simpleExpression = simpleExpression.replace(varNames[0] + ".", "");
                    return evaluate(simpleExpression, sourceNode.getElementParameters());
                }
            }
        }
    }
    if ((variableName != null) && (variableValue != null)) {
        for (IElementParameter param : listParam) {
            if (param.getName().equals(varNames[0])) {
                IElementParameter testedParameter = param;
                Object value = null;
                boolean found = false;
                if (param.getFieldType().equals(EParameterFieldType.TABLE)) {
                    List<Map<String, Object>> tableValues = (List<Map<String, Object>>) param.getValue();
                    if (currentParam == null) {
                        continue;
                    }
                    if (tableValues.size() <= currentParam.getCurrentRow()) {
                        break;
                    }
                    Map<String, Object> currentRow = tableValues.get(currentParam.getCurrentRow());
                    if (currentRow.containsKey(varNames[1])) {
                        for (Object curObj : param.getListItemsValue()) {
                            if (curObj instanceof IElementParameter) {
                                IElementParameter testParam = (IElementParameter) curObj;
                                if (testParam.getName().equals(varNames[1])) {
                                    testedParameter = testParam;
                                    break;
                                }
                            }
                        }
                        if (varNames.length == 2) {
                            // simple value
                            value = currentRow.get(varNames[1]);
                        } else {
                            if ("TYPE".equals(varNames[2])) {
                                //$NON-NLS-1$
                                IMetadataTable baseTable = null;
                                IMetadataColumn baseColumn = null;
                                INode node;
                                Object obj = currentRow.get(testedParameter.getName());
                                //$NON-NLS-1$
                                String columnName = "";
                                if (obj instanceof String) {
                                    columnName = (String) obj;
                                } else if (obj instanceof Integer) {
                                    int index = (Integer) obj;
                                    if (index < testedParameter.getListItemsDisplayName().length && index >= 0) {
                                        columnName = testedParameter.getListItemsDisplayName()[(Integer) obj];
                                    }
                                }
                                if (currentParam.getElement() instanceof INode) {
                                    node = (INode) currentParam.getElement();
                                    switch(testedParameter.getFieldType()) {
                                        case COLUMN_LIST:
                                            baseTable = node.getMetadataList().get(0);
                                            break;
                                        case PREV_COLUMN_LIST:
                                            IConnection connection = null;
                                            for (int i = 0; i < node.getIncomingConnections().size(); i++) {
                                                IConnection curConnec = node.getIncomingConnections().get(i);
                                                if (curConnec.getLineStyle() == EConnectionType.FLOW_MAIN) {
                                                    connection = curConnec;
                                                    break;
                                                }
                                            }
                                            if (connection != null) {
                                                baseTable = connection.getMetadataTable();
                                            }
                                            break;
                                        case LOOKUP_COLUMN_LIST:
                                            List<IConnection> refConnections = new ArrayList<IConnection>();
                                            for (int i = 0; i < node.getIncomingConnections().size(); i++) {
                                                IConnection curConnec = node.getIncomingConnections().get(i);
                                                if (curConnec.getLineStyle() == EConnectionType.FLOW_REF) {
                                                    refConnections.add(curConnec);
                                                }
                                            }
                                            for (IConnection curConnec : refConnections) {
                                                IMetadataTable table = curConnec.getMetadataTable();
                                                for (IMetadataColumn column : table.getListColumns()) {
                                                    //$NON-NLS-1$
                                                    String name = curConnec.getName() + "." + column.getLabel();
                                                    if (name.equals(columnName)) {
                                                        baseColumn = column;
                                                    }
                                                }
                                            }
                                            break;
                                        default:
                                    }
                                    if (baseTable != null) {
                                        for (IMetadataColumn column : baseTable.getListColumns()) {
                                            if (column.getLabel().equals(columnName)) {
                                                baseColumn = column;
                                                break;
                                            }
                                        }
                                    }
                                    if (baseColumn != null) {
                                        switch(LanguageManager.getCurrentLanguage()) {
                                            case JAVA:
                                                value = JavaTypesManager.getTypeToGenerate(baseColumn.getTalendType(), baseColumn.isNullable());
                                                break;
                                            default:
                                                value = baseColumn.getTalendType();
                                        }
                                        if (value.equals(variableValue)) {
                                            found = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else if (param.getFieldType().equals(EParameterFieldType.PROPERTY_TYPE) || param.getFieldType().equals(EParameterFieldType.SCHEMA_TYPE) || param.getFieldType().equals(EParameterFieldType.SCHEMA_REFERENCE) || param.getFieldType().equals(EParameterFieldType.QUERYSTORE_TYPE) || param.getFieldType().equals(EParameterFieldType.ENCODING_TYPE)) {
                    boolean child = false;
                    Map<String, IElementParameter> childParameters = param.getChildParameters();
                    if ("PROPERTY".equals(param.getName()) || EParameterFieldType.PROPERTY_TYPE == param.getFieldType()) {
                        //$NON-NLS-1$
                        if (childParameters != null) {
                            //$NON-NLS-1$
                            IElementParameter iElementParameter = childParameters.get("PROPERTY_TYPE");
                            if (iElementParameter != null) {
                                Object value2 = iElementParameter.getValue();
                                if (variableValue.equals(value2.toString())) {
                                    child = true;
                                    found = true;
                                    value = value2.toString();
                                }
                            }
                        }
                    }
                    if (varNames.length > 1 && varNames[1] != null) {
                        IElementParameter tempParam = childParameters.get(varNames[1]);
                        if (tempParam != null) {
                            value = tempParam.getValue();
                            if (value.equals(variableValue)) {
                                found = true;
                            }
                            child = true;
                        }
                    }
                    if (!child) {
                        value = testedParameter.getValue();
                    }
                } else {
                    value = testedParameter.getValue();
                }
                if (value instanceof Integer) {
                    if (((Integer) value) < testedParameter.getListItemsValue().length) {
                        value = testedParameter.getListItemsValue()[(Integer) value];
                    }
                }
                if (value instanceof String) {
                    if (variableValue.equals(value)) {
                        found = true;
                    } else if (testedParameter.getListItemsValue() instanceof Object[]) {
                        Object[] values = testedParameter.getListItemsValue();
                        for (int i = 0; i < values.length && !found; i++) {
                            if (value.equals(values[i])) {
                                String variableCode = testedParameter.getListItemsDisplayCodeName()[i];
                                if (variableCode.equals(variableValue)) {
                                    found = true;
                                }
                            }
                        }
                    }
                } else if (value instanceof Boolean) {
                    Boolean tmpValue = new Boolean(variableValue);
                    if (tmpValue.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (test.equals(EQUALS)) {
                        showParameter = true;
                    }
                } else {
                    if (test.equals(NOT_EQUALS)) {
                        showParameter = true;
                    }
                }
                break;
            }
        }
        if (currentParam != null && "INCOMING_LINK_TYPE".equals(variableName)) {
            //$NON-NLS-1$
            IElement element = currentParam.getElement();
            if (element != null && element instanceof INode) {
                INode node = (INode) element;
                if (node.getComponent() != null && "tPigLoad".equals(node.getComponent().getName())) {
                    //$NON-NLS-1$
                    List<IConnection> connectionsInputs = (List<IConnection>) node.getIncomingConnections();
                    for (IConnection connection : connectionsInputs) {
                        if (connection.isActivate() && connection.getLineStyle().hasConnectionCategory(IConnectionCategory.MAIN) && variableValue.toUpperCase().equals(connection.getConnectorName())) {
                            showParameter = true;
                        }
                    }
                }
            }
        }
    }
    return showParameter;
}
Also used : INode(org.talend.core.model.process.INode) ArrayList(java.util.ArrayList) IConnection(org.talend.core.model.process.IConnection) IElementParameter(org.talend.core.model.process.IElementParameter) ArrayList(java.util.ArrayList) List(java.util.List) IElement(org.talend.core.model.process.IElement) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) Map(java.util.Map)

Example 12 with IElement

use of org.talend.core.model.process.IElement in project tdi-studio-se by Talend.

the class Expression method evaluateInExpression.

/**
     * Only works for any check in the schema actually, and only for DB_TYPE. Syntax should be like:
     *
     * SCHEMA.DB_TYPE IN ['BLOB','CLOB']
     *
     *
     * @param simpleExpression
     * @return
     */
private static boolean evaluateInExpression(String simpleExpression, List<? extends IElementParameter> listParam) {
    String[] strings = null;
    if (simpleExpression.contains(" IN [")) {
        //$NON-NLS-1$
        //$NON-NLS-1$
        strings = simpleExpression.split(" IN \\[");
    } else {
        //$NON-NLS-1$
        strings = simpleExpression.split(" IN\\[");
    }
    //$NON-NLS-1$
    String[] splittedString = strings[0].split("\\.");
    // take parameter name (SCHEMA in example here)
    String parameterName = splittedString[0];
    // we take only the value DB_TYPE
    String variableToTest = splittedString[1];
    IElementParameter currentParam = null;
    for (IElementParameter param : listParam) {
        if (param.getName().equals(parameterName)) {
            currentParam = param;
            break;
        }
    }
    if (currentParam == null) {
        // take the first one, in all case we only
        currentParam = listParam.get(0);
    }
    // want to get the element linked
    // string must be like:
    String valuesToTest = strings[1].substring(0, strings[1].length() - 1);
    // 'BLOB','CLOB']
    // so remove the last ]
    //$NON-NLS-1$
    String[] values = valuesToTest.split("'");
    if (values.length > 1) {
        // like: <empty> / A / , / B / , / C / <empty>
        if ("SCHEMA".equals(parameterName) && variableToTest.contains("DB_TYPE")) {
            //$NON-NLS-1$ //$NON-NLS-2$
            IElement element = currentParam.getElement();
            if (element == null || (!(element instanceof INode))) {
                throwUnsupportedExpression(simpleExpression, currentParam);
                return false;
            }
            INode node = (INode) element;
            for (String value : values) {
                if (value.isEmpty() || value.trim().equals(",")) {
                    //$NON-NLS-1$
                    continue;
                }
                for (IMetadataTable table : node.getMetadataList()) {
                    for (IMetadataColumn column : table.getListColumns()) {
                        if (column.getType() != null && column.getType().equals(value)) {
                            return true;
                        }
                    }
                }
            }
        } else if (currentParam.getFieldType() == EParameterFieldType.TABLE) {
            if (ArrayUtils.contains(currentParam.getListItemsDisplayCodeName(), variableToTest)) {
                List<Map<String, Object>> allLines = (List<Map<String, Object>>) currentParam.getValue();
                for (Map<String, Object> line : allLines) {
                    // for each line, check if the column we want have one of the value defined in the "IN".
                    Object o = line.get(variableToTest);
                    if (o != null && (o instanceof String)) {
                        String currentValue = (String) o;
                        for (String value : values) {
                            if (value.isEmpty() || value.trim().equals(",")) {
                                //$NON-NLS-1$
                                continue;
                            }
                            if (currentValue.equals(value)) {
                                return true;
                            }
                        }
                    }
                }
            }
        } else {
            throwUnsupportedExpression(simpleExpression, currentParam);
        }
    }
    return false;
}
Also used : IMetadataTable(org.talend.core.model.metadata.IMetadataTable) IElement(org.talend.core.model.process.IElement) INode(org.talend.core.model.process.INode) IElementParameter(org.talend.core.model.process.IElementParameter) ArrayList(java.util.ArrayList) List(java.util.List) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) Map(java.util.Map)

Example 13 with IElement

use of org.talend.core.model.process.IElement in project tdi-studio-se by Talend.

the class GenericElementParameter method updateSchema.

private void updateSchema() {
    IElement element = this.getElement();
    if (element instanceof Node) {
        Node node = (Node) element;
        List<INodeConnector> connectors = node.getConnectorsFromType(EConnectionType.FLOW_MAIN);
        for (INodeConnector connector : connectors) {
            if (connector instanceof GenericNodeConnector) {
                Connector componentConnector = ((GenericNodeConnector) connector).getComponentConnector();
                Schema schema = null;
                schema = getRootProperties().getSchema(componentConnector, ((GenericNodeConnector) connector).isOutput());
                IMetadataTable mainTable = node.getMetadataFromConnector(connector.getName());
                if (schema != null && mainTable != null) {
                    MetadataTable metadataTable = MetadataToolAvroHelper.convertFromAvro(schema);
                    IMetadataTable newTable = MetadataToolHelper.convert(metadataTable);
                    if (!mainTable.sameMetadataAs(newTable) || !newTable.sameMetadataAs(mainTable)) {
                        mainTable.setListColumns(newTable.getListColumns());
                        List<IElementParameter> schemaParameters = node.getElementParametersFromField(EParameterFieldType.SCHEMA_REFERENCE);
                        updateSchemaParameters(schemaParameters, connector.getName(), schema);
                        if (this.askPropagate == null && node.getOutgoingConnections().size() != 0) {
                            boolean hasPropagation = false;
                            for (IConnection connection : node.getOutgoingConnections()) {
                                if (connector.getName().equals(connection.getConnectorName())) {
                                    if (isSchemaPropagated(connection.getTarget())) {
                                        hasPropagation = true;
                                        break;
                                    }
                                }
                            }
                            if (hasPropagation) {
                                Display.getDefault().syncExec(new Runnable() {

                                    @Override
                                    public void run() {
                                        askPropagate = ChangeMetadataCommand.askPropagate();
                                    }
                                });
                            }
                        }
                        if (this.askPropagate != null && this.askPropagate) {
                            for (IConnection connection : node.getOutgoingConnections()) {
                                if (connector.getName().equals(connection.getConnectorName())) {
                                    INode target = connection.getTarget();
                                    if (!isSchemaPropagated(target)) {
                                        continue;
                                    }
                                    ChangeMetadataCommand cmd = new ChangeMetadataCommand(target, null, null, newTable, null);
                                    cmd.setPropagate(true);
                                    IProcess process = node.getProcess();
                                    if (process instanceof org.talend.designer.core.ui.editor.process.Process) {
                                        CommandStack commandStack = ((org.talend.designer.core.ui.editor.process.Process) process).getCommandStack();
                                        commandStack.execute(cmd);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        this.askPropagate = null;
    }
}
Also used : INodeConnector(org.talend.core.model.process.INodeConnector) Connector(org.talend.components.api.component.Connector) CommandStack(org.eclipse.gef.commands.CommandStack) IElement(org.talend.core.model.process.IElement) INode(org.talend.core.model.process.INode) Node(org.talend.designer.core.ui.editor.nodes.Node) INode(org.talend.core.model.process.INode) Schema(org.apache.avro.Schema) IConnection(org.talend.core.model.process.IConnection) IProcess(org.talend.core.model.process.IProcess) INodeConnector(org.talend.core.model.process.INodeConnector) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) IElementParameter(org.talend.core.model.process.IElementParameter) IProcess(org.talend.core.model.process.IProcess)

Example 14 with IElement

use of org.talend.core.model.process.IElement in project tdi-studio-se by Talend.

the class PropertiesTableByRowEditorView method createColumns.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.talend.commons.ui.swt.advanced.dataeditor.AbstractDataTableEditorView#createColumns(org.talend.commons.ui
     * .swt.tableviewer.TableViewerCreator, org.eclipse.swt.widgets.Table)
     */
@Override
protected void createColumns(TableViewerCreator<B> tableViewerCreator, Table table) {
    // there's two lists of values, one that will be in the table
    // and the other will be stored as the current value in the property
    // there is two lists because of the undo / redo capabilities
    PropertiesTableEditorModel model = getModel();
    TalendProposalProvider processProposalProvider = new TalendProposalProvider(model.getProcess());
    String[] titles = null;
    // final Element elem = model.getElement();
    final IElementParameter param = model.getElemParameter();
    final IElement element = model.getElement();
    if (element instanceof Node) {
        List<IConnection> listConnection = (List<IConnection>) ((Node) element).getInputs();
        List<String> names = new ArrayList<String>();
        for (IConnection con : listConnection) {
            names.add(con.getName());
        }
        titles = new String[names.size()];
        for (int i = 0; i < names.size(); i++) {
            titles[i] = names.get(i);
        }
    }
    if (titles != null && titles.length > 0) {
        for (int i = 0; i < titles.length; i++) {
            final int curCol = i;
            boolean toDisplay = true;
            if (toDisplay) {
                final TableViewerCreatorColumn column = new TableViewerCreatorColumn(tableViewerCreator);
                column.setTitle(titles[i]);
                column.setModifiable(true);
                column.setMinimumWidth(100);
                column.setWeight(20);
                String[] stringToDisplay = null;
                if (element instanceof Node) {
                    List<IConnection> listConnection = (List<IConnection>) ((Node) element).getInputs();
                    for (IConnection con : listConnection) {
                        if (con.getName().equals(titles[i])) {
                            List<IMetadataColumn> columns = con.getMetadataTable().getListColumns();
                            stringToDisplay = new String[columns.size()];
                            for (int j = 0; j < columns.size(); j++) {
                                stringToDisplay[j] = columns.get(j).getLabel();
                            }
                        }
                    }
                }
                final EditableComboBoxCellEditor cellEditor = new EditableComboBoxCellEditor(table, stringToDisplay);
                // column.setLabelProvider(new IColumnLabelProvider() {
                //
                // @Override
                // public String getLabel(Object bean) {
                // if (bean instanceof Map) {
                // Map valueMap = (Map) bean;
                // String columnName = column.getTitle();
                // if (valueMap.containsKey(columnName)) {
                // String value = (String) valueMap.get(columnName);
                // return value;
                // }
                // }
                //                            return ""; //$NON-NLS-1$
                // }
                // });
                // column.setCellEditor(cellEditor);
                column.setCellEditor(cellEditor, new CellEditorValueAdapter() {

                    @Override
                    public String getColumnText(CellEditor cellEditor, Object bean, Object cellEditorValue) {
                        Map valueMap = (Map) bean;
                        String columnName = column.getTitle();
                        if (valueMap.containsKey(columnName)) {
                            String value = (String) valueMap.get(columnName);
                            return value;
                        }
                        return "";
                    }

                    @Override
                    public Object getOriginalTypedValue(CellEditor cellEditor, Object cellEditorTypedValue) {
                        Object returnedValue = null;
                        CCombo combo = (CCombo) cellEditor.getControl();
                        int rowNumber = ((Table) combo.getParent()).getSelectionIndex();
                        String[] listToDisplay = getItem(element, column.getTitle());
                        if (!Arrays.equals(listToDisplay, ((ComboBoxCellEditor) cellEditor).getItems())) {
                            ((ComboBoxCellEditor) cellEditor).setItems(listToDisplay);
                        }
                        if (cellEditorTypedValue != null && cellEditorTypedValue instanceof String) {
                            return cellEditorTypedValue;
                        }
                        return "";
                    }

                    ;

                    @Override
                    public Object getCellEditorTypedValue(CellEditor cellEditor, Object originalTypedValue) {
                        CCombo combo = (CCombo) cellEditor.getControl();
                        int rowNumber = ((Table) combo.getParent()).getSelectionIndex();
                        String[] listToDisplay = getItem(element, column.getTitle());
                        if (!Arrays.equals(listToDisplay, ((ComboBoxCellEditor) cellEditor).getItems())) {
                            ((ComboBoxCellEditor) cellEditor).setItems(listToDisplay);
                        }
                        Object returnedValue = 0;
                        if (originalTypedValue != null) {
                            String[] namesSet = listToDisplay;
                            for (int j = 0; j < namesSet.length; j++) {
                                if (namesSet[j].equals(originalTypedValue)) {
                                    returnedValue = j;
                                    break;
                                }
                            }
                        }
                        return returnedValue;
                    }

                    ;

                    private String[] getItem(IElement element, String columnName) {
                        List<String> stringToDisplay = new ArrayList<String>();
                        if (element instanceof Node) {
                            List<IConnection> listConnection = (List<IConnection>) ((Node) element).getInputs();
                            for (IConnection con : listConnection) {
                                if (con.getName().equals(columnName)) {
                                    List<IMetadataColumn> columns = con.getMetadataTable().getListColumns();
                                    for (IMetadataColumn column : columns) {
                                        stringToDisplay.add(column.getLabel());
                                    }
                                }
                            }
                        }
                        String[] listToDisplay = stringToDisplay.toArray(new String[0]);
                        return listToDisplay;
                    }
                });
                column.setColumnCellModifier(new ColumnCellModifier(column) {

                    @Override
                    public boolean canModify(Object bean) {
                        boolean canModify = super.canModify(bean);
                        return canModify;
                    }

                    @Override
                    public Object getValue(Object bean) {
                        Map valueMap = (Map) bean;
                        String columnName = column.getTitle();
                        if (valueMap.containsKey(columnName)) {
                            String value = (String) valueMap.get(columnName);
                            return value;
                        }
                        return "";
                    }
                });
                column.setBeanPropertyAccessors(new IBeanPropertyAccessors<B, Object>() {

                    @Override
                    public Object get(B bean) {
                        Map valueMap = (Map) bean;
                        String columnName = column.getTitle();
                        if (valueMap.containsKey(columnName)) {
                            return valueMap.get(columnName);
                        }
                        return "";
                    }

                    @Override
                    public void set(B bean, Object value) {
                        if (value == null) {
                            return;
                        }
                        Map valueMap = (Map) bean;
                        String columnName = column.getTitle();
                        if (valueMap.containsKey(columnName)) {
                            valueMap.remove(columnName);
                            valueMap.put(columnName, value);
                        } else {
                            valueMap.put(columnName, value);
                        }
                    }
                });
            }
        }
    }
}
Also used : TalendProposalProvider(org.talend.core.ui.proposal.TalendProposalProvider) EditableComboBoxCellEditor(org.talend.commons.ui.swt.tableviewer.celleditor.EditableComboBoxCellEditor) CellEditor(org.eclipse.jface.viewers.CellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) Node(org.talend.designer.core.ui.editor.nodes.Node) ArrayList(java.util.ArrayList) CellEditorValueAdapter(org.talend.commons.ui.runtime.swt.tableviewer.behavior.CellEditorValueAdapter) IConnection(org.talend.core.model.process.IConnection) ColumnCellModifier(org.talend.commons.ui.runtime.swt.tableviewer.behavior.ColumnCellModifier) IElementParameter(org.talend.core.model.process.IElementParameter) ArrayList(java.util.ArrayList) List(java.util.List) IElement(org.talend.core.model.process.IElement) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) CCombo(org.eclipse.swt.custom.CCombo) TableViewerCreatorColumn(org.talend.commons.ui.swt.tableviewer.TableViewerCreatorColumn) EditableComboBoxCellEditor(org.talend.commons.ui.swt.tableviewer.celleditor.EditableComboBoxCellEditor) Map(java.util.Map)

Example 15 with IElement

use of org.talend.core.model.process.IElement in project tdi-studio-se by Talend.

the class GenericTableUtilsTest method testSetTableValues.

@Test
public void testSetTableValues() {
    IElement elem = new FakeElement("test");
    IElementParameter tableParam = new ElementParameter(elem);
    tableParam.setName("myTable");
    IElementParameter column1 = new ElementParameter(elem);
    column1.setName("column1");
    column1.setFieldType(EParameterFieldType.TEXT);
    IElementParameter column2 = new ElementParameter(elem);
    column2.setName("column2");
    column2.setFieldType(EParameterFieldType.CHECK);
    IElementParameter column3 = new ElementParameter(elem);
    column3.setName("column3");
    column3.setFieldType(EParameterFieldType.CLOSED_LIST);
    List<String> values = new ArrayList<>();
    values.add("Value1");
    values.add("Value2");
    values.add("Value3");
    column3.setListItemsValue(values.toArray());
    List<String> codeNames = new ArrayList<>();
    codeNames.add("column1");
    codeNames.add("column2");
    codeNames.add("column3");
    tableParam.setListItemsDisplayCodeName(codeNames.toArray(new String[0]));
    List<IElementParameter> childParams = new ArrayList<>();
    childParams.add(column1);
    childParams.add(column2);
    childParams.add(column3);
    tableParam.setListItemsValue(childParams.toArray());
    List<Map<String, Object>> table = new ArrayList<Map<String, Object>>();
    Map<String, Object> line = new HashMap<String, Object>();
    line.put("column1", "Text1");
    line.put("column2", Boolean.TRUE);
    line.put("column3", "Value1");
    table.add(line);
    line = new HashMap<String, Object>();
    line.put("column1", "Text2");
    line.put("column2", Boolean.FALSE);
    line.put("column3", 2);
    table.add(line);
    MyTestTable tableProperties = new MyTestTable("tableProp");
    GenericTableUtils.setTableValues(tableProperties, table, tableParam);
    // call 2 times, to ensure the number of line is not added at each calls
    GenericTableUtils.setTableValues(tableProperties, table, tableParam);
    assertEquals(2, ((List) tableProperties.column1.getValue()).size());
    assertEquals(2, ((List) tableProperties.column2.getValue()).size());
    assertEquals(2, ((List) tableProperties.column3.getValue()).size());
    assertEquals("Text1", ((List) tableProperties.column1.getValue()).get(0));
    assertEquals("Text2", ((List) tableProperties.column1.getValue()).get(1));
    assertEquals("true", ((List) tableProperties.column2.getValue()).get(0));
    assertEquals("false", ((List) tableProperties.column2.getValue()).get(1));
    assertEquals("Value1", ((List) tableProperties.column3.getValue()).get(0));
    assertEquals("Value3", ((List) tableProperties.column3.getValue()).get(1));
}
Also used : IElement(org.talend.core.model.process.IElement) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FakeElement(org.talend.designer.core.model.FakeElement) IElementParameter(org.talend.core.model.process.IElementParameter) ElementParameter(org.talend.designer.core.model.components.ElementParameter) IElementParameter(org.talend.core.model.process.IElementParameter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

IElement (org.talend.core.model.process.IElement)17 IElementParameter (org.talend.core.model.process.IElementParameter)13 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 INode (org.talend.core.model.process.INode)8 List (java.util.List)6 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)6 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)6 IConnection (org.talend.core.model.process.IConnection)5 Node (org.talend.designer.core.ui.editor.nodes.Node)5 HashMap (java.util.HashMap)4 IProcess (org.talend.core.model.process.IProcess)4 CellEditor (org.eclipse.jface.viewers.CellEditor)2 ComboBoxCellEditor (org.eclipse.jface.viewers.ComboBoxCellEditor)2 CCombo (org.eclipse.swt.custom.CCombo)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Test (org.junit.Test)2 CellEditorValueAdapter (org.talend.commons.ui.runtime.swt.tableviewer.behavior.CellEditorValueAdapter)2 ColumnCellModifier (org.talend.commons.ui.runtime.swt.tableviewer.behavior.ColumnCellModifier)2 TableViewerCreatorColumn (org.talend.commons.ui.swt.tableviewer.TableViewerCreatorColumn)2