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