X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fjexer%2FTDirectoryList.java;h=322ff5c4e5cb493f93cf356716117d2274156d26;hp=3a09bf38bc74dc1dda026f4089dde593fee94a32;hb=HEAD;hpb=0d47c5460c8e9d1198928308767a63ad35f46eb8 diff --git a/src/jexer/TDirectoryList.java b/src/jexer/TDirectoryList.java index 3a09bf3..322ff5c 100644 --- a/src/jexer/TDirectoryList.java +++ b/src/jexer/TDirectoryList.java @@ -1,29 +1,27 @@ -/** +/* * Jexer - Java Text User Interface * - * License: LGPLv3 or later - * - * This module is licensed under the GNU Lesser General Public License - * Version 3. Please see the file "COPYING" in this directory for more - * information about the GNU Lesser General Public License Version 3. + * The MIT License (MIT) * - * Copyright (C) 2015 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see - * http://www.gnu.org/licenses/, or write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * @author Kevin Lamonte [kevin.lamonte@gmail.com] * @version 1 @@ -32,139 +30,72 @@ package jexer; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; -import jexer.bits.CellAttributes; -import jexer.event.TKeypressEvent; -import jexer.event.TMouseEvent; -import static jexer.TKeypress.*; +import jexer.bits.StringUtils; /** * TDirectoryList shows the files within a directory. */ -public class TDirectoryList extends TWidget { +public class TDirectoryList extends TList { - /** - * Files in the directory. - */ - private List files; + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * Selected file. + * Files in the directory. */ - private int selectedFile = -1; + private Map files; /** * Root path containing files to display. */ - public File path; + private File path; /** - * Vertical scrollbar. + * The list of filters that a file must match in order to be displayed. */ - private TVScroller vScroller; + private List filters; - /** - * Horizontal scrollbar. - */ - private THScroller hScroller; - - /** - * Maximum width of a single line. - */ - private int maxLineWidth; + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * The action to perform when the user selects an item. + * Public constructor. + * + * @param parent parent widget + * @param path directory path, must be a directory + * @param x column relative to parent + * @param y row relative to parent + * @param width width of text area + * @param height height of text area */ - private TAction action = null; + public TDirectoryList(final TWidget parent, final String path, final int x, + final int y, final int width, final int height) { - /** - * Perform user selection action. - */ - public void dispatch() { - assert (selectedFile >= 0); - assert (selectedFile < files.size()); - if (action != null) { - action.DO(); - } + this(parent, path, x, y, width, height, null, null, null); } /** - * Format one of the entries for drawing on the screen. + * Public constructor. * - * @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. + * @param parent parent widget + * @param path directory path, must be a directory + * @param x column relative to parent + * @param y row relative to parent + * @param width width of text area + * @param height height of text area + * @param action action to perform when an item is selected (enter or + * double-click) */ - public void reflow() { - - // Reset the lines - selectedFile = -1; - maxLineWidth = 0; - files.clear(); - - // Build a list of files in this directory - File [] newFiles = path.listFiles(); - 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); + public TDirectoryList(final TWidget parent, final String path, final int x, + final int y, final int width, final int height, final TAction action) { - // 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); + this(parent, path, x, y, width, height, action, null, null); } /** @@ -176,11 +107,17 @@ public class TDirectoryList extends TWidget { * @param y row relative to parent * @param width width of text area * @param height height of text area + * @param action action to perform when an item is selected (enter or + * double-click) + * @param singleClickAction action to perform when an item is selected + * (single-click) */ public TDirectoryList(final TWidget parent, final String path, final int x, - final int y, final int width, final int height) { + final int y, final int width, final int height, final TAction action, + final TAction singleClickAction) { - this(parent, path, x, y, width, height, null); + this(parent, path, x, y, width, height, action, singleClickAction, + null); } /** @@ -192,122 +129,106 @@ public class TDirectoryList extends TWidget { * @param y row relative to parent * @param width width of text area * @param height height of text area - * @param action action to perform when an item is selected + * @param action action to perform when an item is selected (enter or + * double-click) + * @param singleClickAction action to perform when an item is selected + * (single-click) + * @param filters a list of strings that files must match to be displayed */ public TDirectoryList(final TWidget parent, final String path, final int x, - final int y, final int width, final int height, final TAction action) { + final int y, final int width, final int height, final TAction action, + final TAction singleClickAction, final List filters) { - this.path = new File(path); - this.action = action; - files = new ArrayList(); - reflow(); + super(parent, null, x, y, width, height, action); + files = new HashMap(); + this.filters = filters; + this.singleClickAction = singleClickAction; + + setPath(path); } - /** - * 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() - 1; 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; - } - } + // ------------------------------------------------------------------------ + // TList ------------------------------------------------------------------ + // ------------------------------------------------------------------------ - // Pad the rest with blank lines - for (int i = topY; i < getHeight() - 1; i++) { - getScreen().hLineXY(0, i, getWidth() - 1, ' ', color); - } - } + // ------------------------------------------------------------------------ + // TDirectoryList --------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * Handle mouse press events. + * Set the new path to display. * - * @param mouse mouse button press event + * @param path new path to list files for */ - @Override - public void onMouseDown(final TMouseEvent mouse) { - if (mouse.isMouseWheelUp()) { - vScroller.decrement(); - return; - } - if (mouse.isMouseWheelDown()) { - vScroller.increment(); - return; - } + public void setPath(final String path) { + this.path = new File(path); + + List newStrings = new ArrayList(); + files.clear(); - if ((mouse.getX() < getWidth() - 1) - && (mouse.getY() < getHeight() - 1)) { - if (vScroller.getValue() + mouse.getY() < files.size()) { - selectedFile = vScroller.getValue() + mouse.getY(); + // 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; + } + if (filters != null) { + for (String pattern: filters) { + + /* + System.err.println("newFiles[i] " + + newFiles[i].getName() + " " + pattern + + " " + newFiles[i].getName().matches(pattern)); + */ + + if (newFiles[i].getName().matches(pattern)) { + String key = renderFile(newFiles[i]); + files.put(key, newFiles[i]); + newStrings.add(key); + break; + } + } + } else { + String key = renderFile(newFiles[i]); + files.put(key, newFiles[i]); + newStrings.add(key); + } } - path = files.get(selectedFile); - dispatch(); - return; } + setList(newStrings); - // Pass to children - super.onMouseDown(mouse); + // Select the first entry + if (getMaxSelectedIndex() >= 0) { + setSelectedIndex(0); + } } /** - * Handle mouse release events. + * Get the path that is being displayed. * - * @param mouse mouse button release event + * @return the path */ - @Override - public void onMouseUp(final TMouseEvent mouse) { - // Pass to children - super.onMouseDown(mouse); + public File getPath() { + path = files.get(getSelected()); + return path; } /** - * Handle keystrokes. + * Format one of the entries for drawing on the screen. * - * @param keypress keystroke event + * @param file the File + * @return the line to draw */ - @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)) { - vScroller.decrement(); - } else if (keypress.equals(kbDown)) { - vScroller.increment(); - } else if (keypress.equals(kbPgUp)) { - vScroller.bigDecrement(); - } else if (keypress.equals(kbPgDn)) { - vScroller.bigIncrement(); - } else if (keypress.equals(kbHome)) { - vScroller.toTop(); - } else if (keypress.equals(kbEnd)) { - vScroller.toBottom(); - } else { - // Pass other keys (tab etc.) on - super.onKeypress(keypress); + private String renderFile(final File file) { + String name = file.getName(); + if (StringUtils.width(name) > 20) { + name = name.substring(0, 17) + "..."; } + return String.format("%-20s %5dk", name, (file.length() / 1024)); } }