Geometry-Based Edge Clustering
 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.JRadioButton;
14 import javax.swing.BoxLayout;
15 import javax.swing.JLabel;
16 
23 public class DrawPanel extends JPanel {
24 
25  private boolean bundlingActive;
26  private final ButtonGroup drawModeGroup = new ButtonGroup();
27  private MainWindow window;
28  private JFileChooser fc;
29  private JToggleButton tglbtnNode;
30  private JToggleButton tglbtnEdge;
31  private JToggleButton tglbtnDelete;
32  private JButton btnLoad;
33  private JButton btnSave;
34  private JToggleButton btnBundle;
35  private JToggleButton btnUnbundle;
36  private JSlider slider;
37  private final ButtonGroup bundleGroup = new ButtonGroup();
38  private JRadioButton rdbtnGraph;
39  private JRadioButton rdbtnMesh;
40  private final ButtonGroup graphMeshGroup = new ButtonGroup();
41  private JButton btnConnectAll;
42  private JButton btnClearAll;
43  private JButton btnAutoMesh;
44  private JButton btnClearMesh;
45  private JPanel panel;
46  private JLabel lblCellSize;
47 
51  public DrawPanel(MainWindow window) {
52  this.window = window;
53  fc = new JFileChooser();
54  fc.setFileFilter(new FileNameExtensionFilter("GraphDraw File", "gdf"));
55  fc.setCurrentDirectory(new File("./"));
56 
57  setLayout(new MigLayout("", "[100.00][100.00][77.00][]", "[grow][][][][][][-14.00][][][][][][]"));
58 
59  panel = new JPanel();
60  add(panel, "cell 0 0 1 3,alignx center,aligny center");
61  panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
62 
63  rdbtnGraph = new JRadioButton("Graph");
64  panel.add(rdbtnGraph);
65  rdbtnGraph.setSelected(true);
66  graphMeshGroup.add(rdbtnGraph);
67 
68  rdbtnMesh = new JRadioButton("Mesh");
69  panel.add(rdbtnMesh);
70  graphMeshGroup.add(rdbtnMesh);
71 
72  tglbtnNode = new JToggleButton("Node");
73  drawModeGroup.add(tglbtnNode);
74  add(tglbtnNode, "cell 1 0,growx");
75 
76  tglbtnEdge = new JToggleButton("Edge");
77  drawModeGroup.add(tglbtnEdge);
78  add(tglbtnEdge, "cell 1 1,growx");
79 
80  tglbtnDelete = new JToggleButton("Delete");
81  drawModeGroup.add(tglbtnDelete);
82  add(tglbtnDelete, "flowy,cell 1 2,growx");
83 
84  btnConnectAll = new JButton("Connect All");
85  add(btnConnectAll, "cell 0 3,growx");
86 
87  btnClearAll = new JButton("Clear All");
88  add(btnClearAll, "cell 1 3,growx");
89 
90  btnAutoMesh = new JButton("Auto Mesh");
91  btnAutoMesh.setEnabled(false);
92  add(btnAutoMesh, "cell 0 7,growx");
93 
94  btnClearMesh = new JButton("Clear Mesh");
95  btnClearMesh.setEnabled(false);
96  add(btnClearMesh, "cell 1 7,growx");
97 
98  lblCellSize = new JLabel("Cell Size:");
99  add(lblCellSize, "cell 0 5");
100 
101  slider = new JSlider();
102  slider.setPaintTicks(true);
103  slider.setPaintLabels(true);
104  slider.setMaximum(80);
105  slider.setMinimum(20);
106  slider.setMajorTickSpacing(10);
107  slider.setSnapToTicks(true);
108  slider.setValue(10);
109  add(slider, "cell 0 6 2 1,growx");
110 
111  btnLoad = new JButton("Load");
112  add(btnLoad, "cell 0 9,growx");
113 
114  btnSave = new JButton("Save");
115  add(btnSave, "cell 1 9,growx");
116 
117  btnBundle = new JToggleButton("Bundle");
118  bundleGroup.add(btnBundle);
119  add(btnBundle, "cell 0 10,growx");
120 
121  btnUnbundle = new JToggleButton("Unbundle");
122  btnUnbundle.setSelected(true);
123  bundlingActive = false;
124  bundleGroup.add(btnUnbundle);
125  add(btnUnbundle, "cell 1 10,growx");
126 
127  addListeners();
128  }
129 
134  public boolean isMeshSelected(){
135  return rdbtnMesh.isSelected();
136  }
137 
141  private void addListeners(){
142  rdbtnGraph.addActionListener(new ActionListener() {
143 
144  @Override
145  public void actionPerformed(ActionEvent e) {
146  if(tglbtnNode.isSelected())
147  window.setMouseMode(MouseMode.GRAPHNODE);
148  else if(tglbtnEdge.isSelected())
149  window.setMouseMode(MouseMode.GRAPHEDGE);
150  else if(tglbtnDelete.isSelected())
151  window.setMouseMode(MouseMode.GRAPHDELETE);
152  btnClearAll.setEnabled(true);
153  btnConnectAll.setEnabled(true);
154  btnAutoMesh.setEnabled(false);
155  btnClearMesh.setEnabled(false);
156  window.paintCanvas();
157  }
158  });
159 
160  rdbtnMesh.addActionListener(new ActionListener() {
161 
162  @Override
163  public void actionPerformed(ActionEvent e) {
164  if(tglbtnNode.isSelected())
165  window.setMouseMode(MouseMode.MESHNODE);
166  else if(tglbtnEdge.isSelected())
167  window.setMouseMode(MouseMode.MESHEDGE);
168  else if(tglbtnDelete.isSelected())
169  window.setMouseMode(MouseMode.MESHDELETE);
170  btnClearAll.setEnabled(false);
171  btnConnectAll.setEnabled(false);
172  btnAutoMesh.setEnabled(true);
173  btnClearMesh.setEnabled(true);
174  window.paintCanvas();
175  }
176  });
177 
178  tglbtnNode.addActionListener(new ActionListener() {
179 
180  @Override
181  public void actionPerformed(ActionEvent e) {
182  if(rdbtnMesh.isSelected())
183  window.setMouseMode(MouseMode.MESHNODE);
184  else
185  window.setMouseMode(MouseMode.GRAPHNODE);
186  }
187  });
188 
189  tglbtnEdge.addActionListener(new ActionListener() {
190 
191  @Override
192  public void actionPerformed(ActionEvent e) {
193  if(rdbtnMesh.isSelected())
194  window.setMouseMode(MouseMode.MESHEDGE);
195  else
196  window.setMouseMode(MouseMode.GRAPHEDGE);
197  }
198  });
199 
200  tglbtnDelete.addActionListener(new ActionListener() {
201 
202  @Override
203  public void actionPerformed(ActionEvent e) {
204  if(rdbtnMesh.isSelected())
205  window.setMouseMode(MouseMode.MESHDELETE);
206  else
207  window.setMouseMode(MouseMode.GRAPHDELETE);
208  }
209  });
210 
211  btnLoad.addActionListener(new ActionListener() {
212 
213  @Override
214  public void actionPerformed(ActionEvent e) {
215  int returnVal = fc.showOpenDialog(window.getFrame());
216  if (returnVal == JFileChooser.APPROVE_OPTION){
217  btnUnbundle.setSelected(true);
218  window.unbundleEdges();
219  bundlingActive = false;
220  window.loadGraph(fc.getSelectedFile());
221  }
222  }
223  });
224 
225  btnSave.addActionListener(new ActionListener() {
226 
227  @Override
228  public void actionPerformed(ActionEvent e) {
229  int returnVal = fc.showSaveDialog(window.getFrame());
230  if (returnVal == JFileChooser.APPROVE_OPTION)
231  window.saveGraph(fc.getSelectedFile());
232  }
233  });
234 
235  btnConnectAll.addActionListener(new ActionListener() {
236 
237  @Override
238  public void actionPerformed(ActionEvent e) {
239  window.unbundleEdges();
240  btnUnbundle.setSelected(true);
241  window.connectAll();
242  }
243  });
244 
245  btnClearAll.addActionListener(new ActionListener() {
246 
247  @Override
248  public void actionPerformed(ActionEvent e) {
249  window.unbundleEdges();
250  btnUnbundle.setSelected(true);
251  window.clearCanvas();
252  }
253  });
254 
255  btnAutoMesh.addActionListener(new ActionListener() {
256 
257  @Override
258  public void actionPerformed(ActionEvent e) {
259  window.unbundleEdges();
260  btnUnbundle.setSelected(true);
261  bundlingActive = false;
262  window.autoMesh(slider.getValue(), slider.getValue());
263  window.paintCanvas();
264  }
265  });
266 
267  btnClearMesh.addActionListener(new ActionListener() {
268 
269  @Override
270  public void actionPerformed(ActionEvent e) {
271  window.unbundleEdges();
272  btnUnbundle.setSelected(true);
273  bundlingActive = false;
274  window.clearMesh();
275  }
276  });
277 
278  btnBundle.addActionListener(new ActionListener() {
279 
280  @Override
281  public void actionPerformed(ActionEvent e) {
282  if(!bundlingActive){
283  tglbtnNode.setEnabled(false);
284  tglbtnEdge.setEnabled(false);
285  tglbtnDelete.setEnabled(false);
286  btnLoad.setEnabled(false);
287  btnSave.setEnabled(false);
288  btnBundle.setEnabled(false);
289  btnUnbundle.setEnabled(false);
290  slider.setEnabled(false);
291  btnConnectAll.setEnabled(false);
292  btnClearAll.setEnabled(false);
293  btnAutoMesh.setEnabled(false);
294  btnClearMesh.setEnabled(false);
295 
296  window.bundleEdges();
297  bundlingActive = true;
298 
299  tglbtnNode.setEnabled(true);
300  tglbtnEdge.setEnabled(true);
301  tglbtnDelete.setEnabled(true);
302  btnLoad.setEnabled(true);
303  btnSave.setEnabled(true);
304  btnBundle.setEnabled(true);
305  btnUnbundle.setEnabled(true);
306  slider.setEnabled(true);
307  if(rdbtnGraph.isSelected()){
308  btnConnectAll.setEnabled(true);
309  btnClearAll.setEnabled(true);
310  }
311  if(rdbtnMesh.isSelected()){
312  btnAutoMesh.setEnabled(true);
313  btnClearMesh.setEnabled(true);
314  }
315  }
316  }
317  });
318 
319  btnUnbundle.addActionListener(new ActionListener() {
320 
321  @Override
322  public void actionPerformed(ActionEvent e) {
323  if(bundlingActive){
324  window.unbundleEdges();
325  bundlingActive = false;
326  }
327  }
328  });
329  }
330 }