Merge branch 'subtree'
[fanfix.git] / src / jexer / TList.java
index 46c9307b95eba5cf9f68ed59d82ed4893ca7f1f2..12e0b8a33cce977e93ce4a6fd30080adeb75e8dc 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"),
@@ -32,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.*;
@@ -61,14 +62,20 @@ public class TList extends TScrollableWidget {
     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 selects an item (single-click).
+     */
+    protected TAction singleClickAction = null;
 
     /**
      * The action to perform when the user navigates with keyboard.
      */
-    private TAction moveAction = null;
+    protected TAction moveAction = null;
 
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
@@ -87,7 +94,7 @@ public class TList extends TScrollableWidget {
     public TList(final TWidget parent, final List<String> strings, final int x,
         final int y, final int width, final int height) {
 
-        this(parent, strings, x, y, width, height, null);
+        this(parent, strings, x, y, width, height, null, null, null);
     }
 
     /**
@@ -106,16 +113,7 @@ public class TList extends TScrollableWidget {
         final int y, final int width, final int height,
         final TAction enterAction) {
 
-        super(parent, x, y, width, height);
-        this.enterAction = enterAction;
-        this.strings = new ArrayList<String>();
-        if (strings != null) {
-            this.strings.addAll(strings);
-        }
-
-        hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
-        vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
-        reflowData();
+        this(parent, strings, x, y, width, height, enterAction, null, null);
     }
 
     /**
@@ -136,9 +134,35 @@ public class TList extends TScrollableWidget {
         final int y, final int width, final int height,
         final TAction enterAction, final TAction moveAction) {
 
+        this(parent, strings, x, y, width, height, enterAction, moveAction,
+            null);
+    }
+
+    /**
+     * Public constructor.
+     *
+     * @param parent parent widget
+     * @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
+     * @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<String> strings, final int x,
+        final int y, final int width, final int height,
+        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<String>();
         if (strings != null) {
             this.strings.addAll(strings);
@@ -170,9 +194,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();
+                dispatchSingleClick();
             }
             return;
         }
@@ -189,7 +215,8 @@ public class TList extends TScrollableWidget {
     @Override
     public void onMouseDoubleClick(final TMouseEvent mouse) {
         if ((mouse.getX() < getWidth() - 1)
-            && (mouse.getY() < getHeight() - 1)) {
+            && (mouse.getY() < getHeight() - 1)
+        ) {
             if (getVerticalValue() + mouse.getY() < strings.size()) {
                 selectedString = getVerticalValue() + mouse.getY();
                 dispatchEnter();
@@ -300,6 +327,38 @@ public class TList extends TScrollableWidget {
     // 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.
      */
@@ -312,8 +371,9 @@ public class TList extends TScrollableWidget {
 
         for (int i = 0; i < strings.size(); i++) {
             String line = strings.get(i);
-            if (line.length() > maxLineWidth) {
-                maxLineWidth = line.length();
+            int lineLength = StringUtils.width(line);
+            if (lineLength > maxLineWidth) {
+                maxLineWidth = lineLength;
             }
         }
 
@@ -329,7 +389,7 @@ public class TList extends TScrollableWidget {
     }
 
     /**
-     * Draw the files list.
+     * Draw the list.
      */
     @Override
     public void draw() {
@@ -338,6 +398,9 @@ public class TList extends TScrollableWidget {
         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 {
@@ -355,8 +418,7 @@ public class TList extends TScrollableWidget {
                 color = getTheme().getColor("tlist.inactive");
             }
             String formatString = "%-" + Integer.toString(getWidth() - 1) + "s";
-            getScreen().putStringXY(0, topY, String.format(formatString, line),
-                    color);
+            putStringXY(0, topY, String.format(formatString, line), color);
             topY++;
             if (topY >= getHeight() - 1) {
                 break;
@@ -371,7 +433,7 @@ public class TList extends TScrollableWidget {
 
         // Pad the rest with blank lines
         for (int i = topY; i < getHeight() - 1; i++) {
-            getScreen().hLineXY(0, i, getWidth() - 1, ' ', color);
+            hLineXY(0, i, getWidth() - 1, ' ', color);
         }
     }
 
@@ -397,6 +459,16 @@ public class TList extends TScrollableWidget {
         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.
      *
@@ -418,6 +490,15 @@ public class TList extends TScrollableWidget {
         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.
      *
@@ -436,7 +517,7 @@ public class TList extends TScrollableWidget {
         assert (selectedString >= 0);
         assert (selectedString < strings.size());
         if (enterAction != null) {
-            enterAction.DO();
+            enterAction.DO(this);
         }
     }
 
@@ -447,7 +528,18 @@ public class TList extends TScrollableWidget {
         assert (selectedString >= 0);
         assert (selectedString < strings.size());
         if (moveAction != null) {
-            moveAction.DO();
+            moveAction.DO(this);
+        }
+    }
+
+    /**
+     * Perform single-click action.
+     */
+    public void dispatchSingleClick() {
+        assert (selectedString >= 0);
+        assert (selectedString < strings.size());
+        if (singleClickAction != null) {
+            singleClickAction.DO(this);
         }
     }