Further updates to Editor to allow copy/paste table or selection using

user's Locale.  The Locale is also applied to 'Save As Repository'.
This commit is contained in:
Dale Schultz 2016-09-20 15:52:20 -04:00
parent 641e60e8a0
commit 696f9019c5
5 changed files with 34 additions and 14 deletions

View File

@ -29,6 +29,7 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.io.Serializable; import java.io.Serializable;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.swing.JLabel; import javax.swing.JLabel;
@ -39,6 +40,7 @@ import org.apache.log4j.Logger;
import com.romraider.Settings; import com.romraider.Settings;
import com.romraider.editor.ecu.ECUEditorManager; import com.romraider.editor.ecu.ECUEditorManager;
import com.romraider.util.JEPUtil; import com.romraider.util.JEPUtil;
import com.romraider.util.NumberUtil;
import com.romraider.util.SettingsManager; import com.romraider.util.SettingsManager;
public class DataCell extends JLabel implements MouseListener, Serializable { 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 PERCENT_FORMAT = "#,##0.0%";
private static final String TT_FORMAT = "#,##0.##########"; private static final String TT_FORMAT = "#,##0.##########";
private static final String TT_PERCENT_FORMAT = "#,##0.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_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; 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) { public void setRealValue(String input) {
// create parser // create parser
input = input.replaceAll(REPLACE_TEXT, Settings.BLANK);
try { try {
double result = 0.0; double result = 0.0;
if (!"x".equalsIgnoreCase(input)) { 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) { if (table.getStorageType() != Settings.STORAGE_TYPE_FLOAT) {
result = (int) Math.round(result); result = (int) Math.round(result);
} }
@ -124,7 +128,7 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
this.setBinValue(result); this.setBinValue(result);
} }
} }
} catch (NumberFormatException e) { } catch (ParseException e) {
// Do nothing. input is null or not a valid number. // 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; String displayString = null;
try { try {
FORMATTER.applyPattern(table.getCurrentScale().getFormat()); FORMATTER.applyPattern(table.getCurrentScale().getFormat());
double staticDouble = Double.parseDouble(staticText); double staticDouble = NumberUtil.doubleValue(staticText);
displayString = FORMATTER.format(JEPUtil.evaluate(table.getCurrentScale().getExpression(), staticDouble)); displayString = FORMATTER.format(JEPUtil.evaluate(table.getCurrentScale().getExpression(), staticDouble));
} catch (Exception ex) { } catch (Exception ex) {
displayString = this.staticText; displayString = this.staticText;

View File

@ -63,6 +63,7 @@ import com.romraider.xml.RomAttributeParser;
public abstract class Table extends JPanel implements Serializable { public abstract class Table extends JPanel implements Serializable {
private static final long serialVersionUID = 6559256489995552645L; private static final long serialVersionUID = 6559256489995552645L;
protected static final Logger LOGGER = Logger.getLogger(Table.class); protected static final Logger LOGGER = Logger.getLogger(Table.class);
protected static final String ST_DELIMITER = "\t\n\r\f";
protected static int memModelEndian; protected static int memModelEndian;
protected String name; protected String name;
@ -1137,7 +1138,7 @@ public abstract class Table extends JPanel implements Serializable {
output.append(data[i].getCellText()); output.append(data[i].getCellText());
} }
else { else {
output.append(data[i].getRealValue()); output.append(NumberUtil.stringValue(data[i].getRealValue()));
} }
if (i < data.length - 1) { if (i < data.length - 1) {
output.append(Settings.TAB); output.append(Settings.TAB);
@ -1177,7 +1178,7 @@ public abstract class Table extends JPanel implements Serializable {
StringTokenizer st = new StringTokenizer(Settings.BLANK); StringTokenizer st = new StringTokenizer(Settings.BLANK);
try { try {
String input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); 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 (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */
} catch (IOException ex) { } catch (IOException ex) {
} }
@ -1516,7 +1517,7 @@ class CopySelectionWorker extends SwingWorker<Void, Void> {
//make a string of the selection //make a string of the selection
for (int i = coords[0]; i <= coords[1]; i++) { for (int i = coords[0]; i <= coords[1]; i++) {
if (table.getData()[i].isSelected()) { if (table.getData()[i].isSelected()) {
output = output + table.getData()[i].getText(); output = output + NumberUtil.stringValue(table.getData()[i].getRealValue());
} else { } else {
output = output + "x"; // x represents non-selected cell output = output + "x"; // x represents non-selected cell
} }

View File

@ -305,7 +305,7 @@ public class Table2D extends Table {
String input = Settings.BLANK; String input = Settings.BLANK;
try { try {
input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); 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 (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */
} catch (IOException ex) { } catch (IOException ex) {
} }

View File

@ -45,6 +45,7 @@ import javax.swing.SwingWorker;
import com.romraider.Settings; import com.romraider.Settings;
import com.romraider.editor.ecu.ECUEditorManager; import com.romraider.editor.ecu.ECUEditorManager;
import com.romraider.logger.ecu.ui.swing.vertical.VerticalLabelUI; import com.romraider.logger.ecu.ui.swing.vertical.VerticalLabelUI;
import com.romraider.util.NumberUtil;
import com.romraider.util.SettingsManager; import com.romraider.util.SettingsManager;
import com.romraider.xml.RomAttributeParser; import com.romraider.xml.RomAttributeParser;
@ -322,7 +323,7 @@ public class Table3D extends Table {
output.append(Settings.NEW_LINE); output.append(Settings.NEW_LINE);
for (int y = 0; y < getSizeY(); y++) { 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); output.append(Settings.TAB);
for (int x = 0; x < getSizeX(); x++) { for (int x = 0; x < getSizeX(); x++) {
@ -330,7 +331,7 @@ public class Table3D extends Table {
output.append(data[x][y].getCellText()); output.append(data[x][y].getCellText());
} }
else { else {
output.append(data[x][y].getRealValue()); output.append(NumberUtil.stringValue(data[x][y].getRealValue()));
} }
if (x < getSizeX() - 1) { if (x < getSizeX() - 1) {
output.append(Settings.TAB); output.append(Settings.TAB);
@ -750,7 +751,7 @@ public class Table3D extends Table {
String input = Settings.BLANK; String input = Settings.BLANK;
try { try {
input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); 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 (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */
} catch (IOException ex) { } catch (IOException ex) {
} }
@ -796,7 +797,7 @@ public class Table3D extends Table {
StringTokenizer st = new StringTokenizer(Settings.BLANK); StringTokenizer st = new StringTokenizer(Settings.BLANK);
try { try {
String input = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor); 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 (UnsupportedFlavorException ex) { /* wrong paste type -- do nothing */
} catch (IOException ex) { } catch (IOException ex) {
} }
@ -818,7 +819,7 @@ public class Table3D extends Table {
if (y==startY && checkToken.endsWith("\t")) { if (y==startY && checkToken.endsWith("\t")) {
checkToken = st.nextToken(Settings.NEW_LINE); 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++) { for (int x = startX; currentLine.hasMoreTokens() && x < getSizeX(); x++) {
String currentToken = currentLine.nextToken(); String currentToken = currentLine.nextToken();
@ -1133,7 +1134,7 @@ class CopySelection3DWorker extends SwingWorker<Void, Void> {
for (int y = coords[1]; y <= coords[3]; y++) { for (int y = coords[1]; y <= coords[3]; y++) {
for (int x = coords[0]; x <= coords[2]; x++) { for (int x = coords[0]; x <= coords[2]; x++) {
if (table.get3dData()[x][y].isSelected()) { if (table.get3dData()[x][y].isSelected()) {
output.append(table.get3dData()[x][y].getText()); output.append(NumberUtil.stringValue(table.get3dData()[x][y].getRealValue()));
} else { } else {
output.append("x"); // x represents non-selected cell output.append("x"); // x represents non-selected cell
} }

View File

@ -19,6 +19,7 @@
package com.romraider.util; package com.romraider.util;
import java.text.DecimalFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.util.Locale; import java.util.Locale;
@ -28,13 +29,17 @@ import java.util.Locale;
*/ */
public final class NumberUtil { public final class NumberUtil {
private static final NumberFormat NUM_FORMATTER = NumberFormat.getInstance(Locale.getDefault()); 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() { private NumberUtil() {
} }
/** /**
* Returns the value of the specified number in the default locale as a double. * 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 * @return the numeric value represented by this object after conversion
* to type double. * to type double.
* @exception ParseException is thrown when parse errors are encountered. * @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 { public static double doubleValue(String str) throws ParseException {
return NUM_FORMATTER.parse(str).doubleValue(); 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);
}
} }