+ // ------------------------------------------------------------------------
+ // 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.
+ */
+ @Override
+ public void reflowData() {
+
+ // Reset the lines
+ selectedString = -1;
+ maxLineWidth = 0;
+
+ for (int i = 0; i < strings.size(); i++) {
+ String line = strings.get(i);
+ int lineLength = StringUtils.width(line);
+ if (lineLength > maxLineWidth) {
+ maxLineWidth = lineLength;
+ }
+ }
+
+ 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 (line == null) {
+ line = "";
+ }
+ 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 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.
+ *
+ * @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(this);
+ }
+ }
+
+ /**
+ * Perform list movement action.
+ */
+ public void dispatchMove() {
+ assert (selectedString >= 0);
+ assert (selectedString < strings.size());
+ if (moveAction != null) {
+ moveAction.DO(this);
+ }
+ }
+
+ /**
+ * Perform single-click action.
+ */
+ public void dispatchSingleClick() {
+ assert (selectedString >= 0);
+ assert (selectedString < strings.size());
+ if (singleClickAction != null) {
+ singleClickAction.DO(this);
+ }
+ }
+