001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5.viewer.categoryexplorer; 018 019import javax.swing.*; 020import javax.swing.tree.DefaultTreeCellRenderer; 021import java.awt.*; 022import java.net.URL; 023 024/** 025 * CategoryNodeRenderer 026 * 027 * @author Michael J. Sikorsky 028 * @author Robert Shaw 029 */ 030 031// Contributed by ThoughtWorks Inc. 032 033public class CategoryNodeRenderer extends DefaultTreeCellRenderer { 034 private static final long serialVersionUID = -6046702673278595048L; 035 036 //-------------------------------------------------------------------------- 037 // Constants: 038 //-------------------------------------------------------------------------- 039 040 public static final Color FATAL_CHILDREN = new Color(189, 113, 0); 041 042 //-------------------------------------------------------------------------- 043 // Protected Variables: 044 //-------------------------------------------------------------------------- 045 protected JCheckBox _checkBox = new JCheckBox(); 046 protected JPanel _panel = new JPanel(); 047 protected static ImageIcon _sat = null; 048// protected JLabel _label = new JLabel(); 049 050 //-------------------------------------------------------------------------- 051 // Private Variables: 052 //-------------------------------------------------------------------------- 053 054 //-------------------------------------------------------------------------- 055 // Constructors: 056 //-------------------------------------------------------------------------- 057 public CategoryNodeRenderer() { 058 _panel.setBackground(UIManager.getColor("Tree.textBackground")); 059 060 if (_sat == null) { 061 // Load the satellite image. 062 String resource = 063 "/org/apache/log4j/lf5/viewer/images/channelexplorer_satellite.gif"; 064 URL satURL = getClass().getResource(resource); 065 066 _sat = new ImageIcon(satURL); 067 } 068 069 setOpaque(false); 070 _checkBox.setOpaque(false); 071 _panel.setOpaque(false); 072 073 // The flowlayout set to LEFT is very important so that the editor 074 // doesn't jump around. 075 _panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 076 _panel.add(_checkBox); 077 _panel.add(this); 078 079 setOpenIcon(_sat); 080 setClosedIcon(_sat); 081 setLeafIcon(_sat); 082 } 083 084 //-------------------------------------------------------------------------- 085 // Public Methods: 086 //-------------------------------------------------------------------------- 087 public Component getTreeCellRendererComponent( 088 JTree tree, Object value, 089 boolean selected, boolean expanded, 090 boolean leaf, int row, 091 boolean hasFocus) { 092 093 CategoryNode node = (CategoryNode) value; 094 //FileNode node = (FileNode)value; 095 //String s = tree.convertValueToText(value, selected, 096 // expanded, leaf, row, hasFocus); 097 098 super.getTreeCellRendererComponent( 099 tree, value, selected, expanded, 100 leaf, row, hasFocus); 101 102 if (row == 0) { 103 // Root row -- no check box 104 _checkBox.setVisible(false); 105 } else { 106 _checkBox.setVisible(true); 107 _checkBox.setSelected(node.isSelected()); 108 } 109 String toolTip = buildToolTip(node); 110 _panel.setToolTipText(toolTip); 111 if (node.hasFatalChildren()) { 112 this.setForeground(FATAL_CHILDREN); 113 } 114 if (node.hasFatalRecords()) { 115 this.setForeground(Color.red); 116 } 117 118 return _panel; 119 } 120 121 public Dimension getCheckBoxOffset() { 122 return new Dimension(0, 0); 123 } 124 125 //-------------------------------------------------------------------------- 126 // Protected Methods: 127 //-------------------------------------------------------------------------- 128 129 protected String buildToolTip(CategoryNode node) { 130 StringBuffer result = new StringBuffer(); 131 result.append(node.getTitle()).append(" contains a total of "); 132 result.append(node.getTotalNumberOfRecords()); 133 result.append(" LogRecords."); 134 result.append(" Right-click for more info."); 135 return result.toString(); 136 } 137 //-------------------------------------------------------------------------- 138 // Private Methods: 139 //-------------------------------------------------------------------------- 140 141 //-------------------------------------------------------------------------- 142 // Nested Top-Level Classes or Interfaces: 143 //-------------------------------------------------------------------------- 144 145} 146 147 148 149 150 151