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 org.apache.log4j.chainsaw.Generator;
21  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
22  import org.apache.log4j.chainsaw.icons.LevelIconFactory;
23  import org.apache.log4j.plugins.Plugin;
24  import org.apache.log4j.spi.Thresholdable;
25  
26  import javax.swing.*;
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import javax.swing.tree.DefaultTreeCellRenderer;
29  import java.awt.*;
30  
31  
32  /**
33   * A TreeCellRenderer that can format the information of Receivers
34   * and their children
35   *
36   * @author Paul Smith <psmith@apache.org>
37   */
38  public class ReceiverTreeCellRenderer extends DefaultTreeCellRenderer {
39      private Icon rootIcon = new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT);
40      private JPanel panel = new JPanel();
41      private JLabel levelLabel = new JLabel();
42  
43      public ReceiverTreeCellRenderer() {
44          super();
45          BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
46          panel.setLayout(layout);
47          panel.setOpaque(false);
48          panel.add(levelLabel);
49          //set preferredsize to something wide
50          setPreferredSize(new Dimension(200, 19));
51          panel.add(this);
52      }
53  
54      public Component getTreeCellRendererComponent(
55          JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
56          int row, boolean focus) {
57          super.getTreeCellRendererComponent(
58              tree, value, sel, expanded, leaf, row, focus);
59  
60          DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
61          Object o = node.getUserObject();
62          setText(o.toString());
63  
64          String tooltip = "";
65  
66          setIcon(null);
67          if (
68              o == ((ReceiversTreeModel) tree.getModel()).getRootNode().getUserObject()) {
69              setText(o.toString());
70          } else if (o instanceof String) {
71              setText(o.toString());
72              setIcon(null);
73          } else if (o instanceof Plugin) {
74              setText(((Plugin) o).getName());
75          } else if (o instanceof Generator) {
76              Generator generator = (Generator) o;
77              setText(generator.getName());
78              setIcon(ChainsawIcons.ICON_HELP);
79          } else {
80              setText("(Unknown Type) :: " + o);
81          }
82  
83          if (
84              o == ((ReceiversTreeModel) tree.getModel()).getRootNode().getUserObject()) {
85              setIcon(rootIcon);
86          }
87  
88          levelLabel.setText(null);
89          levelLabel.setIcon(null);
90  
91          if (o instanceof Thresholdable) {
92              Thresholdable t = (Thresholdable) o;
93  
94              if (t.getThreshold() != null) {
95                  levelLabel.setIcon(
96                      LevelIconFactory.getInstance().getLevelToIconMap().get(
97                          t.getThreshold().toString()));
98  
99                  if (levelLabel.getIcon() == null) {
100                     levelLabel.setText(t.getThreshold().toString());
101                 }
102             }
103         }
104 
105         setToolTipText(tooltip);
106 
107         return panel;
108     }
109 }