Package jsynoptic.ui

Source Code of jsynoptic.ui.PrintToImage$ImageWriterFileFilter

/* ========================
* 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-2004, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: PrintToImage.java,v 1.9 2008/05/14 14:26:21 ogor Exp $
*
* Changes
* -------
* 21 juin 2004 : Initial public release (CC);
*
*/
package jsynoptic.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

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

import jsynoptic.base.PrintFormat;
import simtools.diagram.DiagramComponent;
import simtools.util.CurrentPathProvider;

/**
* Class description ...
*
* @author Claude CAZENAVE
*
*/
public class PrintToImage extends PrintFormat {

  protected JFileChooser printFileChooser;
  protected File defaultDirectory;
 
 
  /**
   *
   */
  public PrintToImage() {
   
    if(JSynoptic.gui!=null){
      File currentDirectory = CurrentPathProvider.currentPathProvider.getCurrentPath();
      printFileChooser = new JFileChooser(currentDirectory);
     
      Iterator it=formatsMap.keySet().iterator();
      FileFilter preferredFilter=null;
      while(it.hasNext()){
        String v=(String)it.next();
        FileFilter ff=new ImageWriterFileFilter(v);
        if(v.equals("png")){
          preferredFilter=ff;
        }
        printFileChooser.addChoosableFileFilter(ff);
      }
      if(preferredFilter!=null){
        printFileChooser.setFileFilter(preferredFilter);
      }
      printFileChooser.setAcceptAllFileFilterUsed(false);
    }
  }

  public PrintToImage(File defaultDirectory) {
    this();
    this.defaultDirectory = defaultDirectory;
   
  }
  public class ImageWriterFileFilter extends FileFilter{

    String suffix;
   
    public ImageWriterFileFilter(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;
    }
   
  }

 
  public void print(DiagramComponent d, Color c) throws IOException{
    String format;
    File f;
   
    if(JSynoptic.gui!=null){
      printFileChooser.setDialogTitle(d.getName());
      FileFilter ff=printFileChooser.getFileFilter();
      if(ff==null){
        return;
      }
      String defaultExt="."+((ImageWriterFileFilter)printFileChooser.getFileFilter()).suffix;
      f=new File(printFileChooser.getCurrentDirectory(),d.getName()+defaultExt);
      printFileChooser.setSelectedFile(f);
      int result=printFileChooser.showSaveDialog(JSynoptic.gui.getOwner());
      if (result != JFileChooser.APPROVE_OPTION) return;

      String ext=((ImageWriterFileFilter)printFileChooser.getFileFilter()).suffix;
      f=printFileChooser.getSelectedFile();
      if(!f.getName().endsWith("."+ext)){
        f=new File(f.getParentFile(),f.getName()+"."+ext);
      }
      format=(String)formatsMap.get(ext);
      if(format==null){
        return;
      }
    }else {
      defaultDirectory = (defaultDirectory==null)? new File(System.getProperty("user.dir")) : defaultDirectory;
      f = new File(defaultDirectory, d.getName()+".jpg");
     
      format=(String)formatsMap.get("jpg");
   
   
    // Get Buffered Image from diagram component
    if (f !=null){
      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();

      if (c!=null)
        g2.setColor(c);
      else
        g2.setColor(Color.WHITE);

      g2.fillRect(0,0,r.width,r.height);
      d.printDiagram(g2);
      ImageIO.write(bi,format,f);
    }
    if(JSynoptic.gui!=null){
      JSynoptic.gui.setStatus(JSynopticPanels.messageWriter.print1args("exported", f.getAbsolutePath()));
    }
  }
 
 
 
  public void print(String imageName, BufferedImage bi, Color c) throws IOException{
    String format;
    File f;
   
    if(JSynoptic.gui!=null){
      printFileChooser.setDialogTitle(imageName);
      FileFilter ff=printFileChooser.getFileFilter();
      if(ff==null){
        return;
      }
      String defaultExt="."+((ImageWriterFileFilter)printFileChooser.getFileFilter()).suffix;
      f=new File(printFileChooser.getCurrentDirectory(),imageName+defaultExt);
      printFileChooser.setSelectedFile(f);
      int result=printFileChooser.showSaveDialog(JSynoptic.gui.getOwner());
      if (result != JFileChooser.APPROVE_OPTION) return;

      String ext=((ImageWriterFileFilter)printFileChooser.getFileFilter()).suffix;
      f=printFileChooser.getSelectedFile();
      if(!f.getName().endsWith("."+ext)){
        f=new File(f.getParentFile(),f.getName()+"."+ext);
      }
      format=(String)formatsMap.get(ext);
      if(format==null){
        return;
      }
    }else{
      defaultDirectory = (defaultDirectory==null)? new File(System.getProperty("user.dir")) : defaultDirectory;
      f = new File(defaultDirectory, imageName +".jpg");
     
      format=(String)formatsMap.get("jpg");
   
    ImageIO.write(bi,format,f);
 
 
}
TOP

Related Classes of jsynoptic.ui.PrintToImage$ImageWriterFileFilter

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.