psmith 2003/08/28 22:53:36
Added: src/java/org/apache/log4j/chainsaw LoggerNameModel.java
LoggerNameListener.java EventDetailFormatter.java
LogPanelLoggerTreeModel.java
Log:
Added LoggerNameTreeModel and associated infrastructure.
Work in progress.
The rationale here is to provide a thin interface
that represents something containing a set of Logger names
which can notify listeners when a new, unique Logger name
turns up in the container.
The Model side of thing then can be notified of the new
Logger name arriving via the receiving events, and
adjust itself accordingly.
This TreeModel will then be used to allow the user to
specify which Logger's to display, and at what Level
to display them
Revision Changes Path
1.1
jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggerNameModel.java
Index: LoggerNameModel.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
/*
*/
package org.apache.log4j.chainsaw;
import java.util.Collection;
/**
* Implementations of this model contain all the known Logger
* names within it's model space.
*
* @author Paul Smith [EMAIL PROTECTED]
*/
public interface LoggerNameModel {
/**
* Returns an unmodifiable Collection of the uniquely
* known LoggerNames within this model.
*
* @return unmodifiable Collection of Logger name Strings
*/
public Collection getLoggerNames();
public void addLoggerNameListener(LoggerNameListener l);
public void removeLoggerNameListener(LoggerNameListener l);
}
1.1
jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggerNameListener.java
Index: LoggerNameListener.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package org.apache.log4j.chainsaw;
/**
* Implementations are notified when new Logger names
* are added to the related LoggerNameModel instance.
*
* @author Paul Smith <[EMAIL PROTECTED]>
*/
public interface LoggerNameListener {
/**
* Called by LoggerNameModel instances when a new unique
* Logger name string has been introduced into the model
* @param loggerName the new, unique loggerName
*/
public void loggerNameAdded(String loggerName);
}
1.1
jakarta-log4j/src/java/org/apache/log4j/chainsaw/EventDetailFormatter.java
Index: EventDetailFormatter.java
===================================================================
/*
*/
package org.apache.log4j.chainsaw;
import javax.swing.text.JTextComponent;
import org.apache.log4j.spi.LoggingEvent;
/**
* When a particular event row has been selected in a Chainsaw
* LogPanel, then the formatting of the detail
* of the event is delegated to the registered EventDetailFormatter
* instance.
*
*
* @author Paul Smith
*/
public interface EventDetailFormatter {
/**
* This formatting instance is given an opportunity to
* take the LoggingEvent, format it, and configure the JTextComponent
* with the formatted details.
*
* This method is guaranteed to be invoked by Swing's Event Dispatching
* thread.
*
* Implementations <b>MUST</b> be thread-safe.
*
* Implementations should ensure that the TextComponent is configured
* correctly regarding Content types etc, and should make no guarantees
* as to the current configuration of the JTextComponent. In particular
* implementations should not make any assumptions about the JTextComponent
* between calls, Chainsaw makes no guarantee that the same component will
* be passed in each time. This allows other JTextComponents to be
* used as requested by the user etc.
*
* @param component the component that should receive the formatted
* details of the event
*
* @param event the event object whose information should be formatted
* and displayed in the JTextComponent
*/
public void format(JTextComponent component, LoggingEvent event);
}
1.1
jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogPanelLoggerTreeModel.java
Index: LogPanelLoggerTreeModel.java
===================================================================
/*
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "log4j" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation. For more information on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
/*
*/
package org.apache.log4j.chainsaw;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Enumeration;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
/**
*
* A TreeModel that represents the Loggers for a given LogPanel
*
* @author Paul Smith <[EMAIL PROTECTED]>
*/
class LogPanelLoggerTreeModel extends DefaultTreeModel
implements LoggerNameListener {
private LogUI.LogPanel logPanel;
LogPanelLoggerTreeModel(LogUI.LogPanel logPanel) {
this();
this.logPanel = logPanel;
}
private LogPanelLoggerTreeModel() {
super(new DefaultMutableTreeNode("Root"));
}
/* (non-Javadoc)
* @see
org.apache.log4j.chainsaw.LoggerNameListener#loggerNameAdded(java.lang.String)
*/
public void loggerNameAdded(String loggerName) {
String[] packages = tokenize(loggerName);
/**
* The packages array is effectively the tree
* path that must exist within the tree, so
* we walk the tree ensuring each level is present
*/
DefaultMutableTreeNode current = (DefaultMutableTreeNode) getRoot();
/**
* This label is used to break out when descending the
* current tree hierachy, and it has matched a package name
* with an already existing TreeNode.
*/
outerFor: for (int i = 0; i < packages.length; i++) {
String packageName = packages[i];
Enumeration enum = current.children();
while (enum.hasMoreElements()) {
DefaultMutableTreeNode child =
(DefaultMutableTreeNode) enum.nextElement();
String childName = child.getUserObject().toString();
if (childName.equals(packageName)) {
/**
* This the current known branch to descend
*/
current = child;
/**
* we've found it, so break back to the outer
* for loop to continue processing further
* down the tree
*/
continue outerFor;
}
}
/*
* So we haven't found this index in the current children,
* better create the child
*/
DefaultMutableTreeNode newChild =
new DefaultMutableTreeNode(packageName);
current.add(newChild);
current = newChild;
}
nodeStructureChanged((TreeNode) getRoot());
}
/**
* Takes the loggerName and tokenizes it into it's
* package name lements returning the elements
* via the Stirng[]
* @param loggerName
* @return array of strings representing the package hierarchy
*/
private String[] tokenize(String loggerName) {
StringTokenizer tok = new StringTokenizer(loggerName, ".");
String[] tokens = new String[tok.countTokens()];
int index = 0;
while (tok.hasMoreTokens()) {
tokens[index++] = tok.nextToken();
}
return tokens;
}
public static void main(String[] args) {
try {
JFrame frame = new JFrame("TestBed");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
final LogPanelLoggerTreeModel model = new LogPanelLoggerTreeModel();
JTree tree = new JTree(model);
container.add(tree);
frame.pack();
frame.setSize(new Dimension(640,480));
frame.setVisible(true);
Thread.sleep(2000);
final String[] packageNames =
new String[] {
"org.apache.log4j.chainsaw",
"org.apache.log4j",
"au.com.lawlex",
"org",
"org.apache.log4j.chainsaw.prefs",
"com.mycompany",
"org.apache.joran",
};
final int delay = 500;
for (int i = 0; i < packageNames.length; i++) {
final int index = i;
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
model.loggerNameAdded(packageNames[index]);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]