From 7885cb94970124f0be85caa3416b21c6a14e1ade Mon Sep 17 00:00:00 2001 From: stirkac Date: Mon, 15 Apr 2019 11:56:11 +0200 Subject: [PATCH] add "size" option to table, so that 16- and 32-bit bytes are supported --- .../romraider/maps/TableBitwiseSwitch.java | 348 ++++++++++-------- 1 file changed, 197 insertions(+), 151 deletions(-) diff --git a/src/main/java/com/romraider/maps/TableBitwiseSwitch.java b/src/main/java/com/romraider/maps/TableBitwiseSwitch.java index b1830ecf..16ddaa5d 100644 --- a/src/main/java/com/romraider/maps/TableBitwiseSwitch.java +++ b/src/main/java/com/romraider/maps/TableBitwiseSwitch.java @@ -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 controlBits = new HashMap(); - private ArrayList checkboxes; - - public TableBitwiseSwitch() { - super(); - storageType = 1; - removeAll(); - setLayout(new BorderLayout()); - checkboxes = new ArrayList(); - } + private static final long serialVersionUID = -4887718305447362308L; + private final Map controlBits = new HashMap(); + private ArrayList 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 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(); + } + + @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 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 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 getSwitchStates() { - return this.controlBits; - } + @Override + public byte[] saveFile(byte[] input) { + + for (Entry 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 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 sortByValue(Map unsortMap) { + List> list = + new LinkedList>(unsortMap.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Map.Entry o1, + Map.Entry o2) { + return (o1.getValue()).compareTo(o2.getValue()); + } + }); + + Map sortedMap = new LinkedHashMap(); + for (Map.Entry entry : list) { + sortedMap.put(entry.getKey(), entry.getValue()); + } + + return sortedMap; + } }