1   /****************************************************************************
2    **
3    ** This file is part of the yFiles extension package yExport-1.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) 2007-2012 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.yext.export;
16  
17  
18  import javax.swing.JButton;
19  import javax.swing.JToolBar;
20  
21  import y.base.Node;
22  import y.io.LinkInfo;
23  import y.io.LinkMap;
24  import y.option.OptionHandler;
25  import y.view.EditMode;
26  import y.view.HitInfo;
27  import y.view.ViewMode;
28  
29  import yext.export.io.SWFOutputHandler;
30  
31  
32  /**
33   * Demonstrates how to export a graph to SWF format
34   * and how to add hyperlinks to the exported swf. 
35   */
36  public class SWFExportDemo extends AbstractExportDemo
37  {
38    private static final String SWF_URL = "swf_url";
39    private static final String SWF_TARGET = "swf_target";
40  
41    //Map used to store URLs for nodes, edges and labels.
42    LinkMap linkMap;
43  
44  
45    public SWFExportDemo() {
46      //add right click handler
47      view.addViewMode(new RightClickMode());
48    }
49  
50    /**
51     * Creates a toolbar for this demo.
52     */
53    protected JToolBar createToolBar() {
54      JToolBar jtb = super.createToolBar();
55      jtb.addSeparator();
56      // the link map will be used for storing hyperlinks for graph items
57      // and will be queried for hyperlinks by the output handler
58      linkMap = new LinkMap();
59      jtb.add(new JButton(new ExportAction("SWF Export", new SWFOutputHandler(linkMap))));
60      return jtb;
61    }
62  
63    /**
64     * Create an edit mode that displays the URL
65     * of a node as tool tip.
66     */
67    protected EditMode createEditMode()
68    {
69      EditMode em = new EditMode() {
70        public String getNodeTip(Node v) {
71          String url = null;
72          LinkInfo info = linkMap.get(v);
73          if( null != info ) {
74            url = info.getAttribute(SWF_URL);
75          }
76          if(url != null)
77          {
78            return "<html>URL: <b>" + url + "</b></html>";
79          }
80          else
81          {
82            return "<html>No URL assigned to node. <br><b>Right click on node</b></html>";
83          }
84        }
85      };
86      em.showNodeTips(true);
87      return em;
88    }
89  
90  
91    /**
92     * ViewMode responsible for processing a right mouse click.
93     * Right clicking on nodes, edges amd labels will bring up
94     * an option handler that allows to enter a URL for each item.
95     * Hyperlinks to these URLs will be integrated in the SVG output.
96     */
97    class RightClickMode extends ViewMode {
98      public void mouseReleasedRight(double x, double y)
99      {
100       HitInfo hitInfo = getGraph2D().getHitInfo(x,y, false);
101 
102       if(hitInfo.getHitNodeLabel() != null)
103       {
104         editLinkInfo(hitInfo.getHitNodeLabel(), "node label");
105       }
106       else if(hitInfo.getHitEdgeLabel() != null)
107       {
108         editLinkInfo(hitInfo.getHitEdgeLabel(), "edge label");
109       }
110       else if(hitInfo.getHitNode() != null)
111       {
112         editLinkInfo(hitInfo.getHitNode(), "node");
113       }
114       else if(hitInfo.getHitEdge() != null)
115       {
116         editLinkInfo(hitInfo.getHitEdge(), "edge");
117       }
118 
119     }
120 
121     void editLinkInfo(Object key, String type)
122     {
123       String url = null;
124       OptionHandler op = new OptionHandler("Hyperlink for " + type);
125       LinkInfo info = linkMap.get(key);
126       if( null != info ) {
127         url = info.getAttribute(SWF_URL);
128       }
129       if(url != null)
130       {
131         op.addString("Hyperlink URL",url);
132       }
133       else
134       {
135         op.addString("Hyperlink URL","");
136       }
137       if(op.showEditor(view.getFrame()))
138       {
139         url = op.getString("Hyperlink URL");
140         if(url.length() > 0)
141         {
142           LinkInfo newInfo = new LinkInfo();
143           newInfo.setAttribute(SWF_URL,url);
144           newInfo.setAttribute(SWF_TARGET,"_self");
145           linkMap.put(key, newInfo);
146         }
147         else
148         {
149           linkMap.remove(key);
150         }
151       }
152     }
153   }
154 
155   /**
156    * Launches this demo.
157    */
158   public static void main(String[] args) {
159     initLnF();
160     (new SWFExportDemo()).start();
161   }
162 }