|
| 1 | +package tudelft.ti2806.pl3.demo; |
| 2 | + |
| 3 | +import org.graphstream.graph.Graph; |
| 4 | + |
| 5 | +import tudelft.ti2806.pl3.data.graph.GraphData; |
| 6 | +import tudelft.ti2806.pl3.visualization.DisplayView; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileNotFoundException; |
| 10 | + |
| 11 | +import javax.swing.JFileChooser; |
| 12 | +import javax.swing.JFrame; |
| 13 | +import javax.swing.filechooser.FileFilter; |
| 14 | + |
| 15 | +public class Demo { |
| 16 | + /** |
| 17 | + * Demo for sprint 1, week 2. Demonstrating the parsing of the edge and node |
| 18 | + * files and displaying them with the GraphStream library. |
| 19 | + * |
| 20 | + * @param par |
| 21 | + * should be empty |
| 22 | + */ |
| 23 | + public static void main(String[] par) { |
| 24 | + JFrame frame = new JFrame(); |
| 25 | + File nodeFile = selectFile("Select node file", frame, ".node.graph"); |
| 26 | + File edgeFile = selectFile("Select edge file", frame, ".edge.graph"); |
| 27 | + |
| 28 | + GraphData graph = null; |
| 29 | + try { |
| 30 | + graph = GraphData.parseGraph(nodeFile, edgeFile); |
| 31 | + } catch (FileNotFoundException e) { |
| 32 | + e.printStackTrace(); |
| 33 | + } |
| 34 | + Graph displayGraph = DisplayView.getGraph("", graph.getNodes(), |
| 35 | + graph.getEdges()); |
| 36 | + displayGraph.display(); |
| 37 | + } |
| 38 | + |
| 39 | + private static File selectFile(String title, JFrame frame, String filter) { |
| 40 | + JFileChooser chooser = new JFileChooser(); |
| 41 | + chooser.setMultiSelectionEnabled(true); |
| 42 | + chooser.setDialogTitle(title); |
| 43 | + chooser.setFileFilter(new FileFilter() { |
| 44 | + @Override |
| 45 | + public boolean accept(File file) { |
| 46 | + if (file.isDirectory()) { |
| 47 | + return true; |
| 48 | + } else { |
| 49 | + String path = file.getAbsolutePath().toLowerCase(); |
| 50 | + if (path.endsWith(filter)) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDescription() { |
| 59 | + return filter; |
| 60 | + } |
| 61 | + }); |
| 62 | + int option = chooser.showOpenDialog(frame); |
| 63 | + if (option == JFileChooser.APPROVE_OPTION) { |
| 64 | + File[] sf = chooser.getSelectedFiles(); |
| 65 | + if (sf.length == 1) { |
| 66 | + return sf[0]; |
| 67 | + } |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | +} |
0 commit comments