Search in sources :

Example 1 with AttributeVector

use of dna.dataStructures.AttributeVector in project dna by leifeld.

the class Popup method saveContents.

/**
 * In a statement popup window, read the contents from all combo boxes and save them into the database and GUI data structure.
 *
 * @param gridBagPanel  The panel that contains the combo boxes
 * @param statementID   The ID of the statement that is being edited
 */
@SuppressWarnings("unchecked")
public static void saveContents(JPanel gridBagPanel, int statementID) {
    Component[] com = gridBagPanel.getComponents();
    for (int i = 0; i < com.length; i++) {
        // the value of a variable, e.g., "EPA"
        Object content = null;
        // the variable name, e.g., "organization"
        String contentType = null;
        if (com[i].getClass().getName().equals("javax.swing.JComboBox")) {
            // short text
            contentType = ((JLabel) com[i - 1]).getText();
            // save the combo box
            JComboBox<?> box = (JComboBox<?>) com[i];
            // save its value as a string (no matter if it's a string or an attribute vector)
            String value = box.getEditor().getItem().toString();
            // look up if it's an attribute vector
            int attributeId = Dna.data.getAttributeId((String) value, contentType, statementTypeId);
            boolean newAttribute = false;
            if (attributeId == -1) {
                // if not, create a new attribute vector and interpret as string below
                newAttribute = true;
                int id = Dna.data.generateNewId("attributes");
                int statementTypeId = Dna.data.getStatement(statementID).getStatementTypeId();
                AttributeVector attributeVector = new AttributeVector(id, (String) value, new Color(0, 0, 0), "", "", "", "", statementTypeId, contentType);
                Dna.dna.addAttributeVector(attributeVector);
            }
            if (newAttribute == false) {
                try {
                    content = ((AttributeVector) ((JComboBox<AttributeVector>) com[i]).getEditor().getItem()).getValue();
                } catch (java.lang.ClassCastException e) {
                    // attribute exists, but combo box only contains text for some weird reason
                    content = value;
                }
            } else {
                content = value;
            }
            if (content == null) {
                content = "";
            }
            Dna.dna.updateVariable(statementId, statementTypeId, content, contentType);
        } else if (com[i].getClass().getName().equals("javax.swing.JCheckBox")) {
            // boolean
            contentType = ((JLabel) com[i - 1]).getText();
            content = ((JCheckBox) com[i]).isSelected();
            int intBool;
            if ((Boolean) content == false) {
                intBool = 0;
            } else {
                intBool = 1;
            }
            Dna.dna.updateVariable(statementId, statementTypeId, intBool, contentType);
        } else if (com[i].getClass().getName().equals("javax.swing.JScrollPane")) {
            // long text
            contentType = ((JLabel) com[i - 1]).getText();
            JScrollPane jsp = ((JScrollPane) com[i]);
            JTextArea jta = (JTextArea) jsp.getViewport().getView();
            content = jta.getText();
            if (content == null) {
                content = "";
            }
            Dna.dna.updateVariable(statementId, statementTypeId, content, contentType);
        } else if (com[i].getClass().getName().equals("javax.swing.JPanel")) {
            // integer
            contentType = ((JLabel) com[i - 1]).getText();
            JPanel jp = (JPanel) com[i];
            JSpinner jsp = (JSpinner) jp.getComponent(0);
            content = jsp.getValue();
            Dna.dna.updateVariable(statementId, statementTypeId, content, contentType);
        }
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) AttributeVector(dna.dataStructures.AttributeVector) JTextArea(javax.swing.JTextArea) JComboBox(javax.swing.JComboBox) Color(java.awt.Color) JLabel(javax.swing.JLabel) Point(java.awt.Point) JCheckBox(javax.swing.JCheckBox) JSpinner(javax.swing.JSpinner) Component(java.awt.Component)

Example 2 with AttributeVector

use of dna.dataStructures.AttributeVector in project dna by leifeld.

the class AttributePanel method variableFilter.

private void variableFilter(String variable, int statementTypeId) {
    RowFilter<AttributeTableModel, Integer> attributeFilter = new RowFilter<AttributeTableModel, Integer>() {

        public boolean include(Entry<? extends AttributeTableModel, ? extends Integer> entry) {
            AttributeTableModel atm = entry.getModel();
            AttributeVector av = atm.get(entry.getIdentifier());
            int stId = av.getStatementTypeId();
            String var = av.getVariable();
            if (variable.equals(var) && stId == statementTypeId) {
                return true;
            } else {
                return false;
            }
        }
    };
    sorter.setRowFilter(attributeFilter);
}
Also used : AttributeVector(dna.dataStructures.AttributeVector) RowFilter(javax.swing.RowFilter) AttributeTableModel(dna.renderer.AttributeTableModel)

Example 3 with AttributeVector

use of dna.dataStructures.AttributeVector in project dna by leifeld.

the class SqlConnection method getAllAttributes.

/**
 * Read all meta-variables/attributes from SQL database and return them as an array list of attribute vectors.
 *
 * @return array list of attribute vectors, containing attributes for the statement values
 */
private ArrayList<AttributeVector> getAllAttributes() {
    ArrayList<AttributeVector> al = new ArrayList<AttributeVector>();
    try {
        String myQuery = "SELECT ATTRIBUTES.*, VARIABLES.StatementTypeId, VARIABLES.Variable FROM ATTRIBUTES LEFT JOIN VARIABLES ON ATTRIBUTES.VariableId = VARIABLES.ID";
        PreparedStatement preStatement = (PreparedStatement) connection.prepareStatement(myQuery);
        ResultSet result = preStatement.executeQuery();
        int id;
        String value;
        Color color;
        String type;
        String alias;
        String notes;
        String childOf;
        int statementTypeId;
        String variable;
        AttributeVector av;
        if (result.next()) {
            do {
                id = result.getInt("ID");
                value = result.getString("Value");
                color = new Color(result.getInt("Red"), result.getInt("Green"), result.getInt("Blue"));
                type = result.getString("Type");
                alias = result.getString("Alias");
                notes = result.getString("Notes");
                childOf = result.getString("childOf");
                statementTypeId = result.getInt("StatementTypeId");
                variable = result.getString("Variable");
                av = new AttributeVector(id, value, color, type, alias, notes, childOf, statementTypeId, variable);
                al.add(av);
            } while (result.next());
        }
        result.close();
        preStatement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return al;
}
Also used : AttributeVector(dna.dataStructures.AttributeVector) SQLException(java.sql.SQLException) Color(java.awt.Color) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with AttributeVector

use of dna.dataStructures.AttributeVector in project dna by leifeld.

the class RecodePanel method recode.

public int recode() {
    int count = 0;
    int statementTypeId = ((StatementType) typeComboBox.getSelectedItem()).getId();
    String variable = (String) entryBox.getSelectedItem();
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        String original = (String) tableModel.getValueAt(i, 0);
        String edited = (String) tableModel.getValueAt(i, 1);
        if (!original.equals(edited)) {
            // if the entry on the left and on the right of the recode table don't match...
            for (int j = 0; j < Dna.data.getStatements().size(); j++) {
                // ...go through the statements and change all instances
                String value = (String) Dna.data.getStatements().get(j).getValues().get(variable);
                if (value != null && value.equals(original)) {
                    int statementId = Dna.data.getStatements().get(j).getId();
                    Dna.dna.updateVariable(statementId, statementTypeId, edited, variable);
                    count++;
                }
            }
            int avIndexOriginal = Dna.data.getAttributeIndex(original, variable, statementTypeId);
            AttributeVector avOriginal = Dna.data.getAttributes().get(avIndexOriginal);
            int avIndexTarget = Dna.data.getAttributeIndex(edited, variable, statementTypeId);
            if (avIndexTarget == -1) {
                // edited version does not exist yet, so just rename it
                Dna.dna.updateAttributeValue(avIndexOriginal, edited);
            } else {
                // already exists
                AttributeVector avTarget = Dna.data.getAttributes().get(avIndexTarget);
                ArrayList<Integer> indicesToDelete = new ArrayList<Integer>();
                if ((// both are identical or the original one is empty, so delete the original one
                avOriginal.getType().equals("") && avOriginal.getAlias().equals("") && avOriginal.getNotes().equals("") && avOriginal.getColor().equals(Color.BLACK)) || (avOriginal.getType().equals(avTarget.getType()) && avOriginal.getAlias().equals(avTarget.getAlias()) && avOriginal.getNotes().equals(avTarget.getNotes()) && avOriginal.getColor().equals(avTarget.getColor()))) {
                    indicesToDelete.add(avIndexOriginal);
                } else if (// target is empty while original is not, so delete target and rename original
                avTarget.getType().equals("") && avTarget.getAlias().equals("") && avTarget.getNotes().equals("") && avTarget.getColor().equals(Color.BLACK) && (!avOriginal.getType().equals("") || !avOriginal.getAlias().equals("") || !avOriginal.getNotes().equals("") || !avOriginal.getColor().equals(Color.BLACK))) {
                    Dna.dna.updateAttributeValue(avIndexOriginal, edited);
                    indicesToDelete.add(avIndexTarget);
                } else {
                    // more complicated: both contain some contents, so keep both
                    System.out.println("Case 3: " + avTarget.getValue() + avOriginal.getValue());
                // nothing to do
                }
                if (indicesToDelete.size() > 0) {
                    // now delete those that were marked earlier
                    for (int j = indicesToDelete.size() - 1; j > -1; j--) {
                        Dna.dna.deleteAttributeVector(indicesToDelete.get(j));
                    }
                }
            }
        }
    }
    return count;
}
Also used : AttributeVector(dna.dataStructures.AttributeVector) StatementType(dna.dataStructures.StatementType) ArrayList(java.util.ArrayList)

Example 5 with AttributeVector

use of dna.dataStructures.AttributeVector in project dna by leifeld.

the class AttributeComboBoxRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value == null) {
        return new JLabel("");
    } else {
        AttributeVector a = (AttributeVector) value;
        JLabel label = new JLabel(a.getValue());
        label.setForeground(a.getColor());
        if (a.isInDataset() == false) {
            label.setBackground(new Color(255, 102, 102));
            label.setOpaque(true);
        }
        return label;
    }
}
Also used : AttributeVector(dna.dataStructures.AttributeVector) Color(java.awt.Color) JLabel(javax.swing.JLabel)

Aggregations

AttributeVector (dna.dataStructures.AttributeVector)5 Color (java.awt.Color)3 ArrayList (java.util.ArrayList)2 JLabel (javax.swing.JLabel)2 StatementType (dna.dataStructures.StatementType)1 AttributeTableModel (dna.renderer.AttributeTableModel)1 Component (java.awt.Component)1 Point (java.awt.Point)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 JCheckBox (javax.swing.JCheckBox)1 JComboBox (javax.swing.JComboBox)1 JPanel (javax.swing.JPanel)1 JScrollPane (javax.swing.JScrollPane)1 JSpinner (javax.swing.JSpinner)1 JTextArea (javax.swing.JTextArea)1 RowFilter (javax.swing.RowFilter)1