use of de.johni0702.minecraft.gui.element.advanced.GuiDropdownMenu in project jGui by ReplayMod.
the class GuiFileChooserPopup method setFolder.
public void setFolder(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("Folder has to be a directory.");
}
try {
this.folder = folder = folder.getCanonicalFile();
} catch (IOException e) {
close();
throw new RuntimeException(e);
}
updateButton();
for (GuiElement element : new ArrayList<>(pathPanel.getElements().keySet())) {
pathPanel.removeElement(element);
}
for (GuiElement element : new ArrayList<>(fileList.getListPanel().getElements().keySet())) {
fileList.getListPanel().removeElement(element);
}
File[] files = folder.listFiles();
if (files != null) {
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
if (f1.isDirectory() && !f2.isDirectory()) {
return -1;
} else if (!f1.isDirectory() && f2.isDirectory()) {
return 1;
}
return f1.getName().compareToIgnoreCase(f2.getName());
}
});
for (final File file : files) {
if (file.isDirectory()) {
fileList.getListPanel().addElements(new VerticalLayout.Data(0), new GuiButton().onClick(new Runnable() {
@Override
public void run() {
setFolder(file);
}
}).setLabel(file.getName() + File.separator));
} else {
if (hasValidExtension(file.getName())) {
fileList.getListPanel().addElements(new VerticalLayout.Data(0), new GuiButton().onClick(new Runnable() {
@Override
public void run() {
setFileName(file.getName());
}
}).setLabel(file.getName()));
}
}
}
}
fileList.setOffsetY(0);
File[] roots = File.listRoots();
if (roots != null && roots.length > 1) {
// Windows can have multiple file system roots
// So we place a dropdown menu (skinned like a button) at the front of the path
final GuiDropdownMenu<File> dropdown = new GuiDropdownMenu<File>(pathPanel) {
private final GuiButton skin = new GuiButton();
@Override
protected ReadableDimension calcMinSize() {
ReadableDimension dim = super.calcMinSize();
return new Dimension(dim.getWidth() - 5 - MCVer.getFontRenderer().fontHeight, dim.getHeight());
}
@Override
public void layout(ReadableDimension size, RenderInfo renderInfo) {
super.layout(size, renderInfo);
if (renderInfo.layer == 0) {
skin.layout(size, renderInfo);
}
}
@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
super.draw(renderer, size, renderInfo);
if (renderInfo.layer == 0) {
skin.setLabel(getSelectedValue().toString());
skin.draw(renderer, size, renderInfo);
}
}
};
List<File> actualRoots = new ArrayList<>();
File selected = null;
for (File root : roots) {
// Windows apparently also has file system roots that aren't directories, so we'll have to filter those
if (root.isDirectory()) {
actualRoots.add(root);
if (folder.getAbsolutePath().startsWith(root.getAbsolutePath())) {
selected = root;
}
}
}
assert selected != null;
// First set values and current selection
dropdown.setValues(actualRoots.toArray(new File[actualRoots.size()])).setSelected(selected);
// then add selection handler afterwards
dropdown.onSelection(new Consumer<Integer>() {
@Override
public void consume(Integer old) {
setFolder(dropdown.getSelectedValue());
}
});
}
LinkedList<File> parents = new LinkedList<>();
while (folder != null) {
parents.addFirst(folder);
folder = folder.getParentFile();
}
for (final File parent : parents) {
pathPanel.addElements(null, new GuiButton().onClick(new Runnable() {
@Override
public void run() {
setFolder(parent);
}
}).setLabel(parent.getName() + File.separator));
}
pathScrollable.setOffsetX(Integer.MAX_VALUE);
}
Aggregations