Package simtools.images.svg.test

Source Code of simtools.images.svg.test.TestSVG$TestThread

/* ========================
* 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: TestSVG.java,v 1.5 2009/01/08 17:10:33 ogor Exp $
*
* Changes
* -------
* 20 d�c. 2005 : Initial public release (CC);
*
*/
package simtools.images.svg.test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import simtools.diagram.DiagramClipboard;
import simtools.shapes.AbstractShape;
import simtools.shapes.LabelShape;
import simtools.shapes.ShapeListener;
import simtools.shapes.ShapesContainer;
import simtools.ui.DesktopCardPanel;
import simtools.util.FileSerializer;

/**
* A test application for SVG image factory
* @see <code>TestSVGImageFactory</code>
* @see <code>TestSVGShape</code>
* @see <code>ShapesContainer</code>
* @see <code>DesktopCardPanel</code>
*/
public class TestSVG extends JFrame{

  static final Font sFont=new Font("TimesRoman",Font.PLAIN,10);
 
  static final int GRID_STEP=50;
  static final int GRID_SIZE=10;

  DesktopCardPanel editor;
 
  ShapesContainer svg;
 
  ByteArrayOutputStream bo;
 
  /**
   * A JFrame with one shapes container in it
   */
  public TestSVG(){
    super("SVG Test");

    // load and layout the different components
    editor=new DesktopCardPanel(true);
    getContentPane().add(BorderLayout.CENTER, editor);
   
    JPanel p=new JPanel();
    JButton b;
    b=new JButton("dump");
    b.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        factory.dump("TestSVG : SVGTest");
      }
    });
    p.add(b);
    b=new JButton("save");
    b.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        bo=new ByteArrayOutputStream();
        try {
          ObjectOutputStream oo=new ObjectOutputStream(bo);
          oo.writeObject(svg);
        } catch (IOException ee) {
          ee.printStackTrace();
        }
      }
    });
    p.add(b);
    b=new JButton("load");
    b.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        if(bo==null){
          return;
        }
        ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream io;
        editor.removeComponent(svg.getComponent());
        svg.removeAllElements();
        try {
          io = new ObjectInputStream(bi);
          svg=(ShapesContainer)io.readObject();
          editor.addComponent(svg.getComponent());
          for (Iterator it = svg.iterator(); it.hasNext();) {
            AbstractShape abs = (AbstractShape)it.next();
            abs.addListener( (ShapeListener)svg.getComponent());
          }
             
        } catch (IOException e1) {
          e1.printStackTrace();
        } catch (ClassNotFoundException e2) {
          e2.printStackTrace();
        }
      }
    });
    p.add(b);

    getContentPane().add(BorderLayout.NORTH, p);

    // on exit
    addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
   
    // a container with coordinates
    svg=new ShapesContainer("SVG");
    LabelShape l;
    for(int i=0;i<GRID_SIZE;i++){
      l=new LabelShape(""+i,GRID_STEP*i,0,LabelShape.CENTER,false);
      l.setBounds(sFont);
      svg.addElement(l);
      l=new LabelShape(""+i,0,GRID_STEP*i,LabelShape.CENTER,false);
      l.setBounds(sFont);
      svg.addElement(l);
    }
     editor.addComponent(svg.getComponent());

    DiagramClipboard.create();

    // display the frame
     int s=GRID_SIZE*GRID_STEP;
     editor.setPreferredSize(new Dimension(s,s+30));
    pack()
    show();
  }

  /**
   * The factory instance
   */
  static TestSVGImageFactory factory=new TestSVGImageFactory();
 
  /**
   * The file serializer instance
   */
  static FileSerializer serializer=new FileSerializer();
 
  /**
   * Get the file located in the current class directory
   * @param o on object of that class
   * @param name the file name
   * @return the file
   */
  static File getFile(Object o,String name){
    URL url = o.getClass().getResource(name);
    File file = new File(url.getFile());
    return file;
  }
 
  /**
   * A thread to load several SVG shapes and display them at a given
   * vertical coordinate
   */
  static public class TestThread extends Thread{
    int y;
    TestSVG frame;
   
    /**
     * @param y the vertical coordinate
     */
    TestThread(TestSVG frame, int y){
      this.y=y;
      this.frame=frame;
    }
    public void run(){
      if(y%2==0){
        for(int i=1;i<=6;i++){
          frame.svg.addElement(new TestSVGShape(getFile(frame,"svg"+i+".svg"), GRID_STEP*(i-1)+2, GRID_STEP*(y+1) + 2));
        }
      }
      else{
        for(int i=6;i>=1;i--){
          frame.svg.addElement(new TestSVGShape(getFile(frame,"svg"+i+".svg"), GRID_STEP*(6-i)+2, GRID_STEP*(y+1) + 2));
        }
      }
    }
  }
 
  /**
   * Do the testing...
   * @param args
   */
  static public void main(String args[]) {
    TestSVG frame=new TestSVG();
   
    for(int i=0;i<6;i++){
      TestThread t=new TestThread(frame,i);
      t.start();
    }
  }

}
TOP

Related Classes of simtools.images.svg.test.TestSVG$TestThread

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.