psmith      2003/06/17 16:26:48

  Added:       src/java/org/apache/log4j/chainsaw
                        ReceiverTreeCellRenderer.java
                        ReceiversTreeModel.java
  Log:
  Moved out some inner classes to top level, as they were getting a bit
  big and are more relevant to other areas than just inside the Toolbar
  and menu class.
  
  Revision  Changes    Path
  1.1                  
jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/ReceiverTreeCellRenderer.java
  
  Index: ReceiverTreeCellRenderer.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.chainsaw;
  
  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
  import org.apache.log4j.net.AddressBased;
  import org.apache.log4j.net.NetworkBased;
  import org.apache.log4j.net.PortBased;
  import org.apache.log4j.plugins.Receiver;
  
  import java.awt.Component;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  
  import javax.swing.Icon;
  import javax.swing.ImageIcon;
  import javax.swing.JTree;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeCellRenderer;
  
  
  /**
   * A TreeCellRenderer that can format the information of Receivers
   * and their children
   *
   * @author Paul Smith <[EMAIL PROTECTED]>
   */
  public class ReceiverTreeCellRenderer extends DefaultTreeCellRenderer {
    private Icon activeReceiverIcon =
      new ImageIcon(ChainsawIcons.ICON_ACTIVE_RECEIVER);
    private Icon inactiveReceiverIcon =
      new ImageIcon(ChainsawIcons.ICON_INACTIVE_RECEIVER);
    private Icon rootIcon = new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT);
    private FormatterChain chain;
  
    public ReceiverTreeCellRenderer() {
      super();
      chain = new FormatterChain();
      chain.add(
        new Formatter() {
          public String formatDetail(Object o) {
            if (!(o instanceof NetworkBased)) {
              return null;
            }
  
            NetworkBased networkBased = (NetworkBased) o;
  
            StringBuffer buf = new StringBuffer(networkBased.getName());
  
            if (o instanceof AddressBased) {
              buf.append("::").append(((AddressBased) o).getAddress());
            }
  
            if (o instanceof PortBased) {
              PortBased portBased = (PortBased) o;
  
              buf.append("::").append(portBased.getPort());
            }
  
            buf.append(" ").append("(")
               .append(networkBased.isActive() ? "running" : "inactive").append(
              ")");
  
            return buf.toString();
          }
        });
  
      chain.add(
        new Formatter() {
          public String formatDetail(Object o) {
            return "(Unknown Type) :: " + o;
          }
        });
    }
  
    public Component getTreeCellRendererComponent(
      JTree tree, Object value, boolean selected, boolean expanded, boolean leaf,
      int row, boolean hasFocus) {
      super.getTreeCellRendererComponent(
        tree, value, selected, expanded, leaf, row, hasFocus);
  
      DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
      Object obj = node.getUserObject();
      setText(obj.toString());
  
      if (tree.getModel().getRoot().equals(value)) {
        setIcon(rootIcon);
      } else {
        setText(chain.formatDetail(obj));
      }
  
      if (obj instanceof NetworkBased) {
        NetworkBased networkBased = (NetworkBased) obj;
  
        if (networkBased.isActive()) {
          setIcon(activeReceiverIcon);
        } else {
          setIcon(inactiveReceiverIcon);
        }
      }
  
      return this;
    }
  
    private static interface Formatter {
      /**
       * Returns a formatted string for the receiver or null if it cannot.
       * @param receiver
       * @return
       */
      public String formatDetail(Object networkBased);
    }
  
    private static class FormatterChain implements Formatter {
      private Collection collection = new ArrayList();
  
      /**
       * @param formatter
       */
      public void add(Formatter formatter) {
        collection.add(formatter);
      }
  
      /* (non-Javadoc)
       * @see 
org.apache.log4j.chainsaw.ChainsawToolBarAndMenus.ReceiverTreeCellRenderer.ReceiverFormatter#formatDetail(org.apache.log4j.plugins.Receiver)
       */
      public String formatDetail(Object receiver) {
        for (Iterator iter = collection.iterator(); iter.hasNext();) {
          ReceiverTreeCellRenderer.Formatter item =
            (ReceiverTreeCellRenderer.Formatter) iter.next();
          String detail = item.formatDetail(receiver);
  
          if (detail != null) {
            return detail;
          }
        }
  
        return null;
      }
    }
  }
  
  
  
  1.1                  
jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/ReceiversTreeModel.java
  
  Index: ReceiversTreeModel.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.chainsaw;
  
  import org.apache.log4j.LogManager;
  import org.apache.log4j.net.SocketReceiver;
  import org.apache.log4j.plugins.PluginRegistry;
  import org.apache.log4j.plugins.Receiver;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeModel;
  
  
  /**
   * A TreeModel that encapsulates the details of all the Receivers and their
   * related information in the Log4j framework
   *
   * @author Paul Smith <[EMAIL PROTECTED]>
   */
  public class ReceiversTreeModel extends DefaultTreeModel {
    private ReceiversTreeModel() {
      super(new DefaultMutableTreeNode("Receivers"));
    }
  
    /**
     * Creates a new ReceiversTreeModel by querying the Log4j Plugin Repository
     * and building up the required information.
     *
     * @return ReceiversTreeModel
     */
    public static final ReceiversTreeModel create() {
      ReceiversTreeModel model = new ReceiversTreeModel();
  
      Collection receivers =
        PluginRegistry.getPlugins(
          LogManager.getLoggerRepository(), Receiver.class);
  
      for (Iterator iter = receivers.iterator(); iter.hasNext();) {
        Receiver item = (Receiver) iter.next();
        DefaultMutableTreeNode receiverNode = new DefaultMutableTreeNode(item);
  
        if (item instanceof SocketReceiver) {
          for (
            Iterator iterator =
              ((SocketReceiver) item).getConnectedSocketDetails().iterator();
              iterator.hasNext();) {
            Object details = (Object) iterator.next();
            receiverNode.add(new DefaultMutableTreeNode(details));
          }
        }
  
        model.getRootNode().add(receiverNode);
      }
  
      return model;
    }
  
    DefaultMutableTreeNode getRootNode() {
      return (DefaultMutableTreeNode) getRoot();
    }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to