001package polaris.sdk.ui.controls;
002import polaris.sdk.ui.*;
003
004import java.nio.file.*;
005import java.util.function.Consumer;
006public class FilePicker extends Button {
007 protected boolean directoriesOnly;private Path selected;private Consumer<Path> selection;
008 public FilePicker(){super("选择文件…");w=260;}public Path path(){return selected;}public FilePicker onSelect(Consumer<Path> c){selection=c;return this;}
009 public FilePicker path(Path path){selected=path;text=path==null?(directoriesOnly?"选择目录…":"选择文件…"):path.getFileName()==null?path.toString():path.getFileName().toString();return this;}
010 @Override public void activate(){if(ui!=null){Path start=selected==null?Paths.get(System.getProperty("user.home",".")):directoriesOnly?selected:selected.getParent();ui.show(new FileBrowser(directoriesOnly,start,p->{path(p);changed();if(selection!=null)selection.accept(p);}));}}
011}