| 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 jexer.THelpWindow; |
| 32 | import jexer.TWidget; |
| 33 | import jexer.bits.CellAttributes; |
| 34 | import jexer.bits.StringUtils; |
| 35 | import jexer.event.TKeypressEvent; |
| 36 | import jexer.event.TMouseEvent; |
| 37 | import static jexer.TKeypress.*; |
| 38 | |
| 39 | /** |
| 40 | * TWord contains either a string to display or a clickable link. |
| 41 | */ |
| 42 | public class TWord extends TWidget { |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Constants -------------------------------------------------------------- |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Variables -------------------------------------------------------------- |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | /** |
| 53 | * The word(s) to display. |
| 54 | */ |
| 55 | private String words; |
| 56 | |
| 57 | /** |
| 58 | * Link to another Topic. |
| 59 | */ |
| 60 | private Link link; |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Constructors ----------------------------------------------------------- |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * Public constructor. |
| 68 | * |
| 69 | * @param words the words to display |
| 70 | * @param link link to other topic, or null |
| 71 | */ |
| 72 | public TWord(final String words, final Link link) { |
| 73 | |
| 74 | // TWord is created by THelpText before the TParagraph is belongs to |
| 75 | // is created, so pass null as parent for now. |
| 76 | super(null, 0, 0, StringUtils.width(words), 1); |
| 77 | |
| 78 | this.words = words; |
| 79 | this.link = link; |
| 80 | |
| 81 | // Don't make text-only words "active". |
| 82 | if (link == null) { |
| 83 | setEnabled(false); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // ------------------------------------------------------------------------ |
| 88 | // Event handlers --------------------------------------------------------- |
| 89 | // ------------------------------------------------------------------------ |
| 90 | |
| 91 | /** |
| 92 | * Handle mouse press events. |
| 93 | * |
| 94 | * @param mouse mouse button press event |
| 95 | */ |
| 96 | @Override |
| 97 | public void onMouseDown(final TMouseEvent mouse) { |
| 98 | if (mouse.isMouse1()) { |
| 99 | if (link != null) { |
| 100 | ((THelpWindow) getWindow()).setHelpTopic(link.getTopic()); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Handle keystrokes. |
| 107 | * |
| 108 | * @param keypress keystroke event |
| 109 | */ |
| 110 | @Override |
| 111 | public void onKeypress(final TKeypressEvent keypress) { |
| 112 | if (keypress.equals(kbEnter)) { |
| 113 | if (link != null) { |
| 114 | ((THelpWindow) getWindow()).setHelpTopic(link.getTopic()); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | // TWidget ---------------------------------------------------------------- |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
| 123 | /** |
| 124 | * Draw the words. |
| 125 | */ |
| 126 | @Override |
| 127 | public void draw() { |
| 128 | CellAttributes color = getTheme().getColor("thelpwindow.text"); |
| 129 | if (link != null) { |
| 130 | if (isAbsoluteActive()) { |
| 131 | color = getTheme().getColor("thelpwindow.link.active"); |
| 132 | } else { |
| 133 | color = getTheme().getColor("thelpwindow.link"); |
| 134 | } |
| 135 | } |
| 136 | putStringXY(0, 0, words, color); |
| 137 | } |
| 138 | |
| 139 | // ------------------------------------------------------------------------ |
| 140 | // TWord ------------------------------------------------------------------ |
| 141 | // ------------------------------------------------------------------------ |
| 142 | |
| 143 | } |