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 package org.apache.log4j.gui; 18 19 import java.awt.Color; 20 import java.awt.Image; 21 import java.awt.Toolkit; 22 import java.awt.BorderLayout; 23 24 import javax.swing.*; 25 import javax.swing.text.StyledDocument; 26 import javax.swing.text.SimpleAttributeSet; 27 import javax.swing.text.MutableAttributeSet; 28 import javax.swing.text.StyleConstants; 29 30 import java.util.Hashtable; 31 import java.util.StringTokenizer; 32 import java.util.Enumeration; 33 import java.util.ArrayList; 34 35 import org.apache.log4j.*; 36 37 public class LogTextPanel extends JPanel { 38 39 private JScrollBar scrollBar; 40 private JTextPane textPane; 41 private JCheckBox cbxTail; 42 private StyledDocument doc; 43 44 private Hashtable fontAttributes; 45 46 private int eventBufferMaxSize = 10000; 47 private ArrayList eventBuffer = new ArrayList(eventBufferMaxSize); 48 private int eventViewIndex = 0; 49 LogTextPanel()50 public LogTextPanel() { 51 constructComponents(); 52 createDefaultFontAttributes(); 53 } 54 constructComponents()55 private void constructComponents() { 56 // setup the panel's additional components... 57 this.setLayout(new BorderLayout()); 58 59 cbxTail = new JCheckBox(); 60 cbxTail.setSelected(true); 61 cbxTail.setText("Tail log events"); 62 63 JPanel bottomPanel = new JPanel(); 64 bottomPanel.add(cbxTail, null); 65 66 textPane = new JTextPane(); 67 textPane.setEditable(false); 68 textPane.setText(""); 69 doc = textPane.getStyledDocument(); 70 71 scrollBar = new JScrollBar(JScrollBar.VERTICAL); 72 73 this.add(bottomPanel, BorderLayout.SOUTH); 74 this.add(scrollBar, BorderLayout.EAST); 75 this.add(textPane, BorderLayout.CENTER); 76 } 77 78 public setTextBackground(Color color)79 void setTextBackground(Color color) { 80 textPane.setBackground(color); 81 } 82 83 public setTextBackground(String v)84 void setTextBackground(String v) { 85 textPane.setBackground(parseColor(v)); 86 } 87 createDefaultFontAttributes()88 private void createDefaultFontAttributes() { 89 Priority[] prio = Priority.getAllPossiblePriorities(); 90 91 fontAttributes = new Hashtable(); 92 for (int i=0; i<prio.length;i++) { 93 MutableAttributeSet att = new SimpleAttributeSet(); 94 fontAttributes.put(prio[i], att); 95 //StyleConstants.setFontSize(att,11); 96 } 97 98 setTextColor(Priority.FATAL, Color.red); 99 setTextColor(Priority.ERROR, Color.magenta); 100 setTextColor(Priority.WARN, Color.orange); 101 setTextColor(Priority.INFO, Color.blue); 102 setTextColor(Priority.DEBUG, Color.black); 103 } 104 105 private parseColor(String v)106 Color parseColor (String v) { 107 StringTokenizer st = new StringTokenizer(v,","); 108 int val[] = {255,255,255,255}; 109 int i=0; 110 while (st.hasMoreTokens()) { 111 val[i]=Integer.parseInt(st.nextToken()); 112 i++; 113 } 114 return new Color(val[0],val[1],val[2],val[3]); 115 } 116 setTextColor(Priority p, String v)117 void setTextColor(Priority p, String v) { 118 StyleConstants.setForeground( 119 (MutableAttributeSet)fontAttributes.get(p),parseColor(v)); 120 } 121 setTextColor(Priority p, Color c)122 void setTextColor(Priority p, Color c) { 123 StyleConstants.setForeground( 124 (MutableAttributeSet)fontAttributes.get(p),c); 125 } 126 setTextFontSize(int size)127 void setTextFontSize(int size) { 128 Enumeration e = fontAttributes.elements(); 129 while (e.hasMoreElements()) { 130 StyleConstants.setFontSize((MutableAttributeSet)e.nextElement(),size); 131 } 132 return; 133 } 134 setTextFontName(String name)135 void setTextFontName(String name) { 136 Enumeration e = fontAttributes.elements(); 137 while (e.hasMoreElements()) { 138 StyleConstants.setFontFamily((MutableAttributeSet)e.nextElement(),name); 139 } 140 return; 141 } 142 setEventBufferSize(int bufferSize)143 void setEventBufferSize(int bufferSize) { 144 eventBufferMaxSize = bufferSize; 145 } 146 newEvents(EventBufferElement[] evts)147 void newEvents(EventBufferElement[] evts) { 148 149 if((eventBuffer.size() + evts.length) >= eventBufferMaxSize) { 150 for(int i=0; i < evts.length; i++) { 151 eventBuffer.remove(0); 152 } 153 eventViewIndex -= evts.length; 154 if(eventViewIndex < 0) 155 eventViewIndex = 0; 156 } 157 for(int i=0; i < evts.length; i++) 158 eventBuffer.add(evts[i]); 159 160 if((eventBuffer.size() > maxR) && cbxTail.isSelected()) { 161 eventViewIndex = (eventBuffer.size() - maxR); 162 } 163 164 // only redraw if new line is visible... 165 if((maxR < 0) || (eventBuffer.size() >= eventViewIndex && eventBuffer.size() <= (eventViewIndex + maxR))) 166 drawText(); 167 } 168 169 int maxR = -1; 170 drawText()171 void drawText() { 172 if(maxR < 0) 173 maxR = textPane.getHeight() / textPane.getFontMetrics(textPane.getFont()).getHeight(); 174 try { 175 doc.remove(0, doc.getLength()); 176 } catch(Exception e) { e.printStackTrace(); } 177 178 for(int i=eventViewIndex; (i < eventBuffer.size()) && (i < (eventViewIndex + maxR)); i++) { 179 EventBufferElement evt = (EventBufferElement)eventBuffer.get(i); 180 181 try { 182 doc.insertString(doc.getLength(), evt.text, (MutableAttributeSet)fontAttributes.get(evt.prio)); 183 } catch(Exception e) { e.printStackTrace(); } 184 } 185 } 186 187 188 }