Package simtools.logging.ui

Source Code of simtools.logging.ui.LoggingBufferPanel$TestBinary

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

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import simtools.logging.LoggingEntryByteBuffer;
import simtools.logging.SocketBufferHandler;
import simtools.ui.MenuResourceBundle;

public class LoggingBufferPanel extends LoggingPanel {

  public LoggingBufferPanel(JFrame parent, String propertiesPrefix,
      MenuResourceBundle menuResources) {
    super(parent, propertiesPrefix, menuResources);
  }

  protected AbstractLoggingTableModel createTableModel(){
    return new LoggingBufferTableModel(new LoggingEntryByteBuffer(),menuResources);
  }

  protected LoggingEntryByteBuffer getBuffer(){
    return ((LoggingBufferTableModel)getLogbookModel()).buffer;
  }

  /**
   * For testing
   * @param args
   */
  public static void main(final String[] args) {
    if(args.length>0){
      JFrame f=new JFrame("Reading XML");
      LoggingBufferPanel lp=new LoggingBufferPanel(f,"test",null);
      f.getContentPane().add(lp);

      final LoggingEntryByteBuffer buffer=lp.getBuffer();
      try {
        buffer.readXML(new File(args[0]));
      } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
        System.exit(0);
      } catch (SAXException e1) {
        e1.printStackTrace();
        System.exit(0);
      } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
      }

      f.addWindowListener( new WindowAdapter() {
        public void windowClosing(WindowEvent e){
          new TestBinary(buffer,args[0]);
        }
      });

      f.pack();
      f.show();
    }
    else{
      JFrame f=new JFrame("Reading Binary Socket");
      LoggingBufferPanel lp=new LoggingBufferPanel(f,"test",null);
      f.getContentPane().add(lp);

      final LoggingEntryByteBuffer buffer=lp.getBuffer();
      try {
        buffer.openServer(4321);
        ((LoggingBufferTableModel)lp.getLogbookModel()).startRefreshLoop();
      } catch(IOException ie){
        ie.printStackTrace();
        System.exit(0);
      }
      Logger lr = null;
      try {
        SocketBufferHandler sbh=new SocketBufferHandler("localhost",4321);
        lr=Logger.getLogger("TEST");
        lr.addHandler(sbh);
        lr.setUseParentHandlers(false);
        lr.warning("Start");
      } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
      }
      f.addWindowListener( new WindowAdapter() {
        public void windowClosing(WindowEvent e){
          try {
            buffer.closeServer();
          } catch (IOException e1) {
            e1.printStackTrace();
            System.exit(0);
          }
          new TestBinary(buffer,"TEST");
        }
      });

      f.pack();
      f.show();

      int i=0;
      while(true){
        lr.info("Write "+i);
        i++;
        if(i%10==0){
          lr.log(Level.SEVERE,"dummy",new IOException("bar"));
        }
        try {
          Thread.sleep(100);
        } catch (InterruptedException e1) {
          e1.printStackTrace();
        }
      }
    }
  }

  static class TestBinary {
    public TestBinary(LoggingEntryByteBuffer buffer, String fileName){
      try {
        buffer.save(new File(fileName+".bin"));
      } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
      }
      JFrame f2=new JFrame("Reading binary");
      LoggingBufferPanel lp2=new LoggingBufferPanel(f2,"test",null);
      f2.getContentPane().add(lp2);

      LoggingEntryByteBuffer buffer2=lp2.getBuffer();
      try {
        buffer2.read(new File(fileName+".bin"));
      } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
      }

      f2.addWindowListener( new WindowAdapter() {
        public void windowClosing(WindowEvent e){
          System.exit(0);
        }
      });

      f2.pack();
      f2.show();

    }
  }

    /**
     * Method clearLogs
     * <br><b>Summary:</b><br>
     * This method clear the logs.
     * @return <b>(void)</b>  A void.
     */
    public void clearLogs() {
        getBuffer().clear();
        table.tableModel.clearLogs();
    }
}
TOP

Related Classes of simtools.logging.ui.LoggingBufferPanel$TestBinary

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.