Package org.gvt.action

Source Code of org.gvt.action.SaveAsImageAction

package org.gvt.action;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.*;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.draw2d.*;
import org.gvt.ChisioMain;
import org.gvt.model.CompoundModel;
import org.gvt.editpart.ChsRootEditPart;
import org.gvt.editpart.ChsScalableRootEditPart;

import java.io.File;

/**
* Action for saving the graph or view as an image.
*
* @author Cihan Kucukkececi
*
* Copyright: I-Vis Research Group, Bilkent University, 2007
*/
public class SaveAsImageAction extends Action
{
  ChisioMain main;
  boolean saveWholeGraph;

  public SaveAsImageAction(ChisioMain chisio, boolean saveGraph)
  {
    this.main = chisio;
    this.saveWholeGraph = saveGraph;
    setText(saveWholeGraph ? "Save Pathway As Image ..." : "Save View As Image ...");
    setToolTipText(getText());
  }

  public void run()
  {
    final Shell shell = main.getShell();
    Figure rootFigure;
    Rectangle bounds;
    double scale = 1.0;
    ScalableLayeredPane layer = null;

    if (main.getViewer() == null) return;

    rootFigure = (Figure) ((ChsScalableRootEditPart) main.getViewer().
      getRootEditPart()).getFigure();

    if (!saveWholeGraph)
    {
      bounds = getBounds(rootFigure);
    }
    else
    {
      rootFigure = (Figure) rootFigure.getChildren().get(0);
      layer = (ScalableLayeredPane)rootFigure.getChildren().get(0);
      scale = layer.getScale();
      layer.setScale(1.0);
      bounds = getBounds(main.getViewer(), rootFigure);
    }

    final Image image = new Image(shell.getDisplay(), bounds);

    GC gc = new GC(image);
   
    if (ChisioMain.runningOnWindows)
    {
      gc.setAntialias(SWT.ON);
      gc.setTextAntialias(SWT.ON);
    }
    else
    {
      gc.setAntialias(SWT.OFF);
      gc.setTextAntialias(SWT.OFF);
    }
   
    Graphics graphics = new SWTGraphics(gc);


  //  Point p = new Point(-50, -50);
  //  graphics.translate(p);

    rootFigure.paint(graphics);
    graphics.dispose();

    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[]{image.getImageData()};
    // Get the user to choose a file name and type to save.
    FileDialog fileChooser = new FileDialog(main.getShell(), SWT.SAVE);
    String tmpfilename = main.getOwlFileName();
    int ind = tmpfilename.lastIndexOf('.');
    tmpfilename = tmpfilename.substring(0, ind);

    fileChooser.setFileName(tmpfilename);
    fileChooser.setFilterExtensions(new String[]{"*.bmp", "*.jpg"/*, "*.gif", "*.png"*/});
    fileChooser.setFilterNames(
      new String[]{"BMP (*.bmp)", "JPEG (*.jpg)"/*, "GIF (*.gif)", "PNG (*.png)"*/});
    String filename = fileChooser.open();

    if (filename == null)
    {
      return;
    }
    else
    {
      File file = new File(filename);
      if (file.exists())
      {
        // The file already exists; asks for confirmation
        MessageBox mb = new MessageBox(fileChooser.getParent(),
          SWT.ICON_WARNING | SWT.YES | SWT.NO);

        // We really should read this string from a
        // resource bundle
        mb.setMessage(filename + " already exists.\nDo you want to overwrite?");
        mb.setText("Confirm Replace File");
        // If they click Yes, we're done and we drop out. If
        // they click No, we quit the operation.
        if (mb.open() != SWT.YES)
        {
          return;
        }
      }
    }

    if (filename.endsWith(".bmp"))
    {
      loader.save(filename, SWT.IMAGE_BMP);
    }
    else if (filename.endsWith(".jpg"))
    {
      loader.save(filename, SWT.IMAGE_JPEG);
    }
        else if (filename.endsWith(".gif"))
    {
      loader.save(filename, SWT.IMAGE_GIF);
    }
        else if (filename.endsWith(".png"))
    {
      loader.save(filename, SWT.IMAGE_PNG);
    }

        if (saveWholeGraph)
    {
      layer.setScale(scale);
    }
  }

  public Rectangle getBounds(ScrollingGraphicalViewer viewer, Figure f)
  {
    CompoundModel rootModel = (CompoundModel) ((ChsRootEditPart) viewer.
      getRootEditPart().getChildren().get(0)).getModel();

    org.eclipse.draw2d.geometry.Rectangle bounds
      = rootModel.calculateBounds();
    org.eclipse.draw2d.geometry.Rectangle boundsRoot = f.getBounds();

    boundsRoot.setSize(bounds.x + bounds.width + CompoundModel.MARGIN_SIZE,
      bounds.y + bounds.height + CompoundModel.MARGIN_SIZE);

    return new Rectangle(boundsRoot.x,
      boundsRoot.y,
      boundsRoot.width,
      boundsRoot.height);
  }

  public Rectangle getBounds(Figure f)
  {
    org.eclipse.draw2d.geometry.Rectangle bounds = f.getBounds();

    return new Rectangle(bounds.x ,
      bounds.y ,
      bounds.width ,
      bounds.height);
  }
}
TOP

Related Classes of org.gvt.action.SaveAsImageAction

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.