X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTDirectoryList.java;h=a2c9fe3268d5fd3ab9937f9e95256857040acf46;hb=3649b9210ea425f398ba8c24f9509669cf72aa96;hp=50cc88b7dfd969f4683cc5934e6e1b41914c0d37;hpb=329fd62e4acdaa8e9f4cccd518d47c0b07e79f51;p=nikiroo-utils.git diff --git a/src/jexer/TDirectoryList.java b/src/jexer/TDirectoryList.java index 50cc88b..a2c9fe3 100644 --- a/src/jexer/TDirectoryList.java +++ b/src/jexer/TDirectoryList.java @@ -34,26 +34,16 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import jexer.bits.CellAttributes; -import jexer.event.TKeypressEvent; -import jexer.event.TMouseEvent; -import static jexer.TKeypress.*; - /** * TDirectoryList shows the files within a directory. */ -public final class TDirectoryList extends TWidget { +public final class TDirectoryList extends TList { /** * Files in the directory. */ private List files; - /** - * Selected file. - */ - private int selectedFile = -1; - /** * Root path containing files to display. */ @@ -66,7 +56,25 @@ public final class TDirectoryList extends TWidget { */ public void setPath(final String path) { this.path = new File(path); - reflow(); + + List newStrings = new ArrayList(); + files.clear(); + + // Build a list of files in this directory + File [] newFiles = this.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]); + newStrings.add(renderFile(files.size() - 1)); + } + } + setList(newStrings); } /** @@ -75,40 +83,10 @@ public final class TDirectoryList extends TWidget { * @return the path */ public File getPath() { + path = files.get(getSelectedIndex()); 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. * @@ -124,70 +102,6 @@ public final class TDirectoryList extends TWidget { 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); - } - /** * Public constructor. * @@ -218,159 +132,9 @@ public final class TDirectoryList extends TWidget { public TDirectoryList(final TWidget parent, final String path, final int x, final int y, final int width, final int height, final TAction action) { - super(parent, x, y, width, height); - this.path = new File(path); - this.action = action; + super(parent, null, x, y, width, height, action); files = new ArrayList(); - reflow(); - } - - /** - * Draw the files list. - */ - @Override - public void draw() { - CellAttributes color = null; - int begin = vScroller.getValue(); - int topY = 0; - for (int i = begin; i < files.size(); i++) { - String line = renderFile(i); - if (hScroller.getValue() < line.length()) { - line = line.substring(hScroller.getValue()); - } else { - line = ""; - } - if (i == selectedFile) { - color = getTheme().getColor("tdirectorylist.selected"); - } else if (isAbsoluteActive()) { - color = getTheme().getColor("tdirectorylist"); - } else { - color = getTheme().getColor("tdirectorylist.inactive"); - } - String formatString = "%-" + Integer.toString(getWidth() - 1) + "s"; - getScreen().putStringXY(0, topY, String.format(formatString, line), - color); - topY++; - if (topY >= getHeight() - 1) { - break; - } - } - - if (isAbsoluteActive()) { - color = getTheme().getColor("tdirectorylist"); - } else { - color = getTheme().getColor("tdirectorylist.inactive"); - } - - // Pad the rest with blank lines - for (int i = topY; i < getHeight() - 1; i++) { - getScreen().hLineXY(0, i, getWidth() - 1, ' ', color); - } - } - - /** - * Handle mouse press events. - * - * @param mouse mouse button press event - */ - @Override - public void onMouseDown(final TMouseEvent mouse) { - if (mouse.isMouseWheelUp()) { - vScroller.decrement(); - return; - } - if (mouse.isMouseWheelDown()) { - vScroller.increment(); - return; - } - - if ((mouse.getX() < getWidth() - 1) - && (mouse.getY() < getHeight() - 1)) { - if (vScroller.getValue() + mouse.getY() < files.size()) { - selectedFile = vScroller.getValue() + mouse.getY(); - } - path = files.get(selectedFile); - dispatch(); - return; - } - - // Pass to children - super.onMouseDown(mouse); - } - - /** - * Handle mouse release events. - * - * @param mouse mouse button release event - */ - @Override - public void onMouseUp(final TMouseEvent mouse) { - // Pass to children - super.onMouseDown(mouse); - } - - /** - * Handle keystrokes. - * - * @param keypress keystroke event - */ - @Override - public void onKeypress(final TKeypressEvent keypress) { - if (keypress.equals(kbLeft)) { - hScroller.decrement(); - } else if (keypress.equals(kbRight)) { - hScroller.increment(); - } else if (keypress.equals(kbUp)) { - if (files.size() > 0) { - if (selectedFile >= 0) { - if (selectedFile > 0) { - selectedFile--; - } - } else { - selectedFile = files.size() - 1; - } - path = files.get(selectedFile); - } - } else if (keypress.equals(kbDown)) { - if (files.size() > 0) { - if (selectedFile >= 0) { - if (selectedFile < files.size() - 1) { - selectedFile++; - } - } else { - selectedFile = 0; - } - path = files.get(selectedFile); - } - } else if (keypress.equals(kbPgUp)) { - vScroller.bigDecrement(); - } else if (keypress.equals(kbPgDn)) { - vScroller.bigIncrement(); - } else if (keypress.equals(kbHome)) { - vScroller.toTop(); - if (files.size() > 0) { - selectedFile = 0; - path = files.get(selectedFile); - } - } else if (keypress.equals(kbEnd)) { - vScroller.toBottom(); - if (files.size() > 0) { - selectedFile = files.size() - 1; - path = files.get(selectedFile); - } - } else if (keypress.equals(kbTab)) { - getParent().switchWidget(true); - } else if (keypress.equals(kbShiftTab) || keypress.equals(kbBackTab)) { - getParent().switchWidget(false); - } else if (keypress.equals(kbEnter)) { - if (selectedFile >= 0) { - path = files.get(selectedFile); - dispatch(); - } - } else { - // Pass other keys (tab etc.) on - super.onKeypress(keypress); - } + setPath(path); } }