Package jsynoptic.ui

Source Code of jsynoptic.ui.HTMLSheetInformation$HtmlWriterFileFilter

package jsynoptic.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.diagram.DiagramComponent;
import simtools.shapes.AbstractShape;
import simtools.shapes.ShapesContainer;
import simtools.ui.HTMLWriter;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import simtools.util.CurrentPathProvider;
import simtools.util.NamedProperties;

/**
* Displays in a HTML document information about a synoptic
* @author zxpletran007
*
*/
public class HTMLSheetInformation {


  static MenuResourceBundle resources = ResourceFinder.getMenu(HTMLSheetInformation.class);

  static final String[] propertyTabTitles = {
    resources.getString("property"),
    resources.getString("value")};

  static final int CIRCLE_SIZE=15;
 
  protected File _htmlFile;
  protected File _imageFile;
  protected String sheetInformationTitle;

  protected JFileChooser htmlFileChooser;

  public File getHTMLFile(){
    return _htmlFile;
  }

  public String getTitle(){
    return sheetInformationTitle;
  }

  public HTMLSheetInformation(ShapesContainer sheet, DiagramComponent d) throws IOException{
   
    // Create current sheet snapshot with points for links
    _imageFile = new File(resources.getString("imageFileName")).getCanonicalFile();
    // Clear cache for image (bug in JTextPane)
    Image oldImage = Toolkit.getDefaultToolkit().getImage(_imageFile.toURL());
    if (oldImage!=null){
      oldImage.flush();
    }
    sheetInformationTitle = resources.getString("htmlFileTitle");
   
    Rectangle r=d.getBounds();
    r.width=(int)((double)r.width/d.getZoom());
    r.height=(int)((double)r.height/d.getZoom());
    BufferedImage bi=new BufferedImage(r.width,r.height,BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g2=bi.createGraphics();
    g2.setColor(sheet.getDiagramParameters().backgr);
    g2.fillRect(0,0,r.width,r.height);
    d.printDiagram(g2);
   
    // add number tags on image
    for(int i=0;i<sheet.size();i++){
      Object o = sheet.get(i);
      if (o instanceof AbstractShape) {
        AbstractShape shape = (AbstractShape)o;
        g2.setColor(Color.BLACK);
        g2.drawRect(shape.getBounds().x, shape.getBounds().y, CIRCLE_SIZE, CIRCLE_SIZE);
       
        g2.setColor(Color.RED);
        g2.fillRect(shape.getBounds().x, shape.getBounds().y, CIRCLE_SIZE, CIRCLE_SIZE);
        g2.setColor(Color.WHITE);
        new TextLayout(new Integer(i).toString(), g2.getFont(), g2.getFontRenderContext()).draw(g2, shape.getBounds().x+4, shape.getBounds().y+ (CIRCLE_SIZE-4));
      }
    }
   
    ImageIO.write(bi,resources.getString("imageFileExtension"),_imageFile);
    _htmlFile = new File(resources.getString("htmlFileName")).getCanonicalFile();

    FileWriter writeFile= new FileWriter(_htmlFile);
    BufferedWriter writeBuf = new BufferedWriter(writeFile)
    HTMLWriter writer = new HTMLWriter(writeBuf);
    writer.writeHtmlBegin();
    try{
      writer.writeTargetName("IMAGETARGET");
      writer.writeH1(sheetInformationTitle, null);

      // make links with descrition
      String[][] targets = new String[sheet.size()][5];

      for(int i=0;i<sheet.size();i++){
        Object o = sheet.get(i);
        if (o instanceof AbstractShape) {
          AbstractShape shape = (AbstractShape)o;

          // left-x, top-y, right-x, bottom-y
          targets[i][0] = new Integer(shape.getBounds().x).toString();
          targets[i][1] = new Integer(shape.getBounds().y).toString();
          targets[i][2] = new Integer(shape.getBounds().x + CIRCLE_SIZE+4).toString();
          targets[i][3] = new Integer(shape.getBounds().y + CIRCLE_SIZE+4).toString();

          // target name
          targets[i][4] = getShapeId(shape, i);

        }
      }
      // Write image with links
      writer.writeImage(_imageFile.getName(),resources.getString("imageFileTitle"), "IMAGEMAP", targets);
    }catch (IOException e){
      e.printStackTrace();
    }

    // Create sheet information
    for(int i=0;i<sheet.size();i++){
      Object o = sheet.get(i);
      if (o instanceof AbstractShape) {
        AbstractShape shape = (AbstractShape)o;

        // Shape name
        writer.writeTargetName(getShapeId(shape,i));
        writer.writeH2(getShapeId(shape, i),null);

        // Shape properties
        String[] propertiesNames = shape.getPropertyNames();
        ArrayList contents = new ArrayList();
       
        if (propertiesNames!=null){
          for(int j=0;j<propertiesNames.length;j++){
            String[] property = new String[2];
            property[0] = propertiesNames[j];

            Object obj = shape.getPropertyValue(propertiesNames[j]);
            if (obj!=null){
              if (obj instanceof NamedProperties){
                String[] objPropertiesNames = ((NamedProperties)obj).getPropertyNames();
                String objToString="";
                for(int k=0;k<objPropertiesNames.length;k++){
                  objToString+=objPropertiesNames[k] + " = " + ((NamedProperties)obj).getPropertyValue(objPropertiesNames[k]) + "\n";
                }
                property[1] = objToString;
             
              }else if (obj instanceof DataSource){
                property[1] = DataInfo.getAll(obj);
              }else{
                property[1] = obj.toString();
              }
              contents.add(property)
            }
          }
        }
        writer.writeTab(propertyTabTitles, contents);
        writer.writeP(resources.getString("goBackToImage"), "IMAGETARGET");
        writer.writeSeparator();
      }
    }
    writer.writeHtmlEnd();
    writeBuf.close();
    writeFile.close();
   
   
    // set html chooser for saving
    if(JSynoptic.gui!=null){
      htmlFileChooser = new JFileChooser();
      htmlFileChooser.setCurrentDirectory(CurrentPathProvider.currentPathProvider.getCurrentPath());
      htmlFileChooser.setFileFilter(new HtmlWriterFileFilter(resources.getString("htmlFileExtension")));
    }
   

  }

  private String getShapeId(AbstractShape s, int number){
    return  (number + ": "  + s.getShapeName());
  }
 

  public void save(Component parent){
    File destination;
    if(htmlFileChooser!=null){
      int result=htmlFileChooser.showSaveDialog(parent);
      if (result != JFileChooser.APPROVE_OPTION) return;
      destination = htmlFileChooser.getSelectedFile();
      if(!destination.getName().endsWith("."((HtmlWriterFileFilter)htmlFileChooser.getFileFilter()).suffix)){
        destination=new File(destination.getParentFile(),destination.getName()+"."+ ((HtmlWriterFileFilter)htmlFileChooser.getFileFilter()).suffix);
      }
     
      if (!destination.equals(_htmlFile)){
        File parentDirectory = destination.getAbsoluteFile().getParentFile();
        copyFile(_htmlFile, destination);
        copyFile(_imageFile, new File(parentDirectory,_imageFile.getName()));
      }
    }
  }

  public class HtmlWriterFileFilter extends FileFilter{
    String suffix;
   
    public HtmlWriterFileFilter(String suff){
      suffix=suff;
    }
    /* (non-Javadoc)
     * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
     */
    public boolean accept(File f) {
      return f.isDirectory() || (f.isFile() && f.getName().endsWith("."+suffix));
    }

    /* (non-Javadoc)
     * @see javax.swing.filechooser.FileFilter#getDescription()
     */
    public String getDescription() {
      return suffix;
    }
  }
 
  protected void copyFile(File source, File destination){
    java.io.FileInputStream sourceFile=null;
    java.io.FileOutputStream destinationFile=null;
   
    if (destination.exists()){
      destination.delete();
    }
    try {
      destination.createNewFile();
      sourceFile = new java.io.FileInputStream(source);
      destinationFile = new java.io.FileOutputStream(destination);
      byte buffer[]=new byte[512*1024];
      int nb;
      while( (nb = sourceFile.read(buffer)) != -1 ) {
        destinationFile.write(buffer, 0, nb);
      }
    } catch( java.io.FileNotFoundException f ) {
    } catch( java.io.IOException e ) {
    } finally {
      try {
        sourceFile.close();
      } catch(Exception e) { }
      try {
        destinationFile.close();
      } catch(Exception e) { }
    }
  }
 
}
TOP

Related Classes of jsynoptic.ui.HTMLSheetInformation$HtmlWriterFileFilter

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.