Prep for 2019 release
[fanfix.git] / src / jexer / TList.java
index 644c5647afafa5cb7ce81d3f3a75b3faa3908dd1..da60af18745737dbc5ed5ecaf7d3a9a08730f430 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -29,6 +29,7 @@
 package jexer;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import jexer.bits.CellAttributes;
@@ -41,6 +42,10 @@ import static jexer.TKeypress.*;
  */
 public class TList extends TScrollableWidget {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The list of strings to display.
      */
@@ -51,120 +56,30 @@ public class TList extends TScrollableWidget {
      */
     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;
-    }
-
-    /**
-     * 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();
-    }
-
     /**
      * 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();
-        }
-    }
-
-    /**
-     * 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);
-        }
-    }
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Public constructor.
@@ -241,48 +156,9 @@ public class TList extends TScrollableWidget {
         reflowData();
     }
 
-    /**
-     * 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) {
-                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);
-        }
-    }
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Handle mouse press events.
@@ -301,10 +177,11 @@ public class TList extends TScrollableWidget {
         }
 
         if ((mouse.getX() < getWidth() - 1)
-            && (mouse.getY() < getHeight() - 1)) {
+            && (mouse.getY() < getHeight() - 1)
+        ) {
             if (getVerticalValue() + mouse.getY() < strings.size()) {
                 selectedString = getVerticalValue() + mouse.getY();
-                dispatchEnter();
+                dispatchSingleClick();
             }
             return;
         }
@@ -313,6 +190,27 @@ public class TList extends TScrollableWidget {
         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.
      *
@@ -408,4 +306,178 @@ public class TList extends TScrollableWidget {
         }
     }
 
+    // ------------------------------------------------------------------------
+    // 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 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";
+            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 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<String> getList() {
+        return new ArrayList<String>(strings);
+    }
+
+    /**
+     * 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();
+        }
+    }
+
+    /**
+     * Perform single-click action.
+     */
+    public void dispatchSingleClick() {
+        assert (selectedString >= 0);
+        assert (selectedString < strings.size());
+        if (singleClickAction != null) {
+            singleClickAction.DO();
+        }
+    }
+
 }