[Webfunds-commits] java/webfunds/ricardian Contract.java Support.java

Ian Grigg iang@cypherpunks.ai
Sat, 7 Oct 2000 14:13:23 -0400 (AST)


iang        00/10/07 14:13:23

  Modified:    webfunds/client AccountCellRenderer.java
               webfunds/ricardian Contract.java Support.java
  Log:
  added Contract support for returning DecimalFactors so that displays
  can get the right number of decimal points.

Revision  Changes    Path
1.10      +14 -8     java/webfunds/client/AccountCellRenderer.java

Index: AccountCellRenderer.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/AccountCellRenderer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- AccountCellRenderer.java	1999/09/28 19:34:24	1.9
+++ AccountCellRenderer.java	2000/10/07 18:13:22	1.10
@@ -1,24 +1,27 @@
 /*
- * $Id: AccountCellRenderer.java,v 1.9 1999/09/28 19:34:24 iang Exp $
+ * $Id: AccountCellRenderer.java,v 1.10 2000/10/07 18:13:22 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
  */
 package webfunds.client;
 
-import java.awt.*;
+
+import java.awt.Component;
+import java.awt.Color;
 import javax.swing.*;
 import javax.swing.table.*;
-import webfunds.sox.*;
-import java.text.*;
+import java.text.DecimalFormat;
 
-// this is not account but subaccount?
+import webfunds.ricardian.Contract;
 
+/**
+ *  Render a value at the SubAccount level.
+ *  (Misnamed...)
+ */
 public class AccountCellRenderer extends DefaultTableCellRenderer
 {
     
-    DecimalFormat df = new DecimalFormat("#.000");
-    
     public Component getTableCellRendererComponent(JTable table, Object value,
                      boolean isSelected, boolean hasFocus, int row, int column)
     {
@@ -26,11 +29,14 @@
         {
             AccountRendererData ard = (AccountRendererData)value;
             
-            if (ard.getContract() == null)
+            Contract con = ard.getContract();
+            if (con == null)
             {
                 return super.getTableCellRendererComponent(
                              table, "", isSelected, hasFocus, row, column);
             }
+
+            DecimalFormat df = con.getDecimalFormat();
 
             if (ard.isPending())
             {



1.47      +45 -1     java/webfunds/ricardian/Contract.java

Index: Contract.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Contract.java,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -r1.46 -r1.47
--- Contract.java	2000/10/04 00:38:58	1.46
+++ Contract.java	2000/10/07 18:13:22	1.47
@@ -1,4 +1,4 @@
-/* $Id: Contract.java,v 1.46 2000/10/04 00:38:58 iang Exp $
+/* $Id: Contract.java,v 1.47 2000/10/07 18:13:22 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -27,6 +27,7 @@
 import java.security.cert.CertificateFactory;
 import java.util.Enumeration;
 import java.util.Hashtable;
+import java.text.DecimalFormat;
 
 // these are for the old canonical hash preparer.
 import java.io.BufferedReader;
@@ -109,6 +110,8 @@
     protected   void          setPower(int p)   { power = p; isPower = true; }
     public      double        getFactor()       { return factor; }
 
+    protected String decimalFormatPattern = null;
+    protected DecimalFormat decimalFormat = null;
 
 
     protected String logfix = "     C ";
@@ -178,6 +181,10 @@
         readAndSetPower();
     }
 
+
+
+///////////  Support for Fractional Contracts  //////////////////////////
+
     /**
      *  Units of account / contract used to be part of the
      *  Currency contract, but have been since widened to
@@ -207,6 +214,43 @@
 
         setPower(0);
         this.factor = 1;
+    }
+
+
+
+    /**
+     *  Set the DecimalFormat(pattern) for all future get calls.
+     */
+    public void setDecimalFormatPattern(String s) { decimalFormatPattern=s; }
+    public void setDecimalFormat(DecimalFormat d) { decimalFormat = d; }
+
+    /**
+     *  Get the DecimalFormatpattern.
+     *  If no call to setDecimalFormatPattern() has been made,
+     *  one is initialised from the power of the Contract.
+     */
+    public String getDecimalFormatPattern()
+    {
+        if (decimalFormatPattern != null)
+           return decimalFormatPattern;
+
+        int power = getPower();
+        decimalFormatPattern = Support.power2DecimalFormatPattern(power);
+        return decimalFormatPattern ;
+    }
+
+    /**
+     *  Get a DecimalFormat object suitable for this Contract.
+     *  If no call to setDecimalFormat() has been made,
+     *  one is initialised from getDecimalFormatPattern().
+     */
+    public DecimalFormat getDecimalFormat()
+    {
+        if (decimalFormat!= null)
+           return decimalFormat;
+
+        decimalFormat= new DecimalFormat(getDecimalFormatPattern());
+        return decimalFormat ;
     }
 
 



1.7       +28 -1     java/webfunds/ricardian/Support.java

Index: Support.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/ricardian/Support.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Support.java	2000/10/04 00:38:59	1.6
+++ Support.java	2000/10/07 18:13:23	1.7
@@ -1,5 +1,5 @@
 /*
- * $Id: Support.java,v 1.6 2000/10/04 00:38:59 iang Exp $
+ * $Id: Support.java,v 1.7 2000/10/07 18:13:23 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -266,6 +266,31 @@
         return s + bigStrings[bigs];
     }
 
+    /**
+     *  Handy call to return a string with a number of repeats.
+     */
+    public static String repeats(int i, String s)
+    {
+        int z = (i < 0) ? -i : i;
+        String zeroes = "";
+        while (z-- > 0)
+            zeroes += s;
+        return zeroes;
+    }
+
+
+    /**
+     *  @return a suitable DecimalFactor pattern for the power
+     */
+    public static String power2DecimalFormatPattern(int p)
+    {
+        if (p == 0)
+            return "#";
+        if (p < 0)
+            return "#" + repeats(p, "#");
+        else // if (p > 0)
+            return "#." + repeats(p, "0");
+    }
 
 
 
@@ -286,6 +311,8 @@
             System.err.println("   power int     " + p);
             s = factor2powerString(d);
             System.err.println("   power string  " + s);
+            s = power2DecimalFormatPattern(p);
+            System.err.println("   DF pattern    " + s);
 
             if (p != test)
                 throw new RuntimeException("test == " + test + "   p == " + p);