Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created March 2, 2016 09:35
Show Gist options
  • Save codenameone/2877412809a8cff646af to your computer and use it in GitHub Desktop.
Save codenameone/2877412809a8cff646af to your computer and use it in GitHub Desktop.
This sample shows the entire Codename One FileSystemStorage hierachy as a tree that can be expaneded
Form hi = new Form("FileSystemTree", new BorderLayout());
TreeModel tm = new TreeModel() {
@Override
public Vector getChildren(Object parent) {
String[] files;
if(parent == null) {
files = FileSystemStorage.getInstance().getRoots();
return new Vector<Object>(Arrays.asList(files));
} else {
try {
files = FileSystemStorage.getInstance().listFiles((String)parent);
} catch(IOException err) {
Log.e(err);
files = new String[0];
}
}
String p = (String)parent;
Vector result = new Vector();
for(String s : files) {
result.add(p + s);
}
return result;
}
@Override
public boolean isLeaf(Object node) {
return !FileSystemStorage.getInstance().isDirectory((String)node);
}
};
Tree t = new Tree(tm) {
@Override
protected String childToDisplayLabel(Object child) {
String n = (String)child;
int pos = n.lastIndexOf("/");
if(pos < 0) {
return n;
}
return n.substring(pos);
}
};
hi.add(BorderLayout.CENTER, t);
hi.show();
@codenameone
Copy link
Author

Sample usage of FileSystemStorage & Tree.

From the Codename One project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment