X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTList.java;h=644c5647afafa5cb7ce81d3f3a75b3faa3908dd1;hb=fe0770f988e64fc0ccafd3d3b086b4a0eb559d3b;hp=e069db7650ab8212e2517ce1f6888028c102d342;hpb=3649b9210ea425f398ba8c24f9509669cf72aa96;p=fanfix.git diff --git a/src/jexer/TList.java b/src/jexer/TList.java index e069db7..644c564 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) 2017 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 @@ -41,7 +39,7 @@ 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 { /** * The list of strings to display. @@ -84,42 +82,23 @@ public class TList extends TWidget { } /** - * Set the new list of strings to display. + * Get the maximum selection index value. * - * @param list new list of strings + * @return -1 if the list is empty */ - public final void setList(final List list) { - strings.clear(); - strings.addAll(list); - reflow(); + public final int getMaxSelectedIndex() { + return strings.size() - 1; } /** - * 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. + * Set the new list of strings to display. * - * @return the horizontal scrollbar + * @param list new list of strings */ - public final THScroller getHScroller() { - return hScroller; + public final void setList(final List list) { + strings.clear(); + strings.addAll(list); + reflowData(); } /** @@ -162,7 +141,8 @@ public class TList extends TWidget { /** * Resize for a new width/height. */ - public void reflow() { + @Override + public void reflowData() { // Reset the lines selectedString = -1; @@ -175,37 +155,15 @@ public class TList extends TWidget { } } - // 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); + setBottomValue(strings.size() - getHeight() + 1); + if (getBottomValue() < 0) { + 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); + setRightValue(maxLineWidth - getWidth() + 1); + if (getRightValue() < 0) { + setRightValue(0); } - hScroller.setBigChange(getWidth() - 1); } /** @@ -246,7 +204,10 @@ public class TList extends TWidget { if (strings != null) { this.strings.addAll(strings); } - reflow(); + + hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); + vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); + reflowData(); } /** @@ -274,7 +235,10 @@ public class TList extends TWidget { if (strings != null) { this.strings.addAll(strings); } - reflow(); + + hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); + vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); + reflowData(); } /** @@ -283,12 +247,12 @@ public class TList extends TWidget { @Override public void draw() { CellAttributes color = null; - int begin = vScroller.getValue(); + int begin = getVerticalValue(); 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()); + if (getHorizontalValue() < line.length()) { + line = line.substring(getHorizontalValue()); } else { line = ""; } @@ -328,20 +292,20 @@ 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(); + if (getVerticalValue() + mouse.getY() < strings.size()) { + selectedString = getVerticalValue() + mouse.getY(); + dispatchEnter(); } - dispatchEnter(); return; } @@ -357,15 +321,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 +345,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 +357,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 +368,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 +379,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbHome)) { - vScroller.toTop(); + toTop(); if (strings.size() > 0) { selectedString = 0; } @@ -423,7 +387,7 @@ public class TList extends TWidget { dispatchMove(); } } else if (keypress.equals(kbEnd)) { - vScroller.toBottom(); + toBottom(); if (strings.size() > 0) { selectedString = strings.size() - 1; }