All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.epfl.gsn.others.visualization.svg.SVGPage Maven / Gradle / Ivy

The newest version!
/**
* Global Sensor Networks (GSN) Source Code
* Copyright (c) 2006-2016, Ecole Polytechnique Federale de Lausanne (EPFL)
* 
* This file is part of GSN.
* 
* GSN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* GSN 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 General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with GSN.  If not, see .
* 
* File: src/ch/epfl/gsn/others/visualization/svg/SVGPage.java
*
* @author Ali Salehi
* @author Sofiane Sarni
*
*/

package ch.epfl.gsn.others.visualization.svg;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;

import javax.imageio.ImageIO;

import org.antlr.stringtemplate.StringTemplate;

public class SVGPage {
   
   private int                           width           = -1;
   
   private int                           height          = -1;
   
   private float                         borderWidth     = 0;
   
   private String                        backgroundImage;
   
   private Color                         backgroundColor = Color.white;
   
   private TreeMap < String , SVGLayer > layers          = new TreeMap( new Comparator( ) {
                                                            
                                                            public int compare ( Object o1 , Object o2 ) {
                                                               if ( o1 == null || o2 == null ) return -1;
                                                               String input1 = ( String ) o1;
                                                               String input2 = ( String ) o2;
                                                               return input1.compareToIgnoreCase( input2 );
                                                               
                                                            }
                                                         } );
   
   public SVGPage ( String backgroundImage ) {
      this.backgroundImage = backgroundImage;
   }
   
   private final static String  SVG_HEADER_TEMPLATE = "\n" + "\n \n";
   
   private static final String  DEF_BEGIN           = "";
   
   private StringTemplate [ ]   defines             = new StringTemplate [ ] { new StringTemplate(
                                                       "     " ) };
   
   private static final String  DEF_END             = "";
   
   private final static String  SVG_END             = "\n";
   
   private final StringTemplate stringTemplate      = new StringTemplate( SVG_HEADER_TEMPLATE );
   
   public SVGPage ( int width , int height , Color backgroundColor ) throws IOException {
      this.width = width;
      this.height = height;
      this.backgroundColor = backgroundColor;
   }
   
   public SVGPage ( int width , int height ) {
      this.width = width;
      this.height = height;
   }
   
   public SVGPage ( int width , int height , int borderWidth ) throws IOException {
      this.width = width;
      this.height = height;
      this.borderWidth = borderWidth;
      
   }
   
   public SVGPage ( int width , int height , int borderWidth , Color backgroundColor ) throws IOException {
      this.width = width;
      this.height = height;
      this.borderWidth = borderWidth;
      this.backgroundColor = backgroundColor;
   }
   
   public int getWidth ( ) {
      return width;
   }
   
   public int getHeight ( ) {
      return height;
   }
   
   public float getBorderWidth ( ) {
      return borderWidth;
   }
   
   public String getBackgroundImage ( ) {
      return backgroundImage;
   }
   
   public Color getBackgroundColor ( ) {
      return backgroundColor;
   }
   
   public void setWidth ( int width ) {
      this.width = width;
   }
   
   public void setHeight ( int height ) {
      this.height = height;
   }
   
   public void setBorderWidth ( int borderWidth ) {
      this.borderWidth = borderWidth;
   }
   
   public void setBackgroundImage ( String backgroundImage ) {
      this.backgroundImage = backgroundImage;
   }
   
   public void setBackgroundColor ( Color backgroundColor ) {
      this.backgroundColor = backgroundColor;
   }
   
   public void drawOn ( StringBuilder input ) {
      stringTemplate.reset( );
      StringBuilder result = new StringBuilder( );
      try {
         if ( backgroundImage != null ) {
            InputStream is = new FileInputStream( backgroundImage );
            BufferedImage bi = ImageIO.read( is );
            width = bi.getWidth( );
            height = bi.getHeight( );
            // TODO Setting the background image
         }
      } catch ( IOException e ) {
         e.printStackTrace( );
         System.exit( 1 );
      }
      if ( width > 0 && height > 0 ) {
         stringTemplate.setAttribute( "WIDTH" , width );
         stringTemplate.setAttribute( "HEIGHT" , height );
         result.append( stringTemplate.toString( ) );
      }
      result.append( DEF_BEGIN );
      for ( StringTemplate defs : defines ) {
         result.append( defs.toString( ) );
      }
      result.append( DEF_END );
      
      if ( backgroundImage != null ) {

      }
      Iterator < SVGLayer > it = layers.values( ).iterator( );
      while ( it.hasNext( ) ) {
         it.next( ).drawOn( result );
      }
      result.append( SVG_END );
      input.append( result );
   }
   
   public String getName ( ) {
      return "SVGPAGE";
   }
   
   public float getOpaqeAlpha ( ) {
      return 1;
   }
   
   public SVGPage addLayer ( SVGLayer layer ) {
      layers.put( layer.getName( ) , layer );
      return this;
   }
   
   public SVGPage removeLayer ( SVGLayer layer ) {
      layers.remove( layer.getName( ) );
      return this;
   }
   
   public SVGPage removeLayer ( String layerName ) {
      layers.remove( layerName );
      return this;
   }
   
   public void drawInFile ( String s ) throws IOException {
      File file = new File( s );
      StringBuilder stringBuilder = new StringBuilder( );
      drawOn( stringBuilder );
      FileWriter fileWriter = new FileWriter( file );
      fileWriter.write( stringBuilder.toString( ) );
      fileWriter.close( );
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy