diff options
Diffstat (limited to 'base/src/examples/java/bjc/utils')
| -rw-r--r-- | base/src/examples/java/bjc/utils/examples/gui/FilesystemBrowser.java | 112 | ||||
| -rw-r--r-- | base/src/examples/java/bjc/utils/examples/gui/package-info.java | 1 |
2 files changed, 113 insertions, 0 deletions
diff --git a/base/src/examples/java/bjc/utils/examples/gui/FilesystemBrowser.java b/base/src/examples/java/bjc/utils/examples/gui/FilesystemBrowser.java new file mode 100644 index 0000000..2a67a46 --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gui/FilesystemBrowser.java @@ -0,0 +1,112 @@ +package bjc.utils.examples.gui; + +import java.awt.BorderLayout; +import java.io.File; + +import javax.swing.JFrame; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +import bjc.utils.gui.browser.JBrowser; +import bjc.utils.gui.browser.JBrowserModel; + +/** + * Example of {@link JBrowser} that browses your home directory + */ +public class FilesystemBrowser { + static class FileSystemBrowserModel implements JBrowserModel { + private final File root; + + public FileSystemBrowserModel(File root) { + this.root = root; + } + + @Override + public Object getRoot() { + return root; + } + + @Override + public int getChildCount(Object parent) { + File f = (File) parent; + File[] kids = f.listFiles(); + return kids == null ? 0 : kids.length; + } + + @Override + public Object getChild(Object parent, int index) { + File f = (File) parent; + File[] kids = f.listFiles(); + if (kids == null || index < 0 || index >= kids.length) + throw new ArrayIndexOutOfBoundsException(index); + return kids[index]; + } + + @Override + public boolean isLeaf(Object node) { + return node instanceof File && ((File) node).isFile(); + } + + @Override + public String getText(Object node) { + File f = (File) node; + String n = f.getName(); + return (n == null || n.isEmpty()) ? f.getPath() : n; + } + + @Override + public boolean isExpandable(Object node) { + return node instanceof File && ((File) node).isDirectory(); + } + + @Override + public String getColumnHeader(Object parentNode, int depthFromRoot) { + File f = (File) parentNode; + return (depthFromRoot == 0) ? f.getPath() : getText(parentNode); + } + + @Override + public int indexOfChild(Object parent, Object child) { + File p = (File) parent, c = (File) child; + File[] kids = p.listFiles(); + if (kids == null) + return -1; + for (int i = 0; i < kids.length; i++) + if (kids[i].equals(c)) + return i; + return -1; + } + } + + // ---------- Manual demo ---------- + + /** + * Main method + * @param args Unused CLI args + */ + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception ignored) { + } + JFrame f = new JFrame("JBrowser Demo (File System)"); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + JBrowser browser = new JBrowser(new FileSystemBrowserModel(new File(System.getProperty("user.home")))); + + browser.setHeadersVisible(true); + browser.setFilteringEnabled(true); + browser.setReservedBlankColumns(3); // preallocate three headered blanks + browser.setColumnWidth(260); + + // Example: custom blank headers "Depth N" (1-based), star the first blank + browser.setBlankHeaderProvider((depth, ord, model) -> (ord == 0 ? "★ " : "") + "Depth " + (depth + 1)); + + browser.addSelectionListener(path -> System.out.println("Selection: " + (path == null ? "(none)" : path))); + f.add(browser, BorderLayout.CENTER); + f.setSize(1100, 540); + f.setLocationRelativeTo(null); + f.setVisible(true); + }); + } +} diff --git a/base/src/examples/java/bjc/utils/examples/gui/package-info.java b/base/src/examples/java/bjc/utils/examples/gui/package-info.java new file mode 100644 index 0000000..fad509b --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gui/package-info.java @@ -0,0 +1 @@ +package bjc.utils.examples.gui;
\ No newline at end of file |
