|
34 | 34 | import java.nio.file.Path; |
35 | 35 | import java.nio.file.Paths; |
36 | 36 | import java.util.*; |
| 37 | +import java.util.List; |
37 | 38 | import java.util.jar.JarEntry; |
38 | 39 | import java.util.jar.JarFile; |
39 | 40 | import java.util.jar.JarInputStream; |
| 41 | +import java.util.stream.Collectors; |
40 | 42 |
|
41 | 43 | import org.apache.commons.cli.*; |
42 | 44 |
|
@@ -807,9 +809,50 @@ public void setLocation(int x, int y) { |
807 | 809 | * @param menu the menu to which the example files will be added |
808 | 810 | */ |
809 | 811 | void addAllExamples(JMenu menu) { |
810 | | - addDirectory(menu, "docs/examples"); |
| 812 | + try (InputStream in = getClass().getClassLoader().getResourceAsStream("examples.txt")) { |
| 813 | + if (in == null) { |
| 814 | + return; |
| 815 | + } |
| 816 | + |
| 817 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 818 | + List<String> paths = reader.lines().collect(Collectors.toList()); |
| 819 | + Map<String, JMenu> menuMap = new HashMap<>(); |
| 820 | + menuMap.put("", menu); // root |
| 821 | + |
| 822 | + for (String path : paths) { |
| 823 | + if (!path.endsWith(".gex")) continue; |
| 824 | + |
| 825 | + String[] parts = path.split("/"); |
| 826 | + String filename = parts[parts.length - 1]; |
| 827 | + String label = filename.substring(0, filename.length() - 4); |
| 828 | + |
| 829 | + // Create submenus if needed |
| 830 | + String currentPath = ""; |
| 831 | + JMenu parent = menu; |
| 832 | + for (int i = 0; i < parts.length - 1; i++) { |
| 833 | + currentPath = currentPath.isEmpty() ? parts[i] : currentPath + "/" + parts[i]; |
| 834 | + if (!menuMap.containsKey(currentPath)) { |
| 835 | + JMenu submenu = new JMenu(parts[i]); |
| 836 | + menuMap.put(currentPath, submenu); |
| 837 | + parent.add(submenu); |
| 838 | + } |
| 839 | + parent = menuMap.get(currentPath); |
| 840 | + } |
| 841 | + |
| 842 | + JMenuItem item = new JMenuItem(label); |
| 843 | + item.setActionCommand("example"); |
| 844 | + item.setName("docs/examples/" + path); // used for loading |
| 845 | + item.setToolTipText(path); |
| 846 | + item.addActionListener(this); |
| 847 | + parent.add(item); |
| 848 | + } |
| 849 | + |
| 850 | + } catch (IOException e) { |
| 851 | + e.printStackTrace(); |
| 852 | + } |
811 | 853 | } |
812 | 854 |
|
| 855 | + |
813 | 856 | /** |
814 | 857 | * Populate a JMenu by scanning a folder on the classpath. |
815 | 858 | * @param menu the menu to fill |
|
0 commit comments