Implement Table name comaprison for ROM property panel table list order

This commit is contained in:
Dale Schultz 2022-04-16 11:37:39 -04:00
parent 4e257ad2ee
commit b648cc1ac1
No known key found for this signature in database
GPG Key ID: EA2C8AD6CB5C2AF2
3 changed files with 106 additions and 101 deletions

View File

@ -34,7 +34,7 @@ import java.io.Serializable;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
@ -66,33 +66,33 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
private static final Logger LOGGER = Logger.getLogger(Rom.class);
private static final ResourceBundle rb = new ResourceUtil().getBundle(
Rom.class.getName());
private RomID romID;
private File definitionPath;
private String fileName = "";
private File fullFileName = new File(".");
private byte[] binData;
private Document doc;
//This keeps track of DataCells on a byte level
//This might also be possible to achieve by using the same Data Tables
protected HashMap<Integer, LinkedList<DataCell>> byteCellMapping = new HashMap<Integer, LinkedList<DataCell>>();
private boolean isAbstract = false;
private final HashMap<String, TableTreeNode> tableNodes = new HashMap<String, TableTreeNode>();
private LinkedList<ChecksumManager> checksumManagers = new LinkedList<ChecksumManager>();
public Rom(RomID romID) {
this.romID = romID;
}
//This makes sure we automatically sort the tables by name
public void sortedAdd(DefaultMutableTreeNode currentParent, DefaultMutableTreeNode newNode) {
boolean found = false;
boolean found = false;
for(int k = 0; k < currentParent.getChildCount(); k++){
TreeNode n = currentParent.getChildAt(k);
//Category nodes should be placed at the top
if(newNode instanceof CategoryTreeNode && !(n instanceof CategoryTreeNode)) {
found = true;
@ -100,21 +100,21 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
else if(!(newNode instanceof CategoryTreeNode) && n instanceof CategoryTreeNode) {
continue;
}
else if(n.toString().compareToIgnoreCase(newNode.toString()) >= 0) {
found = true;
else if(n.toString().compareToIgnoreCase(newNode.toString()) >= 0) {
found = true;
}
if(found) {
currentParent.insert(newNode, k);
break;
}
}
if(!found) {
currentParent.add(newNode);
}
}
public void refreshDisplayedTables() {
// Remove all nodes from the ROM tree node.
super.removeAllChildren();
@ -124,30 +124,30 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
// Add nodes to ROM tree.
for (TableTreeNode tableTreeNode : tableNodes.values()) {
Table table = tableTreeNode.getTable();
String[] categories = table.getCategory().split("//");
if (settings.isDisplayHighTables() || settings.getUserLevel() >= table.getUserLevel()) {
DefaultMutableTreeNode currentParent = this;
for(int i=0; i < categories.length; i++) {
boolean categoryExists = false;
for (int j = 0; j < currentParent.getChildCount(); j++) {
if (currentParent.getChildAt(j).toString().equalsIgnoreCase(categories[i])) {
categoryExists = true;
if (currentParent.getChildAt(j).toString().equalsIgnoreCase(categories[i])) {
categoryExists = true;
currentParent = (DefaultMutableTreeNode) currentParent.getChildAt(j);
break;
}
}
if(!categoryExists) {
CategoryTreeNode categoryNode = new CategoryTreeNode(categories[i]);
sortedAdd(currentParent,categoryNode);
sortedAdd(currentParent,categoryNode);
currentParent = categoryNode;
}
if(i == categories.length - 1){
sortedAdd(currentParent, tableTreeNode);
}
@ -155,12 +155,12 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
}
}
}
public void addTableByName(Table table) {
table.setRom(this);
tableNodes.put(table.getName().toLowerCase(), new TableTreeNode(table));
}
public void removeTableByName(Table table) {
if(tableNodes.containsKey(table.getName().toLowerCase())) {
tableNodes.remove(table.getName().toLowerCase());
@ -184,7 +184,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
}
return result;
}
// Table storage address extends beyond end of file
private void showBadTablePopup(Table table, Exception ex) {
LOGGER.error(table.getName() +
@ -195,27 +195,27 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
MessageFormat.format(rb.getString("ADDROUTOFBNDS"), table.getName()),
rb.getString("ECUDEFERROR"), JOptionPane.ERROR_MESSAGE);
}
private void showNullExceptionPopup(Table table, Exception ex) {
LOGGER.error("Error Populating Table", ex);
JOptionPane.showMessageDialog(null,
MessageFormat.format(rb.getString("TABLELOADERR"), table.getName()),
rb.getString("ECUDEFERROR"), JOptionPane.ERROR_MESSAGE);
}
public void populateTables(byte[] binData, JProgressPane progress) {
this.binData = binData;
int size = tableNodes.size();
int i = 0;
Vector <String> badKeys = new Vector<String>();
for(String name: tableNodes.keySet()) {
for(String name: tableNodes.keySet()) {
// update progress
int currProgress = (int) (i / (double) size * 100);
progress.update(rb.getString("POPTABLES"), currProgress);
Table table = tableNodes.get(name.toLowerCase()).getTable();
try {
if (table.getStorageAddress() >= 0) {
try {
@ -225,8 +225,8 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
if (null != table.getName() && table.getName().equalsIgnoreCase("Checksum Fix")){
setEditStamp(binData, table.getStorageAddress());
}
i++;
i++;
} catch (ArrayIndexOutOfBoundsException ex) {
showBadTablePopup(table, ex);
badKeys.add(table.getName());
@ -235,7 +235,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
showBadTablePopup(table, iex);
badKeys.add(table.getName());
size--;
}
}
} else {
tableNodes.remove(table.getName().toLowerCase());
size--;
@ -247,13 +247,12 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
size--;
}
}
for(String s: badKeys) {
tableNodes.remove(s.toLowerCase());
}
}
//TODO: Move to Subaru checksum
private void setEditStamp(byte[] binData, int address) {
byte[] stampData = new byte[4];
System.arraycopy(binData, address+204, stampData, 0, stampData.length);
@ -283,26 +282,26 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
public String getRomIDString() {
return romID.getXmlid();
}
public byte[] getBinary() {
return binData;
}
public void setDocument(Document d) {
this.doc = d;
}
public Document getDocument() {
return this.doc;
}
public void setDefinitionPath(File s) {
definitionPath = s;
}
public File getDefinitionPath() {
return definitionPath;
}
@Override
public String toString() {
String output = "";
@ -324,6 +323,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
for(TableTreeNode tableNode : tableNodes.values()) {
tables.add(tableNode.getTable());
}
Collections.sort(tables);
return tables;
}
@ -334,11 +334,11 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
public void setFileName(String fileName) {
this.fileName = fileName;
}
private void showChecksumFixPopup(TableTreeNode checksum) {
Object[] options = {rb.getString("YES"), rb.getString("NO")};
final String message = rb.getString("CHKSUMINVALID");
int answer = showOptionDialog(
SwingUtilities.windowForComponent(checksum.getTable().getTableView()),
message,
@ -349,6 +349,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
options,
options[0]);
if (answer == 0) {
//TODO: Move to Subaru checksum
calculateRomChecksum(
binData,
checksum.getTable().getStorageAddress(),
@ -356,7 +357,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
);
}
}
//Most of this function is useless now, since each Datacell is now responsible for each memory region
//It is only used to correct the Subaru Checksum. Should be moved somewhere else TODO
public byte[] saveFile() {
@ -389,6 +390,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
for (TableTreeNode checksum : checksumTables) {
if (!checksum.getTable().isLocked()) {
//TODO: Move to Subaru checksum
calculateRomChecksum(
binData,
checksum.getTable().getStorageAddress(),
@ -397,11 +399,11 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
}
else if (checksum.getTable().isLocked() &&
!checksum.getTable().isButtonSelected()) {
showChecksumFixPopup(checksum);
showChecksumFixPopup(checksum);
}
}
updateChecksum();
updateChecksum();
return binData;
}
@ -411,50 +413,50 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
// Hide and dispose all frames.
for(TableTreeNode tableTreeNode : tableNodes.values()) {
TableFrame frame = tableTreeNode.getFrame();
TableUpdateHandler.getInstance().deregisterTable(tableTreeNode.getTable());
// Quite slow and doesn't seem to be necessary after testing,
// uncomment if you disagree
//tableTreeNode.getTable().clearData();
if(frame != null) {
if(frame != null) {
frame.setVisible(false);
try {
frame.setClosed(true);
} catch (PropertyVetoException e) {
; // Do nothing.
// Do nothing.
}
frame.dispose();
if(frame.getTableView() != null) {
frame.getTableView().setVisible(false);
frame.getTableView().setVisible(false);
frame.getTableView().setData(null);
frame.getTableView().setTable(null);
frame.setTableView(null);
}
}
tableTreeNode.setUserObject(null);
}
clearByteMapping();
checksumManagers.clear();
tableNodes.clear();
binData = null;
doc = null;
}
public void clearByteMapping() {
for(List<?> l: byteCellMapping.values())l.clear();
byteCellMapping.clear();
byteCellMapping = null;
}
public int getRealFileSize() {
return binData.length;
}
@ -475,7 +477,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
public void setAbstract(boolean isAbstract) {
this.isAbstract = isAbstract;
}
@Override
public DefaultMutableTreeNode getChildAt(int i) {
return (DefaultMutableTreeNode) super.getChildAt(i);
@ -484,58 +486,58 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
public void addChecksumManager(ChecksumManager checksumManager) {
this.checksumManagers.add(checksumManager);
}
public int getNumChecksumsManagers() {
return checksumManagers.size();
}
public int validateChecksum() {
int correctChecksums = 0;
boolean valid = true;
if (!checksumManagers.isEmpty()) {
boolean valid = true;
if (!checksumManagers.isEmpty()) {
for(ChecksumManager cm: checksumManagers) {
int localCorrectCs = cm.validate(binData);
if (cm == null || cm.getNumberOfChecksums() != localCorrectCs) {
valid = false;
}
else {
correctChecksums+=localCorrectCs;
}
}
}
}
if(!valid) {
showMessageDialog(null,
rb.getString("INVLAIDCHKSUM"),
rb.getString("CHKSUMFAIL"),
WARNING_MESSAGE);
}
return correctChecksums;
}
public int updateChecksum() {
int updatedCs = 0;
for(ChecksumManager cm: checksumManagers) {
updatedCs+=cm.update(binData);
}
ECUEditorManager.getECUEditor().getStatusPanel().setStatus(
String.format(rb.getString("CHECKSUMFIXED"), updatedCs, getTotalAmountOfChecksums()));
return updatedCs;
}
public int getTotalAmountOfChecksums() {
int cs = 0;
for(ChecksumManager cm: checksumManagers) {
cs+=cm.getNumberOfChecksums();
}
return cs;
}
}

View File

@ -1,6 +1,6 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2021 RomRaider.com
* Copyright (C) 2006-2022 RomRaider.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -33,7 +33,7 @@ import com.romraider.util.JEPUtil;
import com.romraider.util.NumberUtil;
import com.romraider.util.SettingsManager;
public abstract class Table implements Serializable {
public abstract class Table implements Serializable, Comparable<Table> {
private static final long serialVersionUID = 6559256489995552645L;
protected static final Logger LOGGER = Logger.getLogger(Table.class);
protected static final String ST_DELIMITER = "\t\n\r\f";
@ -126,7 +126,7 @@ public abstract class Table implements Serializable {
}
data = null;
}
}
rom = null;
}
@ -137,17 +137,17 @@ public abstract class Table implements Serializable {
public int getRamOffset() {
return this.ramOffset;
}
public Rom getRom() {
return rom;
}
public void setRom(Rom rom) {
this.rom = rom;
}
public void populateTable(Rom rom) throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException {
if(isStaticDataTable()) return;
public void populateTable(Rom rom) throws ArrayIndexOutOfBoundsException, IndexOutOfBoundsException {
if(isStaticDataTable()) return;
validateScaling();
// temporarily remove lock;
@ -501,7 +501,7 @@ public abstract class Table implements Serializable {
if(presetManager == null) presetManager = new PresetManager(this);
presetManager.setPresetValues(name, value, 0, false);
}
public void setPresetValues(String name, String value, int dataCellOffset) {
if(presetManager == null) presetManager = new PresetManager(this);
presetManager.setPresetValues(name, value, dataCellOffset, true);
@ -542,9 +542,9 @@ public abstract class Table implements Serializable {
public void setBitMask(int mask) {
//We dont update the DataCells here!
//Clamp to max size
tableBitMask = (int) Math.min(mask, Math.pow(2,getStorageType()*8)-1);
tableBitMask = (int) Math.min(mask, Math.pow(2,getStorageType()*8)-1);
}
public int getBitMask() {
return tableBitMask;
}
@ -599,7 +599,7 @@ public abstract class Table implements Serializable {
public void selectCellAtWithoutClear(int y) {
if(y >= 0 && y < data.length) {
data[y].setSelected(true);
if(tableView!=null)tableView.highlightBeginY = y;
if(tableView!=null) tableView.highlightBeginY = y;
}
}
@ -722,13 +722,13 @@ public abstract class Table implements Serializable {
public void setCompareTable(Table compareTable) {
this.compareTable = compareTable;
if(tableView!= null)tableView.drawTable();
if(tableView!= null) tableView.drawTable();
}
public void setCompareValueType(Settings.DataType compareValueType) {
this.compareValueType = compareValueType;
if(tableView!= null)tableView.drawTable();
if(tableView!= null) tableView.drawTable();
}
public Settings.DataType getCompareValueType() {
@ -738,7 +738,7 @@ public abstract class Table implements Serializable {
public void colorCells() {
calcCellRanges();
if(tableView!=null)tableView.drawTable();
if(tableView!=null) tableView.drawTable();
}
public void refreshCompare() {
@ -792,4 +792,9 @@ public abstract class Table implements Serializable {
return String.valueOf(marshallingCode);
}
}
@Override
public int compareTo(Table otherTable) {
return this.getName().compareTo(otherTable.getName());
}
}

View File

@ -20,7 +20,6 @@
package com.romraider.swing;
import java.util.ResourceBundle;
import com.romraider.maps.Rom;
import com.romraider.maps.RomID;
import com.romraider.util.ResourceUtil;
@ -29,7 +28,6 @@ import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.AbstractListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
@ -44,7 +42,7 @@ public class RomPropertyPanel extends JPanel {
public RomPropertyPanel(Rom rom) {
initComponents();
RomID romID = rom.getRomID();
// populate fields
fileName.setText(rom.getFileName());
xmlID.setText(romID.getXmlid());
@ -57,7 +55,7 @@ public class RomPropertyPanel extends JPanel {
}
internalID.setText(romID.getInternalIdString());
storageAddress.setText("0x" + Integer.toHexString(romID.getInternalIdAddress()));
make.setText(romID.getMake());
market.setText(romID.getMarket());
year.setText(romID.getYear());
@ -68,7 +66,7 @@ public class RomPropertyPanel extends JPanel {
checksum.setText(romID.getChecksum());
version.setText(romID.getVersion());
author.setText(romID.getAuthor());
lblTables.setText(String.format(rb.getString("LBLTBLS"), rom.getTables().size()));
lblTables.setText(String.format(rb.getString("LBLTBLS"), rom.getTables().size()));
tableList.setListData(rom.getTables());
}
@ -111,7 +109,7 @@ public class RomPropertyPanel extends JPanel {
checksum = new JLabel();
lblChecksum.setText(rb.getString("LBLCHKSUM"));
lblEditStamp.setText(rb.getString("LBLEDIT"));
lblEditStamp.setText(rb.getString("LBLEDIT"));
lblFilename.setText(rb.getString("LBLFN"));
lblECURevision.setText(rb.getString("LBLECU"));
lblFilesize.setText(rb.getString("LBLFS"));
@ -125,9 +123,9 @@ public class RomPropertyPanel extends JPanel {
lblSubmodel.setText(rb.getString("LBLSMDL"));
lblYear.setText(rb.getString("LBLYR"));
lblAuthor.setText(rb.getString("LBLAUT"));
lblVersion.setText(rb.getString("LBLDEFVER"));
lblVersion.setText(rb.getString("LBLDEFVER"));
jScrollPane1.setViewportView(tableList);
GroupLayout layout = new GroupLayout(this);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)