@Override
public void install(Component component) {
super.install(component);
final FileBrowser fileBrowser = (FileBrowser)component;
Resources resources;
try {
resources = new Resources(getClass().getName());
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
WTKXSerializer wtkxSerializer = new WTKXSerializer(resources);
try {
content = (Component)wtkxSerializer.readObject(this, "terra_file_browser_skin.wtkx");
} catch (IOException exception) {
throw new RuntimeException(exception);
} catch (SerializationException exception) {
throw new RuntimeException(exception);
}
fileBrowser.add(content);
wtkxSerializer.bind(this, TerraFileBrowserSkin.class);
driveListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {
@Override
public void selectedIndexChanged(ListButton listButton, int previousSelectedIndex) {
if (previousSelectedIndex != -1) {
File drive = (File)listButton.getSelectedItem();
fileBrowser.setRootDirectory(drive);
}
}
});
pathListButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener() {
@Override
public void selectedIndexChanged(ListButton listButton, int previousSelectedIndex) {
File ancestorDirectory = (File)listButton.getSelectedItem();
if (ancestorDirectory != null) {
fileBrowser.setRootDirectory(ancestorDirectory);
}
}
});
goUpButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
File rootDirectory = fileBrowser.getRootDirectory();
File parentDirectory = rootDirectory.getParentFile();
fileBrowser.setRootDirectory(parentDirectory);
}
});
newFolderButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
// TODO
}
});
goHomeButton.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
fileBrowser.setRootDirectory(new File(System.getProperty("user.home")));
}
});
searchTextInput.getComponentKeyListeners().add(new ComponentKeyListener.Adapter() {
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = super.keyPressed(component, keyCode, keyLocation);
if (keyCode == Keyboard.KeyCode.ESCAPE) {
searchTextInput.setText("");
consumed = true;
} else if (keyCode == Keyboard.KeyCode.DOWN) {
if (files.getLength() > 0) {
fileTableView.setSelectedIndex(0);
fileTableView.requestFocus();
}
}
return consumed;
}
});
searchTextInput.getTextInputTextListeners().add(new TextInputTextListener() {
@Override
public void textChanged(TextInput textInput) {
String text = textInput.getText().trim();
if (text.length() == 0) {
text = null;
}
Filter<File> excludeFileFilter = null;
if (hideDisabledFiles) {
excludeFileFilter = fileBrowser.getDisabledFileFilter();
}
files.setFilter(new IncludeFileFilter(text, excludeFileFilter));
}
});
fileTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {
@Override
public void selectedRangeAdded(TableView tableView, int rangeStart, int rangeEnd) {
updatingSelection = true;
for (int i = rangeStart; i <= rangeEnd; i++) {
File file = files.get(i);
fileBrowser.addSelectedFile(file);
}
updatingSelection = false;
}
@Override
public void selectedRangeRemoved(TableView tableView, int rangeStart, int rangeEnd) {
updatingSelection = true;
for (int i = rangeStart; i <= rangeEnd; i++) {
File file = files.get(i);
fileBrowser.removeSelectedFile(file);
}
updatingSelection = false;
}
@Override
@SuppressWarnings("unchecked")
public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
updatingSelection = true;
Sequence<File> files = (Sequence<File>)tableView.getSelectedRows();
for (int i = 0, n = files.getLength(); i < n; i++) {
File file = files.get(i);
files.update(i, file);
}
fileBrowser.setSelectedFiles(files);
updatingSelection = false;
}
});
fileTableView.getTableViewSortListeners().add(new TableViewSortListener.Adapter() {
@Override
@SuppressWarnings("unchecked")
public void sortChanged(TableView tableView) {
TableView.SortDictionary sort = tableView.getSort();
if (!sort.isEmpty()) {
Dictionary.Pair<String, SortDirection> pair = tableView.getSort().get(0);
List<File> files = (List<File>)tableView.getTableData();
files.setComparator(new FileComparator(pair.key, pair.value));
}
}
});
fileTableView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
private int index = -1;
@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
boolean consumed = super.mouseClick(component, button, x, y, count);
if (count == 1) {
index = fileTableView.getRowAt(y);
} else if (count == 2) {
int index = fileTableView.getRowAt(y);
if (index != -1
&& index == this.index
&& fileTableView.isRowSelected(index)) {
File file = files.get(index);
if (file.isDirectory()) {
fileBrowser.setRootDirectory(file);
consumed = true;
}
}
}
return consumed;
}
});
files.getListListeners().add(new ListListener.Adapter<File>() {
@Override
public void comparatorChanged(List<File> list, Comparator<File> previousComparator) {
fileBrowser.clearSelection();
}
});
files.getFilteredListListeners().add(new FilteredListListener.Adapter<File>() {
@Override
public void filterChanged(FilteredList<File> filteredList,
Filter<File> previousFilter) {
fileBrowser.clearSelection();
}
});
fileTableView.setTableData(files);
fileBrowser.setFocusTraversalPolicy(new IndexFocusTraversalPolicy() {
@Override
public Component getNextComponent(Container container, Component component,
Direction direction) {
Component nextComponent;
if (component == null) {