View Javadoc

1   package com.explosion.expfmodules.monitoring;
2   
3   import java.util.Vector;
4   
5   import javax.swing.JScrollPane;
6   import javax.swing.JTable;
7   import javax.swing.JViewport;
8   import javax.swing.table.DefaultTableModel;
9   
10  import org.apache.log4j.AppenderSkeleton;
11  import org.apache.log4j.PatternLayout;
12  import org.apache.log4j.spi.LoggingEvent;
13  
14  import com.explosion.utilities.GeneralConstants;
15  
16  
17  /* =============================================================================
18   *       
19   *     Copyright 2004 Stephen Cowx
20   *
21   *     Licensed under the Apache License, Version 2.0 (the "License");
22   *     you may not use this file except in compliance with the License.
23   *     You may obtain a copy of the License at
24   *
25   *     http://www.apache.org/licenses/LICENSE-2.0
26   *
27   *     Unless required by applicable law or agreed to in writing, software
28   *     distributed under the License is distributed on an "AS IS" BASIS,
29   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30   *     See the License for the specific language governing permissions and
31   *     limitations under the License.
32   * 
33   * =============================================================================
34   */
35  
36  /***
37   * @author Stephen
38   * Created on Apr 21, 2004
39   */
40  public class TableAppender extends AppenderSkeleton
41  {
42      private JTable table;
43      private JScrollPane scrollPane;
44      private static String LS = GeneralConstants.LS;
45      private int maxLength = 0;
46      private String patternString ;
47      
48      /***
49       * 
50       */
51      public TableAppender(JScrollPane scrollPane)
52      {
53          super();
54          this.scrollPane = scrollPane;
55          this.table = new JTable();
56          
57          Vector columns = new Vector();
58          columns.add("Message");
59          table.setShowGrid(false);
60          
61          table.setModel(new DefaultTableModel(new Vector(), columns));
62  	    table.setColumnSelectionAllowed(false);
63  	    table.setRowSelectionAllowed(true);
64          scrollPane.getViewport().add(table);
65          scrollPane.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
66      }
67      
68      
69      /***
70       * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
71       * @param event
72       */
73      protected void append(LoggingEvent event)
74      {   
75          maxLength = ((Integer) ResourceManagementModule.instance().getPreference(ResourceManagementModule.SYSTEM_RESOURCE_LOGMAX).getValue()).intValue(); 
76          patternString = (String) ResourceManagementModule.instance().getPreference(ResourceManagementModule.SYSTEM_RESOURCE_LOGPATTERN).getValue();
77          
78          DefaultTableModel model = (DefaultTableModel) table.getModel();
79          while (model.getRowCount() >= maxLength)
80          {
81             model.removeRow(0); 
82          }
83          
84          PatternLayout layout = new PatternLayout(patternString);
85          Vector row = new Vector();
86          row.add(layout.format(event));
87  		model.addRow(row);
88  		table.setRowSelectionInterval(model.getRowCount()-1, model.getRowCount()-1);
89  	    model.fireTableRowsInserted(model.getRowCount()-1, model.getRowCount()-1);
90  	    table.scrollRectToVisible(table.getCellRect(table.getSelectedRow(), 1 ,true));
91  	    table.clearSelection();
92  	}
93  
94      /***
95       * @see org.apache.log4j.Appender#close()
96       * 
97       */
98      public void close()
99      {
100       this.table.setModel(new DefaultTableModel());    
101     }
102 
103     /***
104      * @see org.apache.log4j.Appender#requiresLayout()
105      * @return
106      */
107     public boolean requiresLayout()
108     {
109         return false;
110     }
111 
112 }