Commits

Anonymous committed d92523a

Initial import from SourceForge

  • Participants
  • Parent commits b2c73ef

Comments (0)

Files changed (2)

File src/designer/com/opensymphony/workflow/designer/Utils.java

+package com.opensymphony.workflow.designer;
+
+import java.io.File;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import javax.swing.*;
+import javax.swing.filechooser.FileFilter;
+
+import com.jgraph.JGraph;
+
+/**
+ * @author Hani Suleiman (hani@formicary.net)
+ * Date: May 21, 2003
+ * Time: 12:12:44 AM
+ */
+public class Utils
+{
+  public static File promptUserForFile(Component component, int type, boolean save, final String suffix, final String description)
+  {
+    JFileChooser chooser = new JFileChooser(Prefs.INSTANCE.get(Prefs.CURRENT_DIR, System.getProperty("user.dir")));
+    chooser.rescanCurrentDirectory();
+    if(type == JFileChooser.FILES_AND_DIRECTORIES)
+    {
+      FileFilter filter = new FileFilter()
+      {
+        public boolean accept(File f)
+        {
+          return f.isDirectory() || f.getName().toLowerCase().endsWith(suffix);
+        }
+
+        public String getDescription()
+        {
+          return description;
+        }
+      };
+      chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
+      chooser.setFileFilter(filter);
+    }
+    else
+    {
+      chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+    }
+    File selectedFile = null;
+    int result;
+    if(save)
+    {
+      result = chooser.showSaveDialog(component);
+    }
+    else
+    {
+      result = chooser.showOpenDialog(component);
+    }
+
+    if(result == JFileChooser.APPROVE_OPTION)
+    {
+      selectedFile = chooser.getSelectedFile();
+      if(save && type==JFileChooser.FILES_AND_DIRECTORIES && !selectedFile.getName().toLowerCase().endsWith(suffix))
+      {
+        selectedFile = new File(selectedFile.toString() + suffix);
+      }
+    }
+    Prefs.INSTANCE.put(Prefs.CURRENT_DIR, chooser.getCurrentDirectory().toString());
+    return selectedFile;
+  }
+
+  public static BufferedImage toImage(JGraph graph)
+  {
+    Object[] cells = graph.getRoots();
+    if(cells.length > 0)
+    {
+      Rectangle bounds = graph.getCellBounds(cells);
+      graph.toScreen(bounds);
+
+      // Create a Buffered Image
+      Dimension d = bounds.getSize();
+      BufferedImage img = new BufferedImage(d.width + 10, d.height + 10, BufferedImage.TYPE_INT_RGB);
+      Graphics2D graphics = img.createGraphics();
+      graphics.setColor(graph.getBackground());
+      graphics.fillRect(0, 0, img.getWidth(), img.getHeight());
+      graphics.translate(-bounds.x + 5, -bounds.y + 5);
+
+      Object[] selection = graph.getSelectionCells();
+      boolean gridVisible = graph.isGridVisible();
+      graph.setGridVisible(false);
+      graph.clearSelection();
+
+      graph.paint(graphics);
+
+      graph.setSelectionCells(selection);
+      graph.setGridVisible(gridVisible);
+
+      return img;
+    }
+    return null;
+  }
+}

File src/designer/com/opensymphony/workflow/designer/actions/PNGExport.java

+package com.opensymphony.workflow.designer.actions;
+
+import java.awt.event.ActionEvent;
+import java.awt.image.BufferedImage;
+import java.awt.*;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import javax.swing.*;
+import javax.imageio.ImageIO;
+
+import com.opensymphony.workflow.designer.event.WorkspaceListener;
+import com.opensymphony.workflow.designer.event.WorkspaceEvent;
+import com.opensymphony.workflow.designer.*;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author Hani Suleiman (hani@formicary.net)
+ * Date: May 21, 2003
+ * Time: 5:51:14 PM
+ */
+public class PNGExport extends AbstractAction implements WorkspaceListener
+{
+  private static final Log log = LogFactory.getLog(PNGExport.class);
+
+  public PNGExport()
+  {
+    setEnabled(false);
+    putValue(NAME, "PNG export");
+    putValue(SHORT_DESCRIPTION, "Export as PNG");
+    putValue(LONG_DESCRIPTION, "Export currently displayed workflow as a PNG image");
+  }
+
+  public void actionPerformed(ActionEvent e)
+  {
+    File output = Utils.promptUserForFile((Component)e.getSource(), JFileChooser.FILES_AND_DIRECTORIES, true, ".png", "PNG files");
+    if(output==null) return;
+    WorkflowGraph graph = WorkflowDesigner.INSTANCE.getCurrentGraph();
+    BufferedImage image = Utils.toImage(graph);
+    try
+    {
+      ImageIO.write(image, "png", output);
+    }
+    catch(IOException e1)
+    {
+      log.error("Error writing png file", e1);
+    }
+  }
+
+  public void workspaceChanged(WorkspaceEvent event)
+  {
+    if(event.getId()==WorkspaceEvent.WORKSPACE_OPENED)
+    {
+      setEnabled(true);
+    }
+    else
+    {
+      setEnabled(false);
+    }
+  }
+}