package name.abuchen.portfolio.ui.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import name.abuchen.portfolio.model.Values;
import name.abuchen.portfolio.ui.Messages;

import org.eclipse.core.databinding.conversion.IConverter;

public class StringToCurrencyConverter implements IConverter
{
    private static final Pattern PATTERN = Pattern.compile("^([\\d.]*)(,(\\d*))?$"); //$NON-NLS-1$
    private final NumberFormat full = new DecimalFormat("#,###"); //$NON-NLS-1$

    private final double factor;

    public StringToCurrencyConverter(Values<?> type)
    {
        this.factor = type.divider();
    }

    @Override
    public Object getFromType()
    {
        return String.class;
    }

    @Override
    public Object getToType()
    {
        return long.class;
    }

    @Override
    public Object convert(Object fromObject)
    {
        try
        {
            String value = (String) fromObject;
            value = value.trim();
            Matcher m = PATTERN.matcher(String.valueOf(value));
            if (!m.matches())
                throw new IllegalArgumentException(String.format(Messages.CellEditor_NotANumber, value));

            String strBefore = m.group(1);
            Number before = strBefore.trim().length() > 0 ? full.parse(strBefore) : Long.valueOf(0);

            String strAfter = m.group(3);
            int after = 0;
            if (strAfter != null && strAfter.length() > 0)
            {
                after = Integer.parseInt(strAfter);

                int length = (int) Math.log10(factor);
                for (int ii = strAfter.length(); ii > length; ii--)
                    after /= 10;
                for (int ii = strAfter.length(); ii < length; ii++)
                    after *= 10;
            }

            long resultValue = before.longValue() * (int) factor + after;
            return Long.valueOf(resultValue);
        }
        catch (ParseException e)
        {
            throw new IllegalArgumentException(e);
        }
    }

}

@@ -973,34 +973,8 @@
     
-try%0A        %7B%0A            
 Stri
@@ -1003,16 +1003,252 @@
 omObject
+;%0A        value = value.trim();%0A%0A        try%0A        %7B%0A            long result = 0;%0A            for (String part : value.split(%22%5C%5C+%22)) //$NON-NLS-1$%0A                result += convertToLong(part);%0A%0A            return Long.valueOf(result)
 ;%0A      
@@ -1253,38 +1253,194 @@
     
+%7D%0A
     
-value = value.trim();
+    catch (ParseException e)%0A        %7B%0A            throw new IllegalArgumentException(e);%0A        %7D%0A%0A    %7D%0A%0A    private long convertToLong(String part) throws ParseException
 %0A    
+%7B%0A
     
@@ -1490,21 +1490,16 @@
 eOf(
-value));%0A    
+part));%0A
     
@@ -1520,20 +1520,16 @@
 ches())%0A
-    
         
@@ -1617,22 +1617,17 @@
 er, 
-value
+part
 ));%0A%0A
-    
     
@@ -1661,20 +1661,16 @@
 oup(1);%0A
-    
         
@@ -1763,28 +1763,24 @@
 );%0A%0A        
-    
 String strAf
@@ -1801,28 +1801,24 @@
 3);%0A        
-    
 int after = 
@@ -1824,28 +1824,24 @@
  0;%0A        
- 
-   
 if (strAfter
@@ -1879,33 +1879,26 @@
  0)%0A        
-    %7B%0A   
+%7B%0A
             
@@ -1893,25 +1893,24 @@
             
- 
 after = Inte
@@ -1930,28 +1930,24 @@
 strAfter);%0A%0A
-    
             
@@ -1985,20 +1985,16 @@
 actor);%0A
-    
         
@@ -2061,28 +2061,24 @@
             
-    
 after /= 10;
@@ -2074,28 +2074,24 @@
 fter /= 10;%0A
-    
             
@@ -2154,28 +2154,24 @@
             
-    
 after *= 10;
@@ -2183,26 +2183,19 @@
     
-    %7D%0A%0A   
+%7D%0A%0A
         
  lon
@@ -2182,33 +2182,32 @@
      %7D%0A%0A        
- 
 long resultValue
@@ -2264,159 +2264,27 @@
     
-    
 return 
-Long.valueOf(resultValue);%0A        %7D%0A        catch (ParseException e)%0A        %7B%0A            throw new IllegalArgumentException(e);%0A        %7D
+resultValue;
 %0A   

Support additions inside input field to calculate values on the fly