/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: LoggingBufferTableModel.java,v 1.7 2006/09/29 08:26:23 booba_skaya Exp $
*
* Changes
* -------
* 6 sept. 06 : Initial public release (CC);
*
*/
package simtools.logging.ui;
import java.util.Date;
import simtools.logging.LoggingEntryByteBuffer;
import simtools.ui.MenuResourceBundle;
import simtools.util.AbstractCyclicCallerRunnable;
public class LoggingBufferTableModel extends AbstractLoggingTableModel {
final LoggingEntryByteBuffer buffer;
private RefreshCyclicCaller refreshCyclicCaller;
public LoggingBufferTableModel(int size, MenuResourceBundle resources) {
super(resources);
buffer=new LoggingEntryByteBuffer(size);
}
public LoggingBufferTableModel(LoggingEntryByteBuffer buffer, MenuResourceBundle resources) {
super(resources);
this.buffer=buffer;
}
/**
* Method startRefreshLoop
* <br><b>Summary:</b><br>
* This method start or resume the refresh
* @return <b>(void)</b> A void.
*/
void startRefreshLoop(){
if(refreshCyclicCaller == null){
refreshCyclicCaller = new RefreshCyclicCaller();
Thread t=new Thread(refreshCyclicCaller, "TableRefreshLoop");
t.start();
}
refreshCyclicCaller.resume();
}
/**
* Method pauseRefreshLoop
* <br><b>Summary:</b><br>
* Pause the refresh loop.
* @return <b>(void)</b> A void.
*/
protected void pauseRefreshLoop(){
if(refreshCyclicCaller != null){
refreshCyclicCaller.pause();
}
}
/**
* Method stopRefreshLoop
* <br><b>Summary:</b><br>
* Stop the refresh loop.
*/
protected void stopRefreshLoop(){
if(refreshCyclicCaller != null){
refreshCyclicCaller.stop();
refreshCyclicCaller = null;
}
}
public int getLevel(int row) {
return buffer.getLevel(row);
}
public int getSize() {
return buffer.getSize();
}
public void setSize(int newsize) {
buffer.setSize(newsize);
}
public int getRowCount() {
return buffer.getLength();
}
public Object getValueAt(int rowIndex, int column) {
switch(column){
case 0:
// Date
return timeFormatter.format(new Date(buffer.getMillis(rowIndex)));
case 1:
// Sequence Number
return ""+buffer.getSequenceNumber(rowIndex);
case 2:
// Level
int l=buffer.getLevel(rowIndex)/LEVEL_SCALE_FACTOR;
String v=null;
if(l>=0 && l<levelNames.length){
v=levelNames[l];
}
if(v==null){
v=""+buffer.getLevel(rowIndex);
}
return v;
case 3:
// Source
return buffer.getLoggerName(rowIndex);
case 4:
// Source class
return buffer.getSourceClassName(rowIndex);
case 5:
// Source method
return buffer.getSourceMethodName(rowIndex);
case 6:
// Message
return buffer.getMessage(rowIndex);
case 7:
// Thread Id
return ""+buffer.getThreadID(rowIndex);
}
throw new IllegalArgumentException("column="+column);
}
/**
* Class RefreshCyclicCaller
* This method refresh the model.
*/
private class RefreshCyclicCaller extends AbstractCyclicCallerRunnable{
/**(<b>int</b>) index: The current model index.*/
private int index;
/**
* Contructor RefreshCyclicCaller
* <br><b>Summary:</b><br>
* The constructor of the class RefreshCyclicCaller.
*/
public RefreshCyclicCaller(){
//Do go under refresh 500 for better performance.
super(500);
index=buffer.getLength();
}
/* (non-Javadoc)
* @see simtools.util.AbstractCyclicCallerRunnable#process()
*/
public void process() {
//The new index is length
int newIndex=buffer.getLength();
if(index<newIndex){
LoggingBufferTableModel.this.fireTableRowsInserted(index, newIndex-1);
}
else if(index>newIndex){
LoggingBufferTableModel.this.fireTableDataChanged();
}
index=newIndex;
}
}
/* (non-Javadoc)
* @see simtools.logging.ui.AbstractLoggingTableModel#clearLogs()
*/
public void clearLogs() {
buffer.clear();
fireTableDataChanged();
}
}