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,112 +38,125 @@ 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;
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;
public TableBitwiseSwitch() {
super();
storageType = 1;
removeAll();
setLayout(new BorderLayout());
checkboxes = new ArrayList<JCheckBox>();
}
private static final long serialVersionUID = -4887718305447362308L;
private final Map<String, Integer> controlBits = new HashMap<String, Integer>();
private ArrayList<JCheckBox> checkboxes;
private int dataSize = 0;
@Override
public void populateTable(byte[] input, int romRamOffset) throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException {
byte currentValue = input[storageAddress];
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;
public TableBitwiseSwitch() {
super();
storageType = 1;
removeAll();
setLayout(new BorderLayout());
checkboxes = new ArrayList<JCheckBox>();
}
@Override
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];
}
JCheckBox cb = new JCheckBox(entry.getKey());
cb.setSelected(isBitSet(currentValue, entry.getValue()));
checkboxes.add(cb);
radioPanel.add(cb);
}
add(radioPanel, BorderLayout.CENTER);
}
}
@Override
public void setName(String name) {
super.setName(name);
}
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(new JLabel(" " + getName()));
@Override
public TableType getType() {
return Table.TableType.SWITCH;
}
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(bits_array[entry.getValue()]);
checkboxes.add(cb);
radioPanel.add(cb);
}
}
add(radioPanel, BorderLayout.CENTER);
}
@Override
public void setDescription(String description) {
super.setDescription(description);
JTextArea descriptionArea = new JTextArea(description);
descriptionArea.setOpaque(false);
descriptionArea.setEditable(false);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setLineWrap(true);
descriptionArea.setMargin(new Insets(0,5,5,5));
@Override
public void setName(String name) {
super.setName(name);
}
add(descriptionArea, BorderLayout.SOUTH);
}
@Override
public TableType getType() {
return Table.TableType.SWITCH;
}
@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()) {
JCheckBox cb = getButtonByText(entry.getKey());
bools[7 -entry.getValue()] = cb.isSelected();
}
input[storageAddress] = ByteUtil.booleanArrayToBit(bools);
return input;
}
public void setValues(String name, String input) {
controlBits.put(name, Integer.parseInt(input));
}
@Override
public void setDescription(String description) {
super.setDescription(description);
JTextArea descriptionArea = new JTextArea(description);
descriptionArea.setOpaque(false);
descriptionArea.setEditable(false);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setLineWrap(true);
descriptionArea.setMargin(new Insets(0, 5, 5, 5));
public int getValues(String key) {
return controlBits.get(key);
}
add(descriptionArea, BorderLayout.SOUTH);
}
public Map<String, Integer> getSwitchStates() {
return this.controlBits;
}
@Override
public byte[] saveFile(byte[] input) {
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[bitpos] = cb.isSelected();
byte result = ByteUtil.booleanArrayToBit(bools);
input[storageAddress + entry_offset] = result;
}
return input;
}
@Override
public void cursorUp() {
}
public void setValues(String name, String input) {
controlBits.put(name, Integer.parseInt(input));
}
@Override
public void cursorDown() {
}
public int getValues(String key) {
return controlBits.get(key);
}
@Override
public void cursorLeft() {
}
public Map<String, Integer> getSwitchStates() {
return this.controlBits;
}
@Override
public void cursorRight() {
}
@Override
public void cursorUp() {
}
@Override
public void cursorDown() {
}
@Override
public void cursorLeft() {
}
@Override
public void cursorRight() {
}
@Override
public void shiftCursorUp() {
@ -156,87 +174,115 @@ public class TableBitwiseSwitch extends Table {
public void shiftCursorRight() {
}
@Override
public boolean isLiveDataSupported() {
return false;
}
@Override
public boolean isLiveDataSupported() {
return false;
}
@Override
public boolean equals(Object other) {
try {
if(null == other) {
return false;
}
@Override
public boolean equals(Object other) {
try {
if (null == other) {
return false;
}
if(other == this) {
return true;
}
if (other == this) {
return true;
}
if(!(other instanceof TableBitwiseSwitch)) {
return false;
}
if (!(other instanceof TableBitwiseSwitch)) {
return false;
}
TableBitwiseSwitch otherTable = (TableBitwiseSwitch) other;
TableBitwiseSwitch otherTable = (TableBitwiseSwitch) other;
if( (null == this.getName() && null == otherTable.getName())
|| (this.getName().isEmpty() && otherTable.getName().isEmpty()) ) {
;// Skip name compare if name is null or empty.
} else if(!this.getName().equalsIgnoreCase(otherTable.getName())) {
return false;
}
if ((null == this.getName() && null == otherTable.getName())
|| (this.getName().isEmpty() && otherTable.getName().isEmpty())) {
;// Skip name compare if name is null or empty.
} else if (!this.getName().equalsIgnoreCase(otherTable.getName())) {
return false;
}
if(this.getDataSize() != otherTable.getDataSize()) {
return false;
}
if (this.getDataSize() != otherTable.getDataSize()) {
return false;
}
if(this.getSwitchStates().keySet().equals(otherTable.getSwitchStates().keySet())) {
return true;
}
return false;
} catch(Exception ex) {
// TODO: Log Exception.
return false;
}
}
if (this.getSwitchStates().keySet().equals(otherTable.getSwitchStates().keySet())) {
return true;
}
return false;
} catch (Exception ex) {
// TODO: Log Exception.
return false;
}
}
@Override
public void populateCompareValues(Table compareTable) {
return; // Do nothing.
}
@Override
public void setDataSize(int size) {
if (dataSize == 0)
dataSize = size;
}
@Override
public void calcCellRanges() {
return; // Do nothing.
}
@Override
public int getDataSize() {
return dataSize;
}
@Override
public void drawTable()
{
return; // Do nothing.
}
@Override
public void populateCompareValues(Table compareTable) {
return; // Do nothing.
}
@Override
public void updateTableLabel() {
return; // Do nothing.
}
@Override
public void calcCellRanges() {
return; // Do nothing.
}
@Override
public void setCurrentScale(Scale curScale) {
return; // Do nothing.
}
@Override
public void drawTable() {
return; // Do nothing.
}
@Override
public void updateTableLabel() {
return; // Do nothing.
}
@Override
public void setCurrentScale(Scale curScale) {
return; // Do nothing.
}
@Override
public boolean isButtonSelected() {
// TODO Auto-generated method stub
return false;
}
private JCheckBox getButtonByText(String text) {
for (JCheckBox cb : checkboxes) {
if (cb.getText().equalsIgnoreCase(text)) {
return cb;
}
}
return null;
}
private JCheckBox getButtonByText(String text) {
for (JCheckBox cb : checkboxes) {
if (cb.getText().equalsIgnoreCase(text)) {
return cb;
}
}
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;
}
}