Search in sources :

Example 6 with StatementType

use of dna.dataStructures.StatementType 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 7 with StatementType

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

the class StatementTypeComboBoxRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value == null) {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("");
        panel.add(label);
        return panel;
    } else {
        StatementType statementType = (StatementType) value;
        @SuppressWarnings("serial") JButton colorRectangle = (new JButton() {

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(statementType.getColor());
                g.fillRect(2, 2, 14, 14);
            }
        });
        colorRectangle.setPreferredSize(new Dimension(14, 14));
        colorRectangle.setEnabled(false);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        panel.add(colorRectangle);
        JLabel statementTypeLabel = new JLabel(statementType.getLabel());
        panel.add(statementTypeLabel);
        if (isSelected) {
            UIDefaults defaults = javax.swing.UIManager.getDefaults();
            Color bg = defaults.getColor("List.selectionBackground");
            panel.setBackground(bg);
        }
        return panel;
    }
}
Also used : Graphics(java.awt.Graphics) JPanel(javax.swing.JPanel) UIDefaults(javax.swing.UIDefaults) FlowLayout(java.awt.FlowLayout) StatementType(dna.dataStructures.StatementType) Color(java.awt.Color) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension)

Aggregations

StatementType (dna.dataStructures.StatementType)7 ArrayList (java.util.ArrayList)5 Statement (dna.dataStructures.Statement)2 Color (java.awt.Color)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 AttributeVector (dna.dataStructures.AttributeVector)1 Data (dna.dataStructures.Data)1 Dimension (java.awt.Dimension)1 FlowLayout (java.awt.FlowLayout)1 Graphics (java.awt.Graphics)1 GregorianCalendar (java.util.GregorianCalendar)1 DefaultListModel (javax.swing.DefaultListModel)1 JButton (javax.swing.JButton)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 UIDefaults (javax.swing.UIDefaults)1