X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTDirectoryList.java;h=71be2ab5d2ddaa3f97ef17488fa2a1d4b007ee0f;hb=e16dda65585466c8987bd1efd718431450a96605;hp=50cc88b7dfd969f4683cc5934e6e1b41914c0d37;hpb=329fd62e4acdaa8e9f4cccd518d47c0b07e79f51;p=nikiroo-utils.git diff --git a/src/jexer/TDirectoryList.java b/src/jexer/TDirectoryList.java index 50cc88b..71be2ab 100644 --- a/src/jexer/TDirectoryList.java +++ b/src/jexer/TDirectoryList.java @@ -1,29 +1,27 @@ /* * Jexer - Java Text User Interface * - * License: LGPLv3 or later + * The MIT License (MIT) * - * 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. + * Copyright (C) 2016 Kevin Lamonte * - * Copyright (C) 2015 Kevin Lamonte + * 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 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. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * 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. - * - * 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 @@ -34,26 +32,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 +54,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 +81,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 +100,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 +130,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); } }