PMD code sweep, #6 don't add MyWindow twice to MyApplication
[fanfix.git] / src / jexer / TList.java
index f157e220aca6508ac0f601a60c8c7faee54116a2..46c9307b95eba5cf9f68ed59d82ed4893ca7f1f2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -39,7 +39,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.
@@ -51,75 +55,6 @@ 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<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;
-    }
-
     /**
      * Maximum width of a single line.
      */
@@ -135,76 +70,9 @@ public class TList extends TWidget {
      */
     private TAction moveAction = null;
 
-    /**
-     * Perform user selection action.
-     */
-    public void dispatchEnter() {
-        assert (selectedString >= 0);
-        assert (selectedString < strings.size());
-        if (enterAction != null) {
-            enterAction.DO();
-        }
-    }
-
-    /**
-     * Perform list movement action.
-     */
-    public void dispatchMove() {
-        assert (selectedString >= 0);
-        assert (selectedString < strings.size());
-        if (moveAction != null) {
-            moveAction.DO();
-        }
-    }
-
-    /**
-     * Resize for a new width/height.
-     */
-    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);
-
-        // 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);
-    }
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Public constructor.
@@ -244,7 +112,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();
     }
 
     /**
@@ -272,52 +143,16 @@ public class TList extends TWidget {
         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.
      *
@@ -326,20 +161,19 @@ 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();
             return;
         }
 
@@ -347,6 +181,26 @@ 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.
      *
@@ -355,15 +209,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--;
                     }
@@ -379,8 +233,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 {
@@ -391,7 +245,7 @@ public class TList extends TWidget {
                 dispatchMove();
             }
         } else if (keypress.equals(kbPgUp)) {
-            vScroller.bigDecrement();
+            bigVerticalDecrement();
             if (selectedString >= 0) {
                 selectedString -= getHeight() - 1;
                 if (selectedString < 0) {
@@ -402,7 +256,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) {
@@ -413,7 +267,7 @@ public class TList extends TWidget {
                 dispatchMove();
             }
         } else if (keypress.equals(kbHome)) {
-            vScroller.toTop();
+            toTop();
             if (strings.size() > 0) {
                 selectedString = 0;
             }
@@ -421,7 +275,7 @@ public class TList extends TWidget {
                 dispatchMove();
             }
         } else if (keypress.equals(kbEnd)) {
-            vScroller.toBottom();
+            toBottom();
             if (strings.size() > 0) {
                 selectedString = strings.size() - 1;
             }
@@ -442,4 +296,159 @@ public class TList extends TWidget {
         }
     }
 
+    // ------------------------------------------------------------------------
+    // TScrollableWidget ------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * 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);
+            if (line.length() > maxLineWidth) {
+                maxLineWidth = line.length();
+            }
+        }
+
+        setBottomValue(strings.size() - getHeight() + 1);
+        if (getBottomValue() < 0) {
+            setBottomValue(0);
+        }
+
+        setRightValue(maxLineWidth - getWidth() + 1);
+        if (getRightValue() < 0) {
+            setRightValue(0);
+        }
+    }
+
+    /**
+     * Draw the files 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 (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";
+            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);
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // 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 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;
+    }
+
+    /**
+     * Set the new list of strings to display.
+     *
+     * @param list new list of strings
+     */
+    public final void setList(final List<String> 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();
+        }
+    }
+
+    /**
+     * Perform list movement action.
+     */
+    public void dispatchMove() {
+        assert (selectedString >= 0);
+        assert (selectedString < strings.size());
+        if (moveAction != null) {
+            moveAction.DO();
+        }
+    }
+
 }