Common Scrollable interface
[fanfix.git] / src / jexer / TList.java
index e069db7650ab8212e2517ce1f6888028c102d342..71600485fe2b9cb1646a8df8a3c0fa5c7af5755d 100644 (file)
@@ -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.
@@ -91,35 +89,7 @@ public class TList extends TWidget {
     public final void setList(final List<String> 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;
+        reflowData();
     }
 
     /**
@@ -162,7 +132,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 +146,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);
+        setBottomValue(strings.size() - getHeight() + 1);
+        if (getBottomValue() < 0) {
+            setBottomValue(0);
         }
-        vScroller.setBottomValue(strings.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);
+        setRightValue(maxLineWidth - getWidth() + 1);
+        if (getRightValue() < 0) {
+            setRightValue(0);
         }
-        hScroller.setBigChange(getWidth() - 1);
     }
 
     /**
@@ -246,7 +195,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 +226,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 +238,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 +283,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 +312,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 +336,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 +348,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 +359,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 +370,7 @@ public class TList extends TWidget {
                 dispatchMove();
             }
         } else if (keypress.equals(kbHome)) {
-            vScroller.toTop();
+            toTop();
             if (strings.size() > 0) {
                 selectedString = 0;
             }
@@ -423,7 +378,7 @@ public class TList extends TWidget {
                 dispatchMove();
             }
         } else if (keypress.equals(kbEnd)) {
-            vScroller.toBottom();
+            toBottom();
             if (strings.size() > 0) {
                 selectedString = strings.size() - 1;
             }