| 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; |
| 30 | |
| 31 | import java.util.ResourceBundle; |
| 32 | |
| 33 | import jexer.bits.CellAttributes; |
| 34 | import jexer.event.TResizeEvent; |
| 35 | import jexer.help.THelpText; |
| 36 | import jexer.help.Topic; |
| 37 | |
| 38 | /** |
| 39 | * THelpWindow |
| 40 | */ |
| 41 | public class THelpWindow extends TWindow { |
| 42 | |
| 43 | /** |
| 44 | * Translated strings. |
| 45 | */ |
| 46 | private static final ResourceBundle i18n = ResourceBundle.getBundle(THelpWindow.class.getName()); |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Constants -------------------------------------------------------------- |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | // Default help topic keys. Note package private access. |
| 53 | static String HELP_HELP = "Help On Help"; |
| 54 | |
| 55 | // ------------------------------------------------------------------------ |
| 56 | // Variables -------------------------------------------------------------- |
| 57 | // ------------------------------------------------------------------------ |
| 58 | |
| 59 | /** |
| 60 | * The help text window. |
| 61 | */ |
| 62 | private THelpText helpText; |
| 63 | |
| 64 | /** |
| 65 | * The "Contents" button. |
| 66 | */ |
| 67 | private TButton contentsButton; |
| 68 | |
| 69 | /** |
| 70 | * The "Index" button. |
| 71 | */ |
| 72 | private TButton indexButton; |
| 73 | |
| 74 | /** |
| 75 | * The "Previous" button. |
| 76 | */ |
| 77 | private TButton previousButton; |
| 78 | |
| 79 | /** |
| 80 | * The "Close" button. |
| 81 | */ |
| 82 | private TButton closeButton; |
| 83 | |
| 84 | /** |
| 85 | * The X position for the buttons. |
| 86 | */ |
| 87 | private int buttonOffset = 14; |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // Constructors ----------------------------------------------------------- |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
| 93 | /** |
| 94 | * Public constructor. |
| 95 | * |
| 96 | * @param application TApplication that manages this window |
| 97 | * @param topic the topic to start on |
| 98 | */ |
| 99 | public THelpWindow(final TApplication application, final String topic) { |
| 100 | this (application, application.helpFile.getTopic(topic)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Public constructor. |
| 105 | * |
| 106 | * @param application TApplication that manages this window |
| 107 | * @param topic the topic to start on |
| 108 | */ |
| 109 | public THelpWindow(final TApplication application, final Topic topic) { |
| 110 | super(application, i18n.getString("windowTitle"), |
| 111 | 1, 1, 78, 22, CENTERED | RESIZABLE); |
| 112 | |
| 113 | setMinimumWindowHeight(16); |
| 114 | setMinimumWindowWidth(30); |
| 115 | |
| 116 | helpText = new THelpText(this, topic, 1, 1, |
| 117 | getWidth() - buttonOffset - 4, getHeight() - 4); |
| 118 | |
| 119 | setHelpTopic(topic); |
| 120 | |
| 121 | // Buttons |
| 122 | previousButton = addButton(i18n.getString("previousButton"), |
| 123 | getWidth() - buttonOffset, 4, |
| 124 | new TAction() { |
| 125 | public void DO() { |
| 126 | if (application.helpTopics.size() > 1) { |
| 127 | Topic previous = application.helpTopics.remove( |
| 128 | application.helpTopics.size() - 2); |
| 129 | application.helpTopics.remove(application. |
| 130 | helpTopics.size() - 1); |
| 131 | setHelpTopic(previous); |
| 132 | } |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | contentsButton = addButton(i18n.getString("contentsButton"), |
| 137 | getWidth() - buttonOffset, 6, |
| 138 | new TAction() { |
| 139 | public void DO() { |
| 140 | setHelpTopic(application.helpFile.getTableOfContents()); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | indexButton = addButton(i18n.getString("indexButton"), |
| 145 | getWidth() - buttonOffset, 8, |
| 146 | new TAction() { |
| 147 | public void DO() { |
| 148 | setHelpTopic(application.helpFile.getIndex()); |
| 149 | } |
| 150 | }); |
| 151 | |
| 152 | closeButton = addButton(i18n.getString("closeButton"), |
| 153 | getWidth() - buttonOffset, 10, |
| 154 | new TAction() { |
| 155 | public void DO() { |
| 156 | // Don't copy anything, just close the window. |
| 157 | THelpWindow.this.close(); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | // Save this for last: make the close button default action. |
| 162 | activate(closeButton); |
| 163 | |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Public constructor. |
| 168 | * |
| 169 | * @param application TApplication that manages this window |
| 170 | */ |
| 171 | public THelpWindow(final TApplication application) { |
| 172 | this(application, HELP_HELP); |
| 173 | } |
| 174 | |
| 175 | // ------------------------------------------------------------------------ |
| 176 | // Event handlers --------------------------------------------------------- |
| 177 | // ------------------------------------------------------------------------ |
| 178 | |
| 179 | /** |
| 180 | * Handle window/screen resize events. |
| 181 | * |
| 182 | * @param event resize event |
| 183 | */ |
| 184 | @Override |
| 185 | public void onResize(final TResizeEvent event) { |
| 186 | if (event.getType() == TResizeEvent.Type.WIDGET) { |
| 187 | |
| 188 | previousButton.setX(getWidth() - buttonOffset); |
| 189 | contentsButton.setX(getWidth() - buttonOffset); |
| 190 | indexButton.setX(getWidth() - buttonOffset); |
| 191 | closeButton.setX(getWidth() - buttonOffset); |
| 192 | |
| 193 | helpText.setDimensions(1, 1, getWidth() - buttonOffset - 4, |
| 194 | getHeight() - 4); |
| 195 | helpText.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
| 196 | helpText.getWidth(), helpText.getHeight())); |
| 197 | |
| 198 | return; |
| 199 | } else { |
| 200 | super.onResize(event); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------------ |
| 205 | // TWindow ---------------------------------------------------------------- |
| 206 | // ------------------------------------------------------------------------ |
| 207 | |
| 208 | /** |
| 209 | * Retrieve the background color. |
| 210 | * |
| 211 | * @return the background color |
| 212 | */ |
| 213 | @Override |
| 214 | public final CellAttributes getBackground() { |
| 215 | return getTheme().getColor("thelpwindow.background"); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Retrieve the border color. |
| 220 | * |
| 221 | * @return the border color |
| 222 | */ |
| 223 | @Override |
| 224 | public CellAttributes getBorder() { |
| 225 | if (inWindowMove) { |
| 226 | return getTheme().getColor("thelpwindow.windowmove"); |
| 227 | } |
| 228 | return getTheme().getColor("thelpwindow.background"); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Retrieve the color used by the window movement/sizing controls. |
| 233 | * |
| 234 | * @return the color used by the zoom box, resize bar, and close box |
| 235 | */ |
| 236 | @Override |
| 237 | public CellAttributes getBorderControls() { |
| 238 | return getTheme().getColor("thelpwindow.border"); |
| 239 | } |
| 240 | |
| 241 | // ------------------------------------------------------------------------ |
| 242 | // THelpWindow ------------------------------------------------------------ |
| 243 | // ------------------------------------------------------------------------ |
| 244 | |
| 245 | /** |
| 246 | * Set the topic to display. |
| 247 | * |
| 248 | * @param topic the topic to display |
| 249 | */ |
| 250 | public void setHelpTopic(final String topic) { |
| 251 | setHelpTopic(getApplication().helpFile.getTopic(topic)); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Set the topic to display. |
| 256 | * |
| 257 | * @param topic the topic to display |
| 258 | */ |
| 259 | private void setHelpTopic(final Topic topic) { |
| 260 | boolean separator = true; |
| 261 | if ((topic == getApplication().helpFile.getTableOfContents()) |
| 262 | || (topic == getApplication().helpFile.getIndex()) |
| 263 | ) { |
| 264 | separator = false; |
| 265 | } |
| 266 | |
| 267 | getApplication().helpTopics.add(topic); |
| 268 | helpText.setTopic(topic, separator); |
| 269 | } |
| 270 | |
| 271 | } |