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

Ian Grigg iang@cypherpunks.ai
Sun, 17 Sep 2000 16:35:56 -0400 (AST)


iang        00/09/17 16:35:56

  Modified:    webfunds/client AccountBrowserImpl.java
  Log:
  1. moved some common code into routines (seems to work, but not really
  sure if this is the right thing to do with Swing) so that I could add do...
  2. refresh of the account after a create account in order to pick up any
  contracts that the wallet has automatically added.
  a. all of these swing routines should probably be made thread safe, the
  treemodel is documented as being non-thread safe...
  b. there needs to be a way in which the outside code can call back to
  the AccountBrowserImpl in order to notify of changes to the tree structure
  (such as the above, or when a deposit decides to add an account, etc).

Revision  Changes    Path
1.74      +72 -54    java/webfunds/client/AccountBrowserImpl.java

Index: AccountBrowserImpl.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/AccountBrowserImpl.java,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -r1.73 -r1.74
--- AccountBrowserImpl.java	2000/06/05 03:48:15	1.73
+++ AccountBrowserImpl.java	2000/09/17 20:35:55	1.74
@@ -1,5 +1,5 @@
 /*
- * $Id: AccountBrowserImpl.java,v 1.73 2000/06/05 03:48:15 gelderen Exp $
+ * $Id: AccountBrowserImpl.java,v 1.74 2000/09/17 20:35:55 iang Exp $
  *
  * Copyright (c) Systemics Inc 1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -266,21 +266,29 @@
         return contract ;
     }
 
-    public void displayNewContract(Contract con, DefaultMutableTreeNode node)
+    public boolean isPresent(Contract con, DefaultMutableTreeNode node)
     {
         logmsg("**** check " + con);
         Enumeration e = node.children();
-        while (e.hasMoreElements())       // make sure it's not already there
+        while (e.hasMoreElements())   // scan node's children looking for con
         {
           DefaultMutableTreeNode nn;
           nn = (DefaultMutableTreeNode)e.nextElement();
           Contract nodeCon = (Contract)nn.getUserObject();
           if (nodeCon.equals(con))
-            return;
+            return true ;
         }
 
+        return false ;
+    }
+
+    public void displayNewContract(Contract con, DefaultMutableTreeNode node)
+    {
+        if (isPresent(con, node))     // make sure it's not already there
+            return ;
+
         logmsg("**** display " + con);
-        node.add(new DefaultMutableTreeNode(con, false));
+        node.add(new DefaultMutableTreeNode(con, false)); // false == !children
         treemodel.nodeChanged(node);
         treemodel.reload(node);
         DefaultMutableTreeNode parent;
@@ -289,6 +297,32 @@
         tree.expandPath(new TreePath(node.getPath()));
     }
 
+    public void displayNewContracts(ItemId[] ids, DefaultMutableTreeNode node)
+    {
+        for (int i = 0; i < ids.length; i++)
+        {
+            Contract contract = cs.getContract(ids[i]);
+            if (isPresent(contract, node))
+                continue ;
+
+            // displayNewContract(contract, node);
+            node.add(new DefaultMutableTreeNode(contract, false));
+        }
+
+        refreshNode(node);
+    }
+
+    public void refreshNode(DefaultMutableTreeNode node)
+    {
+        logmsg("**** refresh " + node);
+        treemodel.nodeChanged(node);
+        treemodel.reload(node);
+        DefaultMutableTreeNode parent;
+        parent = (DefaultMutableTreeNode)node.getParent();
+        tree.collapsePath(new TreePath(parent.getPath()));
+        tree.expandPath(new TreePath(node.getPath()));
+    }
+
     //
     //  These are added as standard, and also added for custom wallet
     //  plugin popups.
@@ -395,46 +429,51 @@
             SwingWorker worker = new SwingWorker()
             {
               DefaultMutableTreeNode node;
+              WalletInterface wi;
 
               public Object construct()
               {
                 TreePath treepath = treeselection.getSelectionPath();
                 logmsg("TP = " + treepath);
                 node = (DefaultMutableTreeNode)treepath.getLastPathComponent();
-                WalletInterface wi = (WalletInterface)node.getUserObject();
-                return wi.addAccount();
+                wi = (WalletInterface)node.getUserObject();
+                return wi.addAccount();     // AccountInfo, to get() below
               }
 
               public void finished()
               {
-                if (get() != null)
+                if ((AccountInfo)get() == null)
+                    return ;
+
+                Runnable updateTree = new Runnable()
                 {
-                  Runnable updateTree = new Runnable()
+                  public void run()
                   {
-                    public void run()
-                    {
-                      node.add(new DefaultMutableTreeNode(get(), true));
-                      treemodel.nodeChanged(node);
-                      treemodel.reload(node);
-                      DefaultMutableTreeNode parent;
-                      parent = (DefaultMutableTreeNode)node.getParent();
-                      tree.collapsePath(new TreePath(parent.getPath()));
-                      tree.expandPath(new TreePath(node.getPath()));
-                    }
-                  };
-                  SwingUtilities.invokeLater(updateTree);
-                }
+                    AccountInfo acct = (AccountInfo)get();
+                    DefaultMutableTreeNode aNode;
+                    aNode = new DefaultMutableTreeNode(acct, true);
+
+                    node.add(aNode);
+                    refreshNode(node);
+//                    treemodel.nodeChanged(node);
+//                    treemodel.reload(node);
+//                    DefaultMutableTreeNode parent;
+//                    parent = (DefaultMutableTreeNode)node.getParent();
+//                    tree.collapsePath(new TreePath(parent.getPath()));
+//                    tree.expandPath(new TreePath(node.getPath()));
+
+                    /*
+                     *  Some wallets add default contracts.
+                     */
+                    ItemId[] ids = wi.getContracts(acct);
+                    displayNewContracts(ids, aNode);
+                  }
+                };
+                SwingUtilities.invokeLater(updateTree);
               }
 
             };
 
-            //
-            //  Jikes grumbles if a variable is not used.
-            //  These two examples are particularly silly,
-            //  as the event is surplus information, and
-            //  the swing worker is a magic object that uses
-            //  itself.
-            // SwingWorker jikesUse = worker; jikesUse = null;
           }
         };
 
@@ -595,24 +634,6 @@
                       Contract con = cs.getContract((ItemId)get());
 
                       displayNewContract(con, node);
-/*
-                      Enumeration e = node.children();
-                      while (e.hasMoreElements())
-                      {
-                        DefaultMutableTreeNode nn;
-                        nn = (DefaultMutableTreeNode)e.nextElement();
-                        Contract nodeCon = (Contract)nn.getUserObject();
-                        if (nodeCon.equals(con))
-                          return;
-                      }
-                      node.add(new DefaultMutableTreeNode(con, false));
-                      treemodel.nodeChanged(node);
-                      treemodel.reload(node);
-                      DefaultMutableTreeNode parent;
-                      parent = (DefaultMutableTreeNode)node.getParent();
-                      tree.collapsePath(new TreePath(parent.getPath()));
-                      tree.expandPath(new TreePath(node.getPath()));
-*/
                     }
                   };
 
@@ -622,9 +643,6 @@
 
             };
 
-            //
-            //  Jikes grumbles ...
-            // SwingWorker jikesUse = worker; jikesUse = null;
           }
         };
 
@@ -650,7 +668,7 @@
               {
                 boolean successful = ((Boolean)get()).booleanValue();
                 logmsg("Remove was " + successful);
-                if (successful) // ((Boolean)get()).booleanValue())
+                if (successful)
                 {
                   Runnable updateTree = new Runnable()
                   {
@@ -680,9 +698,6 @@
 
             };
 
-            //
-            //  Jikes grumbles ...
-            // SwingWorker jikesUse = worker; jikesUse = null;
           }
         };
 
@@ -730,6 +745,9 @@
 
             AccountInfo acct = (AccountInfo)node.getUserObject();
             JTextPane text = new JTextPane();
+
+//this is DUMB!  should ask the wallet for its info display...
+
             String msg = "Hash of the account is: " +
                          webfunds.utils.Hex.data2hex(acct.getId());
             text.setText(msg);