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.Insets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -33,7 +38,6 @@ import javax.swing.JPanel;
import javax.swing.JTextArea;
import com.romraider.util.ByteUtil;
import static com.romraider.util.ByteUtil.isBitSet;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
@ -43,6 +47,7 @@ public class TableBitwiseSwitch extends Table {
private static final long serialVersionUID = -4887718305447362308L;
private final Map<String, Integer> controlBits = new HashMap<String, Integer>();
private ArrayList<JCheckBox> checkboxes;
private int dataSize = 0;
public TableBitwiseSwitch() {
super();
@ -53,24 +58,34 @@ public class TableBitwiseSwitch extends Table {
}
@Override
public void populateTable(byte[] input, int romRamOffset) throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException {
byte currentValue = input[storageAddress];
public void populateTable(byte[] input, int romRamOffset)
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));
radioPanel.add(new JLabel(" " + getName()));
for (Entry<String, Integer> entry: controlBits.entrySet()) {
if (entry.getValue() > 7) {
String mismatch = String.format("Table: %s%nDefinition file specified an out of range bit switch! Only values 0-7 are valid!", super.getName());
showMessageDialog(this,
mismatch,
"ERROR - Incorrect definition",
ERROR_MESSAGE);
continue;
}
for (Entry<String, Integer> entry : sortByValue(controlBits).entrySet()) {
if (entry.getValue() > maxBitPosition) {
String mismatch = String.format(
"Table: %s%nDefinition file specified an out of range bit switch! Only values >= 0 and < size * 8 are allowed!",
super.getName());
showMessageDialog(this, mismatch, "ERROR - Incorrect definition", ERROR_MESSAGE);
} else {
JCheckBox cb = new JCheckBox(entry.getKey());
cb.setSelected(isBitSet(currentValue, entry.getValue()));
cb.setSelected(bits_array[entry.getValue()]);
checkboxes.add(cb);
radioPanel.add(cb);
}
}
add(radioPanel, BorderLayout.CENTER);
}
@ -99,16 +114,19 @@ public class TableBitwiseSwitch extends Table {
@Override
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()) {
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());
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;
}
@ -199,6 +217,17 @@ public class TableBitwiseSwitch extends Table {
}
}
@Override
public void setDataSize(int size) {
if (dataSize == 0)
dataSize = size;
}
@Override
public int getDataSize() {
return dataSize;
}
@Override
public void populateCompareValues(Table compareTable) {
return; // Do nothing.
@ -210,8 +239,7 @@ public class TableBitwiseSwitch extends Table {
}
@Override
public void drawTable()
{
public void drawTable() {
return; // Do nothing.
}
@ -239,4 +267,22 @@ public class TableBitwiseSwitch extends Table {
}
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;
}
}