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