logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From psm...@apache.org
Subject cvs commit: jakarta-log4j/src/java/org/apache/log4j/chainsaw ReceiversPanel.java ReceiversTreeModel.java
Date Thu, 18 Sep 2003 04:05:05 GMT
psmith      2003/09/17 21:05:05

  Modified:    src/java/org/apache/log4j/chainsaw ReceiversPanel.java
                        ReceiversTreeModel.java
  Log:
  The receivers panel now binds the ReceiversTreeModel
  as a PluginListener and it manages it's own state
  with respect to Receivers starting/stopped.  Less coupling,
  changes to the PluginRegistry by external clients
  are now properly reflected when they happen.
  
  Revision  Changes    Path
  1.12      +105 -61   jakarta-log4j/src/java/org/apache/log4j/chainsaw/ReceiversPanel.java
  
  Index: ReceiversPanel.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ReceiversPanel.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ReceiversPanel.java	9 Sep 2003 02:57:01 -0000	1.11
  +++ ReceiversPanel.java	18 Sep 2003 04:05:05 -0000	1.12
  @@ -49,23 +49,6 @@
   
   package org.apache.log4j.chainsaw;
   
  -import org.apache.log4j.Level;
  -import org.apache.log4j.LogManager;
  -import org.apache.log4j.chainsaw.icons.ChainsawIcons;
  -import org.apache.log4j.chainsaw.icons.LevelIconFactory;
  -import org.apache.log4j.chainsaw.icons.LineIconFactory;
  -import org.apache.log4j.helpers.LogLog;
  -import org.apache.log4j.net.SocketAppender;
  -import org.apache.log4j.net.SocketHubAppender;
  -import org.apache.log4j.net.SocketHubReceiver;
  -import org.apache.log4j.net.SocketNodeEventListener;
  -import org.apache.log4j.net.SocketReceiver;
  -import org.apache.log4j.net.UDPAppender;
  -import org.apache.log4j.net.UDPReceiver;
  -import org.apache.log4j.plugins.Pauseable;
  -import org.apache.log4j.plugins.PluginRegistry;
  -import org.apache.log4j.plugins.Receiver;
  -
   import java.awt.BorderLayout;
   import java.awt.Color;
   import java.awt.Component;
  @@ -77,16 +60,11 @@
   import java.awt.event.ActionEvent;
   import java.awt.event.KeyEvent;
   import java.awt.event.KeyListener;
  -
   import java.beans.PropertyChangeEvent;
   import java.beans.PropertyChangeListener;
  -
   import java.io.IOException;
  -
   import java.lang.reflect.Method;
  -
   import java.net.URL;
  -
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Collections;
  @@ -105,7 +83,6 @@
   import javax.swing.ImageIcon;
   import javax.swing.InputVerifier;
   import javax.swing.JButton;
  -import javax.swing.JCheckBoxMenuItem;
   import javax.swing.JComponent;
   import javax.swing.JDialog;
   import javax.swing.JEditorPane;
  @@ -121,13 +98,34 @@
   import javax.swing.JTree;
   import javax.swing.KeyStroke;
   import javax.swing.SwingUtilities;
  -import javax.swing.event.ChangeEvent;
  -import javax.swing.event.ChangeListener;
   import javax.swing.event.HyperlinkEvent;
   import javax.swing.event.HyperlinkListener;
  +import javax.swing.event.TreeExpansionEvent;
  +import javax.swing.event.TreeModelEvent;
  +import javax.swing.event.TreeModelListener;
   import javax.swing.event.TreeSelectionEvent;
   import javax.swing.event.TreeSelectionListener;
  +import javax.swing.event.TreeWillExpandListener;
   import javax.swing.tree.DefaultMutableTreeNode;
  +import javax.swing.tree.ExpandVetoException;
  +import javax.swing.tree.TreePath;
  +
  +import org.apache.log4j.Level;
  +import org.apache.log4j.LogManager;
  +import org.apache.log4j.chainsaw.icons.ChainsawIcons;
  +import org.apache.log4j.chainsaw.icons.LevelIconFactory;
  +import org.apache.log4j.chainsaw.icons.LineIconFactory;
  +import org.apache.log4j.helpers.LogLog;
  +import org.apache.log4j.net.SocketAppender;
  +import org.apache.log4j.net.SocketHubAppender;
  +import org.apache.log4j.net.SocketHubReceiver;
  +import org.apache.log4j.net.SocketNodeEventListener;
  +import org.apache.log4j.net.SocketReceiver;
  +import org.apache.log4j.net.UDPAppender;
  +import org.apache.log4j.net.UDPReceiver;
  +import org.apache.log4j.plugins.Pauseable;
  +import org.apache.log4j.plugins.PluginRegistry;
  +import org.apache.log4j.plugins.Receiver;
   
   
   /**
  @@ -162,7 +160,50 @@
       setMinimumSize(getPreferredSize());
       setMaximumSize(getPreferredSize());
   
  -    receiversTree.setModel(new ReceiversTreeModel());
  +    final ReceiversTreeModel model = new ReceiversTreeModel();
  +    PluginRegistry.addPluginListener(model);
  +    receiversTree.setModel(model);
  +
  +    receiversTree.setExpandsSelectedPaths(true);
  +    model.addTreeModelListener(
  +      new TreeModelListener() {
  +        public void treeNodesChanged(TreeModelEvent e) {
  +          expandRoot();
  +        }
  +
  +        public void treeNodesInserted(TreeModelEvent e) {
  +          expandRoot();
  +        }
  +
  +        public void treeNodesRemoved(TreeModelEvent e) {
  +          expandRoot();
  +        }
  +
  +        public void treeStructureChanged(TreeModelEvent e) {
  +          expandRoot();
  +        }
  +
  +        private void expandRoot() {
  +          receiversTree.expandPath(
  +            new TreePath(model.getPathToRoot(model.RootNode)));
  +        }
  +      });
  +    receiversTree.expandPath(
  +      new TreePath(model.getPathToRoot(model.RootNode)));
  +
  +    receiversTree.addTreeWillExpandListener(
  +      new TreeWillExpandListener() {
  +        public void treeWillCollapse(TreeExpansionEvent event)
  +          throws ExpandVetoException {
  +          if (event.getPath().getLastPathComponent() == model.RootNode) {
  +            throw new ExpandVetoException(event);
  +          }
  +        }
  +
  +        public void treeWillExpand(TreeExpansionEvent event)
  +          throws ExpandVetoException {
  +        }
  +      });
   
       receiversTree.setToolTipText("Allows you to manage Log4j Receivers");
       newReceiverButtonAction =
  @@ -478,8 +519,6 @@
   
               if (receiver != null) {
                 PluginRegistry.stopPlugin(receiver);
  -
  -              updateReceiverTreeInDispatchThread();
               }
             }
           }).start();
  @@ -525,11 +564,14 @@
      *
      */
     public void updateReceiverTreeInDispatchThread() {
  -    if (SwingUtilities.isEventDispatchThread()) {
  -      updateReceiverTree.run();
  -    } else {
  -      SwingUtilities.invokeLater(updateReceiverTree);
  -    }
  +    LogLog.debug(
  +      "updateReceiverTreeInDispatchThread, should not be needed now");
  +	
  +    //    if (SwingUtilities.isEventDispatchThread()) {
  +    //      updateReceiverTree.run();
  +    //    } else {
  +    //      SwingUtilities.invokeLater(updateReceiverTree);
  +    //    }
     }
   
     /**
  @@ -708,37 +750,39 @@
         add(shutdownReceiverButtonAction);
         addSeparator();
         add(editReceiverButtonAction);
  -	addSeparator();
  -	
  -	  final Receiver r = getCurrentlySelectedReceiver();
  -	  add(createLevelRadioButton(r, Level.DEBUG));
  -	  add(createLevelRadioButton(r, Level.INFO));
  -	  add(createLevelRadioButton(r, Level.WARN));
  -	  add(createLevelRadioButton(r, Level.ERROR));
  -	  addSeparator();
  -	  add(createLevelRadioButton(r, Level.OFF));
  -	  add(createLevelRadioButton(r, Level.ALL));
  -	  
  +      addSeparator();
  +
  +      final Receiver r = getCurrentlySelectedReceiver();
  +      add(createLevelRadioButton(r, Level.DEBUG));
  +      add(createLevelRadioButton(r, Level.INFO));
  +      add(createLevelRadioButton(r, Level.WARN));
  +      add(createLevelRadioButton(r, Level.ERROR));
  +      addSeparator();
  +      add(createLevelRadioButton(r, Level.OFF));
  +      add(createLevelRadioButton(r, Level.ALL));
       }
   
  +    private JRadioButtonMenuItem createLevelRadioButton(
  +      final Receiver r, final Level l) {
  +      Map levelIconMap = LevelIconFactory.getInstance().getLevelToIconMap();
  +
  +      Action action =
  +        new AbstractAction(
  +          l.toString(), (Icon) levelIconMap.get(l.toString())) {
  +          public void actionPerformed(ActionEvent e) {
  +            if (r != null) {
  +              r.setThreshold(l);
  +              updateCurrentlySelectedNodeInDispatchThread();
  +            }
  +          }
  +        };
  +
  +      JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
  +      item.setSelected(r.getThreshold() == l);
  +
  +      return item;
  +    }
   
  -	private JRadioButtonMenuItem createLevelRadioButton(final Receiver r, final Level l) {
  -		Map levelIconMap = LevelIconFactory.getInstance().getLevelToIconMap();
  -
  -		Action action = new AbstractAction(l.toString(),(Icon)levelIconMap.get(l.toString())){
  -
  -				public void actionPerformed(ActionEvent e) {
  -					if(r!=null){
  -						r.setThreshold(l);
  -						updateCurrentlySelectedNodeInDispatchThread();
  -					}
  -				}
  -		};
  -		JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
  -		item.setSelected(r.getThreshold() == l);
  -		return item;
  -	}
  -	
       /**
        * Builds a relevant set of menus for when the Root node in the Receiver
        * tree has been selected
  
  
  
  1.4       +57 -3     jakarta-log4j/src/java/org/apache/log4j/chainsaw/ReceiversTreeModel.java
  
  Index: ReceiversTreeModel.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ReceiversTreeModel.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ReceiversTreeModel.java	5 Sep 2003 06:11:11 -0000	1.3
  +++ ReceiversTreeModel.java	18 Sep 2003 04:05:05 -0000	1.4
  @@ -50,13 +50,19 @@
   package org.apache.log4j.chainsaw;
   
   import java.util.Collection;
  +import java.util.HashMap;
   import java.util.Iterator;
  +import java.util.Map;
   
   import javax.swing.tree.DefaultMutableTreeNode;
   import javax.swing.tree.DefaultTreeModel;
  +import javax.swing.tree.TreeNode;
   
   import org.apache.log4j.LogManager;
   import org.apache.log4j.net.SocketReceiver;
  +import org.apache.log4j.plugins.Plugin;
  +import org.apache.log4j.plugins.PluginEvent;
  +import org.apache.log4j.plugins.PluginListener;
   import org.apache.log4j.plugins.PluginRegistry;
   import org.apache.log4j.plugins.Receiver;
   
  @@ -67,11 +73,13 @@
    *
    * @author Paul Smith <psmith@apache.org>
    */
  -public class ReceiversTreeModel extends DefaultTreeModel {
  +public class ReceiversTreeModel extends DefaultTreeModel
  +  implements PluginListener {
     private static final String ROOTNODE_LABEL = "Receivers";
     final DefaultMutableTreeNode NoReceiversNode =
       new DefaultMutableTreeNode("You have no Receivers defined");
     final DefaultMutableTreeNode RootNode;
  +  private Map pluginNodeMap = new HashMap();
   
     ReceiversTreeModel() {
       super(new DefaultMutableTreeNode(ROOTNODE_LABEL));
  @@ -125,12 +133,58 @@
      * and that listeners are notified.
      */
     void updateRootDisplay() {
  -    getRootNode().setUserObject(
  -      ROOTNODE_LABEL );
  +    getRootNode().setUserObject(ROOTNODE_LABEL);
       nodeChanged(getRootNode());
     }
   
     DefaultMutableTreeNode getRootNode() {
       return (DefaultMutableTreeNode) getRoot();
  +  }
  +
  +  /* (non-Javadoc)
  +   * @see org.apache.log4j.plugins.PluginListener#pluginStarted(org.apache.log4j.plugins.PluginEvent)
  +   */
  +  public void pluginStarted(PluginEvent e) {
  +    if (e.getPlugin() instanceof Receiver) {
  +      if (NoReceiversNode.getParent() == getRootNode()) {
  +        int index = getRootNode().getIndex(NoReceiversNode);
  +        getRootNode().remove(NoReceiversNode);
  +        nodesWereRemoved(
  +          getRootNode(), new int[] { index }, new Object[] { NoReceiversNode });
  +      }
  +
  +      Receiver receiver = (Receiver) e.getPlugin();
  +      DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(receiver);
  +      getRootNode().add(newNode);
  +      nodesWereInserted(
  +        getRootNode(), new int[] { getRootNode().getIndex(newNode) });
  +      pluginNodeMap.put(receiver, newNode);
  +    }
  +  }
  +
  +	TreeNode resolvePluginNode(Plugin p){
  +		return (TreeNode) pluginNodeMap.get(p);
  +	}
  +  /* (non-Javadoc)
  +   * @see org.apache.log4j.plugins.PluginListener#pluginStopped(org.apache.log4j.plugins.PluginEvent)
  +   */
  +  public void pluginStopped(PluginEvent e) {
  +    if (e.getPlugin() instanceof Receiver) {
  +      Receiver receiver = (Receiver) e.getPlugin();
  +      DefaultMutableTreeNode node =
  +        (DefaultMutableTreeNode) resolvePluginNode(receiver);
  +      int index = getRootNode().getIndex(node);
  +      getRootNode().remove(node);
  +      nodesWereRemoved(
  +        getRootNode(), new int[] { index }, new Object[] { node });
  +      pluginNodeMap.remove(receiver);
  +
  +      if (getRootNode().getChildCount() == 0) {
  +        getRootNode().add(NoReceiversNode);
  +
  +        index = getRootNode().getIndex(NoReceiversNode);
  +        nodesWereInserted(getRootNode(), new int[] { index });
  +      }
  +    }
     }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message