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.util.D;
18  import yext.graphml.graph2D.GraphMLIOHandler;
19  import yext.svg.io.SVGNodeRealizerSerializer;
20  
21  import java.awt.event.ActionEvent;
22  import java.io.IOException;
23  import java.io.File;
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.JFileChooser;
27  import javax.swing.filechooser.FileFilter;
28  
29  
30  /**
31   * Demonstrates how to register {@link SVGNodeRealizerSerializer} to enable
32   * serialization of graphs that use {@link yext.svg.view.SVGNodeRealizer}.
33   *
34   */
35  public class GraphMLDemo extends SVGNodeRealizerDemo {
36  
37    /**
38     * Factory method that creates an instance of <code>GraphMLIOHandler</code>
39     * which is configured to support serialization of graphs that use
40     * {@link yext.svg.view.SVGNodeRealizer}.
41     *
42     * @return an instance of <code>GraphMLIOHandler</code>
43     * which is configured to support serialization of graphs that use
44     * {@link yext.svg.view.SVGNodeRealizer}.
45     */
46    protected GraphMLIOHandler createIOHandler() {
47      final GraphMLIOHandler ioh = new GraphMLIOHandler();
48  
49      // register a custom serializer to support SVG nodes
50      ioh.getRealizerSerializerManager()
51         .addNodeRealizerSerializer(new SVGNodeRealizerSerializer());
52  
53      return ioh;
54    }
55  
56    /**
57     * Overwritten to allow deserialization from GraphML format only.
58     */
59    protected Action createLoadAction() {
60      return new GraphMLLoadAction();
61    }
62  
63    /**
64     * Overwritten to allow serialization to GraphML format only.
65     */
66    protected Action createSaveAction() {
67      return new GraphMLSaveAction();
68    }
69  
70  
71    public static void main( final String[] args ) {
72      initLnF();
73  
74      (new GraphMLDemo()).start();
75    }
76  
77  
78    /*
79     * #####################################################################
80     * inner types
81     * #####################################################################
82     */
83  
84    /**
85     * Action that loads the current graph from a file in GraphML format.
86     */
87    private class GraphMLLoadAction extends AbstractAction {
88      GraphMLIOHandler ioh;
89      JFileChooser chooser;
90  
91      public GraphMLLoadAction() {
92        super("Load...");
93        ioh = null;
94        chooser = null;
95      }
96  
97      public void actionPerformed(ActionEvent e) {
98        if (ioh == null) {
99          ioh = createIOHandler();
100       }
101       if (chooser == null) {
102         chooser = new JFileChooser();
103         chooser.addChoosableFileFilter(new FileFilter() {
104           public boolean accept( final File f ) {
105             return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
106           }
107 
108           public String getDescription() {
109             return "GraphML (*.graphml)";
110           }
111         });
112       }
113       if (chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
114         String name = chooser.getSelectedFile().toString();
115         if (!name.endsWith(".graphml")){
116           name = name + ".graphml";
117         }
118         try {
119           view.getGraph2D().clear();
120           ioh.read(view.getGraph2D(), name);
121         } catch (IOException ioe) {
122           D.show(ioe);
123         }
124         //force redisplay of view contents
125         view.fitContent();
126         view.getGraph2D().updateViews();
127       }
128     }
129   }
130 
131   /**
132    * Action that saves the current graph to a file in GraphML format.
133    */
134   private class GraphMLSaveAction extends AbstractAction {
135     GraphMLIOHandler ioh;
136     JFileChooser chooser;
137 
138     public GraphMLSaveAction() {
139       super("Save...");
140       ioh = null;
141       chooser = null;
142     }
143 
144     public void actionPerformed( final ActionEvent e ) {
145       if (ioh == null) {
146          ioh = createIOHandler();
147       }
148       if (chooser == null) {
149         chooser = new JFileChooser();
150         chooser.addChoosableFileFilter(new FileFilter() {
151           public boolean accept( final File f ) {
152             return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
153           }
154 
155           public String getDescription() {
156             return "GraphML (*.graphml)";
157           }
158         });
159       }
160       if (chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
161         String name = chooser.getSelectedFile().toString();
162         if (!name.endsWith(".graphml")) {
163           name = name + ".graphml";
164         }
165         try {
166           ioh.write(view.getGraph2D(), name);
167         } catch (IOException ioe) {
168           D.show(ioe);
169         }
170       }
171     }
172   }
173 }
174