001package com.ganteater.ae.desktop.ui;
002
003import java.util.HashMap;
004import java.util.Map;
005
006import javax.swing.table.AbstractTableModel;
007
008import org.apache.commons.lang.ObjectUtils;
009import org.apache.commons.lang.StringUtils;
010
011import com.ganteater.ae.AEWorkspace;
012import com.ganteater.ae.TemplateProcessor;
013import com.ganteater.ae.util.xml.easyparser.Node;
014
015public class InputTableModel extends AbstractTableModel {
016
017        private static final String PWD_MASK = "************";
018
019        private static final long serialVersionUID = 2907837684253858613L;
020
021        private Node[] theResultVariablesNodes = null;
022        private Map<String, Object> variables;
023        private Map<String, String> types;
024
025        public InputTableModel(TemplateProcessor unit, String title, Node[] theResultVariablesNodes) {
026
027                this.theResultVariablesNodes = theResultVariablesNodes;
028                this.variables = new HashMap<>();
029                this.types = new HashMap<>();
030
031                for (Node node : theResultVariablesNodes) {
032                        String name = node.getAttribute("name");
033                        String value = null;
034                        String valueAttribute = node.getAttribute("value");
035                        String type = node.getAttribute("type");
036                        types.put(name, type);
037
038                        if (valueAttribute != null) {
039                                value = unit.replaceProperties(valueAttribute);
040                        } else {
041                                value = unit.getVariableString(name);
042                        }
043
044                        if (value == null) {
045                                String defaultValue = node.getAttribute("default");
046                                String savedVarName = ".inputValue." + title + "." + StringUtils.upperCase(name);
047                                value = AEWorkspace.getInstance().getDefaultUserConfiguration(savedVarName, defaultValue);
048                        }
049
050                        value = StringUtils.trimToNull(value);
051                        if (value != null) {
052                                this.variables.put(name, value);
053                        }
054                }
055        }
056
057        public int getColumnCount() {
058                return 2;
059        }
060
061        public int getRowCount() {
062                return theResultVariablesNodes.length;
063        }
064
065        public Object getValueAt(int rowIndex, int columnIndex) {
066                String name = theResultVariablesNodes[rowIndex].getAttribute("name");
067
068                Object value = variables.get(name);
069                String type = types.get(name);
070                if ("password".equals(type)) {
071                        if (value != null && columnIndex == 1) {
072                                value = PWD_MASK;
073                        }
074                }
075
076                return columnIndex == 0 ? name : value;
077        }
078
079        @Override
080        public String getColumnName(int column) {
081                return column == 0 ? "Property name" : "Value";
082        }
083
084        @Override
085        public boolean isCellEditable(int rowIndex, int columnIndex) {
086                return columnIndex == 1;
087        }
088
089        @Override
090        public void setValueAt(Object value, int rowIndex, int columnIndex) {
091                String name = theResultVariablesNodes[rowIndex].getAttribute("name");
092                String type = types.get(name);
093                if (!("password".equals(type)
094                                && (PWD_MASK.equals(value) || StringUtils.isBlank(ObjectUtils.toString(value))))) {
095                        variables.put(name, value);
096                }
097        }
098
099}