1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.view.application;
29  
30  import demo.view.DemoBase;
31  import y.option.OptionHandler;
32  import y.view.Graph2DPrinter;
33  import y.view.PrintPreviewPanel;
34  import y.view.GenericNodeRealizer;
35  import y.view.BevelNodePainter;
36  
37  import javax.swing.AbstractAction;
38  import javax.swing.Action;
39  import javax.swing.JButton;
40  import javax.swing.JDialog;
41  import javax.swing.JFrame;
42  import javax.swing.JToolBar;
43  import java.awt.Color;
44  import java.awt.Font;
45  import java.awt.EventQueue;
46  import java.awt.event.ActionEvent;
47  import java.awt.print.PageFormat;
48  import java.awt.print.PrinterJob;
49  import java.util.Locale;
50  import java.util.Map;
51  
52  /**
53   * Demo that centers around the printing facilities of yFiles.
54   * This class shows how to use the yFiles print preview and how to 
55   * add a title and footer to the printed page or poster.
56   *
57   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/printing" target="_blank">Section Printing a Graph's Visual Representation</a> in the yFiles for Java Developer's Guide
58   */
59  public class PrintPreviewDemo extends DemoBase {
60  
61    public PrintPreviewDemo() {
62      loadGraph("resource/PrintPreviewDemo.graphml");
63    }
64  
65    protected void configureDefaultRealizers() {
66      super.configureDefaultRealizers();
67      //register bevel node configuration that is used in initial graph
68      GenericNodeRealizer.Factory factory = GenericNodeRealizer.getFactory();
69      final Map map = factory.createDefaultConfigurationMap();
70      GenericNodeRealizer.Painter painter = new BevelNodePainter();
71      map.put(GenericNodeRealizer.Painter.class, painter);
72      map.put(GenericNodeRealizer.ContainsTest.class, painter);
73      factory.addConfiguration("BevelNodeConfig", map);
74    }
75  
76    protected JToolBar createToolBar() {
77      JToolBar bar = super.createToolBar();
78      bar.add(new PrintPreviewAction());
79      return bar;
80    }
81  
82    /**
83     * Action that brings up a customized print preview panel.
84     */
85    class PrintPreviewAction extends AbstractAction {
86      Graph2DPrintPreviewPanel ppp;
87  
88      PrintPreviewAction() {
89        super("Print Preview");
90  
91        PrinterJob printJob = PrinterJob.getPrinterJob();
92        ppp = new Graph2DPrintPreviewPanel(
93            printJob,
94            new Graph2DPrinter(view),
95            printJob.defaultPage());
96      }
97  
98      public void actionPerformed(ActionEvent e) {
99        final JDialog dialog = new JDialog((JFrame) view.getTopLevelAncestor(), contentPane.getName(), true);
100       dialog.setContentPane(ppp);
101       dialog.setResizable(true);
102       dialog.pack();
103       dialog.setVisible(true);
104     }
105 
106   }
107 
108   /**
109    * Extended print preview panel that incorporates the standard printing options
110    * provided by class {@link y.view.Graph2DView}.
111    */
112   public class Graph2DPrintPreviewPanel extends PrintPreviewPanel {
113     OptionHandler printOptions;
114     Graph2DPrinter gp;
115 
116 
117     public Graph2DPrintPreviewPanel(
118         PrinterJob printJob,
119         final Graph2DPrinter gp,
120         PageFormat pf) {
121       super(printJob,
122           gp,
123           gp.getPosterColumns(),
124           gp.getPosterColumns() * gp.getPosterRows(),
125           pf);
126       this.gp = gp;
127 
128       // the preview's Format and Print actions should execute in the
129       // current thread (the Swing event dispatch thread) because
130       // printing the currently displayed graph in a background thread
131       // might lead to errors (painting EdgeRealizer is not thread-safe)
132       setThreadingEnabled(false);
133 
134       //setup option handler
135       printOptions = new OptionHandler("Print Options");
136       printOptions.useSection("General");
137 
138       printOptions.addInt("Poster Rows", gp.getPosterRows());
139       printOptions.addInt("Poster Columns", gp.getPosterColumns());
140       printOptions.addBool("Add Poster Coords", gp.getPrintPosterCoords());
141       final String[] area = {"View", "Graph"};
142       if (gp.getClipType() == Graph2DPrinter.CLIP_GRAPH) {
143         printOptions.addEnum("Clip Area", area, 1);
144       } else {
145         printOptions.addEnum("Clip Area", area, 0);
146       }
147 
148       printOptions.addBool("Show Title/Footer On Every Page", false);
149 
150       Graph2DPrinter.DefaultTitleDrawable td = new Graph2DPrinter.DefaultTitleDrawable();
151       printOptions.useSection("Title");
152       printOptions.addString("Text", td.getText());
153       printOptions.addColor("Titlebar Color", td.getTitleBarColor(), true);
154       printOptions.addColor("Text Color", td.getTextColor(), true);
155       printOptions.addInt("Font Size", contentPane.getFont().getSize());
156 
157       Graph2DPrinter.DefaultFooterDrawable fd = new Graph2DPrinter.DefaultFooterDrawable();
158       printOptions.useSection("Footer");
159       printOptions.addString("Text", fd.getText());
160       printOptions.addColor("Footer Color", fd.getFooterColor(), true);
161       printOptions.addColor("Text Color", fd.getTextColor(), true);
162       printOptions.addInt("Font Size", contentPane.getFont().getSize());
163       
164       gp.setTextBarType(Graph2DPrinter.TITLE_AND_FOOTER_FOR_ENTIRE_POSTER);
165 
166       //show custom print dialog and adopt values
167       Action optionAction = new AbstractAction("Options...") {
168         public void actionPerformed(ActionEvent ev) {
169           if (!printOptions.showEditor()) {
170             return;
171           }
172           gp.setPosterRows(printOptions.getInt("Poster Rows"));
173           gp.setPosterColumns(printOptions.getInt("Poster Columns"));
174           gp.setPrintPosterCoords(
175               printOptions.getBool("Add Poster Coords"));
176           if ("Graph".equals(printOptions.get("Clip Area"))) {
177             gp.setClipType(Graph2DPrinter.CLIP_GRAPH);
178           } else {
179             gp.setClipType(Graph2DPrinter.CLIP_VIEW);
180           }
181 
182           if (printOptions.getBool("Show Title/Footer On Every Page")) {
183             Graph2DPrinter.RepeatingTitleDrawable title =
184                 new Graph2DPrinter.RepeatingTitleDrawable();
185             title.setText(printOptions.getString("Title", "Text"));
186             title.setTitleBarColor((Color) printOptions.get("Title", "Titlebar Color"));
187             title.setTextColor((Color) printOptions.get("Title", "Text Color"));
188             title.setFont(new Font("Dialog", Font.PLAIN, printOptions.getInt("Title", "Font Size")));
189             gp.setTitleDrawable(title);
190 
191             Graph2DPrinter.RepeatingFooterDrawable footer =
192                 new Graph2DPrinter.RepeatingFooterDrawable();
193             footer.setText(printOptions.getString("Footer", "Text"));
194             footer.setFooterColor((Color) printOptions.get("Footer", "Footer Color"));
195             footer.setTextColor((Color) printOptions.get("Footer", "Text Color"));
196             footer.setFont(new Font("Dialog", Font.PLAIN, printOptions.getInt("Footer", "Font Size")));
197             gp.setFooterDrawable(footer);
198 
199             gp.setTextBarType(Graph2DPrinter.TITLE_AND_FOOTER_FOR_EVERY_PAGE);
200           } else {
201             Graph2DPrinter.DefaultTitleDrawable title =
202                 new Graph2DPrinter.DefaultTitleDrawable();
203             title.setText(printOptions.getString("Title", "Text"));
204             title.setTitleBarColor((Color) printOptions.get("Title", "Titlebar Color"));
205             title.setTextColor((Color) printOptions.get("Title", "Text Color"));
206             title.setFont(new Font("Dialog", Font.PLAIN, printOptions.getInt("Title", "Font Size")));
207             gp.setTitleDrawable(title);
208 
209             Graph2DPrinter.DefaultFooterDrawable footer =
210                 new Graph2DPrinter.DefaultFooterDrawable();
211             footer.setText(printOptions.getString("Footer", "Text"));
212             footer.setFooterColor((Color) printOptions.get("Footer", "Footer Color"));
213             footer.setTextColor((Color) printOptions.get("Footer", "Text Color"));
214             footer.setFont(new Font("Dialog", Font.PLAIN, printOptions.getInt("Footer", "Font Size")));
215             gp.setFooterDrawable(footer);
216 
217             gp.setTextBarType(Graph2DPrinter.TITLE_AND_FOOTER_FOR_ENTIRE_POSTER);
218           }
219 
220           setPages(0,
221               gp.getPosterColumns(),
222               gp.getPosterColumns() * gp.getPosterRows());
223 
224           zoomToFit();
225         }
226       };
227       addControlComponent(new JButton(optionAction));
228 
229     }
230 
231   }
232 
233   public static void main(String[] args) {
234     EventQueue.invokeLater(new Runnable() {
235       public void run() {
236         Locale.setDefault(Locale.ENGLISH);
237         initLnF();
238         (new PrintPreviewDemo()).start();
239       }
240     });
241   }
242 
243 }
244 
245     
246 
247       
248