1   /****************************************************************************
2    **
3    ** This file is part of the yFiles extension package GraphML-3.2-yFiles-2.7.
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-2009 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.yext.graphml;
16  
17  import java.awt.event.ActionEvent;
18  import java.io.IOException;
19  import java.io.File;
20  import java.net.URL;
21  import javax.swing.AbstractAction;
22  import javax.swing.Action;
23  import javax.swing.JFileChooser;
24  import javax.swing.JMenu;
25  import javax.swing.JMenuBar;
26  import javax.swing.filechooser.FileFilter;
27  
28  import y.io.GMLIOHandler;
29  import y.io.IOHandler;
30  import y.io.YGFIOHandler;
31  import y.util.D;
32  import y.view.EditMode;
33  import y.view.Graph2D;
34  import y.view.hierarchy.HierarchyManager;
35  import yext.graphml.graph2D.GraphMLIOHandler;
36  
37  /**
38   * This demo allows to load and save graphs in the GraphML file format. 
39   * Furthermore, it allows to load graphs in GML and YGF file format. 
40   * A small list of predefined GraphML files can also be accessed from the menu. 
41   */
42  public class GraphMLDemo extends ViewActionDemo
43  {
44    HierarchyManager hierarchy;
45    private GraphMLIOHandler ioHandler;
46  
47    private JFileChooser chooser;
48  
49    private File currentDir;
50  
51    /**
52     * Creates a new instance of GraphMLDemo 
53     */
54    public GraphMLDemo()
55    {
56      //to enabled import of hierarchically nested and grouped graphs
57      hierarchy = new HierarchyManager(view.getGraph2D());
58  
59      ioHandler = createGraphMLIOHandler();
60      createChooser();
61    }
62  
63    protected JMenuBar createMenuBar()
64    {
65      JMenuBar jtb = super.createMenuBar();
66      JMenu sampleGraphMenu = createSampleGraphMenu();
67      if(sampleGraphMenu != null) {
68        jtb.add(sampleGraphMenu);
69      }
70      return jtb;
71    }
72    
73    protected JMenu createSampleGraphMenu() {
74      String[] resources = {
75        "resources/ygraph/visual_features.graphml",
76        "resources/ygraph/problemsolving.graphml",
77        "resources/ygraph/funky.graphml",
78        "resources/ygraph/simple.graphml",
79        "resources/ygraph/grouping.graphml",
80      };
81      return createSampleGraphMenu(resources);
82    }
83  
84    JMenu createSampleGraphMenu( final String[] resources ) {
85      JMenu menu = new JMenu("Sample Graphs");
86      int resolvedResourceCount = 0;
87      for (int i = 0; i < resources.length; ++i) {
88        final URL resource = getClass().getResource(resources[i]);
89        if (resource != null) {
90          ++resolvedResourceCount;
91          menu.add(new LoadResourceAction(resource));
92        } else {
93          System.err.println("Could not resolve sample resource " + resources[i]);
94        }
95      }
96      if (resolvedResourceCount > 0) {
97        return menu;
98      } else {
99        return null;
100     }
101   }
102 
103   private void createChooser() {
104     if (chooser == null) {
105       chooser = new JFileChooser();
106 
107       chooser.addChoosableFileFilter(new FileFilter() {
108         public boolean accept(final File f) {
109           String s = f.getName().toLowerCase();
110           return f.isDirectory() || s.endsWith(ioHandler.getFileNameExtension());
111         }
112 
113         public String getDescription() {
114           return ioHandler.getFileFormatString();
115         }
116       });
117     }
118   }
119 
120   protected GraphMLIOHandler createGraphMLIOHandler()
121   {
122     return new GraphMLIOHandler();
123   }
124 
125   public GraphMLIOHandler getGraphMLIOHandler()
126   {
127     return ioHandler;
128   }
129 
130   protected Action createLoadAction()
131   {
132     return new LoadAction();
133   }
134 
135   protected Action createSaveAction()
136   {
137     return new SaveAction();
138   }
139   
140   /**
141    * Action that saves the current graph to a file in GraphML format.
142    */
143   class SaveAction extends AbstractAction
144   {
145     SaveAction()
146     {
147       super("Save...");
148     }
149     
150     public void actionPerformed(ActionEvent e)
151     {
152       if(currentDir != null) {
153         chooser.setCurrentDirectory(currentDir);
154       }
155 
156       if(chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION)
157       {
158         String name = chooser.getSelectedFile().toString();
159         currentDir = chooser.getCurrentDirectory();
160         if (!name.endsWith(".graphml"))  {
161           name = name + ".graphml";
162         }
163         IOHandler ioh = getGraphMLIOHandler();
164         try
165         {
166           ioh.write(view.getGraph2D(),name);
167         }
168         catch (IOException ioe)
169         {
170             D.show(ioe);
171         }
172       }
173     }
174   }
175  
176  /**
177    * Action that loads a graph from a specific file in GraphML format.
178    */
179   class LoadResourceAction extends AbstractAction
180   {
181     URL resource;
182     LoadResourceAction(URL resource)
183     {
184       putValue(AbstractAction.NAME,  urlToName(resource));
185       this.resource = resource;
186     }
187     
188     String urlToName(URL url)
189     {
190       String file = url.getFile();
191       return file.substring(file.lastIndexOf('/')+1);
192     }
193       
194     public void actionPerformed(ActionEvent ev)
195     {
196       try
197       {
198         Graph2D graph = view.getGraph2D();
199         graph.clear();
200         IOHandler ioh = getGraphMLIOHandler();
201         ioh.read(graph, resource);
202         view.fitContent();
203         graph.updateViews();
204       }
205       catch (IOException ioe)
206       {
207         D.show(ioe);
208       }
209     }
210   }
211 
212   /**
213    * Action that loads the current graph from a file in GraphML, YGF or GML format.
214    */
215   class LoadAction extends AbstractAction
216   {
217     LoadAction()
218     {
219       super("Load...");
220     }
221     
222     public void actionPerformed(ActionEvent e)
223     {
224       if(currentDir != null) {
225         chooser.setCurrentDirectory(currentDir);
226       }
227       if(chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION)
228       {
229         String name = chooser.getSelectedFile().toString();
230         currentDir = chooser.getCurrentDirectory();
231         IOHandler ioh = null;
232         if (name.endsWith(".gml")) {
233           ioh = new GMLIOHandler();
234         }
235         else if(name.endsWith(".ygf"))
236         {
237           ioh = new YGFIOHandler();
238         }
239         else //default is GraphML
240         {
241           if (!name.endsWith(".graphml")) {
242             name += ".graphml";
243           }
244           ioh = getGraphMLIOHandler();
245         }
246       
247         try
248         {
249           Graph2D graph = view.getGraph2D();
250           graph.clear();
251           ioh.read(graph, name);
252           view.fitContent();
253           graph.updateViews();
254         }
255         catch (IOException ioe)
256         {
257           D.show(ioe);
258         }
259       }
260     }
261   }
262 
263   /**
264    * Launches this demo.
265    */
266   public static void main(String[] args)
267   {
268     initLnF();
269     final GraphMLDemo demo = new GraphMLDemo();
270     demo.start();
271   }
272 }
273