Package simtools.logging.ui

Source Code of simtools.logging.ui.LoggingTableModel

/* ========================
* 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: LoggingTableModel.java,v 1.4 2006/09/29 08:26:23 booba_skaya Exp $
*
* Changes
* -------
* 5 sept. 06  : Initial public release (CC);
*
*/
package simtools.logging.ui;

import java.util.Date;
import simtools.logging.LoggingEntry;
import simtools.ui.MenuResourceBundle;

public class LoggingTableModel extends AbstractLoggingTableModel {

  static final int DEFAULT_SIZE=100;

  protected LoggingEntry[] entries;
  protected int nbr;
  protected int start;
  protected int nbrMax;

  public LoggingTableModel(MenuResourceBundle menuResources){
    this(-1,menuResources);
  }

  public LoggingTableModel(int size, MenuResourceBundle menuResources) {
    super(menuResources);
    this.nbrMax=size;
    if(nbrMax<0){
      entries=new LoggingEntry[DEFAULT_SIZE];
    }
    else{
      entries=new LoggingEntry[nbrMax];
    }
    nbr=0;
    start=0;
  }

  public int getSize(){
    return nbrMax;
  }

  public void setSize(int newsize){
    if(newsize!=nbrMax){
      nbrMax=newsize;
      reset();
    }
  }

  public void reset(){
    if(nbrMax<0){
      entries=new LoggingEntry[DEFAULT_SIZE];
    }
    else{
      entries=new LoggingEntry[nbrMax];
    }
    nbr=0;
    start=0;
    fireTableDataChanged();
  }

  // assume these events are ordered
  public void add(LoggingEntry log) {
        if ((nbrMax < 0) && ((nbr + start + 1) >= entries.length)) {
            LoggingEntry[] newEntries = new LoggingEntry[entries.length * 2];
            System.arraycopy(entries, 0, newEntries, 0, entries.length);
            entries = newEntries;
        }
        entries[(nbr + start) % entries.length] = log;
        if (nbr < entries.length) {
            fireTableRowsInserted(nbr, nbr);
            nbr++;
        } else {
            start = (start + 1) % entries.length;
            fireTableDataChanged();
        }
    }

  protected void resize(int newSize){
    if(newSize<0){
      nbrMax=newSize;
    }
    else{
      LoggingEntry[] newEntries=new LoggingEntry[newSize];
      if((start+nbr)>entries.length){
        int nup=entries.length-start;
        System.arraycopy(entries, start, newEntries, 0, nup);
        System.arraycopy(entries, 0, newEntries, nup, nbr-nup);
      }
      else{
        System.arraycopy(entries, start, newEntries, 0, nbr);
      }
      entries=newEntries;
      nbrMax=newSize;
    }
  }

  public int getRowCount() {

      return nbr;

  }

  public Object getValueAt(int row, int column) {
    LoggingEntry log;
    log=entries[(row+start)%entries.length];
    return getString(log,column);
  }

  protected String getString(LoggingEntry log, int column){
    switch(column){
     case 0:
      // Date
      return timeFormatter.format(new Date(log.getMillis()));
     case 1:
      // Sequence Number
      return ""+log.getSequenceNumber();
     case 2:
      // Level
      int l=log.getLevel()/LEVEL_SCALE_FACTOR;
      String v=null;
      if(l>=0 && l<levelNames.length){
        v=levelNames[l];
      }
      if(v==null){
        v=""+log.getLevel();
      }
      return v;
     case 3:
      // Source
      return log.getLoggerName();
     case 4:
      // Source class
      return log.getSourceClassName();
     case 5:
      // Source method
      return log.getSourceMethodName();
     case 6:
      // Message
      return log.getMessage();
     case 7:
      // Thread Id
      return ""+log.getThreadID();
    }
    throw new IllegalArgumentException("column="+column);
  }

  public int getLevel(int row) {
    return entries[(row+start)%entries.length].getLevel();
  }

    /* (non-Javadoc)
     * @see simtools.logging.ui.AbstractLoggingTableModel#clearLogs()
     */
    public void clearLogs() {
        reset();
    }
}
TOP

Related Classes of simtools.logging.ui.LoggingTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.