From 7b23479b4a1723a91c157a67449ca5ef436caaf6 Mon Sep 17 00:00:00 2001 From: kascade Date: Wed, 16 Aug 2006 13:14:41 +0000 Subject: [PATCH] declarative convertors added to logger config file, JEP expression parsing moved into a utility class git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@243 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d --- logger.xml | 87 ++++--- .../logger/LoggerControllerImpl.java | 2 +- .../logger/definition/EcuParameter.java | 2 - .../definition/EcuParameterConvertor.java | 11 + .../definition/EcuParameterConvertorImpl.java | 36 +++ .../logger/definition/EcuParameterImpl.java | 1 - .../definition/EcuParameterLoaderImpl.java | 4 +- .../logger/definition/LoggerDefinition.java | 4 - .../definition/LoggerDefinitionHandler.java | 15 +- .../definition/LoggerDefinitionImpl.java | 4 - .../io/serial/SerialConnectionImpl.java | 1 - .../io/serial/SerialPortDiscovererImpl.java | 2 - .../io/serial/protocol/SSMProtocol.java | 2 +- .../manager/TransmissionManagerImpl.java | 4 +- src/enginuity/maps/DataCell.java | 236 +++++++++--------- src/enginuity/maps/Table.java | 170 ++++++------- src/enginuity/util/JEPUtil.java | 17 ++ 17 files changed, 326 insertions(+), 272 deletions(-) create mode 100644 src/enginuity/logger/definition/EcuParameterConvertor.java create mode 100644 src/enginuity/logger/definition/EcuParameterConvertorImpl.java delete mode 100644 src/enginuity/logger/definition/LoggerDefinition.java delete mode 100644 src/enginuity/logger/definition/LoggerDefinitionImpl.java create mode 100644 src/enginuity/util/JEPUtil.java diff --git a/logger.xml b/logger.xml index 1941f51a..dc33b679 100644 --- a/logger.xml +++ b/logger.xml @@ -3,52 +3,63 @@ - -
- 0x000008 -
- enginuity.logger.definition.convertor.GenericTemperatureConvertor -
- -
- 0x00000E - 0x00000F -
- enginuity.logger.definition.convertor.EngineSpeedConvertor -
- -
- 0x000015 -
- enginuity.logger.definition.convertor.ThrottleOpeningAngleConvertor -
- -
- 0x000029 -
- enginuity.logger.definition.convertor.AcceleratorOpeningAngleConvertor -
- -
- 0x000106 -
- enginuity.logger.definition.convertor.ExhaustGasTemperatureConvertor -
- - - - + + +
+ 0x00000E + 0x00000F +
+ + + +
+ + +
+ 0x000015 +
+ + + +
+ + +
+ 0x000029 +
+ + + +
+ + +
+ 0x000046 +
+ + + +
+ + +
+ 0x000106 +
+ + + +
+ +
diff --git a/src/enginuity/logger/LoggerControllerImpl.java b/src/enginuity/logger/LoggerControllerImpl.java index fe1c6ccf..d030da2d 100644 --- a/src/enginuity/logger/LoggerControllerImpl.java +++ b/src/enginuity/logger/LoggerControllerImpl.java @@ -1,9 +1,9 @@ package enginuity.logger; import enginuity.Settings; +import enginuity.logger.definition.EcuParameter; import enginuity.logger.io.serial.SerialPortDiscoverer; import enginuity.logger.io.serial.SerialPortDiscovererImpl; -import enginuity.logger.definition.EcuParameter; import enginuity.logger.manager.QueryManager; import enginuity.logger.manager.QueryManagerImpl; import enginuity.logger.manager.TransmissionManager; diff --git a/src/enginuity/logger/definition/EcuParameter.java b/src/enginuity/logger/definition/EcuParameter.java index f6e3e4c3..07aec3d4 100644 --- a/src/enginuity/logger/definition/EcuParameter.java +++ b/src/enginuity/logger/definition/EcuParameter.java @@ -1,7 +1,5 @@ package enginuity.logger.definition; -import enginuity.logger.definition.convertor.EcuParameterConvertor; - public interface EcuParameter { String getName(); diff --git a/src/enginuity/logger/definition/EcuParameterConvertor.java b/src/enginuity/logger/definition/EcuParameterConvertor.java new file mode 100644 index 00000000..d7828974 --- /dev/null +++ b/src/enginuity/logger/definition/EcuParameterConvertor.java @@ -0,0 +1,11 @@ +package enginuity.logger.definition; + +public interface EcuParameterConvertor { + + double convert(byte[] bytes); + + String getUnits(); + + String format(double value); + +} diff --git a/src/enginuity/logger/definition/EcuParameterConvertorImpl.java b/src/enginuity/logger/definition/EcuParameterConvertorImpl.java new file mode 100644 index 00000000..b1416153 --- /dev/null +++ b/src/enginuity/logger/definition/EcuParameterConvertorImpl.java @@ -0,0 +1,36 @@ +package enginuity.logger.definition; + +import static enginuity.util.ByteUtil.asInt; +import static enginuity.util.JEPUtil.evaluate; +import static enginuity.util.ParamChecker.checkNotNullOrEmpty; + +import java.text.DecimalFormat; + +public final class EcuParameterConvertorImpl implements EcuParameterConvertor { + private final String units; + private final String expression; + private final DecimalFormat format; + + public EcuParameterConvertorImpl(String units, String expression, String format) { + checkNotNullOrEmpty(units, "units"); + checkNotNullOrEmpty(expression, "expression"); + checkNotNullOrEmpty(format, "format"); + this.units = units; + this.expression = expression; + this.format = new DecimalFormat(format); + } + + public double convert(byte[] bytes) { + double value = (double) asInt(bytes); + return evaluate(expression, value); + } + + public String getUnits() { + return units; + } + + public String format(double value) { + return format.format(value); + } + +} diff --git a/src/enginuity/logger/definition/EcuParameterImpl.java b/src/enginuity/logger/definition/EcuParameterImpl.java index 4d0704c4..a2fdbe43 100644 --- a/src/enginuity/logger/definition/EcuParameterImpl.java +++ b/src/enginuity/logger/definition/EcuParameterImpl.java @@ -1,6 +1,5 @@ package enginuity.logger.definition; -import enginuity.logger.definition.convertor.EcuParameterConvertor; import static enginuity.util.ParamChecker.checkNotNull; import static enginuity.util.ParamChecker.checkNotNullOrEmpty; diff --git a/src/enginuity/logger/definition/EcuParameterLoaderImpl.java b/src/enginuity/logger/definition/EcuParameterLoaderImpl.java index abc34fd0..f97454e8 100644 --- a/src/enginuity/logger/definition/EcuParameterLoaderImpl.java +++ b/src/enginuity/logger/definition/EcuParameterLoaderImpl.java @@ -5,10 +5,10 @@ import static enginuity.util.ParamChecker.checkNotNullOrEmpty; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import java.io.InputStream; import java.io.BufferedInputStream; -import java.io.FileInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.List; public final class EcuParameterLoaderImpl implements EcuParameterLoader { diff --git a/src/enginuity/logger/definition/LoggerDefinition.java b/src/enginuity/logger/definition/LoggerDefinition.java deleted file mode 100644 index 7d71c32c..00000000 --- a/src/enginuity/logger/definition/LoggerDefinition.java +++ /dev/null @@ -1,4 +0,0 @@ -package enginuity.logger.definition; - -public interface LoggerDefinition { -} diff --git a/src/enginuity/logger/definition/LoggerDefinitionHandler.java b/src/enginuity/logger/definition/LoggerDefinitionHandler.java index 0e3c3df7..7813326b 100644 --- a/src/enginuity/logger/definition/LoggerDefinitionHandler.java +++ b/src/enginuity/logger/definition/LoggerDefinitionHandler.java @@ -1,7 +1,5 @@ package enginuity.logger.definition; -import enginuity.logger.definition.convertor.EcuParameterConvertor; -import enginuity.logger.exception.ConfigurationException; import enginuity.util.ParamChecker; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; @@ -15,9 +13,13 @@ public final class LoggerDefinitionHandler extends DefaultHandler { private static final String TAG_PROTOCOL = "protocol"; private static final String TAG_PARAMETER = "parameter"; private static final String TAG_CONVERTOR = "convertor"; + private static final String TAG_METRIC = "metric"; private static final String TAG_BYTE = "byte"; private static final String ATTR_ID = "id"; private static final String ATTR_DESC = "desc"; + private static final String ATTR_UNITS = "units"; + private static final String ATTR_EXPRESSION = "expr"; + private static final String ATTR_FORMAT = "format"; private List params; private String paramName; private String paramDesc; @@ -44,6 +46,9 @@ public final class LoggerDefinitionHandler extends DefaultHandler { paramName = attributes.getValue(ATTR_ID); paramDesc = attributes.getValue(ATTR_DESC); addressList = new ArrayList(); + } else if (TAG_METRIC.equals(qName)) { + convertor = new EcuParameterConvertorImpl(attributes.getValue(ATTR_UNITS), attributes.getValue(ATTR_EXPRESSION), + attributes.getValue(ATTR_FORMAT)); } } charBuffer = new StringBuilder(); @@ -61,12 +66,6 @@ public final class LoggerDefinitionHandler extends DefaultHandler { } else if (parseProtocol) { if (TAG_BYTE.equals(qName)) { addressList.add(charBuffer.toString()); - } else if (TAG_CONVERTOR.equals(qName)) { - try { - convertor = (EcuParameterConvertor) Class.forName(charBuffer.toString().trim()).newInstance(); - } catch (Exception e) { - throw new ConfigurationException(e); - } } else if (TAG_PARAMETER.equals(qName)) { String[] addresses = new String[addressList.size()]; addressList.toArray(addresses); diff --git a/src/enginuity/logger/definition/LoggerDefinitionImpl.java b/src/enginuity/logger/definition/LoggerDefinitionImpl.java deleted file mode 100644 index 6bad9f83..00000000 --- a/src/enginuity/logger/definition/LoggerDefinitionImpl.java +++ /dev/null @@ -1,4 +0,0 @@ -package enginuity.logger.definition; - -public final class LoggerDefinitionImpl implements LoggerDefinition { -} diff --git a/src/enginuity/logger/io/serial/SerialConnectionImpl.java b/src/enginuity/logger/io/serial/SerialConnectionImpl.java index da0798bd..e0ee1948 100644 --- a/src/enginuity/logger/io/serial/SerialConnectionImpl.java +++ b/src/enginuity/logger/io/serial/SerialConnectionImpl.java @@ -7,7 +7,6 @@ import enginuity.logger.exception.UnsupportedPortTypeException; import enginuity.logger.io.serial.protocol.ConnectionProperties; import enginuity.logger.io.serial.protocol.Protocol; import enginuity.logger.query.RegisteredQuery; -import enginuity.logger.io.serial.SerialConnection; import static enginuity.util.HexUtil.asHex; import static enginuity.util.ParamChecker.checkGreaterThanZero; import static enginuity.util.ParamChecker.checkNotNull; diff --git a/src/enginuity/logger/io/serial/SerialPortDiscovererImpl.java b/src/enginuity/logger/io/serial/SerialPortDiscovererImpl.java index f352a891..42a274a6 100644 --- a/src/enginuity/logger/io/serial/SerialPortDiscovererImpl.java +++ b/src/enginuity/logger/io/serial/SerialPortDiscovererImpl.java @@ -8,8 +8,6 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import enginuity.logger.io.serial.SerialPortDiscoverer; - public final class SerialPortDiscovererImpl implements SerialPortDiscoverer { @SuppressWarnings({"unchecked"}) diff --git a/src/enginuity/logger/io/serial/protocol/SSMProtocol.java b/src/enginuity/logger/io/serial/protocol/SSMProtocol.java index cc0088c7..317aa712 100644 --- a/src/enginuity/logger/io/serial/protocol/SSMProtocol.java +++ b/src/enginuity/logger/io/serial/protocol/SSMProtocol.java @@ -1,7 +1,7 @@ package enginuity.logger.io.serial.protocol; -import enginuity.logger.query.RegisteredQuery; import enginuity.logger.exception.InvalidResponseException; +import enginuity.logger.query.RegisteredQuery; import static enginuity.util.ByteUtil.asByte; import static enginuity.util.ByteUtil.asInt; import static enginuity.util.HexUtil.asHex; diff --git a/src/enginuity/logger/manager/TransmissionManagerImpl.java b/src/enginuity/logger/manager/TransmissionManagerImpl.java index 496f38dd..92e15d94 100644 --- a/src/enginuity/logger/manager/TransmissionManagerImpl.java +++ b/src/enginuity/logger/manager/TransmissionManagerImpl.java @@ -1,10 +1,10 @@ package enginuity.logger.manager; import enginuity.Settings; -import enginuity.logger.io.serial.SerialConnection; -import enginuity.logger.io.serial.SerialConnectionImpl; import enginuity.logger.exception.NotConnectedException; import enginuity.logger.exception.SerialCommunicationException; +import enginuity.logger.io.serial.SerialConnection; +import enginuity.logger.io.serial.SerialConnectionImpl; import enginuity.logger.io.serial.protocol.Protocol; import enginuity.logger.io.serial.protocol.ProtocolFactory; import enginuity.logger.query.LoggerCallback; diff --git a/src/enginuity/maps/DataCell.java b/src/enginuity/maps/DataCell.java index a01175b1..d1e0dda1 100644 --- a/src/enginuity/maps/DataCell.java +++ b/src/enginuity/maps/DataCell.java @@ -1,39 +1,37 @@ package enginuity.maps; -import java.awt.Color; -import java.awt.Font; +import enginuity.util.JEPUtil; + +import javax.swing.*; +import javax.swing.border.LineBorder; +import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.Serializable; import java.text.DecimalFormat; -import javax.swing.JLabel; -import javax.swing.border.LineBorder; - -import org.nfunk.jep.JEP; - public class DataCell extends JLabel implements MouseListener, Serializable { - - private double binValue = 0; - private double originalValue = 0; - private Scale scale = new Scale(); - private String displayValue = ""; - private double realValue = 0; - private Color scaledColor = new Color(0,0,0); - private Color highlightColor = new Color(155,155,255); - private Color increaseBorder = Color.RED; - private Color decreaseBorder = Color.BLUE; - private Boolean selected = false; - private Boolean highlighted = false; - private Table table; - private int x = 0; - private int y = 0; - private double compareValue = 0; - private int compareType = Table.COMPARE_OFF; - private int compareDisplay = Table.COMPARE_ABSOLUTE; - - public DataCell() { } - + + private double binValue = 0; + private double originalValue = 0; + private Scale scale = new Scale(); + private String displayValue = ""; + private Color scaledColor = new Color(0, 0, 0); + private Color highlightColor = new Color(155, 155, 255); + private Color increaseBorder = Color.RED; + private Color decreaseBorder = Color.BLUE; + private Boolean selected = false; + private Boolean highlighted = false; + private Table table; + private int x = 0; + private int y = 0; + private double compareValue = 0; + private int compareType = Table.COMPARE_OFF; + private int compareDisplay = Table.COMPARE_ABSOLUTE; + + public DataCell() { + } + public DataCell(Scale scale) { this.scale = scale; this.setHorizontalAlignment(CENTER); @@ -44,76 +42,77 @@ public class DataCell extends JLabel implements MouseListener, Serializable { this.setVisible(true); this.addMouseListener(this); } - + public void updateDisplayValue() { DecimalFormat formatter = new DecimalFormat(scale.getFormat()); - + if (getCompareType() == Table.COMPARE_OFF) { displayValue = formatter.format(calcDisplayValue(binValue, table.getScale().getExpression())); - + } else { if (getCompareDisplay() == Table.COMPARE_ABSOLUTE) { displayValue = formatter.format( - calcDisplayValue(binValue, table.getScale().getExpression()) - - calcDisplayValue(compareValue, table.getScale().getExpression())); - + calcDisplayValue(binValue, table.getScale().getExpression()) - + calcDisplayValue(compareValue, table.getScale().getExpression())); + } else if (getCompareDisplay() == Table.COMPARE_PERCENT) { - double difference = calcDisplayValue(binValue, table.getScale().getExpression()) - - calcDisplayValue(compareValue, table.getScale().getExpression()); - if (difference == 0) displayValue = "0%"; - else displayValue = (int)(difference / calcDisplayValue(binValue, table.getScale().getExpression()) * 100)+"%"; + double difference = calcDisplayValue(binValue, table.getScale().getExpression()) - + calcDisplayValue(compareValue, table.getScale().getExpression()); + if (difference == 0) { + displayValue = "0%"; + } else { + displayValue = (int) (difference / calcDisplayValue(binValue, table.getScale().getExpression()) * 100) + "%"; + } } } setText(displayValue); } - + public double calcDisplayValue(double input, String expression) { - JEP parser = new JEP(); - parser.initSymTab(); // clear the contents of the symbol table - parser.addVariable("x", input); - parser.parseExpression(expression); - return parser.getValue(); + return JEPUtil.evaluate(expression, input); } - + public void setColor(Color color) { scaledColor = color; - if (!selected) super.setBackground(color); + if (!selected) { + super.setBackground(color); + } } - + public void setDisplayValue(String displayValue) { this.displayValue = displayValue; this.setText(displayValue); } - - public void setBinValue(double binValue) { - this.binValue = binValue; - - // make sure it's in range - if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT) { - if (binValue < 0) { - this.setBinValue(0); - - } else if (binValue > Math.pow(256, table.getStorageType()) - 1) { - this.setBinValue((int)(Math.pow(256, table.getStorageType()) - 1)); - - } - } - this.updateDisplayValue(); - } - + public void setBinValue(double binValue) { + this.binValue = binValue; + + // make sure it's in range + if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT) { + if (binValue < 0) { + this.setBinValue(0); + + } else if (binValue > Math.pow(256, table.getStorageType()) - 1) { + this.setBinValue((int) (Math.pow(256, table.getStorageType()) - 1)); + + } + } + + this.updateDisplayValue(); + } + public double getBinValue() { return binValue; } - + public String toString() { return displayValue; } - + public Boolean isSelected() { return selected; } - + public void setSelected(Boolean selected) { this.selected = selected; if (selected) { @@ -125,56 +124,64 @@ public class DataCell extends JLabel implements MouseListener, Serializable { } requestFocus(); } - + public void setHighlighted(Boolean highlighted) { if (!table.isStatic()) { this.highlighted = highlighted; if (highlighted) { this.setBackground(getHighlightColor()); } else { - if (!selected) this.setBackground(scaledColor); + if (!selected) { + this.setBackground(scaledColor); + } } } } - + public boolean isHighlighted() { return highlighted; } - + public void mouseEntered(MouseEvent e) { table.highlight(x, y); } - + public void mousePressed(MouseEvent e) { if (!table.isStatic()) { - if (!e.isControlDown()) table.clearSelection(); + if (!e.isControlDown()) { + table.clearSelection(); + } table.startHighlight(x, y); } requestFocus(); } - + public void mouseReleased(MouseEvent e) { if (!table.isStatic()) { table.stopHighlight(); } } - - public void mouseClicked(MouseEvent e) { } - public void mouseExited(MouseEvent e) { } - + + public void mouseClicked(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + public void increment(double increment) { double oldValue = Double.parseDouble(getText()); - if (table.getScale().getCoarseIncrement() < 0) increment = 0 - increment; + if (table.getScale().getCoarseIncrement() < 0) { + increment = 0 - increment; + } - setRealValue((calcDisplayValue(binValue, - scale.getExpression()) + increment) + ""); + setRealValue(String.valueOf((calcDisplayValue(binValue, scale.getExpression()) + increment))); // make sure table is incremented if change isnt great enough - int maxValue = (int)Math.pow(8, (double)table.getStorageType()); - + int maxValue = (int) Math.pow(8, (double) table.getStorageType()); + if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT && - oldValue == Double.parseDouble(getText()) && + oldValue == Double.parseDouble(getText()) && binValue > 0 && binValue < maxValue) { System.out.println(maxValue + " " + binValue); @@ -183,25 +190,25 @@ public class DataCell extends JLabel implements MouseListener, Serializable { } table.colorize(); - + } - + public void setTable(Table table) { this.table = table; } - + public void setXCoord(int x) { this.x = x; } - + public void setYCoord(int y) { this.y = y; } - + public double getOriginalValue() { return originalValue; } - + public void setOriginalValue(double originalValue) { this.originalValue = originalValue; if (binValue != getOriginalValue()) { @@ -210,37 +217,34 @@ public class DataCell extends JLabel implements MouseListener, Serializable { this.setBorder(new LineBorder(Color.BLACK, 1)); } } - + public void undo() { this.setBinValue(originalValue); } - + public void setRevertPoint() { this.setOriginalValue(binValue); } - + public void setRealValue(String input) { // create parser - if (!input.equalsIgnoreCase("x")) { - JEP parser = new JEP(); - parser.initSymTab(); // clear the contents of the symbol table - parser.addVariable("x", Double.parseDouble(input)); - parser.parseExpression(table.getScale().getByteExpression()); - + if (!"x".equalsIgnoreCase(input)) { + double result = JEPUtil.evaluate(table.getScale().getByteExpression(), Double.parseDouble(input)); + if (table.getStorageType() == Table.STORAGE_TYPE_FLOAT) { - this.setBinValue(parser.getValue()); - + this.setBinValue(result); + } else { - this.setBinValue((int)Math.round(parser.getValue())); - + this.setBinValue((int) Math.round(result)); + } } } - + public Color getHighlightColor() { return highlightColor; } - + public void setHighlightColor(Color highlightColor) { this.highlightColor = highlightColor; } @@ -280,32 +284,28 @@ public class DataCell extends JLabel implements MouseListener, Serializable { public int getCompareDisplay() { return compareDisplay; } - + public void setCompareRealValue(String input) { - JEP parser = new JEP(); - parser.initSymTab(); // clear the contents of the symbol table - parser.addVariable("x", Double.parseDouble(input)); - parser.parseExpression(table.getScale().getByteExpression()); - this.setCompareValue((int)Math.round(parser.getValue())); + double result = JEPUtil.evaluate(table.getScale().getByteExpression(), Double.parseDouble(input)); + this.setCompareValue((int) Math.round(result)); } public void setCompareDisplay(int compareDisplay) { this.compareDisplay = compareDisplay; } - + public void refreshValue() { setBinValue(binValue); } - + public void multiply(double factor) { - setRealValue(Double.parseDouble(getText()) * factor+""); + setRealValue(String.valueOf(Double.parseDouble(getText()) * factor)); } /** * Used by 3d graph renderer. - * @return */ - public double getValue() { - return calcDisplayValue(binValue, table.getScale().getExpression()); - } + public double getValue() { + return calcDisplayValue(binValue, table.getScale().getExpression()); + } } \ No newline at end of file diff --git a/src/enginuity/maps/Table.java b/src/enginuity/maps/Table.java index b7dcecba..68aa7f25 100644 --- a/src/enginuity/maps/Table.java +++ b/src/enginuity/maps/Table.java @@ -3,8 +3,8 @@ package enginuity.maps; import enginuity.Settings; import enginuity.swing.TableFrame; import static enginuity.util.ColorScaler.getScaledColor; +import enginuity.util.JEPUtil; import enginuity.xml.RomAttributeParser; -import org.nfunk.jep.JEP; import javax.swing.*; import javax.swing.border.LineBorder; @@ -22,66 +22,66 @@ import java.util.StringTokenizer; import java.util.Vector; public abstract class Table extends JPanel implements Serializable { - - public static final int ENDIAN_LITTLE= 1; - public static final int ENDIAN_BIG = 2; - - public static final int TABLE_1D = 1; - public static final int TABLE_2D = 2; - public static final int TABLE_3D = 3; + + public static final int ENDIAN_LITTLE = 1; + public static final int ENDIAN_BIG = 2; + + public static final int TABLE_1D = 1; + public static final int TABLE_2D = 2; + public static final int TABLE_3D = 3; public static final int TABLE_X_AXIS = 4; public static final int TABLE_Y_AXIS = 5; public static final int TABLE_SWITCH = 6; - - public static final int COMPARE_OFF = 0; + + public static final int COMPARE_OFF = 0; public static final int COMPARE_ORIGINAL = 1; - public static final int COMPARE_TABLE = 2; - public static final int COMPARE_PERCENT = 0; + public static final int COMPARE_TABLE = 2; + public static final int COMPARE_PERCENT = 0; public static final int COMPARE_ABSOLUTE = 1; - + public static final int STORAGE_TYPE_FLOAT = 99; protected static final Color UNCHANGED_VALUE_COLOR = new Color(160, 160, 160); - - protected String name; - protected int type; - protected String category = "Other"; - protected String description = ""; - protected Vector scales = new Vector(); - protected int scaleIndex = 0; // index of selected scale - - protected int storageAddress; - protected int storageType; - protected int endian; - protected boolean flip; - protected DataCell[] data = new DataCell[0]; - protected boolean isStatic = false; - protected boolean beforeRam = false; - protected int ramOffset = 0; - protected BorderLayout borderLayout = new BorderLayout(); - protected GridLayout centerLayout = new GridLayout(1,1,0,0); - protected JPanel centerPanel = new JPanel(centerLayout); - protected TableFrame frame; - protected int verticalOverhead = 103; - protected int horizontalOverhead = 2; - protected int cellHeight = 18; - protected int cellWidth = 42; - protected int minHeight = 100; - protected int minWidth = 425; - protected Rom container; - protected int highlightX; - protected int highlightY; - protected boolean highlight = false; - protected Table axisParent; - protected Color maxColor; - protected Color minColor; - protected boolean isAxis = false; - protected int compareType = 0; - protected int compareDisplay = 1; - protected int userLevel = 0; - protected Settings settings; - protected boolean locked = false; - + + protected String name; + protected int type; + protected String category = "Other"; + protected String description = ""; + protected Vector scales = new Vector(); + protected int scaleIndex = 0; // index of selected scale + + protected int storageAddress; + protected int storageType; + protected int endian; + protected boolean flip; + protected DataCell[] data = new DataCell[0]; + protected boolean isStatic = false; + protected boolean beforeRam = false; + protected int ramOffset = 0; + protected BorderLayout borderLayout = new BorderLayout(); + protected GridLayout centerLayout = new GridLayout(1, 1, 0, 0); + protected JPanel centerPanel = new JPanel(centerLayout); + protected TableFrame frame; + protected int verticalOverhead = 103; + protected int horizontalOverhead = 2; + protected int cellHeight = 18; + protected int cellWidth = 42; + protected int minHeight = 100; + protected int minWidth = 425; + protected Rom container; + protected int highlightX; + protected int highlightY; + protected boolean highlight = false; + protected Table axisParent; + protected Color maxColor; + protected Color minColor; + protected boolean isAxis = false; + protected int compareType = 0; + protected int compareDisplay = 1; + protected int userLevel = 0; + protected Settings settings; + protected boolean locked = false; + public Table(Settings settings) { this.setSettings(settings); this.setLayout(borderLayout); @@ -225,7 +225,7 @@ public abstract class Table extends JPanel implements Serializable { KeyStroke num7 = KeyStroke.getKeyStroke('7'); KeyStroke num8 = KeyStroke.getKeyStroke('8'); KeyStroke num9 = KeyStroke.getKeyStroke('9'); - KeyStroke mulKey = KeyStroke.getKeyStroke('*'); + KeyStroke mulKey = KeyStroke.getKeyStroke('*'); KeyStroke mulKeys = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK); KeyStroke numPoint = KeyStroke.getKeyStroke('.'); KeyStroke copy = KeyStroke.getKeyStroke("control C"); @@ -302,11 +302,11 @@ public abstract class Table extends JPanel implements Serializable { if (scales.isEmpty()) { scales.add(new Scale()); } - + // temporarily remove lock boolean tempLock = locked; locked = false; - + if (!isStatic) { if (!beforeRam) { ramOffset = container.getRomID().getRamOffset(); @@ -325,7 +325,7 @@ public abstract class Table extends JPanel implements Serializable { byteValue[2] = input[storageAddress + i * 4 - ramOffset + 2]; byteValue[3] = input[storageAddress + i * 4 - ramOffset + 3]; data[i].setBinValue(RomAttributeParser.byteToFloat(byteValue, endian)); - + } else { // integer storage type data[i].setBinValue( RomAttributeParser.parseByteValue(input, @@ -338,15 +338,15 @@ public abstract class Table extends JPanel implements Serializable { centerPanel.add(data[i]); data[i].setYCoord(i); data[i].setOriginalValue(data[i].getBinValue()); - + // show locked cell if (tempLock) { data[i].setForeground(Color.GRAY); - } + } } } } - + // reset locked status locked = tempLock; } @@ -499,48 +499,47 @@ public abstract class Table extends JPanel implements Serializable { public void colorize() { if (compareType == COMPARE_OFF) { if (!isStatic && !isAxis) { - + double high = Double.MIN_VALUE; double low = Double.MAX_VALUE; - + if (getScale().getMax() != 0 || getScale().getMin() != 0) { // set min and max values if they are set in scale high = getScale().getMax(); low = getScale().getMin(); - + } else { for (int i = 0; i < getDataSize(); i++) { if (Double.parseDouble(data[i].getText()) > high) { high = Double.parseDouble(data[i].getText()); - } + } if (Double.parseDouble(data[i].getText()) < low) { low = Double.parseDouble(data[i].getText()); } } } - + for (int i = 0; i < getDataSize(); i++) { - + if (Double.parseDouble(data[i].getText()) > high || Double.parseDouble(data[i].getText()) < low) { - + // value exceeds limit - data[i].setColor(getSettings().getWarningColor()); - - } else { + data[i].setColor(getSettings().getWarningColor()); + + } else { // limits not set, scale based on table values - double scale = 0; - + double scale; if (high - low == 0) { // if all values are the same, color will be middle value - scale = .5; + scale = .5; } else { - scale = (Double.parseDouble(data[i].getText()) - low) / (high - low); + scale = (Double.parseDouble(data[i].getText()) - low) / (high - low); } - + data[i].setColor(getScaledColor(scale, getSettings())); } } @@ -636,7 +635,7 @@ public abstract class Table extends JPanel implements Serializable { } } } - + public void multiply(double factor) { if (!isStatic && !locked) { for (DataCell cell : data) { @@ -646,9 +645,9 @@ public abstract class Table extends JPanel implements Serializable { } } colorize(); - } + } - public void setRealValue(String realValue) { + public void setRealValue(String realValue) { if (!isStatic && !locked) { for (DataCell cell : data) { if (cell.isSelected()) { @@ -957,7 +956,7 @@ public abstract class Table extends JPanel implements Serializable { public void applyColorSettings(Settings settings) { this.setSettings(settings); - + // apply settings to cells for (int i = 0; i < getDataSize(); i++) { this.setMaxColor(settings.getMaxColor()); @@ -998,23 +997,18 @@ public abstract class Table extends JPanel implements Serializable { public void validateScaling() { if (type != Table.TABLE_SWITCH && !isStatic) { - JEP parser = new JEP(); - parser.initSymTab(); // clear the contents of the symbol table - parser.addVariable("x", 5); // make sure a scale is present if (scales.isEmpty()) { scales.add(new Scale()); } - parser.parseExpression(scales.get(scaleIndex).getExpression()); - double toReal = parser.getValue(); // calculate real world value of "5" - - parser.addVariable("x", toReal); - parser.parseExpression(scales.get(scaleIndex).getByteExpression()); + double startValue = 5; + double toReal = JEPUtil.evaluate(scales.get(scaleIndex).getExpression(), startValue); // calculate real world value of "5" + double endValue = JEPUtil.evaluate(scales.get(scaleIndex).getByteExpression(), toReal); // if real to byte doesn't equal 5, report conflict - if (Math.abs(parser.getValue() - 5) > .001) { + if (Math.abs(endValue - startValue) > .001) { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 1)); diff --git a/src/enginuity/util/JEPUtil.java b/src/enginuity/util/JEPUtil.java new file mode 100644 index 00000000..c197cad6 --- /dev/null +++ b/src/enginuity/util/JEPUtil.java @@ -0,0 +1,17 @@ +package enginuity.util; + +import org.nfunk.jep.JEP; + +public final class JEPUtil { + + private JEPUtil() { + } + + public static double evaluate(String expression, double value) { + JEP parser = new JEP(); + parser.initSymTab(); // clear the contents of the symbol table + parser.addVariable("x", value); + parser.parseExpression(expression); + return parser.getValue(); + } +}