View Javadoc
1   /*
2    * Copyright (c) 2003, The JUNG Authors
3    * All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    * 
8    */
9   package edu.uci.ics.jung.samples;
10  
11  import java.awt.BorderLayout;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.io.IOException;
15  
16  import javax.swing.JButton;
17  import javax.swing.JFrame;
18  import javax.swing.JPanel;
19  
20  import edu.uci.ics.jung.algorithms.layout.FRLayout;
21  import edu.uci.ics.jung.graph.Graph;
22  import edu.uci.ics.jung.graph.util.TestGraphs;
23  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
24  import edu.uci.ics.jung.visualization.VisualizationViewer;
25  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
26  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
27  import edu.uci.ics.jung.visualization.control.ScalingControl;
28  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
29  import edu.uci.ics.jung.visualization.layout.PersistentLayout;
30  import edu.uci.ics.jung.visualization.layout.PersistentLayoutImpl;
31  
32  
33  /**
34   * Demonstrates the use of <code>PersistentLayout</code>
35   * and <code>PersistentLayoutImpl</code>.
36   * 
37   * @author Tom Nelson
38   * 
39   */
40  public class PersistentLayoutDemo {
41  
42      /**
43       * the graph
44       */
45  	Graph<String, Number> graph = TestGraphs.getOneComponentGraph();
46  
47      /**
48       * the name of the file where the layout is saved
49       */
50      String fileName;
51  
52      /**
53       * the visual component and renderer for the graph
54       */
55      VisualizationViewer<String,Number> vv;
56      
57      PersistentLayout<String,Number> persistentLayout;
58  
59      /**
60       * create an instance of a simple graph with controls to
61       * demo the persistence and zoom features.
62       * 
63       * @param fileName where to save/restore the graph positions
64       */
65      public PersistentLayoutDemo(final String fileName) {
66          this.fileName = fileName;
67          
68          // create a simple graph for the demo
69          persistentLayout = 
70              new PersistentLayoutImpl<String,Number>(new FRLayout<String,Number>(graph));
71  
72          vv = new VisualizationViewer<String,Number>(persistentLayout);
73          
74          // add my listener for ToolTips
75          vv.setVertexToolTipTransformer(new ToStringLabeller());
76          DefaultModalGraphMouse<String,Number> gm = new DefaultModalGraphMouse<String,Number>();
77          vv.setGraphMouse(gm);
78          final ScalingControl scaler = new CrossoverScalingControl();
79          
80          vv.scaleToLayout(scaler);
81  
82          // create a frome to hold the graph
83          final JFrame frame = new JFrame();
84          frame.getContentPane().add(new GraphZoomScrollPane(vv));
85          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
86  
87          // create a control panel and buttons for demo
88          // functions
89          JPanel p = new JPanel();
90          
91          JButton persist = new JButton("Save Layout");
92          // saves the graph vertex positions to a file
93          persist.addActionListener(new ActionListener() {
94              public void actionPerformed(ActionEvent e) {
95                  try {
96                      persistentLayout.persist(fileName);
97                  } catch (IOException e1) {
98                      System.err.println("got "+e1);
99              	}
100             }
101         });
102         p.add(persist);
103 
104         JButton restore = new JButton("Restore Layout");
105         // restores the graph vertex positions from a file
106         // if new vertices were added since the last 'persist',
107         // they will be placed at random locations
108         restore.addActionListener(new ActionListener() {
109             public void actionPerformed(ActionEvent e) {
110 //                PersistentLayout<String,Number> pl = (PersistentLayout<String,Number>) vv.getGraphLayout();
111                 try {
112                     persistentLayout.restore(fileName);
113                 } catch (Exception e1) {
114                     e1.printStackTrace();
115                 }
116             }
117         });
118         p.add(restore);
119         p.add(gm.getModeComboBox());
120 
121         frame.getContentPane().add(p, BorderLayout.SOUTH);
122         frame.pack();//setSize(600, 600);
123         frame.setVisible(true);
124     }
125 
126     /**
127      * a driver for this demo
128      * @param args should hold the filename for the persistence demo
129      */
130     public static void main(String[] args) {
131         String filename;
132         if (args.length >= 1)
133             filename = args[0];
134         else
135             filename = "PersistentLayoutDemo.out";
136         new PersistentLayoutDemo(filename);
137     }
138 }
139