From f59f24346ad0f2e4667ef8d54b6a71e90abf9d61 Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Fri, 6 Feb 2015 16:36:55 +0000 Subject: [PATCH] Parse floating point values using current default locale Fixes #55 : Locales that use a decimal separator other than dot "." would throw NumberFormatException when parsing the content of the Estimated field. Detected three other places in the code that would also parse potentially locale-formatted strings with the universal .valueOf() parsing methods that expect dots. Changed them to use an instance of NumberFormat instead. --- .../internal/ui/editor/EstimatedEditor.java | 16 +++++++++++++++- .../internal/ui/editor/TaskDataValidator.java | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/EstimatedEditor.java b/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/EstimatedEditor.java index b54adb0..2906f9d 100644 --- a/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/EstimatedEditor.java +++ b/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/EstimatedEditor.java @@ -1,5 +1,8 @@ package net.sf.redmine_mylyn.internal.ui.editor; +import java.text.NumberFormat; +import java.text.ParseException; + import net.sf.redmine_mylyn.core.RedmineCorePlugin; import net.sf.redmine_mylyn.internal.ui.Images; import net.sf.redmine_mylyn.internal.ui.Messages; @@ -120,9 +123,20 @@ private int getValue() { private void setValue(String val) { if(!val.equals(getTaskAttribute().getValue())) { - if(Float.valueOf(val) == 0) { + + NumberFormat nf = NumberFormat.getInstance(); + float value = 0; + + try { + value = nf.parse(val).floatValue(); + } catch (ParseException parsex) { + // do nothing + } + + if(value == 0) { val = ""; } + getTaskAttribute().setValue(val); attributeChanged(); } diff --git a/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/TaskDataValidator.java b/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/TaskDataValidator.java index 3649895..05ad6b8 100644 --- a/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/TaskDataValidator.java +++ b/net.sf.redmine_mylyn.ui/src/net/sf/redmine_mylyn/internal/ui/editor/TaskDataValidator.java @@ -1,5 +1,7 @@ package net.sf.redmine_mylyn.internal.ui.editor; +import java.text.NumberFormat; +import java.text.ParseException; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; @@ -78,8 +80,9 @@ protected void validateEstimatedHours(TaskData taskData, ErrorMessageCollector c if (attribute != null) { if (!attribute.getValue().trim().isEmpty()) { try { - Double.valueOf(attribute.getValue().trim()); - } catch (NumberFormatException e) { + NumberFormat nf = NumberFormat.getInstance(); + nf.parse(attribute.getValue().trim()).doubleValue(); + } catch (ParseException e) { collector.add(attribute, RedmineAttribute.ESTIMATED.getLabel() + Messages.ERRMSG_FLOAT); } } @@ -91,8 +94,9 @@ protected void validateTimeEntry(TaskData taskData, ErrorMessageCollector collec if (attribute != null) { if (!attribute.getValue().trim().isEmpty()) { try { - Double.valueOf(attribute.getValue().trim()); - } catch (NumberFormatException e) { + NumberFormat nf = NumberFormat.getInstance(); + nf.parse(attribute.getValue().trim()); + } catch (ParseException e) { collector.add(attribute, RedmineAttribute.TIME_ENTRY_HOURS.getLabel() + Messages.ERRMSG_FLOAT); } @@ -169,13 +173,18 @@ protected void validateCustomAttribute(TaskAttribute taskAttribute, CustomField protected boolean validateCustomAttributeType(String value, CustomField customField) { try { switch (customField.getFieldFormat()) { - case FLOAT: Double.valueOf(value); break; + case FLOAT: + NumberFormat nf = NumberFormat.getInstance(); + nf.parse(value); case INT: Integer.valueOf(value); break; default: return true; } } catch (NumberFormatException e) { return false; + } catch (ParseException e) { + return false; } + return true; }