Force-Directed Edge Bundling
 All Classes Functions
DrawPanel.java
1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3 import java.io.File;
4 
5 import javax.swing.JFileChooser;
6 import javax.swing.JPanel;
7 import javax.swing.JToggleButton;
8 import javax.swing.JSlider;
9 import javax.swing.JButton;
10 import net.miginfocom.swing.MigLayout;
11 import javax.swing.ButtonGroup;
12 import javax.swing.filechooser.FileNameExtensionFilter;
13 import javax.swing.JProgressBar;
14 import javax.swing.BoxLayout;
15 import java.awt.Component;
16 import javax.swing.JLabel;
17 import javax.swing.JCheckBox;
18 
25 public class DrawPanel extends JPanel {
26 
27  private boolean bundlingActive;
28  private final ButtonGroup buttonGroup = new ButtonGroup();
29  private MainWindow window;
30  private JFileChooser fc;
31  private JToggleButton tglbtnNode;
32  private JToggleButton tglbtnEdge;
33  private JToggleButton tglbtnDelete;
34  private JButton btnLoad;
35  private JButton btnSave;
36  private JButton btnClearAll;
37  private JButton btnConnectAll;
38  private JToggleButton btnBundle;
39  private JToggleButton btnUnbundle;
40  private JSlider slider;
41  private final ButtonGroup buttonGroup_1 = new ButtonGroup();
42  private JProgressBar progressBar;
43  private JLabel lblStiffness;
44  private JCheckBox chckbxAnimation;
45 
49  public DrawPanel(MainWindow window) {
50  this.window = window;
51  fc = new JFileChooser();
52  fc.setFileFilter(new FileNameExtensionFilter("GraphDraw File", "gdf"));
53  fc.setCurrentDirectory(new File("./"));
54 
55  setLayout(new MigLayout("", "[90.00][]", "[][][][][][][][]"));
56 
57  tglbtnNode = new JToggleButton("Node");
58  buttonGroup.add(tglbtnNode);
59  add(tglbtnNode, "cell 0 0,growx");
60 
61  btnConnectAll = new JButton("Connect All");
62  add(btnConnectAll, "cell 1 0,growx");
63  btnConnectAll.setAlignmentX(Component.CENTER_ALIGNMENT);
64 
65  tglbtnEdge = new JToggleButton("Edge");
66  buttonGroup.add(tglbtnEdge);
67  add(tglbtnEdge, "cell 0 1,growx");
68 
69  btnClearAll = new JButton("Clear All");
70  add(btnClearAll, "cell 1 1,growx");
71  btnClearAll.setAlignmentX(Component.CENTER_ALIGNMENT);
72 
73  tglbtnDelete = new JToggleButton("Delete");
74  buttonGroup.add(tglbtnDelete);
75  add(tglbtnDelete, "flowy,cell 0 2,growx");
76  bundlingActive = false;
77 
78  chckbxAnimation = new JCheckBox("Animation");
79  add(chckbxAnimation, "cell 1 2,alignx center");
80 
81  lblStiffness = new JLabel("Stiffness:");
82  add(lblStiffness, "cell 0 3");
83 
84  slider = new JSlider();
85  slider.setPaintLabels(true);
86  slider.setPaintTicks(true);
87  slider.setMaximum(10);
88  slider.setMinimum(1);
89  slider.setMajorTickSpacing(1);
90  slider.setSnapToTicks(true);
91  slider.setValue(1);
92  add(slider, "cell 0 4 2 1,growx");
93 
94  btnLoad = new JButton("Load");
95  add(btnLoad, "cell 0 5,growx");
96 
97  btnSave = new JButton("Save");
98  add(btnSave, "cell 1 5,growx");
99 
100  btnBundle = new JToggleButton("Bundle");
101  buttonGroup_1.add(btnBundle);
102  add(btnBundle, "cell 0 6,growx");
103 
104  btnUnbundle = new JToggleButton("Unbundle");
105  btnUnbundle.setSelected(true);
106  buttonGroup_1.add(btnUnbundle);
107  add(btnUnbundle, "cell 1 6,growx");
108 
109  progressBar = new JProgressBar();
110  add(progressBar, "cell 0 7 2 1,growx");
111 
112  addListeners();
113  }
114 
119  public void updateProgress(int progress){
120  progressBar.setValue(progress);
121  progressBar.update(progressBar.getGraphics());
122  }
123 
127  private void addListeners(){
128  tglbtnNode.addActionListener(new ActionListener() {
129 
130  @Override
131  public void actionPerformed(ActionEvent e) {
132  window.setMouseMode(MouseMode.NODE);
133  }
134  });
135 
136  tglbtnEdge.addActionListener(new ActionListener() {
137 
138  @Override
139  public void actionPerformed(ActionEvent e) {
140  window.setMouseMode(MouseMode.EDGE);
141  }
142  });
143 
144  tglbtnDelete.addActionListener(new ActionListener() {
145 
146  @Override
147  public void actionPerformed(ActionEvent e) {
148  window.setMouseMode(MouseMode.DELETE);
149  }
150  });
151 
152  btnLoad.addActionListener(new ActionListener() {
153 
154  @Override
155  public void actionPerformed(ActionEvent e) {
156  int returnVal = fc.showOpenDialog(window.getFrame());
157  if (returnVal == JFileChooser.APPROVE_OPTION){
158  btnUnbundle.setSelected(true);
159  tglbtnNode.setEnabled(true);
160  tglbtnEdge.setEnabled(true);
161  tglbtnDelete.setEnabled(true);
162  tglbtnNode.setSelected(false);
163  tglbtnEdge.setSelected(false);
164  tglbtnDelete.setSelected(false);
165  btnConnectAll.setEnabled(true);
166  btnClearAll.setEnabled(true);
167  chckbxAnimation.setEnabled(true);
168  window.unbundleEdges();
169  bundlingActive = false;
170  window.loadGraph(fc.getSelectedFile());
171  }
172  }
173  });
174 
175  btnSave.addActionListener(new ActionListener() {
176 
177  @Override
178  public void actionPerformed(ActionEvent e) {
179  int returnVal = fc.showSaveDialog(window.getFrame());
180  if (returnVal == JFileChooser.APPROVE_OPTION)
181  window.saveGraph(fc.getSelectedFile());
182  }
183  });
184 
185  btnConnectAll.addActionListener(new ActionListener() {
186 
187  @Override
188  public void actionPerformed(ActionEvent e) {
189  window.connectAll();
190  }
191  });
192 
193  btnClearAll.addActionListener(new ActionListener() {
194 
195  @Override
196  public void actionPerformed(ActionEvent e) {
197  window.clearCanvas();
198  }
199  });
200 
201  btnBundle.addActionListener(new ActionListener() {
202 
203  @Override
204  public void actionPerformed(ActionEvent e) {
205  if(!bundlingActive){
206  window.setMouseMode(MouseMode.CURSOR);
207  tglbtnNode.setEnabled(false);
208  tglbtnEdge.setEnabled(false);
209  tglbtnDelete.setEnabled(false);
210  btnConnectAll.setEnabled(false);
211  btnClearAll.setEnabled(false);
212  chckbxAnimation.setEnabled(false);
213  btnLoad.setEnabled(false);
214  btnSave.setEnabled(false);
215  btnBundle.setEnabled(false);
216  btnUnbundle.setEnabled(false);
217  slider.setEnabled(false);
218  update(getGraphics());
219 
220  window.bundleEdges(slider.getValue(), chckbxAnimation.isSelected());
221  bundlingActive = true;
222 
223  btnLoad.setEnabled(true);
224  btnSave.setEnabled(true);
225  btnBundle.setEnabled(true);
226  btnUnbundle.setEnabled(true);
227  slider.setEnabled(true);
228  }
229  }
230  });
231 
232  btnUnbundle.addActionListener(new ActionListener() {
233 
234  @Override
235  public void actionPerformed(ActionEvent e) {
236  if(bundlingActive){
237  window.unbundleEdges();
238  bundlingActive = false;
239  tglbtnNode.setEnabled(true);
240  tglbtnEdge.setEnabled(true);
241  tglbtnDelete.setEnabled(true);
242  btnConnectAll.setEnabled(true);
243  btnClearAll.setEnabled(true);
244  chckbxAnimation.setEnabled(true);
245  }
246  }
247  });
248  }
249 }