1   /****************************************************************************
2    **
3    ** This file is part of the yFiles extension package ySVG-2.3.
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) 2002-2010 by yWorks GmbH, Vor dem Kreuzberg 28,
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.yext.svg;
15  
16  import yext.svg.io.SVGNodeRealizerSerializer;
17  
18  import y.io.GraphMLIOHandler;
19  import y.util.D;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.Action;
23  import javax.swing.JFileChooser;
24  import javax.swing.filechooser.FileFilter;
25  import java.awt.EventQueue;
26  import java.awt.event.ActionEvent;
27  import java.io.File;
28  import java.io.IOException;
29  
30  
31  /**
32   * Demonstrates how to register {@link SVGNodeRealizerSerializer} to enable
33   * serialization of graphs that use {@link yext.svg.view.SVGNodeRealizer}.
34   *
35   */
36  public class GraphMLDemo extends SVGNodeRealizerDemo {
37  
38    /**
39     * Factory method that creates an instance of <code>GraphMLIOHandler</code>
40     * which is configured to support serialization of graphs that use
41     * {@link yext.svg.view.SVGNodeRealizer}.
42     *
43     * @return an instance of <code>GraphMLIOHandler</code>
44     * which is configured to support serialization of graphs that use
45     * {@link yext.svg.view.SVGNodeRealizer}.
46     */
47    protected GraphMLIOHandler createIOHandler() {
48      final GraphMLIOHandler ioh = new GraphMLIOHandler();
49  
50      // register a custom serializer to support SVG nodes
51      ioh.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    /**
72     * Launches this demo.
73     * @param args ignored.
74     */
75    public static void main( String[] args ) {
76      EventQueue.invokeLater(new Runnable() {
77        public void run() {
78          initLnF();
79          (new GraphMLDemo()).start();
80        }
81      });
82    }
83  
84  
85    /*
86     * #####################################################################
87     * inner types
88     * #####################################################################
89     */
90  
91    /**
92     * Action that loads the current graph from a file in GraphML format.
93     */
94    private class GraphMLLoadAction extends AbstractAction {
95      GraphMLIOHandler ioh;
96      JFileChooser chooser;
97  
98      public GraphMLLoadAction() {
99        super("Load...");
100       ioh = null;
101       chooser = null;
102     }
103 
104     public void actionPerformed(ActionEvent e) {
105       if (ioh == null) {
106         ioh = createIOHandler();
107       }
108       if (chooser == null) {
109         chooser = new JFileChooser();
110         chooser.addChoosableFileFilter(new FileFilter() {
111           public boolean accept(final File f) {
112             return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
113           }
114 
115           public String getDescription() {
116             return "GraphML (*.graphml)";
117           }
118         });
119       }
120       if (chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
121         String name = chooser.getSelectedFile().toString();
122         if (!name.endsWith(".graphml")) {
123           name = name + ".graphml";
124         }
125         try {
126           view.getGraph2D().clear();
127           ioh.read(view.getGraph2D(), name);
128         } catch (IOException ioe) {
129           D.show(ioe);
130         }
131         //force redisplay of view contents
132         view.fitContent();
133         view.getGraph2D().updateViews();
134       }
135     }
136   }
137 
138   /**
139    * Action that saves the current graph to a file in GraphML format.
140    */
141   private class GraphMLSaveAction extends AbstractAction {
142     GraphMLIOHandler ioh;
143     JFileChooser chooser;
144 
145     public GraphMLSaveAction() {
146       super("Save...");
147       ioh = null;
148       chooser = null;
149     }
150 
151     public void actionPerformed(final ActionEvent e) {
152       if (ioh == null) {
153         ioh = createIOHandler();
154       }
155       if (chooser == null) {
156         chooser = new JFileChooser();
157         chooser.addChoosableFileFilter(new FileFilter() {
158           public boolean accept(final File f) {
159             return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
160           }
161 
162           public String getDescription() {
163             return "GraphML (*.graphml)";
164           }
165         });
166       }
167       if (chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
168         String name = chooser.getSelectedFile().toString();
169         if (!name.endsWith(".graphml")) {
170           name = name + ".graphml";
171         }
172         try {
173           ioh.write(view.getGraph2D(), name);
174         } catch (IOException ioe) {
175           D.show(ioe);
176         }
177       }
178     }
179   }
180 }
181