- /**
- * Set the new path to display.
- *
- * @param path new path to list files for
- */
- public void setPath(String path) {
- this.path = new File(path);
- reflow();
- }
-
- /**
- * Get the path that is being displayed.
- *
- * @return the path
- */
- public File getPath() {
- return path;
- }
-
- /**
- * Vertical scrollbar.
- */
- private TVScroller vScroller;
-
- /**
- * Horizontal scrollbar.
- */
- private THScroller hScroller;
-
- /**
- * Maximum width of a single line.
- */
- private int maxLineWidth;
-
- /**
- * The action to perform when the user selects an item.
- */
- private TAction action = null;
-
- /**
- * Perform user selection action.
- */
- public void dispatch() {
- assert (selectedFile >= 0);
- assert (selectedFile < files.size());
- if (action != null) {
- action.DO();
- }
- }
-
- /**
- * Format one of the entries for drawing on the screen.
- *
- * @param index index into files
- * @return the line to draw
- */
- private String renderFile(int index) {
- File file = files.get(index);
- String name = file.getName();
- if (name.length() > 20) {
- name = name.substring(0, 17) + "...";
- }
- return String.format("%-20s %5dk", name, (file.length() / 1024));
- }
-
- /**
- * Resize for a new width/height.
- */
- public void reflow() {
-
- // Reset the lines
- selectedFile = -1;
- maxLineWidth = 0;
- files.clear();
-
- // Build a list of files in this directory
- File [] newFiles = path.listFiles();
- if (newFiles != null) {
- for (int i = 0; i < newFiles.length; i++) {
- if (newFiles[i].getName().startsWith(".")) {
- continue;
- }
- if (newFiles[i].isDirectory()) {
- continue;
- }
- files.add(newFiles[i]);
- }
- }
-
- for (int i = 0; i < files.size(); i++) {
- String line = renderFile(i);
- if (line.length() > maxLineWidth) {
- maxLineWidth = line.length();
- }
- }
-
- // Start at the top
- if (vScroller == null) {
- vScroller = new TVScroller(this, getWidth() - 1, 0,
- getHeight() - 1);
- } else {
- vScroller.setX(getWidth() - 1);
- vScroller.setHeight(getHeight() - 1);
- }
- vScroller.setBottomValue(files.size() - getHeight() - 1);
- vScroller.setTopValue(0);
- vScroller.setValue(0);
- if (vScroller.getBottomValue() < 0) {
- vScroller.setBottomValue(0);
- }
- vScroller.setBigChange(getHeight() - 1);
-
- // Start at the left
- if (hScroller == null) {
- hScroller = new THScroller(this, 0, getHeight() - 1,
- getWidth() - 1);
- } else {
- hScroller.setY(getHeight() - 1);
- hScroller.setWidth(getWidth() - 1);
- }
- hScroller.setRightValue(maxLineWidth - getWidth() + 1);
- hScroller.setLeftValue(0);
- hScroller.setValue(0);
- if (hScroller.getRightValue() < 0) {
- hScroller.setRightValue(0);
- }
- hScroller.setBigChange(getWidth() - 1);
- }