[jboss-cvs] JBossCache/src/org/jboss/cache ...

Manik Surtani manik at jboss.org
Tue Apr 17 03:47:32 EDT 2007


  User: msurtani
  Date: 07/04/17 03:47:32

  Modified:    src/org/jboss/cache  JBossCacheView.java
  Log:
  Rewrote demo GUI
  
  Revision  Changes    Path
  1.3       +231 -176  JBossCache/src/org/jboss/cache/JBossCacheView.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: JBossCacheView.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/JBossCacheView.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- JBossCacheView.java	4 Apr 2007 14:55:06 -0000	1.2
  +++ JBossCacheView.java	17 Apr 2007 07:47:32 -0000	1.3
  @@ -24,24 +24,29 @@
   import javax.swing.table.TableColumn;
   import javax.swing.tree.DefaultMutableTreeNode;
   import javax.swing.tree.DefaultTreeModel;
  -import javax.swing.tree.TreeNode;
   import javax.swing.tree.TreePath;
   import javax.swing.tree.TreeSelectionModel;
   import javax.transaction.Transaction;
   import javax.transaction.TransactionManager;
   import java.awt.*;
   import java.awt.event.ActionEvent;
  +import java.awt.event.InputEvent;
   import java.awt.event.MouseAdapter;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
   import java.awt.event.WindowEvent;
   import java.awt.event.WindowListener;
   import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.HashMap;
   import java.util.LinkedList;
   import java.util.List;
   import java.util.Map;
   import java.util.Set;
  +import java.util.TreeMap;
  +import java.util.Vector;
  +import java.util.concurrent.Executor;
  +import java.util.concurrent.Executors;
   
   
   /**
  @@ -54,7 +59,7 @@
    * data needs to be displayed, the underlying cache will be accessed directly.
    *
    * @author Manik Surtani
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public class JBossCacheView
   {
  @@ -157,7 +162,7 @@
            CacheFactory<String, String> factory = DefaultCacheFactory.getInstance();
            cache = factory.createCache(configurationFile, false);
            // hack to prevent a state transfer for now
  -//         cache.getConfiguration().setFetchInMemoryState(false);
  +         cache.getConfiguration().setFetchInMemoryState(false);
            cache.start();
            view = new JBossCacheView(cache);
            view.useConsole = useConsole;
  @@ -191,10 +196,11 @@
      private JTree jtree = null;
      private DefaultTableModel table_model = new DefaultTableModel();
      private JTable table = new JTable(table_model);
  -   private MyNode root = new MyNode(Fqn.SEPARATOR);
  -   private Fqn<String> selected_node = null;
  +   private DisplayNode myNodeRoot = new DisplayNode(Fqn.SEPARATOR);
  +   private Node<String, String> root;
  +   private Node<String, String> selected_node = null;
      private JPanel tablePanel = null;
  -   private JPopupMenu operationsPopup = null;
  +   private JPopupMenu operationsPopup = null, dataModificationsPopup = null;
      private static final int KEY_COL_WIDTH = 20;
      private static final int VAL_COL_WIDTH = 300;
      private TransactionManager tx_mgr = null;
  @@ -209,7 +215,7 @@
      {
         addNotify();
         this.useConsole = useConsole;
  -      tree_model = new DefaultTreeModel(root);
  +      tree_model = new DefaultTreeModel(new DisplayNode(Fqn.ROOT.toString()));
         jtree = new JTree(tree_model);
         jtree.setDoubleBuffered(true);
         jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  @@ -224,6 +230,18 @@
         table_model.setColumnIdentifiers(new String[]{"Name", "Value"});
         table_model.addTableModelListener(this);
   
  +      // add a mouse listener  to the table model for delete and insert rows.
  +      MouseListener dataMouseListener = new MouseAdapter()
  +      {
  +         public void mouseClicked(MouseEvent e)
  +         {
  +            if (rightClick(e.getModifiers()))
  +            {
  +               dataModificationsPopup.show(e.getComponent(), e.getX(), e.getY());
  +            }
  +         }
  +      };
  +
         setTableColumnWidths();
   
         tablePanel = new JPanel();
  @@ -231,6 +249,8 @@
         tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
         tablePanel.add(table, BorderLayout.CENTER);
   
  +      table.addMouseListener(dataMouseListener);
  +
         mainPanel.add(tablePanel, BorderLayout.SOUTH);
         JSplitPane contentPanel = null;
         if (useConsole)
  @@ -276,10 +296,10 @@
               TreePath selPath = jtree.getPathForLocation(e.getX(), e.getY());
               if (selRow != -1)
               {
  -               selected_node = makeFqn(selPath.getPath());
  +               selected_node = getNode(selPath.getPath());
                  jtree.setSelectionPath(selPath);
   
  -               if (e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK)
  +               if (rightClick(e.getModifiers()))
                  {
                     operationsPopup.show(e.getComponent(), e.getX(), e.getY());
                  }
  @@ -291,8 +311,7 @@
   
         createMenus();
         setLocation(50, 50);
  -      setSize(getInsets().left + getInsets().right + 800, getInsets().top
  -                                                          + getInsets().bottom + 800);
  +      setSize(getInsets().left + getInsets().right + 800, getInsets().top + getInsets().bottom + 800);
   
         // make sure we set the cache BEFORE initialising this!!
         setCache(cache);
  @@ -320,7 +339,8 @@
                     }
                  });
   
  -         this.cache.addCacheListener(this);
  +         cache.addCacheListener(this);
  +         root = cache.getRoot();
            setTitle("JBoss Cache GUI: Local Address=" + getLocalAddress());
            tx_mgr = this.cache.getConfiguration().getRuntimeConfig().getTransactionManager();
            populateTree();
  @@ -330,8 +350,8 @@
            setTitle("Cache undefined");
            if (tree_model != null)
            {
  -            root = new MyNode(Fqn.SEPARATOR);
  -            tree_model.setRoot(root);
  +            myNodeRoot = new DisplayNode(Fqn.SEPARATOR);
  +            tree_model.setRoot(myNodeRoot);
               tree_model.reload();
   
            }
  @@ -342,6 +362,20 @@
         }
      }
   
  +   /**
  +    * Checks whether a click event is considered a "right-click"
  +    *
  +    * @param modifiers mouse event mods
  +    * @return true if deemed a right click
  +    */
  +   private boolean rightClick(int modifiers)
  +   {
  +      // the simple right click case
  +      if (modifiers == InputEvent.BUTTON3_MASK) return true;
  +      // more complex on Mac OS X where a ctrl-click is deemed the same as a right click
  +      return modifiers == InputEvent.BUTTON1_MASK + InputEvent.CTRL_MASK;
  +   }
  +
      public void windowClosed(WindowEvent event)
      {
      }
  @@ -381,31 +415,22 @@
         {
            row = evt.getFirstRow();
            col = evt.getColumn();
  -         if (col == 0)
  -         {// set()
  -            key = (String) table_model.getValueAt(row, col);
  -            val = (String) table_model.getValueAt(row, col + 1);
  -            if (key != null && val != null)
  +
  +         key = (String) table_model.getValueAt(row, col == 0 ? 0 : col - 1);
  +         val = (String) table_model.getValueAt(row, col == 0 ? 1 : col);
  +
  +         if (key != null && val != null && !isPlaceHolder(key) && !isPlaceHolder(val))
               {
                  try
                  {
  -                  cache.put(selected_node, key, val);
  +               selected_node.put(key, val);
  +               Map<String, String> data = selected_node.getData();
  +               populateTable(data);
                  }
                  catch (Exception e)
                  {
                     e.printStackTrace();
                  }
  -
  -            }
  -         }
  -         else
  -         {
  -            key = (String) table_model.getValueAt(row, col - 1);
  -            val = (String) table.getValueAt(row, col);
  -            if (key != null && val != null)
  -            {
  -               put(selected_node, key, val);
  -            }
            }
         }
      }
  @@ -414,29 +439,10 @@
      public void valueChanged(TreeSelectionEvent evt)
      {
         TreePath path = evt.getPath();
  -      String fqnString = Fqn.SEPARATOR;
  -      Fqn<String> fqn;
  -      String component_name;
         Map<String, String> data;
   
  -      for (int i = 0; i < path.getPathCount(); i++)
  -      {
  -         component_name = ((MyNode) path.getPathComponent(i)).name;
  -         if (component_name.equals(Fqn.SEPARATOR))
  -         {
  -            continue;
  -         }
  -         if (fqnString.equals(Fqn.SEPARATOR))
  -         {
  -            fqnString += component_name;
  -         }
  -         else
  -         {
  -            fqnString = fqnString + Fqn.SEPARATOR + component_name;
  -         }
  -      }
  -      fqn = Fqn.fromString(fqnString);
  -      data = getData(fqn);
  +      selected_node = getNode(path.getPath());
  +      data = selected_node.getData();
         if (data != null)
         {
            mainPanel.add(tablePanel, BorderLayout.SOUTH);
  @@ -453,48 +459,75 @@
   
      /* ------------------ CacheListener interface ------------ */
   
  -   public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
  +   // run any work that happens in this interface in a separate thread.  This is good practise.
  +   private Executor executor = Executors.newFixedThreadPool(1);
  +
  +
  +   public void nodeCreated(final Fqn fqn, final boolean pre, boolean isLocal)
  +   {
  +      Runnable r = new Runnable()
  +      {
  +         public void run()
      {
  -      if (!pre)
  +            if (pre)
         {
  -         MyNode n, p;
  +               DisplayNode n;
   
  -         n = root.add(fqn);
  +               n = myNodeRoot.add(fqn);
            if (n != null)
            {
  -            p = (MyNode) n.getParent();
  -            tree_model.reload(p);
  +                  tree_model.setRoot(myNodeRoot);
  +                  tree_model.reload();
               jtree.scrollPathToVisible(new TreePath(n.getPath()));
            }
         }
  -
  +         }
  +      };
  +      executor.execute(r);
      }
   
  -   public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map data)
  +   public void nodeModified(final Fqn fqn, final boolean pre, final boolean isLocal, ModificationType modType, final Map data)
      {
  -      if (!pre)
  +      Runnable r = new Runnable()
         {
  -         populateTable(data);
  +         public void run()
  +         {
  +            if (pre && selected_node != null && selected_node.getFqn().equals(fqn))//&& !isLocal)
  +            {
  +               clearTable();
  +               Node n = root.getChild(fqn);
  +               if (n != null)
  +               {
  +                  populateTable(n.getData());
         }
      }
  +         }
  +      };
  +      executor.execute(r);
  +   }
   
  -   public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map data)
  +   public void nodeRemoved(final Fqn fqn, final boolean pre, boolean isLocal, Map data)
  +   {
  +      Runnable r = new Runnable()
      {
  -      if (!pre)
  +         public void run()
         {
  -         MyNode n;
  -         TreeNode par;
  +            if (pre)
  +            {
  +               DisplayNode n;
   
  -         n = root.findNode(fqn);
  +               n = myNodeRoot.findNode(fqn);
            if (n != null)
            {
               n.removeAllChildren();
  -            par = n.getParent();
               n.removeFromParent();
  -            tree_model.reload(par);
  +                  tree_model.setRoot(myNodeRoot);
  +                  tree_model.reload();
            }
         }
  -
  +         }
  +      };
  +      executor.execute(r);
      }
   
      public void nodeVisited(Fqn fqn, boolean pre)
  @@ -533,7 +566,7 @@
   
      public void viewChange(final View new_view)
      {
  -      Thread t = new Thread()
  +      Runnable r = new Runnable()
         {
            public void run()
            {
  @@ -546,8 +579,7 @@
               }
            }
         };
  -      t.setDaemon(true);
  -      t.start();
  +      executor.execute(r);
      }
   
      /* ---------------- End of CacheListener interface -------- */
  @@ -592,30 +624,40 @@
       */
      private void addGuiNode(Fqn<String> fqn)
      {
  -      Set<String> children;
  +      Set<Object> children;
   
         if (fqn == null) return;
   
         // 1 . Add myself
  -      root.add(fqn);
  +      myNodeRoot.add(fqn);
   
         // 2. Then add my children
  -      children = getChildrenNames(fqn);
  +      children = cache.getRoot().getChild(fqn).getChildrenNames();
         if (children != null)
         {
  -         for (String child_name : children)
  +         for (Object child_name : children)
            {
  -            addGuiNode(new Fqn<String>(fqn, child_name));
  +            addGuiNode(new Fqn<String>(fqn, (String) child_name));
            }
         }
      }
   
   
  -   private Fqn<String> makeFqn(Object[] path)
  +   private Node getNode(Object[] path)
      {
  +      Fqn<String> fqnToPath;
  +      if (path.length == 0) fqnToPath = Fqn.ROOT;
         List<String> elements = convertMyNodeArrayToStringArray(path);
  -      if (path.length == 0) return Fqn.ROOT;
  -      return new Fqn<String>(elements);
  +      fqnToPath = new Fqn<String>(elements);
  +      if (root.hasChild(fqnToPath))
  +      {
  +         return root.getChild(fqnToPath);
  +      }
  +      else
  +      {
  +         // implicit creation
  +         return root.addChild(fqnToPath);
  +      }
      }
   
      private List<String> convertMyNodeArrayToStringArray(Object[] path)
  @@ -623,7 +665,7 @@
         List<String> list = new LinkedList<String>();
         for (Object o : path)
         {
  -         MyNode n = (MyNode) o;
  +         DisplayNode n = (DisplayNode) o;
            if (!n.name.equals(Fqn.SEPARATOR)) list.add(n.name);
         }
         return list;
  @@ -650,14 +692,16 @@
         String key;
         String val;
         int num_rows;
  -
  +      clearTable();
         if (data == null) return;
         num_rows = data.size();
  -      clearTable();
   
         if (num_rows > 0)
         {
  -         for (Map.Entry<String, String> entry : data.entrySet())
  +         // sort this according to the natural sort order of keys
  +         TreeMap<String, String> sortedMap = new TreeMap<String, String>(data);
  +
  +         for (Map.Entry<String, String> entry : sortedMap.entrySet())
            {
               key = entry.getKey();
               val = entry.getValue();
  @@ -703,6 +747,7 @@
         commit_tx.putValue(AbstractAction.NAME, "Commit TX");
         RollbackTransaction rollback_tx = new RollbackTransaction();
         rollback_tx.putValue(AbstractAction.NAME, "Rollback TX");
  +
         operationsMenu.add(addNode);
         operationsMenu.add(load_action);
         operationsMenu.add(removeNode);
  @@ -722,6 +767,15 @@
         operationsPopup.add(evict_action);
         operationsPopup.add(removeNode);
         operationsPopup.add(addModAction);
  +
  +      InsertRowAction insertRow = new InsertRowAction();
  +      insertRow.putValue(AbstractAction.NAME, "Insert New Row");
  +      RemoveLastRowAction removeLastRow = new RemoveLastRowAction();
  +      removeLastRow.putValue(AbstractAction.NAME, "Remove Last Row");
  +
  +      dataModificationsPopup = new JPopupMenu();
  +      dataModificationsPopup.add(insertRow);
  +      dataModificationsPopup.add(removeLastRow);
      }
   
      private Object getLocalAddress()
  @@ -737,15 +791,6 @@
         }
      }
   
  -   private Map<String, String> getData(Fqn<String> fqn)
  -   {
  -      if (fqn == null) return null;
  -      Node<String, String> child = cache.getRoot().getChild(fqn);
  -      if (child == null) return null;
  -      return child.getData();
  -   }
  -
  -
      private void load(Fqn<String> fqn)
      {
         try
  @@ -759,66 +804,53 @@
         }
      }
   
  -   private void evict(Fqn<String> fqn)
  +   private List<Address> getMembers()
      {
         try
         {
  -         cache.evict(fqn, true);
  +         return new ArrayList<Address>(cache.getMembers());
         }
         catch (Throwable t)
         {
  -         log.error("JBossCacheGUI.evict(): " + t);
  +         log.error("TreeCacheGui.getMembers(): ", t);
  +         return null;
         }
      }
   
  -   private void put(Fqn<String> fqn, Map<String, String> m)
  -   {
  -      try
  +   private String[] getRowPlaceHolderData()
         {
  -         cache.put(fqn, m);
  -      }
  -      catch (Throwable t)
  -      {
  -         log.error("JBossCacheGUI.put(): " + t);
  -      }
  -   }
  +      Collection keys = extractKeys(table_model.getDataVector());
  +      // try all place holders
   
  -   private void put(Fqn<String> fqn, String key, String value)
  -   {
  -      try
  +      int count = 0;
  +      String placeHolderKey = "{ --- Add Key --- }";
  +      while (keys.contains(placeHolderKey))
         {
  -         cache.put(fqn, key, value);
  -      }
  -      catch (Throwable t)
  -      {
  -         log.error("JBossCacheGUI.put(): " + t);
  -      }
  +         count++;
  +         placeHolderKey = "{ --- Add Key " + count + " --- }";
      }
   
  -   private Set getChildrenNames(Fqn<String> fqn)
  -   {
  -      try
  -      {
  -         return cache.getRoot().getChild(fqn).getChildrenNames();
  +      String placeHolderValue = "{ --- Add Value " + (count == 0 ? "" : count) + " --- }";
  +      return new String[]{placeHolderKey, placeHolderValue};
         }
  -      catch (Throwable t)
  +
  +   private boolean isPlaceHolder(String s)
         {
  -         log.error("JBossCacheGUI.getChildrenNames(): " + t);
  -         return null;
  -      }
  +      if (s.startsWith("{ --- Add Key ") && s.endsWith(" --- }")) return true;
  +      if (s.startsWith("{ --- Add Value ") && s.endsWith(" --- }")) return true;
  +      return false;
      }
   
  -   private List<Address> getMembers()
  -   {
  -      try
  +   private Collection extractKeys(Vector<Vector> v)
         {
  -         return new ArrayList<Address>(cache.getMembers());
  -      }
  -      catch (Throwable t)
  +      // very odd data structure.  Entire table is represented as a Vector.  Each row (element in the Vector) is a Vector of 2 elements (key and value)
  +      List l = new LinkedList();
  +      for (Vector row : v)
         {
  -         log.error("TreeCacheGui.getMembers(): ", t);
  -         return null;
  +         // just add keys
  +         l.add(row.get(0));
         }
  +      return l;
      }
   
      /* -------------------------- End of Private Methods ------------------------------ */
  @@ -855,6 +887,34 @@
         System.exit(0);
      }
   
  +
  +   class InsertRowAction extends AbstractAction
  +   {
  +      private static final long serialVersionUID = 7084928639244438800L;
  +
  +      public void actionPerformed(ActionEvent e)
  +      {
  +         table_model.addRow(getRowPlaceHolderData());
  +      }
  +   }
  +
  +   class RemoveLastRowAction extends AbstractAction
  +   {
  +      private static final long serialVersionUID = 7084928639244438800L;
  +
  +      public void actionPerformed(ActionEvent e)
  +      {
  +         int lastRow = table_model.getRowCount() - 1;
  +         if (lastRow > -1)
  +         {
  +            String keyToRemove = (String) table_model.getValueAt(lastRow, 0);
  +            table_model.removeRow(lastRow);
  +            selected_node.remove(keyToRemove);
  +         }
  +      }
  +   }
  +
  +
      class AddNodeAction extends AbstractAction
      {
         private static final long serialVersionUID = 7084928639244438800L;
  @@ -864,8 +924,7 @@
            JTextField fqnTextField = new JTextField();
            if (selected_node != null)
            {
  -            System.out.println("Seletcednode to string = " + selected_node.toString());
  -            fqnTextField.setText(selected_node.toString());
  +            fqnTextField.setText(selected_node.getFqn().toString());
            }
            else
            {
  @@ -887,7 +946,7 @@
            if (userChoice == 0)
            {
               String userInput = fqnTextField.getText();
  -            put(Fqn.fromString(userInput), null);
  +            cache.put(Fqn.fromString(userInput), null);
            }
         }
      }
  @@ -901,8 +960,7 @@
            JTextField fqnTextField = new JTextField();
            if (selected_node != null)
            {
  -            System.out.println("Seletcednode to string = " + selected_node.toString());
  -            fqnTextField.setText(selected_node.toString());
  +            fqnTextField.setText(selected_node.getFqn().toString());
            }
            else
            {
  @@ -939,8 +997,7 @@
            JTextField fqnTextField = new JTextField();
            if (selected_node != null)
            {
  -            System.out.println("Seletcednode to string = " + selected_node.toString());
  -            fqnTextField.setText(selected_node.toString());
  +            fqnTextField.setText(selected_node.getFqn().toString());
            }
            else
            {
  @@ -962,7 +1019,7 @@
            if (userChoice == 0)
            {
               String userInput = fqnTextField.getText();
  -            evict(Fqn.fromString(userInput));
  +            cache.evict(Fqn.fromString(userInput), true);
            }
         }
      }
  @@ -1066,7 +1123,7 @@
         {
            try
            {
  -            cache.removeNode(selected_node);
  +            cache.removeNode(selected_node.getFqn());
            }
            catch (Throwable t)
            {
  @@ -1081,32 +1138,30 @@
   
         public void actionPerformed(ActionEvent e)
         {
  -         Map<String, String> data = getData(selected_node);
  -         if (data != null && data.size() > 0)
  -         {
  -         }
  -         else
  -         {
               clearTable();
  +         Map<String, String> data = selected_node.getData();
  +         if (data == null || data.isEmpty())
  +         {
               data = new HashMap<String, String>();
  -            data.put("Add Key", "Add Value");
  -
  +            String[] placeHolder = getRowPlaceHolderData();
  +            data.put(placeHolder[0], placeHolder[1]);
            }
  +
            populateTable(data);
  -         getContentPane().add(tablePanel, BorderLayout.SOUTH);
  -         validate();
   
  +         mainPanel.add(tablePanel, BorderLayout.SOUTH);
  +         validate();
         }
      }
   
   
  -   class MyNode extends DefaultMutableTreeNode
  +   class DisplayNode extends DefaultMutableTreeNode
      {
         private static final long serialVersionUID = 4882445905140460053L;
         String name = "<unnamed>";
   
   
  -      MyNode(String name)
  +      DisplayNode(String name)
         {
            this.name = name;
         }
  @@ -1116,9 +1171,9 @@
          * Adds a new node to the view. Intermediary nodes will be created if they don't yet exist.
          * Returns the first node that was created or null if node already existed
          */
  -      public MyNode add(Fqn<String> fqn)
  +      public DisplayNode add(Fqn<String> fqn)
         {
  -         MyNode curr, n, ret = null;
  +         DisplayNode curr, n, ret = null;
   
            if (fqn == null) return null;
            curr = this;
  @@ -1128,7 +1183,7 @@
               n = curr.findChild(child_name);
               if (n == null)
               {
  -               n = new MyNode(child_name);
  +               n = new DisplayNode(child_name);
                  if (ret == null) ret = n;
                  curr.add(n);
               }
  @@ -1147,9 +1202,9 @@
         }
   
   
  -      private MyNode findNode(Fqn<String> fqn)
  +      private DisplayNode findNode(Fqn<String> fqn)
         {
  -         MyNode curr, n;
  +         DisplayNode curr, n;
   
            if (fqn == null) return null;
            curr = this;
  @@ -1167,9 +1222,9 @@
         }
   
   
  -      private MyNode findChild(String relative_name)
  +      private DisplayNode findChild(String relative_name)
         {
  -         MyNode child;
  +         DisplayNode child;
   
            if (relative_name == null || getChildCount() == 0)
            {
  @@ -1177,7 +1232,7 @@
            }
            for (int i = 0; i < getChildCount(); i++)
            {
  -            child = (MyNode) getChildAt(i);
  +            child = (DisplayNode) getChildAt(i);
               if (child.name == null)
               {
                  continue;
  
  
  



More information about the jboss-cvs-commits mailing list