alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package org.apache.log4j.gui;


/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.BorderLayout;

import javax.swing.*;
import javax.swing.text.StyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;

import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;
import java.util.ArrayList;

import org.apache.log4j.*;

public class LogTextPanel extends JPanel {

  private JScrollBar scrollBar;
  private JTextPane textPane;
  private JCheckBox cbxTail;
  private StyledDocument doc;

  private Hashtable fontAttributes;

  private int eventBufferMaxSize = 10000;
  private ArrayList eventBuffer = new ArrayList(eventBufferMaxSize);
  private int eventViewIndex = 0;

  public LogTextPanel() {
    constructComponents();
    createDefaultFontAttributes();
  }

  private void constructComponents() {
      // setup the panel's additional components...
    this.setLayout(new BorderLayout());

    cbxTail = new JCheckBox();
    cbxTail.setSelected(true);
    cbxTail.setText("Tail log events");

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(cbxTail, null);

    textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setText("");
    doc = textPane.getStyledDocument();

    scrollBar = new JScrollBar(JScrollBar.VERTICAL);

    this.add(bottomPanel, BorderLayout.SOUTH);
    this.add(scrollBar, BorderLayout.EAST);
    this.add(textPane, BorderLayout.CENTER);
  }

  public
  void setTextBackground(Color color) {
    textPane.setBackground(color);
  }

  public
  void setTextBackground(String v) {
    textPane.setBackground(parseColor(v));
  }

  private void createDefaultFontAttributes() {
    Priority[] prio = Priority.getAllPossiblePriorities();

    fontAttributes = new Hashtable();
    for (int i=0; i= eventBufferMaxSize) {
      for(int i=0; i < evts.length; i++) {
        eventBuffer.remove(0);
      }
      eventViewIndex -= evts.length;
      if(eventViewIndex < 0)
        eventViewIndex = 0;
    }
    for(int i=0; i < evts.length; i++)
      eventBuffer.add(evts[i]);

    if((eventBuffer.size() > maxR) && cbxTail.isSelected()) {
      eventViewIndex = (eventBuffer.size() - maxR);
    }

    // only redraw if new line is visible...
    if((maxR < 0) || (eventBuffer.size() >= eventViewIndex && eventBuffer.size() <= (eventViewIndex + maxR)))
      drawText();
  }

  int maxR = -1;

  void drawText() {
    if(maxR < 0)
      maxR =  textPane.getHeight() / textPane.getFontMetrics(textPane.getFont()).getHeight();
    try {
      doc.remove(0, doc.getLength());
    } catch(Exception e) { e.printStackTrace(); }

    for(int i=eventViewIndex; (i < eventBuffer.size()) && (i < (eventViewIndex + maxR)); i++) {
      EventBufferElement evt = (EventBufferElement)eventBuffer.get(i);

      try {
        doc.insertString(doc.getLength(), evt.text, (MutableAttributeSet)fontAttributes.get(evt.prio));
      } catch(Exception e) { e.printStackTrace(); }
    }
  }


}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.