psmith      2003/09/04 20:39:43

  Modified:    src/java/org/apache/log4j/chainsaw LogUI.java
                        ChainsawCyclicBufferTableModel.java
                        EventContainer.java
  Log:
  Added NewKeyListener support to the model and the GUI.
  
  Now when a new MDC key arrives via a LoggingEvent into
  the table model, a listener ensures that a new Column
  is automatically added to the visible Table to ensure it is displayed
  correctly.
  
  Revision  Changes    Path
  1.21      +26 -9     jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java
  
  Index: LogUI.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- LogUI.java        4 Sep 2003 23:45:38 -0000       1.20
  +++ LogUI.java        5 Sep 2003 03:39:43 -0000       1.21
  @@ -772,10 +772,9 @@
     }
   
     void removeWelcomePanel() {
  -    if(tabbedPane.containsWelcomePanel()) {
  -    tabbedPane.remove(
  -      tabbedPane.getComponentAt(tabbedPane.indexOfTab("Welcome")));
  -        
  +    if (tabbedPane.containsWelcomePanel()) {
  +      tabbedPane.remove(
  +        tabbedPane.getComponentAt(tabbedPane.indexOfTab("Welcome")));
       }
     }
   
  @@ -1148,6 +1147,8 @@
             ((ChainsawEventBatchEntry) eventBatchEntrys.get(0)).getEventType();
           final LogPanel thisPanel = new LogPanel(ident, eventType);
   
  +        sm.configure(thisPanel);
  +
           /**
            * Let the new LogPanel receive this batch
            */
  @@ -1166,8 +1167,6 @@
               }
             });
   
  -        sm.configure(thisPanel);
  -
           String msg = "added tab " + ident;
           LogLog.debug(msg);
           statusBar.setMessage(msg);
  @@ -1261,6 +1260,16 @@
   
         table.setAutoCreateColumnsFromModel(false);
   
  +      /**
  +               * We listen for new Key's coming in so we can get them automatically 
added as columns
  +              */
  +      tableModel.addNewKeyListener(
  +        new NewKeyListener() {
  +          public void newKeyAdded(NewKeyEvent e) {
  +            table.addColumn(new TableColumn(e.getNewModelIndex()));
  +          }
  +        });
  +
         table.setRowHeight(20);
         table.setShowGrid(false);
   
  @@ -2231,9 +2240,16 @@
   
           while (e.hasMoreElements()) {
             TableColumn c = (TableColumn) e.nextElement();
  -          o.writeObject(
  -            new TableColumnData(
  -              (String) c.getHeaderValue(), c.getModelIndex(), c.getWidth()));
  +
  +          if (c.getModelIndex() < ChainsawColumns.getColumnsNames().size()) {
  +            o.writeObject(
  +              new TableColumnData(
  +                (String) c.getHeaderValue(), c.getModelIndex(), c.getWidth()));
  +          } else {
  +            LogLog.debug(
  +              "Not saving col ' " + c.getHeaderValue()
  +              + "' not part of standard columns");
  +          }
           }
   
           o.flush();
  @@ -2742,6 +2758,7 @@
       }
   
       public void columnAdded(TableColumnModelEvent e) {
  +      LogLog.debug("Detected columnAdded" + e);
       }
   
       public void columnRemoved(TableColumnModelEvent e) {
  
  
  
  1.4       +76 -16    
jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawCyclicBufferTableModel.java
  
  Index: ChainsawCyclicBufferTableModel.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawCyclicBufferTableModel.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ChainsawCyclicBufferTableModel.java       3 Sep 2003 05:44:51 -0000       1.3
  +++ ChainsawCyclicBufferTableModel.java       5 Sep 2003 03:39:43 -0000       1.4
  @@ -49,21 +49,24 @@
   
   package org.apache.log4j.chainsaw;
   
  -import org.apache.log4j.helpers.LogLog;
  -import org.apache.log4j.spi.LocationInfo;
  -import org.apache.log4j.spi.LoggingEvent;
  -
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Collections;
   import java.util.Date;
  +import java.util.HashSet;
   import java.util.Iterator;
   import java.util.List;
  +import java.util.Set;
   import java.util.Vector;
   
   import javax.swing.SwingUtilities;
  +import javax.swing.event.EventListenerList;
   import javax.swing.table.AbstractTableModel;
   
  +import org.apache.log4j.helpers.LogLog;
  +import org.apache.log4j.spi.LocationInfo;
  +import org.apache.log4j.spi.LoggingEvent;
  +
   
   /**
    * A CyclicBuffer implementation of the EventContainer.
  @@ -84,9 +87,12 @@
     private final int INITIAL_CAPACITY = 1024;
     final List unfilteredList;
     final List filteredList;
  -  private Vector countListeners = new Vector();
  +
  +  //  private Vector countListeners = new Vector();
     private boolean currentSortAscending;
     private int currentSortColumn;
  +  private EventListenerList eventListenerList = new EventListenerList();
  +  private List columnNames = new ArrayList(ChainsawColumns.getColumnsNames());
   
     /**
      * @deprecated should use new filtering stuff
  @@ -102,6 +108,7 @@
     //because we may be using a cyclic buffer, if an ID is not provided in the 
property, 
     //use and increment this row counter as the ID for each received row
     int uniqueRow;
  +  private Set uniqueMDCKeys = new HashSet();
   
     public ChainsawCyclicBufferTableModel(boolean isCyclic, int bufferSize) {
       this.cyclic = isCyclic;
  @@ -166,7 +173,7 @@
     }
   
     public void addEventCountListener(EventCountListener listener) {
  -    countListeners.add(listener);
  +    eventListenerList.add(EventCountListener.class, listener);
     }
   
     public void filterChanged() {
  @@ -178,8 +185,12 @@
     }
   
     public void notifyCountListeners() {
  -    for (int i = 0; i < countListeners.size(); i++) {
  -      ((EventCountListener) countListeners.get(i)).eventCountChanged(
  +    EventCountListener[] listeners =
  +      (EventCountListener[]) eventListenerList.getListeners(
  +        EventCountListener.class);
  +
  +    for (int i = 0; i < listeners.length; i++) {
  +      listeners[i].eventCountChanged(
           filteredList.size(), unfilteredList.size());
       }
     }
  @@ -409,11 +420,11 @@
     }
   
     public int getColumnCount() {
  -    return ChainsawColumns.getColumnsNames().size();
  +    return columnNames.size();
     }
   
     public String getColumnName(int column) {
  -    return ChainsawColumns.getColumnsNames().get(column).toString();
  +    return columnNames.get(column).toString();
     }
   
     public LoggingEvent getRow(int row) {
  @@ -443,6 +454,7 @@
         if (id != null) {
           return id;
         }
  +
         return new Integer(rowIndex);
   
       case ChainsawColumns.INDEX_LEVEL_COL_NAME:
  @@ -485,8 +497,11 @@
         return (info != null) ? info.getMethodName() : "";
   
       default:
  -      return "";
  +     if(columnIndex<=columnNames.size()){
  +             return event.getMDC(columnNames.get(columnIndex).toString());
  +     }
       }
  +     return "";
     }
   
     public boolean isAddRow(LoggingEvent e, boolean valueIsAdjusting) {
  @@ -524,10 +539,28 @@
   
       int newRow = filteredList.size() - 1;
   
  -    if (!isCyclic()) {
  +     /**
  +      * Is this a new MDC key we haven't seen before?
  +      */
  +    boolean newColumn = uniqueMDCKeys.addAll(e.getMDCKeySet());
  +
  +     /**
  +      * If so, we should add them as columns and notify listeners.
  +      */
  +    for (Iterator iter = e.getMDCKeySet().iterator(); iter.hasNext();) {
  +      Object key = (Object) iter.next();
  +
  +      if (!columnNames.contains(key)) {
  +        columnNames.add(key);
  +        LogLog.debug("Adding col '" + key + "', columNames=" + columnNames);
  +        fireNewKeyColumnAdded(new NewKeyEvent(this, columnNames.indexOf(key), key, 
e.getMDC(key.toString())));
  +      }
  +    }
  +
  +    if (!isCyclic() && !newColumn) {
         fireTableRowsInserted(newRow, newRow);
       } else {
  -      if (
  +      if (newColumn || 
           unfilteredList.size() == ((CyclicBufferList) unfilteredList)
             .getMaxSize()) {
           fireTableDataChanged();
  @@ -540,9 +573,22 @@
     }
   
     /**
  -   * Returns true if this model is Cyclic (bounded) or not
  -   * @return true/false
  -   */
  +  * @param key
  +  */
  +  private void fireNewKeyColumnAdded(NewKeyEvent e) {
  +    NewKeyListener[] listeners =
  +      (NewKeyListener[]) eventListenerList.getListeners(NewKeyListener.class);
  +
  +    for (int i = 0; i < listeners.length; i++) {
  +      NewKeyListener listener = listeners[i];
  +      listener.newKeyAdded(e);
  +    }
  +  }
  +
  +  /**
  +     * Returns true if this model is Cyclic (bounded) or not
  +     * @return true/false
  +     */
     public boolean isCyclic() {
       return cyclic;
     }
  @@ -557,6 +603,20 @@
       }
   
       return ((CyclicBufferList) unfilteredList).getMaxSize();
  +  }
  +
  +  /* (non-Javadoc)
  +   * @see 
org.apache.log4j.chainsaw.EventContainer#addNewKeyListener(org.apache.log4j.chainsaw.NewKeyListener)
  +   */
  +  public void addNewKeyListener(NewKeyListener l) {
  +    eventListenerList.add(NewKeyListener.class, l);
  +  }
  +
  +  /* (non-Javadoc)
  +   * @see 
org.apache.log4j.chainsaw.EventContainer#removeNewKeyListener(org.apache.log4j.chainsaw.NewKeyListener)
  +   */
  +  public void removeNewKeyListener(NewKeyListener l) {
  +    eventListenerList.remove(NewKeyListener.class, l);
     }
   
     class SortExecutor implements Runnable {
  
  
  
  1.5       +20 -6     
jakarta-log4j/src/java/org/apache/log4j/chainsaw/EventContainer.java
  
  Index: EventContainer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/EventContainer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- EventContainer.java       3 Sep 2003 05:46:25 -0000       1.4
  +++ EventContainer.java       5 Sep 2003 03:39:43 -0000       1.5
  @@ -49,9 +49,10 @@
   
   package org.apache.log4j.chainsaw;
   
  +import org.apache.log4j.spi.LoggingEvent;
  +
   import java.util.List;
   
  -import org.apache.log4j.spi.LoggingEvent;
   
   /**
    * To allow pluggable TableModel implementations for Chainsaw, this interface has 
been factored out.
  @@ -60,15 +61,29 @@
    *
    * @author Paul Smith <[EMAIL PROTECTED]>
    * @author Scott Deboy <[EMAIL PROTECTED]>
  - * 
  + *
    */
  -public interface EventContainer extends SortTableModel, FilterChangedListener, 
LoggerNameModel {
  +public interface EventContainer extends SortTableModel, FilterChangedListener,
  +  LoggerNameModel {
     /**
      * Adds an EventCountListener, to be notified when the # of events changes
      * @param listener
      */
     void addEventCountListener(EventCountListener listener);
   
  +     /**
  +      * Adds a NewKeyListener to be notified when unique Key (MDC/Property keys)
  +      * arrive into this EventContainer
  +      * @param l
  +      */
  +  void addNewKeyListener(NewKeyListener l);
  +
  +     /**
  +      * Removes a listener from being notified of NewKey events.
  +      * @param l
  +      */
  +  void removeNewKeyListener(NewKeyListener l);
  +
     /**
      * Clears the model completely
      *
  @@ -76,11 +91,11 @@
     void clearModel();
   
     /**
  -   * Returns true if this model is Cyclic (bounded) or not. 
  +   * Returns true if this model is Cyclic (bounded) or not.
      * @return true/false
      */
     public boolean isCyclic();
  -  
  +
     /**
      * Locates a row number, starting from startRow, containing the text
      * within any column.
  @@ -129,5 +144,4 @@
      * Causes the EventContainer to sort according to it's configured attributes
      */
     void sort();
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to