diff --git a/src/main/java/com/romraider/maps/DataCell.java b/src/main/java/com/romraider/maps/DataCell.java index e9a03274..a8dc3b27 100644 --- a/src/main/java/com/romraider/maps/DataCell.java +++ b/src/main/java/com/romraider/maps/DataCell.java @@ -29,6 +29,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.Serializable; import java.text.DecimalFormat; +import java.text.ParseException; import java.util.StringTokenizer; import javax.swing.JLabel; @@ -39,6 +40,7 @@ import org.apache.log4j.Logger; import com.romraider.Settings; import com.romraider.editor.ecu.ECUEditorManager; import com.romraider.util.JEPUtil; +import com.romraider.util.NumberUtil; import com.romraider.util.SettingsManager; public class DataCell extends JLabel implements MouseListener, Serializable { @@ -50,6 +52,7 @@ public class DataCell extends JLabel implements MouseListener, Serializable { private static final String PERCENT_FORMAT = "#,##0.0%"; private static final String TT_FORMAT = "#,##0.##########"; private static final String TT_PERCENT_FORMAT = "#,##0.0#########%"; + private static final String REPLACE_TEXT = "\u0020|\u00a0"; private static int UNSELECT_MASK1 = MouseEvent.BUTTON1_DOWN_MASK + MouseEvent.CTRL_DOWN_MASK + MouseEvent.ALT_DOWN_MASK; private static int UNSELECT_MASK2 = MouseEvent.BUTTON3_DOWN_MASK + MouseEvent.CTRL_DOWN_MASK + MouseEvent.ALT_DOWN_MASK; @@ -112,10 +115,11 @@ public class DataCell extends JLabel implements MouseListener, Serializable { public void setRealValue(String input) { // create parser + input = input.replaceAll(REPLACE_TEXT, Settings.BLANK); try { double result = 0.0; if (!"x".equalsIgnoreCase(input)) { - result = JEPUtil.evaluate(table.getCurrentScale().getByteExpression(), Double.parseDouble(input)); + result = JEPUtil.evaluate(table.getCurrentScale().getByteExpression(), NumberUtil.doubleValue(input)); if (table.getStorageType() != Settings.STORAGE_TYPE_FLOAT) { result = (int) Math.round(result); } @@ -124,7 +128,7 @@ public class DataCell extends JLabel implements MouseListener, Serializable { this.setBinValue(result); } } - } catch (NumberFormatException e) { + } catch (ParseException e) { // Do nothing. input is null or not a valid number. } } @@ -548,7 +552,7 @@ public class DataCell extends JLabel implements MouseListener, Serializable { String displayString = null; try { FORMATTER.applyPattern(table.getCurrentScale().getFormat()); - double staticDouble = Double.parseDouble(staticText); + double staticDouble = NumberUtil.doubleValue(staticText); displayString = FORMATTER.format(JEPUtil.evaluate(table.getCurrentScale().getExpression(), staticDouble)); } catch (Exception ex) { displayString = this.staticText; diff --git a/src/main/java/com/romraider/maps/Table.java b/src/main/java/com/romraider/maps/Table.java index 483c066c..ff6cb7d5 100644 --- a/src/main/java/com/romraider/maps/Table.java +++ b/src/main/java/com/romraider/maps/Table.java @@ -63,6 +63,7 @@ import com.romraider.xml.RomAttributeParser; public abstract class Table extends JPanel implements Serializable { 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"; protected static int memModelEndian; protected String name; @@ -1137,7 +1138,7 @@ public abstract class Table extends JPanel implements Serializable { output.append(data[i].getCellText()); } else { - output.append(data[i].getRealValue()); + output.append(NumberUtil.stringValue(data[i].getRealValue())); } if (i < data.length - 1) { output.append(Settings.TAB); @@ -1177,7 +1178,7 @@ public abstract class Table extends JPanel implements Serializable { StringTokenizer st = new StringTokenizer(Settings.BLANK); try { String input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); - st = new StringTokenizer(input); + st = new StringTokenizer(input, ST_DELIMITER); } catch (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */ } catch (IOException ex) { } @@ -1516,7 +1517,7 @@ class CopySelectionWorker extends SwingWorker { //make a string of the selection for (int i = coords[0]; i <= coords[1]; i++) { if (table.getData()[i].isSelected()) { - output = output + table.getData()[i].getText(); + output = output + NumberUtil.stringValue(table.getData()[i].getRealValue()); } else { output = output + "x"; // x represents non-selected cell } diff --git a/src/main/java/com/romraider/maps/Table2D.java b/src/main/java/com/romraider/maps/Table2D.java index e997efd0..9fc3011e 100644 --- a/src/main/java/com/romraider/maps/Table2D.java +++ b/src/main/java/com/romraider/maps/Table2D.java @@ -305,7 +305,7 @@ public class Table2D extends Table { String input = Settings.BLANK; try { input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); - st = new StringTokenizer(input); + st = new StringTokenizer(input, ST_DELIMITER); } catch (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */ } catch (IOException ex) { } diff --git a/src/main/java/com/romraider/maps/Table3D.java b/src/main/java/com/romraider/maps/Table3D.java index 8d969c40..c391be6f 100644 --- a/src/main/java/com/romraider/maps/Table3D.java +++ b/src/main/java/com/romraider/maps/Table3D.java @@ -45,6 +45,7 @@ import javax.swing.SwingWorker; import com.romraider.Settings; import com.romraider.editor.ecu.ECUEditorManager; import com.romraider.logger.ecu.ui.swing.vertical.VerticalLabelUI; +import com.romraider.util.NumberUtil; import com.romraider.util.SettingsManager; import com.romraider.xml.RomAttributeParser; @@ -322,7 +323,7 @@ public class Table3D extends Table { output.append(Settings.NEW_LINE); for (int y = 0; y < getSizeY(); y++) { - output.append(yAxis.data[y].getRealValue()); + output.append(NumberUtil.stringValue(yAxis.data[y].getRealValue())); output.append(Settings.TAB); for (int x = 0; x < getSizeX(); x++) { @@ -330,7 +331,7 @@ public class Table3D extends Table { output.append(data[x][y].getCellText()); } else { - output.append(data[x][y].getRealValue()); + output.append(NumberUtil.stringValue(data[x][y].getRealValue())); } if (x < getSizeX() - 1) { output.append(Settings.TAB); @@ -750,7 +751,7 @@ public class Table3D extends Table { String input = Settings.BLANK; try { input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); - st = new StringTokenizer(input); + st = new StringTokenizer(input, ST_DELIMITER); } catch (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */ } catch (IOException ex) { } @@ -796,7 +797,7 @@ public class Table3D extends Table { StringTokenizer st = new StringTokenizer(Settings.BLANK); try { String input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); - st = new StringTokenizer(input); + st = new StringTokenizer(input, ST_DELIMITER); } catch (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */ } catch (IOException ex) { } @@ -818,7 +819,7 @@ public class Table3D extends Table { if (y==startY && checkToken.endsWith("\t")) { checkToken = st.nextToken(Settings.NEW_LINE); } - StringTokenizer currentLine = new StringTokenizer(checkToken); + StringTokenizer currentLine = new StringTokenizer(checkToken, ST_DELIMITER); for (int x = startX; currentLine.hasMoreTokens() && x < getSizeX(); x++) { String currentToken = currentLine.nextToken(); @@ -1133,7 +1134,7 @@ class CopySelection3DWorker extends SwingWorker { for (int y = coords[1]; y <= coords[3]; y++) { for (int x = coords[0]; x <= coords[2]; x++) { if (table.get3dData()[x][y].isSelected()) { - output.append(table.get3dData()[x][y].getText()); + output.append(NumberUtil.stringValue(table.get3dData()[x][y].getRealValue())); } else { output.append("x"); // x represents non-selected cell } diff --git a/src/main/java/com/romraider/util/NumberUtil.java b/src/main/java/com/romraider/util/NumberUtil.java index fd8f1ef4..db9548bc 100644 --- a/src/main/java/com/romraider/util/NumberUtil.java +++ b/src/main/java/com/romraider/util/NumberUtil.java @@ -19,6 +19,7 @@ package com.romraider.util; +import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; @@ -28,13 +29,17 @@ import java.util.Locale; */ public final class NumberUtil { private static final NumberFormat NUM_FORMATTER = NumberFormat.getInstance(Locale.getDefault()); + private static final String NAN = "NaN"; + static { + ((DecimalFormat) NUM_FORMATTER).applyPattern("0.0#################"); + } private NumberUtil() { } /** * Returns the value of the specified number in the default locale as a double. - * @param str - string to be converted. + * @param str - string to be converted. * @return the numeric value represented by this object after conversion * to type double. * @exception ParseException is thrown when parse errors are encountered. @@ -42,4 +47,13 @@ public final class NumberUtil { public static double doubleValue(String str) throws ParseException { return NUM_FORMATTER.parse(str).doubleValue(); } + + /** + * Format a double using the current Locale + * @param value to format + * @return the number as a String + */ + public static String stringValue(double value) { + return Double.isNaN(value) ? NAN : NUM_FORMATTER.format(value); + } }