[Webfunds-commits] java/webfunds/client SimplePaymentFrame.java

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Wed, 14 Mar 2001 14:29:04 -0400 (AST)


gelderen    01/03/14 14:29:04

  Modified:    webfunds/client SimplePaymentFrame.java
  Log:
  - Rename some variables and generally make the code more readable;
  - Fix bug that would cause dialog malfunctioning if the AddressBook could
    not be obtained; The dialog would pop up empty when that happened. We
    now pop up a working dialog as 'more correct' behaviour but the real fix
    is to make sure getAddressBook never fails;
  - Remove huge pile of disabled (commented-out) code;
  - We now throw an exception when run gets called before init has succeeded.
    I suspect this is correct behaviour and being strict will allow us to
    catch buggy callers;
  - Construct the payment using the platform specific line endings;
  - Use the newly introduced webfunds.utils.ClipboardHelper to convert the
    above-mentioned payment to a format that can be copied and pasted without
    being mangled. See webfunds.utils.ClipboardHelper for details.
  
  More cleanups to be done...

Revision  Changes    Path
1.49      +213 -276  java/webfunds/client/SimplePaymentFrame.java

Index: SimplePaymentFrame.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/SimplePaymentFrame.java,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -r1.48 -r1.49
--- SimplePaymentFrame.java	2001/02/27 01:28:20	1.48
+++ SimplePaymentFrame.java	2001/03/14 18:29:03	1.49
@@ -1,270 +1,251 @@
 /*
- * $Id: SimplePaymentFrame.java,v 1.48 2001/02/27 01:28:20 iang Exp $
+ * $Id: SimplePaymentFrame.java,v 1.49 2001/03/14 18:29:03 gelderen 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.datatransfer.*;
+import java.awt.event.*;
+import java.io.IOException;
 import javax.swing.*;
 import javax.swing.border.*;
-import java.awt.event.*;
 import java.util.Date;
 
-import webfunds.utils.Hex;
-import webfunds.sox.*;
-import webfunds.ricardian.Contract;
-import webfunds.client.plugins.Plugin;
 import webfunds.client.plugins.ModeNotSupportedException;
+import webfunds.client.plugins.Plugin;
+import webfunds.ricardian.Contract;
+import webfunds.sox.*;
+import webfunds.utils.ClipboardHelper;
+import webfunds.utils.Hex;
 
+
 public class SimplePaymentFrame
     extends Plugin implements ActionListener
 {
+    private static final String NEWLINE = System.getProperty("line.separator");
 
     protected String name           = "SimplePaymentFrame";
-    protected String contractaction = "Pay";
+    protected String contractAction = "Pay";
 
     public String getPluginName()     { return name; }
-    public String getContractAction() { return contractaction; }
+    public String getContractAction() { return contractAction; }
 
     JFrame  frame;
     TextField amount;
     TextField target;
     JTextField validFrom;
     JTextField validTill;
-    TextArea   payment;
+    TextArea   paymentArea;
+
     WalletInterface wallet;
     AccountInfo source;
     JTextArea description;
     JComboBox combo;
     ItemId contractid;
-    
+
+
     public SimplePaymentFrame() { }
-    
+
+
     public void init(WalletInterface wi, AccountInfo source, ItemId contract)
         throws ModeNotSupportedException
     {
-        try
-        {
-//System.err.println("PluginManager: " + pm);
-            wallet      = wi;
-            this.source = source;
-            contractid = contract;
+        this.wallet     = wi;
+        this.source     = source;
+        this.contractid = contract;
+
 
+        //
+        // XXX: this -I think- is bogus, getAddressBook/getAccounts should
+        //      simply never fail but always return at least something
+        //      sensible so that we don't have to juggle with exceptions
+        //      everywhere.
+        //
+        //      For now we do defensive programming and provide an empty
+        //      DefaultComboBoxModel when getAddressBook fails. At least
+        //      this allows for a partially functional dialog.
+        //
+        DefaultComboBoxModel dcbm;
+        try {
             AccountInfo[] accounts = pm.getAddressbook(this).getAccounts();
-            DefaultComboBoxModel dcbm;
             dcbm = new DefaultComboBoxModel(accounts);
+        } catch(IOException e) {
+            e.printStackTrace();
+            System.err.println("Couldn't obtain addressbook :-(");
+            dcbm = new DefaultComboBoxModel();
+        }
 
-            AccountInfo bearer = new AccountInfo();
-            dcbm.insertElementAt(bearer, 0);
-            dcbm.setSelectedItem(bearer);
-            amount      = new TextField(15);
-            target      = new TextField(15);
-            combo       = new JComboBox(dcbm);
-            validFrom   = new JTextField(15);
-            validTill   = new JTextField(15);
-            payment     = new TextArea(20, 55);
-            description = new JTextArea(5, 15);
-            frame       = new JFrame("Payment from account: " +
-                                     source.toString());
-            JPanel pane = (JPanel)frame.getContentPane();
-            
-            JLabel amountlbl        = new JLabel("Amount:");
-            JLabel targetlbl        = new JLabel("Target:");
-            JLabel target2lbl       = new JLabel("Target:");
-            JLabel validFromlbl     = new JLabel("Valid from:");
-            JLabel validTilllbl     = new JLabel("Valid till:");
-            JLabel desclbl          = new JLabel("Description:");
-            JButton ok              = new JButton("Make Payment");
-            JButton directdeposit   = new JButton("Direct Transfer");
-            JButton close           = new JButton("Close");
-            JPanel butpanel         = new JPanel();
-            JPanel textpanel        = new JPanel();
-            GridBagConstraints c    = new GridBagConstraints();
-            JMenuBar menu           = new JMenuBar();
-            JMenu filemenu          = new JMenu("File");
-            // JMenuItem exititem      = new JMenuItem("Close");
-            // JMenuItem configureitem = new JMenuItem("Configure");
-            
-            frame.setJMenuBar(menu);
-            // menu.add(filemenu);
-            // filemenu.add(configureitem);
-            // filemenu.add(new JSeparator());
-            // filemenu.add(exititem);
-
-            /*
-            amount.setActionCommand("amount");
-            target.setActionCommand("target");
-            validFrom.setActionCommand("validFrom");
-            validTill.setActionCommand("validTill");
-            description.setActionCommand("desc");
-            */
-            
-            ok.setToolTipText("Make Payment");
-            close.setToolTipText("close frame");
-            
-            /*
-            amount.setToolTipText("Number of unit to make the payment in");
-            target.setToolTipText("Target account");
-            validFrom.setToolTipText("Date from which the payment is valid");
-            validTill.setToolTipText("Date till which the payment can be cashed");
-            description.setToolTipText("Description");
-            amountlbl.setToolTipText("Number of unit to make the payment in");
-            targetlbl.setToolTipText("Target account");
-            validFromlbl.setToolTipText("Date from which the payment is valid");
-            validTilllbl.setToolTipText("Date till which the payment can be cashed");
-            desclbl.setToolTipText("Description");
-            */
-    
-    
-            // exititem.addActionListener(this);
-            // configureitem.addActionListener(this);
-            ok.addActionListener(this);
-            close.addActionListener(this);
-            directdeposit.addActionListener(this);
-            /*
-            amount.addActionListener(this);
-            target.addActionListener(this);
-            validFrom.addActionListener(this);
-            validTill.addActionListener(this);
-            description.addActionListener(this);
-            */
+        AccountInfo bearer = new AccountInfo();
+        dcbm.insertElementAt(bearer, 0);
+        dcbm.setSelectedItem(bearer);
+
+        amount      = new TextField(15);
+        target      = new TextField(15);
+        combo       = new JComboBox(dcbm);
+        validFrom   = new JTextField(15);
+        validTill   = new JTextField(15);
+        paymentArea = new TextArea(20, 55);
+        description = new JTextArea(5, 15);
+        frame       = new JFrame("Payment from account: " + source.toString());
+        JPanel pane = (JPanel)frame.getContentPane();
+            
+        JLabel amountlbl        = new JLabel("Amount:");
+        JLabel targetlbl        = new JLabel("Target:");
+        JLabel target2lbl       = new JLabel("Target:");
+        JLabel validFromlbl     = new JLabel("Valid from:");
+        JLabel validTilllbl     = new JLabel("Valid till:");
+        JLabel desclbl          = new JLabel("Description:");
+        JButton ok              = new JButton("Make Payment");
+        JButton directdeposit   = new JButton("Direct Transfer");
+        JButton close           = new JButton("Close");
+        JPanel butpanel         = new JPanel();
+        JPanel textpanel        = new JPanel();
+        GridBagConstraints c    = new GridBagConstraints();
+        JMenuBar menu           = new JMenuBar();
+        JMenu filemenu          = new JMenu("File");
+            
+        frame.setJMenuBar(menu);
+            
+        ok.setToolTipText("Make Payment");
+        close.setToolTipText("close frame");
+            
+        ok.addActionListener(this);
+        close.addActionListener(this);
+        directdeposit.addActionListener(this);
     
-            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-            //description.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
-            //payment.setEnabled(false);
-            
-            // filemenu.setMnemonic('f');
-            // exititem.setMnemonic('x');
-            // configureitem.setMnemonic('n');
-            ok.setMnemonic('m');
-            close.setMnemonic('c');
+        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+            
+        ok.setMnemonic('m');
+        close.setMnemonic('c');
     
-            amountlbl.setLabelFor(amount);
-            targetlbl.setLabelFor(target);
-            target2lbl.setLabelFor(combo);
-            validFromlbl.setLabelFor(validFrom);
-            validTilllbl.setLabelFor(validTill);
-            desclbl.setLabelFor(description);
+        amountlbl.setLabelFor(amount);
+        targetlbl.setLabelFor(target);
+        target2lbl.setLabelFor(combo);
+        validFromlbl.setLabelFor(validFrom);
+        validTilllbl.setLabelFor(validTill);
+        desclbl.setLabelFor(description);
     
-            amountlbl.setDisplayedMnemonic('a');
-            targetlbl.setDisplayedMnemonic('t');
-            target2lbl.setDisplayedMnemonic('r');
-            validFromlbl.setDisplayedMnemonic('v');
-            validTilllbl.setDisplayedMnemonic('l');
-            desclbl.setDisplayedMnemonic('d');
-            
-            textpanel.setLayout(new GridBagLayout());
-            
-            int x = 0;
-            int y = 0;
-            
-            c.gridx = x++;
-            c.gridy = y++;
-            c.gridwidth = GridBagConstraints.RELATIVE;
-            c.gridheight = 1;
-            c.weightx = 0;
-            c.weighty = 0;
-            c.anchor = GridBagConstraints.NORTHWEST;
-            c.fill = GridBagConstraints.NONE;
-            c.insets = new Insets(10, 3, 3, 3);
-            textpanel.add(amountlbl, c);
-            
-            c.gridy = y++;
-            c.insets = new Insets(3, 3, 3, 3);
-            textpanel.add(targetlbl, c);
-            
-            c.gridy = y++;
-            textpanel.add(target2lbl, c);
-            
-            c.gridy = y++;
-            textpanel.add(validFromlbl, c);
-            
-            c.gridy = y++;
-            textpanel.add(validTilllbl, c);
-            
-            c.gridy = y++;
-            c.weighty = 1;
-            textpanel.add(desclbl, c);
-            
-            y = 0;
-            
-            c.gridx = x++;
-            c.gridy = y++;
-            c.weighty = 0;
-            c.weightx = 1;
-            c.fill = GridBagConstraints.HORIZONTAL;
-            c.insets = new Insets(10, 3, 3, 3);
-            textpanel.add(amount, c);
+        amountlbl.setDisplayedMnemonic('a');
+        targetlbl.setDisplayedMnemonic('t');
+        target2lbl.setDisplayedMnemonic('r');
+        validFromlbl.setDisplayedMnemonic('v');
+        validTilllbl.setDisplayedMnemonic('l');
+        desclbl.setDisplayedMnemonic('d');
+            
+        textpanel.setLayout(new GridBagLayout());
+            
+        int x = 0;
+        int y = 0;
+            
+        c.gridx = x++;
+        c.gridy = y++;
+        c.gridwidth = GridBagConstraints.RELATIVE;
+        c.gridheight = 1;
+        c.weightx = 0;
+        c.weighty = 0;
+        c.anchor = GridBagConstraints.NORTHWEST;
+        c.fill = GridBagConstraints.NONE;
+        c.insets = new Insets(10, 3, 3, 3);
+        textpanel.add(amountlbl, c);
+            
+        c.gridy = y++;
+        c.insets = new Insets(3, 3, 3, 3);
+        textpanel.add(targetlbl, c);
+            
+        c.gridy = y++;
+        textpanel.add(target2lbl, c);
+        
+        c.gridy = y++;
+        textpanel.add(validFromlbl, c);
+            
+        c.gridy = y++;
+        textpanel.add(validTilllbl, c);
+            
+        c.gridy = y++;
+        c.weighty = 1;
+        textpanel.add(desclbl, c);
+            
+        y = 0;
+            
+        c.gridx = x++;
+        c.gridy = y++;
+        c.weighty = 0;
+        c.weightx = 1;
+        c.fill = GridBagConstraints.HORIZONTAL;
+        c.insets = new Insets(10, 3, 3, 3);
+        textpanel.add(amount, c);
     
-            c.gridy = y++;
-            c.insets = new Insets(3, 3, 3, 3);
-            textpanel.add(target, c);
-            
-            c.gridy = y++;
-            textpanel.add(combo, c);
-
-            c.gridy = y++;
-            textpanel.add(validFrom, c);
-            
-            c.gridy = y++;
-            textpanel.add(validTill, c);
-            
-            c.gridy = y++;
-            c.weighty = 1;
-            textpanel.add(description,c);
-
-            pane.setLayout(new GridBagLayout());
-            
-            butpanel.add(ok);
-            butpanel.add(directdeposit);
-            butpanel.add(close);
-            
-            c.gridx = 1;
-            c.gridy = 0;
-            c.gridwidth = GridBagConstraints.REMAINDER;
-            c.gridheight = GridBagConstraints.RELATIVE;
-            c.weightx = 1;
-            c.weighty = 1;
-            pane.add(textpanel,c);
-            
-            c.gridx = 0;
-            c.gridy = 1;
-            c.gridwidth = GridBagConstraints.REMAINDER;
-            c.gridheight = GridBagConstraints.REMAINDER;
-            c.fill = GridBagConstraints.BOTH;
-            c.weightx = 0;
-            c.weighty = 0;
-            pane.add(butpanel, c);
+        c.gridy = y++;
+        c.insets = new Insets(3, 3, 3, 3);
+        textpanel.add(target, c);
+            
+        c.gridy = y++;
+        textpanel.add(combo, c);
+
+        c.gridy = y++;
+        textpanel.add(validFrom, c);
+            
+        c.gridy = y++;
+        textpanel.add(validTill, c);
+            
+        c.gridy = y++;
+        c.weighty = 1;
+        textpanel.add(description,c);
+
+        pane.setLayout(new GridBagLayout());
+            
+        butpanel.add(ok);
+        butpanel.add(directdeposit);
+        butpanel.add(close);
+            
+        c.gridx = 1;
+        c.gridy = 0;
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        c.gridheight = GridBagConstraints.RELATIVE;
+        c.weightx = 1;
+        c.weighty = 1;
+        pane.add(textpanel,c);
+            
+        c.gridx = 0;
+        c.gridy = 1;
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        c.gridheight = GridBagConstraints.REMAINDER;
+        c.fill = GridBagConstraints.BOTH;
+        c.weightx = 0;
+        c.weighty = 0;
+        pane.add(butpanel, c);
     
-            c.gridx = 0;
-            c.gridy = 0;
-            c.gridwidth = GridBagConstraints.RELATIVE;
-            c.gridheight = GridBagConstraints.RELATIVE;
-            c.weightx = 0.0;
-            c.weighty = 0;
-            c.fill = GridBagConstraints.NONE;
-            pane.add(payment, c);
+        c.gridx = 0;
+        c.gridy = 0;
+        c.gridwidth = GridBagConstraints.RELATIVE;
+        c.gridheight = GridBagConstraints.RELATIVE;
+        c.weightx = 0.0;
+        c.weighty = 0;
+        c.fill = GridBagConstraints.NONE;
+        pane.add(paymentArea, c);
             
-            frame.pack();
-        }
-        catch(Exception e)
-        {
-            e.printStackTrace();
-        }
-        
+        frame.pack();
     }
-    
+
+
+    /**
+     * @throws IllegalStateException
+     *         When init() wasn't called first (or when init() failed).
+     */
     public void run()
     {
-        if (frame != null)
-        {
-            frame.show();
-        }
+        // we expect init() to have been called already
+        if( this.frame == null )
+            throw new IllegalStateException("init not called or crashed");
+
+        this.frame.show();
     }
 
+
     public void actionPerformed(ActionEvent evt)
     {
         String command = evt.getActionCommand();
@@ -278,47 +259,13 @@
             this.stop();
             return ;
         }
-        else if (command.equals("Configure"))
-        {
-            System.out.println("Should bring up config screen");
-            return ;
-        }
-        /*
-        else if (command.equals("amount"))
-        {
-            System.out.println(amount.getText());
-            target.requestFocus();
-            return ;
-        }
-        else if (command.equals("target"))
-        {
-            System.out.println(target.getText());
-            validFrom.requestFocus();
-            return ;
-        }
-        else if (command.equals("validFrom"))
-        {
-            System.out.println(validFrom.getText());
-            validTill.requestFocus();
-            return ;
-        }
-        else if (command.equals("validTill"))
-        {
-            System.out.println(validTill.getText());
-            ok.requestFocus();
-            return ;
-        }
-        */
 
         //
         // Now we have to do a payment.  Sort out standard details first.
         //
 
-        // what does this mean?
-            // need to change null sometime soon;
-
         // amount in units of contract
-        Contract contract = cs.getContract(contractid);
+        Contract contract = this.cs.getContract(contractid);
         double dub;
         try {
             dub = new Double(amount.getText()).doubleValue();
@@ -335,7 +282,7 @@
             return ;
         }
 
-        long longamount = contract.getUnitsOfContract(dub);;
+        long longamount = contract.getUnitsOfContract(dub);
 
         // who to
         AccountInfo tgt = (AccountInfo)combo.getSelectedItem(); //default BEARER
@@ -357,11 +304,11 @@
         Date validTill = new Date(System.currentTimeMillis() + future);
 
         // the payment itself
-        byte[] pay = wallet.makePayment(contractid, source, tgt,
-                             longamount, description.getText().getBytes(),
-                             validFrom, validTill);
+        byte[] payment = wallet.makePayment(contractid, source, tgt,
+                            longamount, description.getText().getBytes(),
+                            validFrom, validTill);
 
-        if (pay == null)     // wallet encountered error, already reported
+        if (payment == null)     // wallet encountered error, already reported
             return ;
 
         //
@@ -380,21 +327,16 @@
             else
                 tla = tla + " ";
 
-            String msg = "\nThis is a payment for " + tla + am + nam +
-                         "\nfrom " + source.getName() +
-                             " (" + Hex.data2hex(source.getByteArray()) + ")" +
-                         "\nto   " + tgtName +
-                         "\n";
-
-                         // " from wallet " + wallet.getShortName() +
-
-            payment.append(msg);;
-            payment.append(new String(pay)); 
-
-            Toolkit toolkit = Toolkit.getDefaultToolkit();
-            Clipboard clipboard = toolkit.getSystemClipboard();
-            StringSelection string = new StringSelection(payment.getText());
-            clipboard.setContents(string, string);
+            String msg = NEWLINE +
+                "This is a payment for " + tla + am + nam + NEWLINE +
+                "from " + source.getName() +
+                " (" + Hex.data2hex(source.getByteArray()) + ")" + NEWLINE +
+                "to   " + tgtName + NEWLINE +
+                new String(payment);
+
+            paymentArea.append(ClipboardHelper.toUnixText(msg));
+
+            ClipboardHelper.setClipboardText( paymentArea.getText() );
         }
 
         else if ("Direct Transfer".equals(command))
@@ -406,12 +348,7 @@
                 return ;
             }
 
-            wallet.makeDeposit(pay, tgt, "Deposit".getBytes());
-            // frame.dispose();
-            // this.stop();
-            // pm.getUInterface(this).infoMessage("Transfer done!");
+            wallet.makeDeposit(payment, tgt, "Deposit".getBytes());
         }
     }
-        
-
 }