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

Ian Grigg iang@cypherpunks.ai
Sat, 30 Sep 2000 14:48:07 -0400 (AST)


iang        00/09/30 14:48:07

  Modified:    webfunds/client AccountBrowserImpl.java
                        AccountTreeModel.java
  Log:
  Handling of missing contract, only partially done, numbers missing.
  Tree node might want to really be the itemId not the Contract.

Revision  Changes    Path
1.78      +91 -31    java/webfunds/client/AccountBrowserImpl.java

Index: AccountBrowserImpl.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/AccountBrowserImpl.java,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -r1.77 -r1.78
--- AccountBrowserImpl.java	2000/09/24 23:58:08	1.77
+++ AccountBrowserImpl.java	2000/09/30 18:48:06	1.78
@@ -1,5 +1,5 @@
 /*
- * $Id: AccountBrowserImpl.java,v 1.77 2000/09/24 23:58:08 iang Exp $
+ * $Id: AccountBrowserImpl.java,v 1.78 2000/09/30 18:48:06 iang Exp $
  *
  * Copyright (c) Systemics Inc 1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -272,29 +272,32 @@
         return contract ;
     }
 
-    public boolean isPresent(Contract con, DefaultMutableTreeNode node)
+    public boolean isPresent(ItemId id, DefaultMutableTreeNode node)
     {
-        logmsg("**** check " + con);
+        logmsg("**** check " + id.fp());
         Enumeration e = node.children();
         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))
+          // Contract nodeCon = (Contract)nn.getUserObject();
+          ItemId nodeId = getItemId(nn.getUserObject());
+          if (nodeId.equals(id))
             return true ;
         }
 
         return false ;
     }
 
-    public void displayNewContract(Contract con, DefaultMutableTreeNode node)
+    public void displayNewContract(ItemId id, DefaultMutableTreeNode node)
     {
-        if (isPresent(con, node))     // make sure it's not already there
+        if (isPresent(id, node))     // make sure it's not already there
             return ;
 
-        logmsg("**** display " + con);
-        node.add(new DefaultMutableTreeNode(con, false)); // false == !children
+        logmsg("**** display " + id.fp());
+        Object obj = getBestLastNode(id);  // Contract or ItemId
+        node.add(new DefaultMutableTreeNode(obj, false)); // false == !children
+
         treemodel.nodeChanged(node);
         treemodel.reload(node);
         DefaultMutableTreeNode parent;
@@ -307,12 +310,13 @@
     {
         for (int i = 0; i < ids.length; i++)
         {
-            Contract contract = cs.getContract(ids[i]);
-            if (isPresent(contract, node))
+            ItemId id = ids[i];
+            if (isPresent(id, node))
                 continue ;
 
+            Object obj = getBestLastNode(id);  // Contract or ItemId
             // displayNewContract(contract, node);
-            node.add(new DefaultMutableTreeNode(contract, false));
+            node.add(new DefaultMutableTreeNode(obj, false));
         }
 
         refreshNode(node);
@@ -336,6 +340,43 @@
             wallets[i].adminEvent(code, reason);
     }
 
+    /**
+     *  The last node should be a contract, but sometimes the contract
+     *  is lost, in which case, we get by with the ItemId.
+     *  As the ItemId is the thing most often required, here's a handy
+     *  conversion routine.
+     */
+    protected ItemId getItemId(Object obj)
+    {
+        if (obj == null)
+            return null;
+
+        if (obj instanceof ItemId)
+            return (ItemId)obj;
+
+        if (obj instanceof Contract)
+        {
+            Contract contract = (Contract)obj;
+            return contract.getId();
+        }
+        Class clss = obj.getClass();
+        throw new IllegalArgumentException("getItemId( " + clss + " )");
+    }
+
+    /**
+     *  The last node should be a contract.  Return the best for this.
+     */
+    protected Object getBestLastNode(ItemId id)
+    {
+        if (id == null)
+            return null;
+
+        Contract contract = cs.getContract(id);
+        if (contract == null)
+            return (Object) id;
+        else
+            return (Object) contract;
+    }
 
     //
     //  These are added as standard, and also added for custom wallet
@@ -637,9 +678,9 @@
                   {
                     public void run()
                     {
-                      Contract con = cs.getContract((ItemId)get());
+                      // Contract con = cs.getContract((ItemId)get());
 
-                      displayNewContract(con, node);
+                      displayNewContract((ItemId)get(), node);
                     }
                   };
 
@@ -807,12 +848,11 @@
                   DefaultMutableTreeNode n;
                   n = (DefaultMutableTreeNode)treepath.getLastPathComponent();
                   Object[] objectpath = n.getUserObjectPath();
-                  Contract contract = (Contract)objectpath[3];
                   WalletInterface wi = (WalletInterface)objectpath[1];
                   AccountInfo acct = (AccountInfo)objectpath[2];
 
                   boolean success;
-                  success = wi.removeContract(acct, contract.getId());
+                  success = wi.removeContract(acct, getItemId(objectpath[3]));
                   if (success)
                   {
 // woops, this might remove the account as well.
@@ -832,6 +872,18 @@
         {
           public void actionPerformed(ActionEvent evt)
           {
+            TreePath treepath = treeselection.getSelectionPath();
+            DefaultMutableTreeNode node;
+            node = (DefaultMutableTreeNode)treepath.getLastPathComponent();
+            Object obj = node.getUserObject();
+            if (obj instanceof ItemId)
+            {
+                String e1 = "Name Change failed: no Contract available!";
+                String e2 = "Change failed";
+                JOptionPane.showMessageDialog(frame, e1, e2,
+                                JOptionPane.INFORMATION_MESSAGE);
+                return ;
+            }
 
             logmsg("Changing contract name");
 
@@ -839,10 +891,7 @@
             String q2 = "New Contract name";
             String name = JOptionPane.showInputDialog(frame, q1, q2,
                                       JOptionPane.QUESTION_MESSAGE);
-            TreePath treepath = treeselection.getSelectionPath();
-            DefaultMutableTreeNode node;
-            node = (DefaultMutableTreeNode)treepath.getLastPathComponent();
-            Contract contract = (Contract)node.getUserObject();
+            Contract contract = (Contract)obj;
             try {
                 contract.setApplicationName(name);
             } catch (ContractException ex) {
@@ -877,13 +926,12 @@
               AccountInfo account = (AccountInfo)obj;
               rb.init(account);
             }
-            else if (obj instanceof Contract)
+            else if (obj instanceof Contract || obj instanceof ItemId)
             {
               DefaultMutableTreeNode parent;
               parent = (DefaultMutableTreeNode)node.getParent();
               AccountInfo acct = (AccountInfo)(parent).getUserObject();
-              Contract contract = (Contract)obj;
-              rb.init(acct, contract.getId());
+              rb.init(acct, getItemId(obj));
             }
           }
         };
@@ -901,10 +949,9 @@
                   DefaultMutableTreeNode n;
                   n = (DefaultMutableTreeNode)treepath.getLastPathComponent();
                   Object[] objectpath = n.getUserObjectPath();
-                  Contract contract = (Contract)objectpath[3];
                   WalletInterface wi = (WalletInterface)objectpath[1];
                   AccountInfo acct = (AccountInfo)objectpath[2];
-                  wi.update(acct, contract.getId());
+                  wi.update(acct, getItemId(objectpath[3]));
                   treemodel.nodeChanged(n);
                 }
               };
@@ -920,10 +967,18 @@
             TreePath treepath = treeselection.getSelectionPath();
             DefaultMutableTreeNode node;
             node = (DefaultMutableTreeNode)treepath.getLastPathComponent();
-            Contract contract = (Contract)node.getUserObject();
-            ItemId hash = contract.getId();
+            Object obj = node.getUserObject();
+            ItemId hash = getItemId(obj);
             JTextPane text = new JTextPane();
-            text.setText("Hash of the contract is: " + hash);
+
+            String s = "";
+            if (obj instanceof Contract)
+                s += "Contract " + ((Contract)obj).toString();
+            else
+                s += "(Contract is not available)";
+            s += "\n\nHash of the contract is:\n\n" + hash;
+            
+            text.setText(s);
             text.setEditable(false);
             text.setBackground(frame.getBackground());
             JOptionPane.showMessageDialog(frame, text, "Contract information",
@@ -1267,8 +1322,14 @@
         }
 
         Object obj = node.getUserObject();
+        if (obj == null)
+        {
+            logmsg("no UserObject in node? " + node);
+            return ;
+        }
+
         JPopupMenu popup;
-        if (obj instanceof Contract)
+        if (obj instanceof Contract || obj instanceof ItemId)
         {
 //logmsg("getting popup");
             popup = (JPopupMenu)contractPopups.get(wi);
@@ -1348,10 +1409,9 @@
         else if (mode == Plugin.CONTRACTMODE)
         {
             Object[] objectpath = node.getUserObjectPath();
-            Contract contract = (Contract)objectpath[3];
             WalletInterface wi = (WalletInterface)objectpath[1];
             AccountInfo acct = (AccountInfo)objectpath[2];
-            ItemId id = contract.getId();
+            ItemId id = getItemId(objectpath[3]);
             c.getPluginManager(this).getInstance(command, wi, acct, id);
         }
     }



1.22      +23 -10    java/webfunds/client/AccountTreeModel.java

Index: AccountTreeModel.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/AccountTreeModel.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- AccountTreeModel.java	2000/09/24 23:14:30	1.21
+++ AccountTreeModel.java	2000/09/30 18:48:06	1.22
@@ -1,5 +1,5 @@
 /*
- * $Id: AccountTreeModel.java,v 1.21 2000/09/24 23:14:30 iang Exp $
+ * $Id: AccountTreeModel.java,v 1.22 2000/09/30 18:48:06 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -47,6 +47,8 @@
         setContractStore(cs);
         init();
     }
+
+    protected void logmsg(String s) { System.err.println("   ATM-" + s); }
     
     private void init()
     {
@@ -54,22 +56,33 @@
         for(int w = 0; w < wallets.length;w++)
         {
             DefaultMutableTreeNode node;
-            node = new DefaultMutableTreeNode(wallets[w], true);
+            WalletInterface wallet = wallets[w];
+            node = new DefaultMutableTreeNode(wallet, true);
             ((DefaultMutableTreeNode)root).add(node );
-            AccountInfo[] accts = wallets[w].getAccounts();
+            AccountInfo[] accts = wallet.getAccounts();
+logmsg("wallet " + wallet.getShortName() + " has " + accts.length + " accounts");
             for(int a = 0; a < accts.length;a++)
             {
+                AccountInfo ac = accts[a];
                 DefaultMutableTreeNode accountnode;
-                accountnode = new DefaultMutableTreeNode(accts[a], true);
+                accountnode = new DefaultMutableTreeNode(ac, true);
                 node.add(accountnode);
-System.err.println("getting...");
-                ItemId[] items = wallets[w].getContracts(accts[a]);
-System.err.println("    got...");
+                ItemId[] items = wallets[w].getContracts(ac);
+logmsg("     " + ac + " has " + items.length + " items");
                 for(int i = 0; i < items.length;i++)
                 {
-System.err.println("    " + i + " " + items[i]);
-                    Contract con = cs.getContract(items[i]);
-                    accountnode.add(new DefaultMutableTreeNode(con, false));
+                    ItemId id = items[i];
+                    Contract con = cs.getContract(id);
+logmsg("          " + i + ": " + id.fp() + " con " + con);
+                    DefaultMutableTreeNode nod;
+                    if (con == null)
+                    {
+                        logmsg("No Contract: " + id + " !?!");
+                        nod = new DefaultMutableTreeNode(id, false);
+                    }
+                    else
+                        nod = new DefaultMutableTreeNode(con, false);
+                    accountnode.add(nod);
                 }
             }
         }