Added multiple scale support

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@156 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Jared Gould 2006-07-15 18:02:12 +00:00
parent 54bd8940e3
commit 1587e33b27
8 changed files with 159 additions and 53 deletions

View File

@ -287,4 +287,8 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
public void setCompareDisplay(int compareDisplay) {
this.compareDisplay = compareDisplay;
}
public void refreshValue() {
setBinValue(binValue);
}
}

View File

@ -9,18 +9,22 @@ public class Scale implements Serializable {
public static final int LINEAR = 1;
public static final int INVERSE = 2;
private String name = "Default";
private String unit = "0x";
private String expression = "x";
private String byteExpression = "x";
private String format = "#";
private double coarseIncrement = 2;
private double fineIncrement = 1;
private double min = 0;
private double max = 0;
public Scale() {
}
public String toString() {
return "\n ---- Scale ----" +
"\n Name: " + getName() +
"\n Expression: " + getExpression() +
"\n Unit: " + getUnit() +
"\n ---- End Scale ----";
@ -83,4 +87,12 @@ public class Scale implements Serializable {
public void setFineIncrement(double fineIncrement) {
this.fineIncrement = fineIncrement;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -18,6 +18,7 @@ import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
@ -49,41 +50,43 @@ public abstract class Table extends JPanel implements Serializable {
public static final int STORAGE_TYPE_FLOAT = 99;
protected String name;
protected int type;
protected String category = "Other";
protected String description = "";
protected Scale scale = new 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 = 370;
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 String name;
protected int type;
protected String category = "Other";
protected String description = "";
protected Vector<Scale> scales = new Vector<Scale>();
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;
public Table() {
this.setLayout(borderLayout);
this.add(centerPanel, BorderLayout.CENTER);
@ -288,7 +291,7 @@ public abstract class Table extends JPanel implements Serializable {
for (int i = 0; i < data.length; i++) {
if (data[i] == null) {
data[i] = new DataCell(scale);
data[i] = new DataCell(scales.get(scaleIndex));
data[i].setTable(this);
// populate data cells
@ -355,11 +358,32 @@ public abstract class Table extends JPanel implements Serializable {
}
public Scale getScale() {
return scale;
return scales.get(scaleIndex);
}
public void setScale(Scale scale) {
this.scale = scale;
public Vector<Scale> getScales() {
return scales;
}
public Scale getScaleByName(String inputName) throws Exception {
// look for scale, else throw exception
for (int i = 0; i < scales.size(); i++) {
if (scales.get(i).getName().equalsIgnoreCase(inputName)) {
return scales.get(i);
}
}
throw new Exception();
}
public void setScale(Scale scale) {
// look for scale, replace or add new
for (int i = 0; i < scales.size(); i++) {
if (scales.get(i).getName().equalsIgnoreCase(scale.getName())) {
scales.remove(i);
break;
}
}
scales.add(scale);
}
public int getStorageAddress() {
@ -447,7 +471,9 @@ public abstract class Table extends JPanel implements Serializable {
double high = -999999999;
double low = 999999999;
for (int i = 0; i < getDataSize(); i++) {
for (int i = 0; i < getDataSize(); i++) {
if (data[i].getBinValue() > high) {
high = data[i].getBinValue();
}
@ -876,11 +902,11 @@ public abstract class Table extends JPanel implements Serializable {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", 5);
parser.parseExpression(scale.getExpression());
parser.parseExpression(scales.get(scaleIndex).getExpression());
double toReal = parser.getValue(); // calculate real world value of "5"
parser.addVariable("x", toReal);
parser.parseExpression(scale.getByteExpression());
parser.parseExpression(scales.get(scaleIndex).getByteExpression());
// if real to byte doesn't equal 5, report conflict
if (Math.abs(parser.getValue() - 5) > .001) {
@ -888,8 +914,8 @@ public abstract class Table extends JPanel implements Serializable {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 1));
panel.add(new JLabel("The real value and byte value conversion expressions for table " + name + " are invalid."));
panel.add(new JLabel("To real value: " + scale.getExpression()));
panel.add(new JLabel("To byte: " + scale.getByteExpression()));
panel.add(new JLabel("To real value: " + scales.get(scaleIndex).getExpression()));
panel.add(new JLabel("To byte: " + scales.get(scaleIndex).getByteExpression()));
JCheckBox check = new JCheckBox("Always display this message", true);
check.setHorizontalAlignment(JCheckBox.RIGHT);
@ -935,4 +961,19 @@ public abstract class Table extends JPanel implements Serializable {
if (userLevel > 5) userLevel = 5;
else if (userLevel < 1) userLevel = 1;
}
public int getScaleIndex() {
return scaleIndex;
}
public void setScaleIndex(int scaleIndex) {
this.scaleIndex = scaleIndex;
refreshValues();
}
public void refreshValues() {
if (!isStatic) {
for (int i = 0; i < getDataSize(); i++) data[i].refreshValue();
}
}
}

View File

@ -22,7 +22,7 @@ public class Table1D extends Table {
for (int i = 0; i < this.getDataSize(); i++) {
centerPanel.add(this.getDataCell(i));
}
this.add(new JLabel(name + " (" + scale.getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
add(new JLabel(name + " (" + scales.get(scaleIndex).getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
}
public String toString() {

View File

@ -86,7 +86,7 @@ public class Table2D extends Table {
if (axis.isStatic()) add(new JLabel(axis.getName(), JLabel.CENTER), BorderLayout.NORTH);
else add(new JLabel(axis.getName() + " (" + axis.getScale().getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
add(new JLabel(scale.getUnit(), JLabel.CENTER), BorderLayout.SOUTH);
add(new JLabel(scales.get(scaleIndex).getUnit(), JLabel.CENTER), BorderLayout.SOUTH);
//this.colorize();
}

View File

@ -119,7 +119,7 @@ public class Table3D extends Table {
for (int x = 0; x < yAxis.getDataSize(); x++) {
centerPanel.add(yAxis.getDataCell(x));
for (int y = 0; y < xAxis.getDataSize(); y++) {
data[y][x] = new DataCell(scale);
data[y][x] = new DataCell(scales.get(scaleIndex));
data[y][x].setTable(this);
// populate data cells
@ -151,7 +151,7 @@ public class Table3D extends Table {
GridLayout topLayout = new GridLayout(2,1);
JPanel topPanel = new JPanel(topLayout);
this.add(topPanel, BorderLayout.NORTH);
topPanel.add(new JLabel(name + " (" + scale.getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
topPanel.add(new JLabel(name + " (" + scales.get(scaleIndex).getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
topPanel.add(new JLabel(xAxis.getName() + " (" + xAxis.getScale().getUnit() + ")", JLabel.CENTER), BorderLayout.NORTH);
JLabel yLabel = new JLabel();
yLabel.setFont(new Font("Arial", Font.BOLD, 12));
@ -745,5 +745,15 @@ public class Table3D extends Table {
super.validateScaling();
xAxis.validateScaling();
yAxis.validateScaling();
}
}
public void refreshValues() {
if (!isStatic && !isAxis) {
for (int x = 0; x < data.length; x++) {
for (int y = 0; y < data[0].length; y++) {
data[x][y].refreshValue();
}
}
}
}
}

View File

@ -1,20 +1,25 @@
package enginuity.swing;
import enginuity.swing.TableFrame;
import enginuity.maps.Scale;
import enginuity.maps.Table;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
@ -22,7 +27,7 @@ import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;
public class TableToolBar extends JToolBar implements MouseListener {
public class TableToolBar extends JToolBar implements MouseListener, ItemListener {
private JButton incrementFine = new JButton(new ImageIcon("./graphics/icon-incfine.png"));
private JButton decrementFine = new JButton(new ImageIcon("./graphics/icon-decfine.png"));
@ -34,6 +39,8 @@ public class TableToolBar extends JToolBar implements MouseListener {
private JFormattedTextField incrementByCoarse = new JFormattedTextField(new DecimalFormat("#.####"));
private JFormattedTextField setValueText = new JFormattedTextField(new DecimalFormat("#.####"));
private JComboBox scaleSelection = new JComboBox();
private Table table;
private TableFrame frame;
@ -53,6 +60,8 @@ public class TableToolBar extends JToolBar implements MouseListener {
this.add(setValueText);
this.add(new JLabel(" "));
this.add(setValue);
this.add(new JLabel(" "));
this.add(scaleSelection);
incrementFine.setMaximumSize(new Dimension(33,33));
incrementFine.setBorder(new LineBorder(new Color(150,150,150), 1));
@ -64,6 +73,8 @@ public class TableToolBar extends JToolBar implements MouseListener {
decrementCoarse.setBorder(new LineBorder(new Color(150,150,150), 1));
setValue.setMaximumSize(new Dimension(33,23));
setValue.setBorder(new LineBorder(new Color(150,150,150), 1));
scaleSelection.setMaximumSize(new Dimension(80,23));
scaleSelection.setFont(new Font("Tahoma", Font.PLAIN, 11));
incrementByFine.setAlignmentX(JTextArea.CENTER_ALIGNMENT);
incrementByFine.setAlignmentY(JTextArea.CENTER_ALIGNMENT);
@ -89,9 +100,14 @@ public class TableToolBar extends JToolBar implements MouseListener {
incrementCoarse.addMouseListener(this);
decrementCoarse.addMouseListener(this);
setValue.addMouseListener(this);
scaleSelection.addItemListener(this);
incrementByFine.setValue(Math.abs(table.getScale().getFineIncrement()));
incrementByCoarse.setValue(Math.abs(table.getScale().getCoarseIncrement()));
try {
incrementByFine.setValue(Math.abs(table.getScale().getFineIncrement()));
incrementByCoarse.setValue(Math.abs(table.getScale().getCoarseIncrement()));
} catch (Exception ex) {
// scaling units haven't been added yet -- no problem
}
// key binding actions
Action enterAction = new AbstractAction() {
@ -118,11 +134,19 @@ public class TableToolBar extends JToolBar implements MouseListener {
setValueText.getInputMap().put(enter, "enterAction");
setValue.getInputMap().put(enter, "enterAction");
incrementFine.getInputMap().put(enter, "enterAction");
setScales(table.getScales());
}
public Table getTable() {
return table;
}
public void setScales(Vector<Scale> scales) {
for (int i = 0; i < scales.size(); i++) {
scaleSelection.addItem(scales.get(i).getName());
}
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == incrementCoarse) incrementCoarse();
@ -194,4 +218,11 @@ public class TableToolBar extends JToolBar implements MouseListener {
public void setFrame(TableFrame frame) {
this.frame = frame;
}
public void itemStateChanged(ItemEvent e) {
// scale changed
if (e.getSource() == scaleSelection) {
table.setScaleIndex(scaleSelection.getSelectedIndex());
}
}
}

View File

@ -306,7 +306,13 @@ public class DOMRomUnmarshaller {
}
} else if (n.getNodeName().equalsIgnoreCase("scaling")) {
table.setScale(unmarshallScale(n, table.getScale()));
// check whether scale already exists. if so, modify, else use new instance
Scale baseScale = new Scale();
try {
baseScale = table.getScaleByName(unmarshallAttribute(n, "name", "x"));
} catch (Exception ex) { }
table.setScale(unmarshallScale(n, baseScale));
} else if (n.getNodeName().equalsIgnoreCase("data")) {
// parse and add data to table
@ -331,10 +337,12 @@ public class DOMRomUnmarshaller {
} else { /*unexpected element in Table (skip) */ }
} else { /* unexpected node-type in Table (skip) */ }
}
return table;
}
private Scale unmarshallScale (Node scaleNode, Scale scale) {
scale.setName(unmarshallAttribute(scaleNode, "name", scale.getName()));
scale.setUnit(unmarshallAttribute(scaleNode, "units", scale.getUnit()));
scale.setExpression(unmarshallAttribute(scaleNode, "expression", scale.getExpression()));
scale.setByteExpression(unmarshallAttribute(scaleNode, "to_byte", scale.getByteExpression()));