| 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.event.TCommandEvent; |
| 34 | import jexer.event.TKeypressEvent; |
| 35 | import jexer.event.TMouseEvent; |
| 36 | import jexer.event.TResizeEvent; |
| 37 | import jexer.menu.TMenu; |
| 38 | import static jexer.TCommand.*; |
| 39 | import static jexer.TKeypress.*; |
| 40 | |
| 41 | /** |
| 42 | * TTableWindow is used to display and edit regular two-dimensional tables of |
| 43 | * cells. |
| 44 | */ |
| 45 | public class TTableWindow extends TScrollableWindow { |
| 46 | |
| 47 | /** |
| 48 | * Translated strings. |
| 49 | */ |
| 50 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TTableWindow.class.getName()); |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Variables -------------------------------------------------------------- |
| 54 | // ------------------------------------------------------------------------ |
| 55 | |
| 56 | /** |
| 57 | * The table widget. |
| 58 | */ |
| 59 | private TTableWidget tableField; |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Constructors ----------------------------------------------------------- |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | /** |
| 66 | * Public constructor sets window title. |
| 67 | * |
| 68 | * @param parent the main application |
| 69 | * @param title the window title |
| 70 | */ |
| 71 | public TTableWindow(final TApplication parent, final String title) { |
| 72 | |
| 73 | super(parent, title, 0, 0, parent.getScreen().getWidth() / 2, |
| 74 | parent.getScreen().getHeight() / 2 - 2, RESIZABLE | CENTERED); |
| 75 | |
| 76 | tableField = new TTableWidget(this, 0, 0, getWidth() - 2, getHeight() - 2); |
| 77 | setupAfterTable(); |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | // Event handlers --------------------------------------------------------- |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | /** |
| 85 | * Called by application.switchWindow() when this window gets the |
| 86 | * focus, and also by application.addWindow(). |
| 87 | */ |
| 88 | public void onFocus() { |
| 89 | // Enable the table menu items. |
| 90 | getApplication().enableMenuItem(TMenu.MID_CUT); |
| 91 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_NONE); |
| 92 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_ALL); |
| 93 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); |
| 94 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_LEFT); |
| 95 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_TOP); |
| 96 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_BOTTOM); |
| 97 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM); |
| 98 | getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM); |
| 99 | getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_LEFT); |
| 100 | getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_UP); |
| 101 | getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_ROW); |
| 102 | getApplication().enableMenuItem(TMenu.MID_TABLE_DELETE_COLUMN); |
| 103 | getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_LEFT); |
| 104 | getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_RIGHT); |
| 105 | getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_ABOVE); |
| 106 | getApplication().enableMenuItem(TMenu.MID_TABLE_INSERT_BELOW); |
| 107 | getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW); |
| 108 | getApplication().enableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN); |
| 109 | getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV); |
| 110 | getApplication().enableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Called by application.switchWindow() when another window gets the |
| 115 | * focus. |
| 116 | */ |
| 117 | public void onUnfocus() { |
| 118 | // Disable the table menu items. |
| 119 | getApplication().disableMenuItem(TMenu.MID_CUT); |
| 120 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_NONE); |
| 121 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_ALL); |
| 122 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); |
| 123 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_LEFT); |
| 124 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_TOP); |
| 125 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_BOTTOM); |
| 126 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM); |
| 127 | getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM); |
| 128 | getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_LEFT); |
| 129 | getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_UP); |
| 130 | getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_ROW); |
| 131 | getApplication().disableMenuItem(TMenu.MID_TABLE_DELETE_COLUMN); |
| 132 | getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_LEFT); |
| 133 | getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_RIGHT); |
| 134 | getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_ABOVE); |
| 135 | getApplication().disableMenuItem(TMenu.MID_TABLE_INSERT_BELOW); |
| 136 | getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_NARROW); |
| 137 | getApplication().disableMenuItem(TMenu.MID_TABLE_COLUMN_WIDEN); |
| 138 | getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_CSV); |
| 139 | getApplication().disableMenuItem(TMenu.MID_TABLE_FILE_SAVE_TEXT); |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------------------------------------------ |
| 143 | // TWindow ---------------------------------------------------------------- |
| 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | /** |
| 147 | * Draw the window. |
| 148 | */ |
| 149 | @Override |
| 150 | public void draw() { |
| 151 | // Draw as normal. |
| 152 | super.draw(); |
| 153 | |
| 154 | // Add borders on rows and columns. |
| 155 | // TODO |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Handle mouse press events. |
| 160 | * |
| 161 | * @param mouse mouse button press event |
| 162 | */ |
| 163 | @Override |
| 164 | public void onMouseDown(final TMouseEvent mouse) { |
| 165 | // Use TWidget's code to pass the event to the children. |
| 166 | super.onMouseDown(mouse); |
| 167 | |
| 168 | if (mouseOnTable(mouse)) { |
| 169 | // The table might have changed, update the scollbars. |
| 170 | // TODO |
| 171 | /* |
| 172 | setBottomValue(editField.getMaximumRowNumber()); |
| 173 | setVerticalValue(editField.getVisibleRowNumber()); |
| 174 | setRightValue(editField.getMaximumColumnNumber()); |
| 175 | setHorizontalValue(editField.getEditingColumnNumber()); |
| 176 | */ |
| 177 | } else { |
| 178 | if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) { |
| 179 | // Vertical scrollbar actions |
| 180 | // TODO |
| 181 | // editField.setVisibleRowNumber(getVerticalValue()); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Handle mouse release events. |
| 188 | * |
| 189 | * @param mouse mouse button release event |
| 190 | */ |
| 191 | @Override |
| 192 | public void onMouseUp(final TMouseEvent mouse) { |
| 193 | // Use TWidget's code to pass the event to the children. |
| 194 | super.onMouseUp(mouse); |
| 195 | |
| 196 | if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { |
| 197 | // Clicked on vertical scrollbar |
| 198 | // TODO |
| 199 | // editField.setVisibleRowNumber(getVerticalValue()); |
| 200 | } |
| 201 | |
| 202 | // TODO: horizontal scrolling |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Method that subclasses can override to handle mouse movements. |
| 207 | * |
| 208 | * @param mouse mouse motion event |
| 209 | */ |
| 210 | @Override |
| 211 | public void onMouseMotion(final TMouseEvent mouse) { |
| 212 | // Use TWidget's code to pass the event to the children. |
| 213 | super.onMouseMotion(mouse); |
| 214 | |
| 215 | if (mouseOnTable(mouse) && mouse.isMouse1()) { |
| 216 | // The editor might have changed, update the scollbars. |
| 217 | // TODO |
| 218 | /* |
| 219 | setBottomValue(editField.getMaximumRowNumber()); |
| 220 | setVerticalValue(editField.getVisibleRowNumber()); |
| 221 | setRightValue(editField.getMaximumColumnNumber()); |
| 222 | setHorizontalValue(editField.getEditingColumnNumber()); |
| 223 | */ |
| 224 | } else { |
| 225 | if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { |
| 226 | // Clicked/dragged on vertical scrollbar |
| 227 | // TODO |
| 228 | // editField.setVisibleRowNumber(getVerticalValue()); |
| 229 | } |
| 230 | |
| 231 | // TODO: horizontal scrolling |
| 232 | } |
| 233 | |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Handle keystrokes. |
| 238 | * |
| 239 | * @param keypress keystroke event |
| 240 | */ |
| 241 | @Override |
| 242 | public void onKeypress(final TKeypressEvent keypress) { |
| 243 | // Use TWidget's code to pass the event to the children. |
| 244 | super.onKeypress(keypress); |
| 245 | |
| 246 | // The editor might have changed, update the scollbars. |
| 247 | // TODO |
| 248 | /* |
| 249 | setBottomValue(editField.getMaximumRowNumber()); |
| 250 | setVerticalValue(editField.getVisibleRowNumber()); |
| 251 | setRightValue(editField.getMaximumColumnNumber()); |
| 252 | setHorizontalValue(editField.getEditingColumnNumber()); |
| 253 | */ |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Handle window/screen resize events. |
| 258 | * |
| 259 | * @param event resize event |
| 260 | */ |
| 261 | @Override |
| 262 | public void onResize(final TResizeEvent event) { |
| 263 | if (event.getType() == TResizeEvent.Type.WIDGET) { |
| 264 | // Resize the table |
| 265 | TResizeEvent tableSize = new TResizeEvent(TResizeEvent.Type.WIDGET, |
| 266 | event.getWidth() - 2, event.getHeight() - 2); |
| 267 | tableField.onResize(tableSize); |
| 268 | |
| 269 | // Have TScrollableWindow handle the scrollbars |
| 270 | super.onResize(event); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // Pass to children instead |
| 275 | for (TWidget widget: getChildren()) { |
| 276 | widget.onResize(event); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // ------------------------------------------------------------------------ |
| 281 | // TTableWindow ----------------------------------------------------------- |
| 282 | // ------------------------------------------------------------------------ |
| 283 | |
| 284 | /** |
| 285 | * Setup other fields after the table is created. |
| 286 | */ |
| 287 | private void setupAfterTable() { |
| 288 | hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20); |
| 289 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); |
| 290 | setMinimumWindowWidth(25); |
| 291 | setMinimumWindowHeight(10); |
| 292 | setTopValue(1); |
| 293 | // setBottomValue(editField.getMaximumRowNumber()); |
| 294 | setLeftValue(1); |
| 295 | // setRightValue(editField.getMaximumColumnNumber()); |
| 296 | |
| 297 | statusBar = newStatusBar(i18n.getString("statusBar")); |
| 298 | statusBar.addShortcutKeypress(kbF1, cmHelp, |
| 299 | i18n.getString("statusBarHelp")); |
| 300 | /* |
| 301 | statusBar.addShortcutKeypress(kbF2, cmSave, |
| 302 | i18n.getString("statusBarSave")); |
| 303 | statusBar.addShortcutKeypress(kbF3, cmOpen, |
| 304 | i18n.getString("statusBarOpen")); |
| 305 | */ |
| 306 | statusBar.addShortcutKeypress(kbF10, cmMenu, |
| 307 | i18n.getString("statusBarMenu")); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Check if a mouse press/release/motion event coordinate is over the |
| 312 | * table. |
| 313 | * |
| 314 | * @param mouse a mouse-based event |
| 315 | * @return whether or not the mouse is on the table |
| 316 | */ |
| 317 | private boolean mouseOnTable(final TMouseEvent mouse) { |
| 318 | if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1) |
| 319 | && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1) |
| 320 | && (mouse.getAbsoluteY() >= getAbsoluteY() + 1) |
| 321 | && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1) |
| 322 | ) { |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | } |