| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer.help; |
| 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | import jexer.TWidget; |
| 34 | |
| 35 | /** |
| 36 | * TParagraph contains a reflowable collection of TWords, some of which are |
| 37 | * clickable links. |
| 38 | */ |
| 39 | public class TParagraph extends TWidget { |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | // Constants -------------------------------------------------------------- |
| 43 | // ------------------------------------------------------------------------ |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Variables -------------------------------------------------------------- |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | /** |
| 50 | * Topic text and links converted to words. |
| 51 | */ |
| 52 | private List<TWord> words; |
| 53 | |
| 54 | /** |
| 55 | * If true, add one row to height as a paragraph separator. Note package |
| 56 | * private access. |
| 57 | */ |
| 58 | boolean separator = true; |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | // Constructors ----------------------------------------------------------- |
| 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | /** |
| 65 | * Public constructor. |
| 66 | * |
| 67 | * @param parent parent widget |
| 68 | * @param words the pieces of the paragraph to display |
| 69 | */ |
| 70 | public TParagraph(final THelpText parent, final List<TWord> words) { |
| 71 | |
| 72 | // Set parent and window |
| 73 | super(parent, 0, 0, parent.getWidth() - 1, 1); |
| 74 | |
| 75 | this.words = words; |
| 76 | for (TWord word: words) { |
| 77 | word.setParent(this, false); |
| 78 | } |
| 79 | |
| 80 | reflowData(); |
| 81 | } |
| 82 | |
| 83 | // ------------------------------------------------------------------------ |
| 84 | // TWidget ---------------------------------------------------------------- |
| 85 | // ------------------------------------------------------------------------ |
| 86 | |
| 87 | // ------------------------------------------------------------------------ |
| 88 | // TParagraph ------------------------------------------------------------- |
| 89 | // ------------------------------------------------------------------------ |
| 90 | |
| 91 | /** |
| 92 | * Reposition the words in this paragraph to reflect the new width, and |
| 93 | * set the paragraph height. |
| 94 | */ |
| 95 | public void reflowData() { |
| 96 | int x = 0; |
| 97 | int y = 0; |
| 98 | for (TWord word: words) { |
| 99 | if (x + word.getWidth() >= getWidth()) { |
| 100 | x = 0; |
| 101 | y++; |
| 102 | } |
| 103 | word.setX(x); |
| 104 | word.setY(y); |
| 105 | x += word.getWidth() + 1; |
| 106 | } |
| 107 | if (separator) { |
| 108 | setHeight(y + 2); |
| 109 | } else { |
| 110 | setHeight(y + 1); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Try to select a previous link. |
| 116 | * |
| 117 | * @return true if there was a previous link in this paragraph to select |
| 118 | */ |
| 119 | public boolean up() { |
| 120 | if (words.size() == 0) { |
| 121 | return false; |
| 122 | } |
| 123 | if (getActiveChild() == this) { |
| 124 | // No selectable links |
| 125 | return false; |
| 126 | } |
| 127 | TWord firstWord = null; |
| 128 | TWord lastWord = null; |
| 129 | for (TWord word: words) { |
| 130 | if (word.isEnabled()) { |
| 131 | if (firstWord == null) { |
| 132 | firstWord = word; |
| 133 | } |
| 134 | lastWord = word; |
| 135 | } |
| 136 | } |
| 137 | if (getActiveChild() == firstWord) { |
| 138 | return false; |
| 139 | } |
| 140 | switchWidget(false); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Try to select a next link. |
| 146 | * |
| 147 | * @return true if there was a next link in this paragraph to select |
| 148 | */ |
| 149 | public boolean down() { |
| 150 | if (words.size() == 0) { |
| 151 | return false; |
| 152 | } |
| 153 | if (getActiveChild() == this) { |
| 154 | // No selectable links |
| 155 | return false; |
| 156 | } |
| 157 | TWord firstWord = null; |
| 158 | TWord lastWord = null; |
| 159 | for (TWord word: words) { |
| 160 | if (word.isEnabled()) { |
| 161 | if (firstWord == null) { |
| 162 | firstWord = word; |
| 163 | } |
| 164 | lastWord = word; |
| 165 | } |
| 166 | } |
| 167 | if (getActiveChild() == lastWord) { |
| 168 | return false; |
| 169 | } |
| 170 | switchWidget(true); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | } |