1   /****************************************************************************
2    **
3    ** This file is part of the yFiles extension package ySVG-2.1.
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2007 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.yext.svg;
16  
17  import y.io.SuffixFileFilter;
18  import y.option.ConstraintManager;
19  import y.option.OptionGroup;
20  import y.option.OptionHandler;
21  import y.util.D;
22  import y.view.DefaultBackgroundRenderer;
23  import y.view.Graph2D;
24  import y.view.Graph2DView;
25  import yext.svg.io.SVGIOHandler;
26  import yext.svg.io.SVGZIOHandler;
27  
28  import java.awt.Point;
29  import java.awt.Rectangle;
30  import java.awt.event.ActionEvent;
31  import java.io.IOException;
32  import javax.swing.AbstractAction;
33  import javax.swing.Action;
34  import javax.swing.ImageIcon;
35  import javax.swing.JFileChooser;
36  import javax.swing.JToolBar;
37  
38  /** 
39   *  Demonstrates how to export a graph to SVG or SVGZ. 
40   */
41  public class SVGExportDemo extends ViewActionDemo
42  {  
43    /**
44     * Creates a toolbar for this demo.
45     */
46    protected JToolBar createToolBar()
47    {
48      JToolBar jtb = super.createToolBar();
49      jtb.addSeparator();
50      jtb.add(new ExportAction(createSVGIOHandler(false), createSVGIOHandler(true)));
51      return jtb;
52    }
53    
54    /**
55     * Creates and configures the SVGIOHandlers to be used.
56     */
57    protected SVGIOHandler createSVGIOHandler(boolean svgz)
58    {
59      return svgz ? new SVGZIOHandler() : new SVGIOHandler();
60    }
61      
62    /**
63     * Action that exports the given graph to SVG format
64     */
65    class ExportAction extends AbstractAction
66    {
67      SVGIOHandler svg;
68      SVGIOHandler svgz;
69      OptionHandler options;
70  
71      ExportAction( SVGIOHandler svg, SVGIOHandler svgz ) {
72        final String name = "SVG Export";
73        putValue(Action.NAME, name);
74        putValue(Action.SMALL_ICON, new ImageIcon(SVGExportDemo.this.getClass().getResource("resource/Export16.gif")));
75        putValue(Action.SHORT_DESCRIPTION, name);
76  
77        this.svg  = svg;
78        this.svgz = svgz;
79  
80        options = new OptionHandler(name);
81        final OptionGroup group = new OptionGroup();
82        group.setAttribute(OptionGroup.ATTRIBUTE_TITLE, name + " Options");
83        group.addItem(options.addBool("Compressed Output (SVGZ)", false));
84        group.addItem(options.addEnum("Size", new String[]{"Use Original Size", "Use Custom Width", "Use Custom Height"}, 0));
85        group.addItem(options.addInt("Custom Width",  500));
86        group.addItem(options.addInt("Custom Height", 500));
87        group.addItem(options.addInt("Empty Border Width", 10));
88        group.addItem(options.addEnum("Clip Region", new String[]{"Graph","View"}, 0));
89        group.addItem(options.addBool("Transparent Background", true));
90  
91        final ConstraintManager cm = new ConstraintManager(options);
92        cm.setEnabledOnValueEquals("Size", "Use Custom Width", "Custom Width");
93        cm.setEnabledOnValueEquals("Size", "Use Custom Height", "Custom Height");
94      }
95  
96  
97      void configureViewPort(Graph2DView viewPort)
98      {
99        Graph2D graph = view.getGraph2D();
100       
101       double width  = options.getInt("Custom Width");
102       double height = options.getInt("Custom Height");
103       Point viewPoint = viewPort.getViewPoint();
104       double zoom = 1.0;
105       
106       if ("Graph".equals(options.get("Clip Region")))
107       {       
108         Rectangle box = graph.getBoundingBox();
109         int border = options.getInt("Empty Border Width");
110         box.width  += 2*border;
111         box.height += 2*border;
112         box.x -= border;
113         box.y -= border;
114         
115         if ("Use Custom Height".equals(options.get("Size")))
116         {
117           width = height*box.getWidth()/box.getHeight();
118         }
119         else if ("Use Custom Width".equals(options.get("Size")))
120         {
121           height = width*box.getHeight()/box.getWidth();
122         }
123         else
124         {
125           width =  box.getWidth();
126           height = box.getHeight();
127         }
128         zoom = width/box.getWidth();
129         viewPoint = new Point(box.x, box.y);
130       }
131       else if ("View".equals(options.get("Clip Region")))
132       {
133         if ("Use Custom Height".equals(options.get("Size")))
134         {
135           width = height*view.getWidth()/view.getHeight();
136         }
137         else if ("Use Custom Width".equals(options.get("Size")))
138         {
139           height = width*view.getHeight()/view.getWidth();
140         }
141         else
142         {
143           width =  view.getWidth();
144           height = view.getHeight();
145         }
146         viewPoint = view.getViewPoint();
147         zoom = view.getZoom()*width/view.getWidth();
148       }
149       
150       viewPort.setZoom(zoom);
151       viewPort.setSize((int)width, (int)height);
152       viewPort.setViewPoint(viewPoint.x, viewPoint.y);
153     }
154     
155     public void actionPerformed(ActionEvent e)
156     {
157       if (!options.showEditor()) {
158         return;
159       }
160       
161       SVGIOHandler ioh = options.getBool("Compressed Output (SVGZ)") ? svgz : svg;
162       Graph2D graph = view.getGraph2D();
163       Graph2DView viewPort = ioh.createDefaultGraph2DView(graph);
164       configureViewPort(viewPort);
165       if (options.getBool("Transparent Background")) {
166         DefaultBackgroundRenderer dbr = new DefaultBackgroundRenderer(viewPort);
167         dbr.setColor(null);
168         viewPort.setBackgroundRenderer(dbr);
169       }
170       graph.setCurrentView(viewPort);
171       
172       export(ioh);
173       
174       graph.removeView(viewPort); //unregister viewport as view 
175     }
176     
177     void export(SVGIOHandler ioh)
178     {
179       JFileChooser chooser = new JFileChooser();
180       chooser.setAcceptAllFileFilterUsed(false);
181       String fne = ioh.getFileNameExtension(); 
182       chooser.setFileFilter(new SuffixFileFilter(
183         fne, ioh.getFileFormatString()));
184       
185       if (chooser.showSaveDialog(SVGExportDemo.this) == JFileChooser.APPROVE_OPTION)
186       {
187         String name = chooser.getSelectedFile().toString();
188         if (!name.endsWith(fne)) {
189           name = name + '.' + fne;
190         }
191         
192         try {
193           double pdt = view.getPaintDetailThreshold();
194           view.setPaintDetailThreshold(0.0);
195           ioh.write( view.getGraph2D(), name );
196           view.setPaintDetailThreshold(pdt);
197         }
198         catch(IOException ex) 
199         {
200           D.show(ex);
201         }
202       }
203     }
204   }
205   
206   /**
207    * Launches this demo.
208    */
209   public static void main(String[] args)
210   {
211     initLnF();
212 
213     (new SVGExportDemo()).start();
214   }
215 }