1
2
3
4
5
6
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
35
36
37
38
39
40 public class PersistentLayoutDemo {
41
42
43
44
45 Graph<String, Number> graph = TestGraphs.getOneComponentGraph();
46
47
48
49
50 String fileName;
51
52
53
54
55 VisualizationViewer<String,Number> vv;
56
57 PersistentLayout<String,Number> persistentLayout;
58
59
60
61
62
63
64
65 public PersistentLayoutDemo(final String fileName) {
66 this.fileName = fileName;
67
68
69 persistentLayout =
70 new PersistentLayoutImpl<String,Number>(new FRLayout<String,Number>(graph));
71
72 vv = new VisualizationViewer<String,Number>(persistentLayout);
73
74
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
83 final JFrame frame = new JFrame();
84 frame.getContentPane().add(new GraphZoomScrollPane(vv));
85 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
86
87
88
89 JPanel p = new JPanel();
90
91 JButton persist = new JButton("Save Layout");
92
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
106
107
108 restore.addActionListener(new ActionListener() {
109 public void actionPerformed(ActionEvent e) {
110
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();
123 frame.setVisible(true);
124 }
125
126
127
128
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