| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 6 | * This module is licensed under the GNU Lesser General Public License |
| 7 | * Version 3. Please see the file "COPYING" in this directory for more |
| 8 | * information about the GNU Lesser General Public License Version 3. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 3 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this program; if not, see |
| 24 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.backend; |
| 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | import jexer.event.TInputEvent; |
| 36 | import jexer.io.SwingScreen; |
| 37 | import jexer.io.SwingTerminal; |
| 38 | |
| 39 | /** |
| 40 | * This class uses standard Swing calls to handle screen, keyboard, and mouse |
| 41 | * I/O. |
| 42 | */ |
| 43 | public final class SwingBackend extends Backend { |
| 44 | |
| 45 | /** |
| 46 | * Input events are processed by this Terminal. |
| 47 | */ |
| 48 | private SwingTerminal terminal; |
| 49 | |
| 50 | /** |
| 51 | * Public constructor. |
| 52 | * |
| 53 | * @param listener the object this backend needs to wake up when new |
| 54 | * input comes in |
| 55 | */ |
| 56 | public SwingBackend(final Object listener) { |
| 57 | // Create a screen |
| 58 | SwingScreen screen = new SwingScreen(); |
| 59 | this.screen = screen; |
| 60 | |
| 61 | // Create the Swing event listeners |
| 62 | terminal = new SwingTerminal(listener, screen); |
| 63 | |
| 64 | // Hang onto the session info |
| 65 | this.sessionInfo = terminal.getSessionInfo(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sync the logical screen to the physical device. |
| 70 | */ |
| 71 | @Override |
| 72 | public void flushScreen() { |
| 73 | screen.flushPhysical(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get keyboard, mouse, and screen resize events. |
| 78 | * |
| 79 | * @param queue list to append new events to |
| 80 | */ |
| 81 | @Override |
| 82 | public void getEvents(final List<TInputEvent> queue) { |
| 83 | if (terminal.hasEvents()) { |
| 84 | terminal.getEvents(queue); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Close the I/O, restore the console, etc. |
| 90 | */ |
| 91 | @Override |
| 92 | public void shutdown() { |
| 93 | ((SwingScreen) screen).shutdown(); |
| 94 | } |
| 95 | |
| 96 | } |