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;
}
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;
}
}
Aggregations