summaryrefslogtreecommitdiff
path: root/base/src/examples/java/bjc/utils
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2025-09-24 17:40:17 -0400
committerBenjamin Culkin <scorpress@gmail.com>2025-09-24 17:40:17 -0400
commit35cc451a5c53b23531a46eb2bff901cd63e4df09 (patch)
tree0bb1e1d17604aae16d34a5bc255a472a320966c4 /base/src/examples/java/bjc/utils
parent16ca6878d9ff246e7f742f1268482b8a51665892 (diff)
Add a JBrowser component + example
This adds a JBrowser Swing component that is intended to work similarly to how the NSBrowser component in Swing works
Diffstat (limited to 'base/src/examples/java/bjc/utils')
-rw-r--r--base/src/examples/java/bjc/utils/examples/gui/FilesystemBrowser.java112
-rw-r--r--base/src/examples/java/bjc/utils/examples/gui/package-info.java1
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