X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTList.java;h=12e0b8a33cce977e93ce4a6fd30080adeb75e8dc;hb=HEAD;hp=e069db7650ab8212e2517ce1f6888028c102d342;hpb=3649b9210ea425f398ba8c24f9509669cf72aa96;p=fanfix.git diff --git a/src/jexer/TList.java b/src/jexer/TList.java index e069db7..12e0b8a 100644 --- a/src/jexer/TList.java +++ b/src/jexer/TList.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) 2019 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,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import jexer.bits.CellAttributes; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import static jexer.TKeypress.*; @@ -41,7 +40,11 @@ import static jexer.TKeypress.*; /** * TList shows a list of strings, and lets the user select one. */ -public class TList extends TWidget { +public class TList extends TScrollableWidget { + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * The list of strings to display. @@ -53,175 +56,64 @@ public class TList extends TWidget { */ private int selectedString = -1; - /** - * Get the selection index. - * - * @return -1 if nothing is selected, otherwise the index into the list - */ - public final int getSelectedIndex() { - return selectedString; - } - - /** - * Set the selected string index. - * - * @param index -1 to unselect, otherwise the index into the list - */ - public final void setSelectedIndex(final int index) { - selectedString = index; - } - - /** - * Get the selected string. - * - * @return the selected string, or null of nothing is selected yet - */ - public final String getSelected() { - if ((selectedString >= 0) && (selectedString <= strings.size() - 1)) { - return strings.get(selectedString); - } - return null; - } - - /** - * Set the new list of strings to display. - * - * @param list new list of strings - */ - public final void setList(final List list) { - strings.clear(); - strings.addAll(list); - reflow(); - } - - /** - * Vertical scrollbar. - */ - private TVScroller vScroller; - - /** - * Get the vertical scrollbar. This is used by subclasses. - * - * @return the vertical scrollbar - */ - public final TVScroller getVScroller() { - return vScroller; - } - - /** - * Horizontal scrollbar. - */ - private THScroller hScroller; - - /** - * Get the horizontal scrollbar. This is used by subclasses. - * - * @return the horizontal scrollbar - */ - public final THScroller getHScroller() { - return hScroller; - } - /** * Maximum width of a single line. */ private int maxLineWidth; /** - * The action to perform when the user selects an item (clicks or enter). + * The action to perform when the user selects an item (double-clicks or + * enter). */ - private TAction enterAction = null; + protected TAction enterAction = null; /** - * The action to perform when the user navigates with keyboard. + * The action to perform when the user selects an item (single-click). */ - private TAction moveAction = null; + protected TAction singleClickAction = null; /** - * Perform user selection action. + * The action to perform when the user navigates with keyboard. */ - public void dispatchEnter() { - assert (selectedString >= 0); - assert (selectedString < strings.size()); - if (enterAction != null) { - enterAction.DO(); - } - } + protected TAction moveAction = null; - /** - * Perform list movement action. - */ - public void dispatchMove() { - assert (selectedString >= 0); - assert (selectedString < strings.size()); - if (moveAction != null) { - moveAction.DO(); - } - } + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * Resize for a new width/height. + * Public constructor. + * + * @param parent parent widget + * @param strings list of strings to show + * @param x column relative to parent + * @param y row relative to parent + * @param width width of text area + * @param height height of text area */ - public void reflow() { - - // Reset the lines - selectedString = -1; - maxLineWidth = 0; - - for (int i = 0; i < strings.size(); i++) { - String line = strings.get(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(strings.size() - getHeight() + 1); - vScroller.setTopValue(0); - vScroller.setValue(0); - if (vScroller.getBottomValue() < 0) { - vScroller.setBottomValue(0); - } - vScroller.setBigChange(getHeight() - 1); + public TList(final TWidget parent, final List strings, final int x, + final int y, final int width, final int height) { - // 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, strings, x, y, width, height, null, null, null); } /** * Public constructor. * * @param parent parent widget - * @param strings list of strings to show + * @param strings list of strings to show. This is allowed to be null + * and set later with setList() or by subclasses. * @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 enterAction action to perform when an item is selected */ public TList(final TWidget parent, final List strings, final int x, - final int y, final int width, final int height) { + final int y, final int width, final int height, + final TAction enterAction) { - this(parent, strings, x, y, width, height, null); + this(parent, strings, x, y, width, height, enterAction, null, null); } /** @@ -235,18 +127,15 @@ public class TList extends TWidget { * @param width width of text area * @param height height of text area * @param enterAction action to perform when an item is selected + * @param moveAction action to perform when the user navigates to a new + * item with arrow/page keys */ public TList(final TWidget parent, final List strings, final int x, final int y, final int width, final int height, - final TAction enterAction) { + final TAction enterAction, final TAction moveAction) { - super(parent, x, y, width, height); - this.enterAction = enterAction; - this.strings = new ArrayList(); - if (strings != null) { - this.strings.addAll(strings); - } - reflow(); + this(parent, strings, x, y, width, height, enterAction, moveAction, + null); } /** @@ -262,64 +151,32 @@ public class TList extends TWidget { * @param enterAction action to perform when an item is selected * @param moveAction action to perform when the user navigates to a new * item with arrow/page keys + * @param singleClickAction action to perform when the user clicks on an + * item */ public TList(final TWidget parent, final List strings, final int x, final int y, final int width, final int height, - final TAction enterAction, final TAction moveAction) { + final TAction enterAction, final TAction moveAction, + final TAction singleClickAction) { super(parent, x, y, width, height); this.enterAction = enterAction; this.moveAction = moveAction; + this.singleClickAction = singleClickAction; this.strings = new ArrayList(); if (strings != null) { this.strings.addAll(strings); } - reflow(); - } - - /** - * Draw the files list. - */ - @Override - public void draw() { - CellAttributes color = null; - int begin = vScroller.getValue(); - int topY = 0; - for (int i = begin; i < strings.size(); i++) { - String line = strings.get(i); - if (hScroller.getValue() < line.length()) { - line = line.substring(hScroller.getValue()); - } else { - line = ""; - } - if (i == selectedString) { - color = getTheme().getColor("tlist.selected"); - } else if (isAbsoluteActive()) { - color = getTheme().getColor("tlist"); - } else { - color = getTheme().getColor("tlist.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("tlist"); - } else { - color = getTheme().getColor("tlist.inactive"); - } - - // Pad the rest with blank lines - for (int i = topY; i < getHeight() - 1; i++) { - getScreen().hLineXY(0, i, getWidth() - 1, ' ', color); - } + hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); + vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); + reflowData(); } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Handle mouse press events. * @@ -328,20 +185,21 @@ public class TList extends TWidget { @Override public void onMouseDown(final TMouseEvent mouse) { if (mouse.isMouseWheelUp()) { - vScroller.decrement(); + verticalDecrement(); return; } if (mouse.isMouseWheelDown()) { - vScroller.increment(); + verticalIncrement(); return; } if ((mouse.getX() < getWidth() - 1) - && (mouse.getY() < getHeight() - 1)) { - if (vScroller.getValue() + mouse.getY() < strings.size()) { - selectedString = vScroller.getValue() + mouse.getY(); + && (mouse.getY() < getHeight() - 1) + ) { + if (getVerticalValue() + mouse.getY() < strings.size()) { + selectedString = getVerticalValue() + mouse.getY(); + dispatchSingleClick(); } - dispatchEnter(); return; } @@ -349,6 +207,27 @@ public class TList extends TWidget { super.onMouseDown(mouse); } + /** + * Handle mouse double click. + * + * @param mouse mouse double click event + */ + @Override + public void onMouseDoubleClick(final TMouseEvent mouse) { + if ((mouse.getX() < getWidth() - 1) + && (mouse.getY() < getHeight() - 1) + ) { + if (getVerticalValue() + mouse.getY() < strings.size()) { + selectedString = getVerticalValue() + mouse.getY(); + dispatchEnter(); + } + return; + } + + // Pass to children + super.onMouseDoubleClick(mouse); + } + /** * Handle keystrokes. * @@ -357,15 +236,15 @@ public class TList extends TWidget { @Override public void onKeypress(final TKeypressEvent keypress) { if (keypress.equals(kbLeft)) { - hScroller.decrement(); + horizontalDecrement(); } else if (keypress.equals(kbRight)) { - hScroller.increment(); + horizontalIncrement(); } else if (keypress.equals(kbUp)) { if (strings.size() > 0) { if (selectedString >= 0) { if (selectedString > 0) { - if (selectedString - vScroller.getValue() == 0) { - vScroller.decrement(); + if (selectedString - getVerticalValue() == 0) { + verticalDecrement(); } selectedString--; } @@ -381,8 +260,8 @@ public class TList extends TWidget { if (selectedString >= 0) { if (selectedString < strings.size() - 1) { selectedString++; - if (selectedString - vScroller.getValue() == getHeight() - 1) { - vScroller.increment(); + if (selectedString - getVerticalValue() == getHeight() - 1) { + verticalIncrement(); } } } else { @@ -393,7 +272,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbPgUp)) { - vScroller.bigDecrement(); + bigVerticalDecrement(); if (selectedString >= 0) { selectedString -= getHeight() - 1; if (selectedString < 0) { @@ -404,7 +283,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbPgDn)) { - vScroller.bigIncrement(); + bigVerticalIncrement(); if (selectedString >= 0) { selectedString += getHeight() - 1; if (selectedString > strings.size() - 1) { @@ -415,7 +294,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbHome)) { - vScroller.toTop(); + toTop(); if (strings.size() > 0) { selectedString = 0; } @@ -423,7 +302,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbEnd)) { - vScroller.toBottom(); + toBottom(); if (strings.size() > 0) { selectedString = strings.size() - 1; } @@ -444,4 +323,224 @@ public class TList extends TWidget { } } + // ------------------------------------------------------------------------ + // TScrollableWidget ------------------------------------------------------ + // ------------------------------------------------------------------------ + + /** + * Override TWidget's width: we need to set child widget widths. + * + * @param width new widget width + */ + @Override + public void setWidth(final int width) { + super.setWidth(width); + if (hScroller != null) { + hScroller.setWidth(getWidth() - 1); + } + if (vScroller != null) { + vScroller.setX(getWidth() - 1); + } + } + + /** + * Override TWidget's height: we need to set child widget heights. + * + * @param height new widget height + */ + @Override + public void setHeight(final int height) { + super.setHeight(height); + if (hScroller != null) { + hScroller.setY(getHeight() - 1); + } + if (vScroller != null) { + vScroller.setHeight(getHeight() - 1); + } + } + + /** + * Resize for a new width/height. + */ + @Override + public void reflowData() { + + // Reset the lines + selectedString = -1; + maxLineWidth = 0; + + for (int i = 0; i < strings.size(); i++) { + String line = strings.get(i); + int lineLength = StringUtils.width(line); + if (lineLength > maxLineWidth) { + maxLineWidth = lineLength; + } + } + + setBottomValue(strings.size() - getHeight() + 1); + if (getBottomValue() < 0) { + setBottomValue(0); + } + + setRightValue(maxLineWidth - getWidth() + 1); + if (getRightValue() < 0) { + setRightValue(0); + } + } + + /** + * Draw the list. + */ + @Override + public void draw() { + CellAttributes color = null; + int begin = getVerticalValue(); + int topY = 0; + for (int i = begin; i < strings.size(); i++) { + String line = strings.get(i); + if (line == null) { + line = ""; + } + if (getHorizontalValue() < line.length()) { + line = line.substring(getHorizontalValue()); + } else { + line = ""; + } + if (i == selectedString) { + if (isAbsoluteActive()) { + color = getTheme().getColor("tlist.selected"); + } else { + color = getTheme().getColor("tlist.selected.inactive"); + } + } else if (isAbsoluteActive()) { + color = getTheme().getColor("tlist"); + } else { + color = getTheme().getColor("tlist.inactive"); + } + String formatString = "%-" + Integer.toString(getWidth() - 1) + "s"; + putStringXY(0, topY, String.format(formatString, line), color); + topY++; + if (topY >= getHeight() - 1) { + break; + } + } + + if (isAbsoluteActive()) { + color = getTheme().getColor("tlist"); + } else { + color = getTheme().getColor("tlist.inactive"); + } + + // Pad the rest with blank lines + for (int i = topY; i < getHeight() - 1; i++) { + hLineXY(0, i, getWidth() - 1, ' ', color); + } + } + + // ------------------------------------------------------------------------ + // TList ------------------------------------------------------------------ + // ------------------------------------------------------------------------ + + /** + * Get the selection index. + * + * @return -1 if nothing is selected, otherwise the index into the list + */ + public final int getSelectedIndex() { + return selectedString; + } + + /** + * Set the selected string index. + * + * @param index -1 to unselect, otherwise the index into the list + */ + public final void setSelectedIndex(final int index) { + selectedString = index; + } + + /** + * Get a selectable string by index. + * + * @param idx index into list + * @return the string at idx in the list + */ + public final String getListItem(final int idx) { + return strings.get(idx); + } + + /** + * Get the selected string. + * + * @return the selected string, or null of nothing is selected yet + */ + public final String getSelected() { + if ((selectedString >= 0) && (selectedString <= strings.size() - 1)) { + return strings.get(selectedString); + } + return null; + } + + /** + * Get the maximum selection index value. + * + * @return -1 if the list is empty + */ + public final int getMaxSelectedIndex() { + return strings.size() - 1; + } + + /** + * Get a copy of the list of strings to display. + * + * @return the list of strings + */ + public final List getList() { + return new ArrayList(strings); + } + + /** + * Set the new list of strings to display. + * + * @param list new list of strings + */ + public final void setList(final List list) { + strings.clear(); + strings.addAll(list); + reflowData(); + } + + /** + * Perform user selection action. + */ + public void dispatchEnter() { + assert (selectedString >= 0); + assert (selectedString < strings.size()); + if (enterAction != null) { + enterAction.DO(this); + } + } + + /** + * Perform list movement action. + */ + public void dispatchMove() { + assert (selectedString >= 0); + assert (selectedString < strings.size()); + if (moveAction != null) { + moveAction.DO(this); + } + } + + /** + * Perform single-click action. + */ + public void dispatchSingleClick() { + assert (selectedString >= 0); + assert (selectedString < strings.size()); + if (singleClickAction != null) { + singleClickAction.DO(this); + } + } + }