add "size" option to table, so that 16- and 32-bit bytes are supported

This commit is contained in:
stirkac 2019-04-15 11:56:11 +02:00
parent fee885e8c8
commit 7885cb9497
1 changed files with 197 additions and 151 deletions

View File

@ -23,7 +23,12 @@ import java.awt.BorderLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Insets; import java.awt.Insets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -33,7 +38,6 @@ import javax.swing.JPanel;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import com.romraider.util.ByteUtil; import com.romraider.util.ByteUtil;
import static com.romraider.util.ByteUtil.isBitSet;
import static javax.swing.JOptionPane.ERROR_MESSAGE; import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog; import static javax.swing.JOptionPane.showMessageDialog;
@ -43,6 +47,7 @@ public class TableBitwiseSwitch extends Table {
private static final long serialVersionUID = -4887718305447362308L; private static final long serialVersionUID = -4887718305447362308L;
private final Map<String, Integer> controlBits = new HashMap<String, Integer>(); private final Map<String, Integer> controlBits = new HashMap<String, Integer>();
private ArrayList<JCheckBox> checkboxes; private ArrayList<JCheckBox> checkboxes;
private int dataSize = 0;
public TableBitwiseSwitch() { public TableBitwiseSwitch() {
super(); super();
@ -53,24 +58,34 @@ public class TableBitwiseSwitch extends Table {
} }
@Override @Override
public void populateTable(byte[] input, int romRamOffset) throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException { public void populateTable(byte[] input, int romRamOffset)
byte currentValue = input[storageAddress]; throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException {
int maxBitPosition = ((dataSize * 8) - 1);
boolean[] bits_array = new boolean[maxBitPosition + 1];
for (int i = 0; i < dataSize; i++) {
boolean[] byte_values = ByteUtil.byteToBoolArr(input[storageAddress + i]);
for (int j = 0; j < 8; j++) {
bits_array[((dataSize - 1 - i) *8)+ j] = byte_values[7-j];
}
}
JPanel radioPanel = new JPanel(new GridLayout(0, 1)); JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(new JLabel(" " + getName())); radioPanel.add(new JLabel(" " + getName()));
for (Entry<String, Integer> entry: controlBits.entrySet()) {
if (entry.getValue() > 7) { for (Entry<String, Integer> entry : sortByValue(controlBits).entrySet()) {
String mismatch = String.format("Table: %s%nDefinition file specified an out of range bit switch! Only values 0-7 are valid!", super.getName()); if (entry.getValue() > maxBitPosition) {
showMessageDialog(this, String mismatch = String.format(
mismatch, "Table: %s%nDefinition file specified an out of range bit switch! Only values >= 0 and < size * 8 are allowed!",
"ERROR - Incorrect definition", super.getName());
ERROR_MESSAGE); showMessageDialog(this, mismatch, "ERROR - Incorrect definition", ERROR_MESSAGE);
continue; } else {
}
JCheckBox cb = new JCheckBox(entry.getKey()); JCheckBox cb = new JCheckBox(entry.getKey());
cb.setSelected(isBitSet(currentValue, entry.getValue())); cb.setSelected(bits_array[entry.getValue()]);
checkboxes.add(cb); checkboxes.add(cb);
radioPanel.add(cb); radioPanel.add(cb);
} }
}
add(radioPanel, BorderLayout.CENTER); add(radioPanel, BorderLayout.CENTER);
} }
@ -92,23 +107,26 @@ public class TableBitwiseSwitch extends Table {
descriptionArea.setEditable(false); descriptionArea.setEditable(false);
descriptionArea.setWrapStyleWord(true); descriptionArea.setWrapStyleWord(true);
descriptionArea.setLineWrap(true); descriptionArea.setLineWrap(true);
descriptionArea.setMargin(new Insets(0,5,5,5)); descriptionArea.setMargin(new Insets(0, 5, 5, 5));
add(descriptionArea, BorderLayout.SOUTH); add(descriptionArea, BorderLayout.SOUTH);
} }
@Override @Override
public byte[] saveFile(byte[] input) { public byte[] saveFile(byte[] input) {
// get original values
boolean[] bools = ByteUtil.byteToBoolArr(input[storageAddress]);
// set to new state from checkboxes, in reverse order for (Entry<String, Integer> entry : controlBits.entrySet()) {
for (Entry<String, Integer> entry: controlBits.entrySet()) { int entry_offset = (dataSize - 1) - (entry.getValue() / 8);
int bitpos = 7 - (entry.getValue() % 8);
boolean[] bools = ByteUtil.byteToBoolArr(input[storageAddress + entry_offset]);
JCheckBox cb = getButtonByText(entry.getKey()); JCheckBox cb = getButtonByText(entry.getKey());
bools[7 -entry.getValue()] = cb.isSelected(); bools[bitpos] = cb.isSelected();
byte result = ByteUtil.booleanArrayToBit(bools);
input[storageAddress + entry_offset] = result;
} }
input[storageAddress] = ByteUtil.booleanArrayToBit(bools);
return input; return input;
} }
@ -164,41 +182,52 @@ public class TableBitwiseSwitch extends Table {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
try { try {
if(null == other) { if (null == other) {
return false; return false;
} }
if(other == this) { if (other == this) {
return true; return true;
} }
if(!(other instanceof TableBitwiseSwitch)) { if (!(other instanceof TableBitwiseSwitch)) {
return false; return false;
} }
TableBitwiseSwitch otherTable = (TableBitwiseSwitch) other; TableBitwiseSwitch otherTable = (TableBitwiseSwitch) other;
if( (null == this.getName() && null == otherTable.getName()) if ((null == this.getName() && null == otherTable.getName())
|| (this.getName().isEmpty() && otherTable.getName().isEmpty()) ) { || (this.getName().isEmpty() && otherTable.getName().isEmpty())) {
;// Skip name compare if name is null or empty. ;// Skip name compare if name is null or empty.
} else if(!this.getName().equalsIgnoreCase(otherTable.getName())) { } else if (!this.getName().equalsIgnoreCase(otherTable.getName())) {
return false; return false;
} }
if(this.getDataSize() != otherTable.getDataSize()) { if (this.getDataSize() != otherTable.getDataSize()) {
return false; return false;
} }
if(this.getSwitchStates().keySet().equals(otherTable.getSwitchStates().keySet())) { if (this.getSwitchStates().keySet().equals(otherTable.getSwitchStates().keySet())) {
return true; return true;
} }
return false; return false;
} catch(Exception ex) { } catch (Exception ex) {
// TODO: Log Exception. // TODO: Log Exception.
return false; return false;
} }
} }
@Override
public void setDataSize(int size) {
if (dataSize == 0)
dataSize = size;
}
@Override
public int getDataSize() {
return dataSize;
}
@Override @Override
public void populateCompareValues(Table compareTable) { public void populateCompareValues(Table compareTable) {
return; // Do nothing. return; // Do nothing.
@ -210,8 +239,7 @@ public class TableBitwiseSwitch extends Table {
} }
@Override @Override
public void drawTable() public void drawTable() {
{
return; // Do nothing. return; // Do nothing.
} }
@ -239,4 +267,22 @@ public class TableBitwiseSwitch extends Table {
} }
return null; return null;
} }
private Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) {
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
} }