1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
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-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.io;
15  
16  import java.io.File;
17  import java.io.FileWriter;
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  
21  import y.base.Edge;
22  import y.base.Node;
23  import y.base.NodeList;
24  import y.io.GIFIOHandler;
25  import y.io.ImageMapOutputHandler;
26  import y.io.LinkInfo;
27  import y.io.LinkMap;
28  import y.view.Graph2D;
29  import y.view.Graph2DView;
30  
31  /**
32   * This class shows how to generate an image and a hyperlinked 
33   * HTML image map of a graph.
34   *
35   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/image_export.html#image_maps">Section Image Maps</a> in the yFiles for Java Developer's Guide
36   */
37  public class ImageMapDemo
38  {
39    
40    public ImageMapDemo(String imageFileName, String htmlFileName)
41    {
42      Graph2D tree = new Graph2D();
43      
44      LinkMap linkMap = new LinkMap();
45      
46      buildTreeFromData(tree, linkMap);
47      
48      //layout as a tree. Other tree layout algorithms can be found
49      //in package y.layout.tree.
50      y.layout.tree.TreeLayouter tLayouter = new y.layout.tree.TreeLayouter(); 
51      tLayouter.doLayout(tree); 
52      
53      //output to GIF. Other output handlers can be found in package y.io. 
54      GIFIOHandler gifIO = new GIFIOHandler();
55      Graph2DView view = gifIO.createDefaultGraph2DView(tree);
56      tree.setCurrentView(view);
57      
58      //use ImageMapOutputHandler to generate an html image map that matches
59      //the generated image.
60      ImageMapOutputHandler htmlIO = new ImageMapOutputHandler();
61      linkMap.setMapName("image");
62      htmlIO.setReferences(linkMap);
63      
64      try
65      {
66        File file = new File(imageFileName);
67        System.out.println("Writing GIF to " + file.getCanonicalPath());
68        gifIO.write(tree, imageFileName);
69      
70        file = new File(htmlFileName);
71        System.out.println("Writing HTML to " + file.getCanonicalPath());
72        
73        PrintWriter htmlOut = new PrintWriter(new FileWriter(htmlFileName));
74        String htmlMap = htmlIO.createHTMLString(tree);
75        
76        //create valid html page that can be displayed in a browser.
77        htmlOut.println(
78            "<html>\n<head></head>\n<body>" + 
79            htmlMap + "\n" +
80            "<img src=" + imageFileName + " usemap=\"#image\" border=\"0\">\n" + 
81            "</body></html>"); 
82        htmlOut.close();
83      }
84      catch(IOException ioex)
85      {
86        ioex.printStackTrace();
87      }
88    }
89    
90    /**
91     * Build a tree structure and provide link hyperlink information
92     * for some nodes.
93     */
94    void buildTreeFromData(Graph2D graph, LinkMap linkMap)
95    {
96      NodeList queue = new NodeList();
97      queue.add(graph.createNode(0,0, 100, 30, "Root"));
98      for(int i = 0; i < 10; i++)
99      {
100       Node root = queue.popNode();
101       LinkInfo link = new LinkInfo();
102       link.setAttribute(LinkInfo.HTML_REFERENCE, "http://www.yworks.com");
103       link.setAttribute(LinkInfo.HTML_ALT, "Visit yWorks");
104       link.setAttribute(LinkInfo.HTML_TITLE, "Visit yWorks");
105       linkMap.put(root, link);
106       Node c1 = graph.createNode(0,0, 80, 30, "c1_" + graph.N());
107       Edge e1 = graph.createEdge(root, c1);
108       Node c2 = graph.createNode(0,0, 60, 30, "c2_" + graph.N());
109       Edge e2 = graph.createEdge(root, c2);
110       
111       linkMap.put(e1, link);
112       linkMap.put(e2, link);
113 
114       queue.add(c2);
115       queue.add(c1);
116     }
117   }
118   
119   public static void main(String[] args)
120   {
121     ImageMapDemo demo = new ImageMapDemo("ImageMapDemo.gif","ImageMapDemo.html");
122   }
123  
124 }
125 
126     
127 
128       
129