View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.chainsaw.receivers;
    19
    20  import java.awt.*;
    21
    22  import javax.swing.*;
    23  import javax.swing.tree.DefaultMutableTreeNode;
    24  import javax.swing.tree.DefaultTreeCellRenderer;
    25
    26  import org.apache.log4j.chainsaw.Generator;
    27  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
    28  import org.apache.log4j.chainsaw.icons.LevelIconFactory;
    29  import org.apache.log4j.plugins.Plugin;
    30  import org.apache.log4j.spi.Thresholdable;
    31
    32
    33  /**
    34   * A TreeCellRenderer that can format the information of Receivers
    35   * and their children
    36   *
    37   * @author Paul Smith &lt;psmith@apache.org&gt;
    38   */
    39  public class ReceiverTreeCellRenderer extends DefaultTreeCellRenderer {
    40    private Icon rootIcon = new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT);
    41    private JPanel panel = new JPanel();
    42    private JLabel levelLabel = new JLabel();
    43
    44    public ReceiverTreeCellRenderer() {
    45      super();
    46      BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
    47      panel.setLayout(layout);
    48      panel.setOpaque(false);
    49      panel.add(levelLabel);
    50      //set preferredsize to something wide
    51      setPreferredSize(new Dimension(200, 19));
    52      panel.add(this);
    53    }
    54
    55    public Component getTreeCellRendererComponent(
    56      JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
    57      int row, boolean focus) {
    58      super.getTreeCellRendererComponent(
    59        tree, value, sel, expanded, leaf, row, focus);
    60
    61      DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    62      Object o = node.getUserObject();
    63      setText(o.toString());
    64
    65      String tooltip = "";
    66
    67      setIcon(null);
    68      if (
    69        o == ((ReceiversTreeModel) tree.getModel()).getRootNode().getUserObject()) {
    70        setText(o.toString());
    71      } else if (o instanceof String) {
    72        setText(o.toString());
    73        setIcon(null);
    74       }else if(o instanceof Plugin){
    75         setText(((Plugin)o).getName());
    76       }else if(o instanceof Generator){
    77      	Generator generator = (Generator) o;
    78      	setText(generator.getName());
    79      	setIcon(ChainsawIcons.ICON_HELP);
    80      }
    81      else {
    82        setText("(Unknown Type) :: " + o);
    83      }
    84
    85      if (
    86        o == ((ReceiversTreeModel) tree.getModel()).getRootNode().getUserObject()) {
    87        setIcon(rootIcon);
    88      }
    89
    90      levelLabel.setText(null);
    91      levelLabel.setIcon(null);
    92
    93      if (o instanceof Thresholdable) {
    94        Thresholdable t = (Thresholdable) o;
    95
    96        if (t.getThreshold() != null) {
    97          levelLabel.setIcon(
    98            (Icon) LevelIconFactory.getInstance().getLevelToIconMap().get(
    99              t.getThreshold().toString()));
    100
    101         if (levelLabel.getIcon() == null) {
    102           levelLabel.setText(t.getThreshold().toString());
    103         }
    104       }
    105     }
    106
    107     setToolTipText(tooltip);
    108
    109     return panel;
    110   }
    111 }