2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.io
.InputStream
;
32 import java
.io
.IOException
;
33 import java
.io
.OutputStream
;
34 import java
.io
.PrintWriter
;
35 import java
.io
.Reader
;
36 import java
.io
.UnsupportedEncodingException
;
37 import java
.util
.Collections
;
38 import java
.util
.Date
;
39 import java
.util
.HashMap
;
40 import java
.util
.ArrayList
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
45 import jexer
.bits
.CellAttributes
;
46 import jexer
.bits
.ColorTheme
;
47 import jexer
.bits
.GraphicsChars
;
48 import jexer
.event
.TCommandEvent
;
49 import jexer
.event
.TInputEvent
;
50 import jexer
.event
.TKeypressEvent
;
51 import jexer
.event
.TMenuEvent
;
52 import jexer
.event
.TMouseEvent
;
53 import jexer
.event
.TResizeEvent
;
54 import jexer
.backend
.Backend
;
55 import jexer
.backend
.SwingBackend
;
56 import jexer
.backend
.ECMA48Backend
;
57 import jexer
.io
.Screen
;
58 import jexer
.menu
.TMenu
;
59 import jexer
.menu
.TMenuItem
;
60 import static jexer
.TCommand
.*;
61 import static jexer
.TKeypress
.*;
64 * TApplication sets up a full Text User Interface application.
66 public class TApplication
implements Runnable
{
68 // ------------------------------------------------------------------------
69 // Public constants -------------------------------------------------------
70 // ------------------------------------------------------------------------
73 * If true, emit thread stuff to System.err.
75 private static final boolean debugThreads
= false;
78 * If true, emit events being processed to System.err.
80 private static final boolean debugEvents
= false;
83 * Two backend types are available.
85 public static enum BackendType
{
92 * An ECMA48 / ANSI X3.64 / XTERM style terminal.
102 // ------------------------------------------------------------------------
103 // Primary/secondary event handlers ---------------------------------------
104 // ------------------------------------------------------------------------
107 * WidgetEventHandler is the main event consumer loop. There are at most
108 * two such threads in existence: the primary for normal case and a
109 * secondary that is used for TMessageBox, TInputBox, and similar.
111 private class WidgetEventHandler
implements Runnable
{
113 * The main application.
115 private TApplication application
;
118 * Whether or not this WidgetEventHandler is the primary or secondary
121 private boolean primary
= true;
124 * Public constructor.
126 * @param application the main application
127 * @param primary if true, this is the primary event handler thread
129 public WidgetEventHandler(final TApplication application
,
130 final boolean primary
) {
132 this.application
= application
;
133 this.primary
= primary
;
142 while (!application
.quit
) {
144 // Wait until application notifies me
145 while (!application
.quit
) {
147 synchronized (application
.drainEventQueue
) {
148 if (application
.drainEventQueue
.size() > 0) {
153 synchronized (this) {
155 System
.err
.printf("%s %s sleep\n", this,
156 primary ?
"primary" : "secondary");
162 System
.err
.printf("%s %s AWAKE\n", this,
163 primary ?
"primary" : "secondary");
167 && (application
.secondaryEventReceiver
== null)
169 // Secondary thread, emergency exit. If we
170 // got here then something went wrong with
171 // the handoff between yield() and
173 synchronized (application
.primaryEventHandler
) {
174 application
.primaryEventHandler
.notify();
176 application
.secondaryEventHandler
= null;
177 throw new RuntimeException(
178 "secondary exited at wrong time");
182 } catch (InterruptedException e
) {
187 // Wait for drawAll() or doIdle() to be done, then handle the
189 boolean oldLock
= lockHandleEvent();
190 assert (oldLock
== false);
192 // Pull all events off the queue
194 TInputEvent event
= null;
195 synchronized (application
.drainEventQueue
) {
196 if (application
.drainEventQueue
.size() == 0) {
199 event
= application
.drainEventQueue
.remove(0);
201 application
.repaint
= true;
203 primaryHandleEvent(event
);
205 secondaryHandleEvent(event
);
208 && (application
.secondaryEventReceiver
== null)
210 // Secondary thread, time to exit.
212 // DO NOT UNLOCK. Primary thread just came back from
213 // primaryHandleEvent() and will unlock in the else
214 // block below. Just wake it up.
215 synchronized (application
.primaryEventHandler
) {
216 application
.primaryEventHandler
.notify();
218 // Now eliminate my reference so that
219 // wakeEventHandler() resumes working on the primary.
220 application
.secondaryEventHandler
= null;
227 // Unlock. Either I am primary thread, or I am secondary
228 // thread and still running.
229 oldLock
= unlockHandleEvent();
230 assert (oldLock
== true);
232 // I have done some work of some kind. Tell the main run()
233 // loop to wake up now.
234 synchronized (application
) {
235 application
.notify();
238 } // while (true) (main runnable loop)
243 * The primary event handler thread.
245 private volatile WidgetEventHandler primaryEventHandler
;
248 * The secondary event handler thread.
250 private volatile WidgetEventHandler secondaryEventHandler
;
253 * The widget receiving events from the secondary event handler thread.
255 private volatile TWidget secondaryEventReceiver
;
258 * Spinlock for the primary and secondary event handlers.
259 * WidgetEventHandler.run() is responsible for setting this value.
261 private volatile boolean insideHandleEvent
= false;
264 * Wake the sleeping active event handler.
266 private void wakeEventHandler() {
267 if (secondaryEventHandler
!= null) {
268 synchronized (secondaryEventHandler
) {
269 secondaryEventHandler
.notify();
272 assert (primaryEventHandler
!= null);
273 synchronized (primaryEventHandler
) {
274 primaryEventHandler
.notify();
280 * Set the insideHandleEvent flag to true. lockoutEventHandlers() will
281 * spin indefinitely until unlockHandleEvent() is called.
283 * @return the old value of insideHandleEvent
285 private boolean lockHandleEvent() {
287 System
.err
.printf(" >> lockHandleEvent(): oldValue %s",
290 boolean oldValue
= true;
292 synchronized (this) {
293 // Wait for TApplication.run() to finish using the global state
294 // before allowing further event processing.
295 while (lockoutHandleEvent
== true) {
297 // Backoff so that the backend can finish its work.
299 } catch (InterruptedException e
) {
304 oldValue
= insideHandleEvent
;
305 insideHandleEvent
= true;
309 System
.err
.printf(" ***\n");
315 * Set the insideHandleEvent flag to false. lockoutEventHandlers() will
316 * spin indefinitely until unlockHandleEvent() is called.
318 * @return the old value of insideHandleEvent
320 private boolean unlockHandleEvent() {
322 System
.err
.printf(" << unlockHandleEvent(): oldValue %s\n",
325 synchronized (this) {
326 boolean oldValue
= insideHandleEvent
;
327 insideHandleEvent
= false;
333 * Spinlock for the primary and secondary event handlers. When true, the
334 * event handlers will spinlock wait before calling handleEvent().
336 private volatile boolean lockoutHandleEvent
= false;
339 * TApplication.run() needs to be able rely on the global data structures
340 * being intact when calling doIdle() and drawAll(). Tell the event
341 * handlers to wait for an unlock before handling their events.
343 private void stopEventHandlers() {
345 System
.err
.printf(">> stopEventHandlers()");
348 lockoutHandleEvent
= true;
349 // Wait for the last event to finish processing before returning
350 // control to TApplication.run().
351 while (insideHandleEvent
== true) {
353 // Backoff so that the event handler can finish its work.
355 } catch (InterruptedException e
) {
361 System
.err
.printf(" XXX\n");
366 * TApplication.run() needs to be able rely on the global data structures
367 * being intact when calling doIdle() and drawAll(). Tell the event
368 * handlers that it is now OK to handle their events.
370 private void startEventHandlers() {
372 System
.err
.printf("<< startEventHandlers()\n");
374 lockoutHandleEvent
= false;
377 // ------------------------------------------------------------------------
378 // TApplication attributes ------------------------------------------------
379 // ------------------------------------------------------------------------
382 * Access to the physical screen, keyboard, and mouse.
384 private Backend backend
;
389 * @return the Backend
391 public final Backend
getBackend() {
400 public final Screen
getScreen() {
401 return backend
.getScreen();
405 * Actual mouse coordinate X.
410 * Actual mouse coordinate Y.
415 * Old version of mouse coordinate X.
417 private int oldMouseX
;
420 * Old version mouse coordinate Y.
422 private int oldMouseY
;
425 * Event queue that is filled by run().
427 private List
<TInputEvent
> fillEventQueue
;
430 * Event queue that will be drained by either primary or secondary
433 private List
<TInputEvent
> drainEventQueue
;
436 * Top-level menus in this application.
438 private List
<TMenu
> menus
;
441 * Stack of activated sub-menus in this application.
443 private List
<TMenu
> subMenus
;
446 * The currently acive menu.
448 private TMenu activeMenu
= null;
451 * Active keyboard accelerators.
453 private Map
<TKeypress
, TMenuItem
> accelerators
;
458 private List
<TMenuItem
> menuItems
;
461 * Windows and widgets pull colors from this ColorTheme.
463 private ColorTheme theme
;
466 * Get the color theme.
470 public final ColorTheme
getTheme() {
475 * The top-level windows (but not menus).
477 private List
<TWindow
> windows
;
480 * Timers that are being ticked.
482 private List
<TTimer
> timers
;
485 * When true, exit the application.
487 private volatile boolean quit
= false;
490 * When true, repaint the entire screen.
492 private volatile boolean repaint
= true;
495 * Y coordinate of the top edge of the desktop. For now this is a
496 * constant. Someday it would be nice to have a multi-line menu or
499 private static final int desktopTop
= 1;
502 * Get Y coordinate of the top edge of the desktop.
504 * @return Y coordinate of the top edge of the desktop
506 public final int getDesktopTop() {
511 * Y coordinate of the bottom edge of the desktop.
513 private int desktopBottom
;
516 * Get Y coordinate of the bottom edge of the desktop.
518 * @return Y coordinate of the bottom edge of the desktop
520 public final int getDesktopBottom() {
521 return desktopBottom
;
524 // ------------------------------------------------------------------------
525 // General behavior -------------------------------------------------------
526 // ------------------------------------------------------------------------
529 * Display the about dialog.
531 protected void showAboutDialog() {
532 messageBox("About", "Jexer Version " +
533 this.getClass().getPackage().getImplementationVersion(),
534 TMessageBox
.Type
.OK
);
537 // ------------------------------------------------------------------------
538 // Constructors -----------------------------------------------------------
539 // ------------------------------------------------------------------------
542 * Public constructor.
544 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
546 * @throws UnsupportedEncodingException if an exception is thrown when
547 * creating the InputStreamReader
549 public TApplication(final BackendType backendType
)
550 throws UnsupportedEncodingException
{
552 switch (backendType
) {
554 backend
= new SwingBackend(this);
559 backend
= new ECMA48Backend(this, null, null);
562 throw new IllegalArgumentException("Invalid backend type: "
569 * Public constructor. The backend type will be BackendType.ECMA48.
571 * @param input an InputStream connected to the remote user, or null for
572 * System.in. If System.in is used, then on non-Windows systems it will
573 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
574 * mode. input is always converted to a Reader with UTF-8 encoding.
575 * @param output an OutputStream connected to the remote user, or null
576 * for System.out. output is always converted to a Writer with UTF-8
578 * @throws UnsupportedEncodingException if an exception is thrown when
579 * creating the InputStreamReader
581 public TApplication(final InputStream input
,
582 final OutputStream output
) throws UnsupportedEncodingException
{
584 backend
= new ECMA48Backend(this, input
, output
);
589 * Public constructor. The backend type will be BackendType.ECMA48.
591 * @param input the InputStream underlying 'reader'. Its available()
592 * method is used to determine if reader.read() will block or not.
593 * @param reader a Reader connected to the remote user.
594 * @param writer a PrintWriter connected to the remote user.
595 * @param setRawMode if true, set System.in into raw mode with stty.
596 * This should in general not be used. It is here solely for Demo3,
597 * which uses System.in.
598 * @throws IllegalArgumentException if input, reader, or writer are null.
600 public TApplication(final InputStream input
, final Reader reader
,
601 final PrintWriter writer
, final boolean setRawMode
) {
603 backend
= new ECMA48Backend(this, input
, reader
, writer
, setRawMode
);
608 * Public constructor. The backend type will be BackendType.ECMA48.
610 * @param input the InputStream underlying 'reader'. Its available()
611 * method is used to determine if reader.read() will block or not.
612 * @param reader a Reader connected to the remote user.
613 * @param writer a PrintWriter connected to the remote user.
614 * @throws IllegalArgumentException if input, reader, or writer are null.
616 public TApplication(final InputStream input
, final Reader reader
,
617 final PrintWriter writer
) {
619 this(input
, reader
, writer
, false);
623 * Public constructor. This hook enables use with new non-Jexer
626 * @param backend a Backend that is already ready to go.
628 public TApplication(final Backend backend
) {
629 this.backend
= backend
;
634 * Finish construction once the backend is set.
636 private void TApplicationImpl() {
637 theme
= new ColorTheme();
638 desktopBottom
= getScreen().getHeight() - 1;
639 fillEventQueue
= new ArrayList
<TInputEvent
>();
640 drainEventQueue
= new ArrayList
<TInputEvent
>();
641 windows
= new LinkedList
<TWindow
>();
642 menus
= new LinkedList
<TMenu
>();
643 subMenus
= new LinkedList
<TMenu
>();
644 timers
= new LinkedList
<TTimer
>();
645 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
646 menuItems
= new ArrayList
<TMenuItem
>();
648 // Setup the main consumer thread
649 primaryEventHandler
= new WidgetEventHandler(this, true);
650 (new Thread(primaryEventHandler
)).start();
653 // ------------------------------------------------------------------------
654 // Screen refresh loop ----------------------------------------------------
655 // ------------------------------------------------------------------------
658 * Invert the cell color at a position. This is used to track the mouse.
660 * @param x column position
661 * @param y row position
663 private void invertCell(final int x
, final int y
) {
665 System
.err
.printf("invertCell() %d %d\n", x
, y
);
667 CellAttributes attr
= getScreen().getAttrXY(x
, y
);
668 attr
.setForeColor(attr
.getForeColor().invert());
669 attr
.setBackColor(attr
.getBackColor().invert());
670 getScreen().putAttrXY(x
, y
, attr
, false);
676 private void drawAll() {
678 System
.err
.printf("drawAll() enter\n");
683 System
.err
.printf("drawAll() !repaint\n");
685 synchronized (getScreen()) {
686 if ((oldMouseX
!= mouseX
) || (oldMouseY
!= mouseY
)) {
687 // The only thing that has happened is the mouse moved.
688 // Clear the old position and draw the new position.
689 invertCell(oldMouseX
, oldMouseY
);
690 invertCell(mouseX
, mouseY
);
694 if (getScreen().isDirty()) {
695 backend
.flushScreen();
702 System
.err
.printf("drawAll() REDRAW\n");
705 // If true, the cursor is not visible
706 boolean cursor
= false;
708 // Start with a clean screen
711 // Draw the background
712 CellAttributes background
= theme
.getColor("tapplication.background");
713 getScreen().putAll(GraphicsChars
.HATCH
, background
);
715 // Draw each window in reverse Z order
716 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
717 Collections
.sort(sorted
);
718 TWindow topLevel
= sorted
.get(0);
719 Collections
.reverse(sorted
);
720 for (TWindow window
: sorted
) {
721 window
.drawChildren();
724 // Draw the blank menubar line - reset the screen clipping first so
725 // it won't trim it out.
726 getScreen().resetClipping();
727 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
728 theme
.getColor("tmenu"));
729 // Now draw the menus.
731 for (TMenu menu
: menus
) {
732 CellAttributes menuColor
;
733 CellAttributes menuMnemonicColor
;
734 if (menu
.isActive()) {
735 menuColor
= theme
.getColor("tmenu.highlighted");
736 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
739 menuColor
= theme
.getColor("tmenu");
740 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
742 // Draw the menu title
743 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
745 getScreen().putStringXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
746 // Draw the highlight character
747 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
748 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
750 if (menu
.isActive()) {
752 // Reset the screen clipping so we can draw the next title.
753 getScreen().resetClipping();
755 x
+= menu
.getTitle().length() + 2;
758 for (TMenu menu
: subMenus
) {
759 // Reset the screen clipping so we can draw the next sub-menu.
760 getScreen().resetClipping();
764 // Draw the status bar of the top-level window
765 TStatusBar statusBar
= topLevel
.getStatusBar();
766 if (statusBar
!= null) {
767 getScreen().resetClipping();
768 statusBar
.setWidth(getScreen().getWidth());
769 statusBar
.setY(getScreen().getHeight() - topLevel
.getY());
772 CellAttributes barColor
= new CellAttributes();
773 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
774 getScreen().hLineXY(0, desktopBottom
, getScreen().getWidth(), ' ',
778 // Draw the mouse pointer
779 invertCell(mouseX
, mouseY
);
783 // Place the cursor if it is visible
784 TWidget activeWidget
= null;
785 if (sorted
.size() > 0) {
786 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
787 if (activeWidget
.isCursorVisible()) {
788 getScreen().putCursor(true, activeWidget
.getCursorAbsoluteX(),
789 activeWidget
.getCursorAbsoluteY());
796 getScreen().hideCursor();
799 // Flush the screen contents
800 if (getScreen().isDirty()) {
801 backend
.flushScreen();
807 // ------------------------------------------------------------------------
808 // Main loop --------------------------------------------------------------
809 // ------------------------------------------------------------------------
812 * Run this application until it exits.
816 // Timeout is in milliseconds, so default timeout after 1 second
820 // If I've got no updates to render, wait for something from the
821 // backend or a timer.
823 && ((mouseX
== oldMouseX
) && (mouseY
== oldMouseY
))
825 // Never sleep longer than 50 millis. We need time for
826 // windows with background tasks to update the display, and
827 // still flip buffers reasonably quickly in
828 // backend.flushPhysical().
829 timeout
= getSleepTime(50);
833 // As of now, I've got nothing to do: no I/O, nothing from
834 // the consumer threads, no timers that need to run ASAP. So
835 // wait until either the backend or the consumer threads have
839 System
.err
.println("sleep " + timeout
+ " millis");
841 synchronized (this) {
844 } catch (InterruptedException e
) {
845 // I'm awake and don't care why, let's see what's going
851 // Prevent stepping on the primary or secondary event handler.
854 // Pull any pending I/O events
855 backend
.getEvents(fillEventQueue
);
857 // Dispatch each event to the appropriate handler, one at a time.
859 TInputEvent event
= null;
860 if (fillEventQueue
.size() == 0) {
863 event
= fillEventQueue
.remove(0);
864 metaHandleEvent(event
);
867 // Wake a consumer thread if we have any pending events.
868 if (drainEventQueue
.size() > 0) {
872 // Process timers and call doIdle()'s
876 synchronized (getScreen()) {
880 // Let the event handlers run again.
881 startEventHandlers();
885 // Shutdown the event consumer threads
886 if (secondaryEventHandler
!= null) {
887 synchronized (secondaryEventHandler
) {
888 secondaryEventHandler
.notify();
891 if (primaryEventHandler
!= null) {
892 synchronized (primaryEventHandler
) {
893 primaryEventHandler
.notify();
897 // Shutdown the user I/O thread(s)
900 // Close all the windows. This gives them an opportunity to release
907 * Peek at certain application-level events, add to eventQueue, and wake
908 * up the consuming Thread.
910 * @param event the input event to consume
912 private void metaHandleEvent(final TInputEvent event
) {
915 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
916 event
)); System
.err
.flush();
920 // Do no more processing if the application is already trying
925 // Special application-wide events -------------------------------
928 if (event
instanceof TCommandEvent
) {
929 TCommandEvent command
= (TCommandEvent
) event
;
930 if (command
.getCmd().equals(cmAbort
)) {
937 if (event
instanceof TResizeEvent
) {
938 TResizeEvent resize
= (TResizeEvent
) event
;
939 synchronized (getScreen()) {
940 getScreen().setDimensions(resize
.getWidth(),
942 desktopBottom
= getScreen().getHeight() - 1;
951 // Peek at the mouse position
952 if (event
instanceof TMouseEvent
) {
953 TMouseEvent mouse
= (TMouseEvent
) event
;
954 synchronized (getScreen()) {
955 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
958 mouseX
= mouse
.getX();
959 mouseY
= mouse
.getY();
964 // Put into the main queue
965 drainEventQueue
.add(event
);
969 * Dispatch one event to the appropriate widget or application-level
970 * event handler. This is the primary event handler, it has the normal
971 * application-wide event handling.
973 * @param event the input event to consume
974 * @see #secondaryHandleEvent(TInputEvent event)
976 private void primaryHandleEvent(final TInputEvent event
) {
979 System
.err
.printf("Handle event: %s\n", event
);
982 // Special application-wide events -----------------------------------
984 // Peek at the mouse position
985 if (event
instanceof TMouseEvent
) {
986 // See if we need to switch focus to another window or the menu
987 checkSwitchFocus((TMouseEvent
) event
);
990 // Handle menu events
991 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
992 TMenu menu
= activeMenu
;
994 if (event
instanceof TMouseEvent
) {
995 TMouseEvent mouse
= (TMouseEvent
) event
;
997 while (subMenus
.size() > 0) {
998 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
999 if (subMenu
.mouseWouldHit(mouse
)) {
1002 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1003 && (!mouse
.isMouse1())
1004 && (!mouse
.isMouse2())
1005 && (!mouse
.isMouse3())
1006 && (!mouse
.isMouseWheelUp())
1007 && (!mouse
.isMouseWheelDown())
1011 // We navigated away from a sub-menu, so close it
1015 // Convert the mouse relative x/y to menu coordinates
1016 assert (mouse
.getX() == mouse
.getAbsoluteX());
1017 assert (mouse
.getY() == mouse
.getAbsoluteY());
1018 if (subMenus
.size() > 0) {
1019 menu
= subMenus
.get(subMenus
.size() - 1);
1021 mouse
.setX(mouse
.getX() - menu
.getX());
1022 mouse
.setY(mouse
.getY() - menu
.getY());
1024 menu
.handleEvent(event
);
1028 if (event
instanceof TKeypressEvent
) {
1029 TKeypressEvent keypress
= (TKeypressEvent
) event
;
1031 // See if this key matches an accelerator, and is not being
1032 // shortcutted by the active window, and if so dispatch the menu
1034 boolean windowWillShortcut
= false;
1035 for (TWindow window
: windows
) {
1036 if (window
.isActive()) {
1037 if (window
.isShortcutKeypress(keypress
.getKey())) {
1038 // We do not process this key, it will be passed to
1039 // the window instead.
1040 windowWillShortcut
= true;
1045 if (!windowWillShortcut
&& !modalWindowActive()) {
1046 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
1047 TMenuItem item
= null;
1048 synchronized (accelerators
) {
1049 item
= accelerators
.get(keypressLowercase
);
1052 if (item
.isEnabled()) {
1053 // Let the menu item dispatch
1059 // Handle the keypress
1060 if (onKeypress(keypress
)) {
1066 if (event
instanceof TCommandEvent
) {
1067 if (onCommand((TCommandEvent
) event
)) {
1072 if (event
instanceof TMenuEvent
) {
1073 if (onMenu((TMenuEvent
) event
)) {
1078 // Dispatch events to the active window -------------------------------
1079 for (TWindow window
: windows
) {
1080 if (window
.isActive()) {
1081 if (event
instanceof TMouseEvent
) {
1082 TMouseEvent mouse
= (TMouseEvent
) event
;
1083 // Convert the mouse relative x/y to window coordinates
1084 assert (mouse
.getX() == mouse
.getAbsoluteX());
1085 assert (mouse
.getY() == mouse
.getAbsoluteY());
1086 mouse
.setX(mouse
.getX() - window
.getX());
1087 mouse
.setY(mouse
.getY() - window
.getY());
1090 System
.err
.printf("TApplication dispatch event: %s\n",
1093 window
.handleEvent(event
);
1099 * Dispatch one event to the appropriate widget or application-level
1100 * event handler. This is the secondary event handler used by certain
1101 * special dialogs (currently TMessageBox and TFileOpenBox).
1103 * @param event the input event to consume
1104 * @see #primaryHandleEvent(TInputEvent event)
1106 private void secondaryHandleEvent(final TInputEvent event
) {
1107 secondaryEventReceiver
.handleEvent(event
);
1111 * Enable a widget to override the primary event thread.
1113 * @param widget widget that will receive events
1115 public final void enableSecondaryEventReceiver(final TWidget widget
) {
1116 assert (secondaryEventReceiver
== null);
1117 assert (secondaryEventHandler
== null);
1118 assert ((widget
instanceof TMessageBox
)
1119 || (widget
instanceof TFileOpenBox
));
1120 secondaryEventReceiver
= widget
;
1121 secondaryEventHandler
= new WidgetEventHandler(this, false);
1122 (new Thread(secondaryEventHandler
)).start();
1126 * Yield to the secondary thread.
1128 public final void yield() {
1129 assert (secondaryEventReceiver
!= null);
1130 // This is where we handoff the event handler lock from the primary
1131 // to secondary thread. We unlock here, and in a future loop the
1132 // secondary thread locks again. When it gives up, we have the
1133 // single lock back.
1134 boolean oldLock
= unlockHandleEvent();
1137 while (secondaryEventReceiver
!= null) {
1138 synchronized (primaryEventHandler
) {
1140 primaryEventHandler
.wait();
1141 } catch (InterruptedException e
) {
1149 * Do stuff when there is no user input.
1151 private void doIdle() {
1153 System
.err
.printf("doIdle()\n");
1156 // Now run any timers that have timed out
1157 Date now
= new Date();
1158 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
1159 for (TTimer timer
: timers
) {
1160 if (timer
.getNextTick().getTime() <= now
.getTime()) {
1162 if (timer
.recurring
) {
1163 keepTimers
.add(timer
);
1166 keepTimers
.add(timer
);
1169 timers
= keepTimers
;
1172 for (TWindow window
: windows
) {
1177 // ------------------------------------------------------------------------
1178 // TWindow management -----------------------------------------------------
1179 // ------------------------------------------------------------------------
1182 * Close window. Note that the window's destructor is NOT called by this
1183 * method, instead the GC is assumed to do the cleanup.
1185 * @param window the window to remove
1187 public final void closeWindow(final TWindow window
) {
1188 synchronized (windows
) {
1189 int z
= window
.getZ();
1192 Collections
.sort(windows
);
1194 TWindow activeWindow
= null;
1195 for (TWindow w
: windows
) {
1197 w
.setZ(w
.getZ() - 1);
1198 if (w
.getZ() == 0) {
1201 assert (activeWindow
== null);
1213 // Perform window cleanup
1216 // Check if we are closing a TMessageBox or similar
1217 if (secondaryEventReceiver
!= null) {
1218 assert (secondaryEventHandler
!= null);
1220 // Do not send events to the secondaryEventReceiver anymore, the
1221 // window is closed.
1222 secondaryEventReceiver
= null;
1224 // Wake the secondary thread, it will wake the primary as it
1226 synchronized (secondaryEventHandler
) {
1227 secondaryEventHandler
.notify();
1233 * Switch to the next window.
1235 * @param forward if true, then switch to the next window in the list,
1236 * otherwise switch to the previous window in the list
1238 public final void switchWindow(final boolean forward
) {
1239 // Only switch if there are multiple windows
1240 if (windows
.size() < 2) {
1244 synchronized (windows
) {
1246 // Swap z/active between active window and the next in the list
1247 int activeWindowI
= -1;
1248 for (int i
= 0; i
< windows
.size(); i
++) {
1249 if (windows
.get(i
).isActive()) {
1254 assert (activeWindowI
>= 0);
1256 // Do not switch if a window is modal
1257 if (windows
.get(activeWindowI
).isModal()) {
1263 nextWindowI
= (activeWindowI
+ 1) % windows
.size();
1265 if (activeWindowI
== 0) {
1266 nextWindowI
= windows
.size() - 1;
1268 nextWindowI
= activeWindowI
- 1;
1271 windows
.get(activeWindowI
).setActive(false);
1272 windows
.get(activeWindowI
).setZ(windows
.get(nextWindowI
).getZ());
1273 windows
.get(activeWindowI
).onUnfocus();
1274 windows
.get(nextWindowI
).setZ(0);
1275 windows
.get(nextWindowI
).setActive(true);
1276 windows
.get(nextWindowI
).onFocus();
1278 } // synchronized (windows)
1283 * Add a window to my window list and make it active.
1285 * @param window new window to add
1287 public final void addWindow(final TWindow window
) {
1288 synchronized (windows
) {
1289 // Do not allow a modal window to spawn a non-modal window. If a
1290 // modal window is active, then this window will become modal
1292 if (modalWindowActive()) {
1293 window
.flags
|= TWindow
.MODAL
;
1295 for (TWindow w
: windows
) {
1300 w
.setZ(w
.getZ() + 1);
1302 windows
.add(window
);
1304 window
.setActive(true);
1310 * Check if there is a system-modal window on top.
1312 * @return true if the active window is modal
1314 private boolean modalWindowActive() {
1315 if (windows
.size() == 0) {
1319 for (TWindow w
: windows
) {
1329 * Close all open windows.
1331 private void closeAllWindows() {
1332 // Don't do anything if we are in the menu
1333 if (activeMenu
!= null) {
1336 while (windows
.size() > 0) {
1337 closeWindow(windows
.get(0));
1342 * Re-layout the open windows as non-overlapping tiles. This produces
1343 * almost the same results as Turbo Pascal 7.0's IDE.
1345 private void tileWindows() {
1346 synchronized (windows
) {
1347 // Don't do anything if we are in the menu
1348 if (activeMenu
!= null) {
1351 int z
= windows
.size();
1357 a
= (int)(Math
.sqrt(z
));
1361 if (((a
* b
) + c
) == z
) {
1369 int newWidth
= (getScreen().getWidth() / a
);
1370 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
1371 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
1373 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1374 Collections
.sort(sorted
);
1375 Collections
.reverse(sorted
);
1376 for (int i
= 0; i
< sorted
.size(); i
++) {
1377 int logicalX
= i
/ b
;
1378 int logicalY
= i
% b
;
1379 if (i
>= ((a
- 1) * b
)) {
1381 logicalY
= i
- ((a
- 1) * b
);
1384 TWindow w
= sorted
.get(i
);
1385 w
.setX(logicalX
* newWidth
);
1386 w
.setWidth(newWidth
);
1387 if (i
>= ((a
- 1) * b
)) {
1388 w
.setY((logicalY
* newHeight2
) + 1);
1389 w
.setHeight(newHeight2
);
1391 w
.setY((logicalY
* newHeight1
) + 1);
1392 w
.setHeight(newHeight1
);
1399 * Re-layout the open windows as overlapping cascaded windows.
1401 private void cascadeWindows() {
1402 synchronized (windows
) {
1403 // Don't do anything if we are in the menu
1404 if (activeMenu
!= null) {
1409 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1410 Collections
.sort(sorted
);
1411 Collections
.reverse(sorted
);
1412 for (TWindow window
: sorted
) {
1417 if (x
> getScreen().getWidth()) {
1420 if (y
>= getScreen().getHeight()) {
1427 // ------------------------------------------------------------------------
1428 // TMenu management -------------------------------------------------------
1429 // ------------------------------------------------------------------------
1432 * Check if a mouse event would hit either the active menu or any open
1435 * @param mouse mouse event
1436 * @return true if the mouse would hit the active menu or an open
1439 private boolean mouseOnMenu(final TMouseEvent mouse
) {
1440 assert (activeMenu
!= null);
1441 List
<TMenu
> menus
= new LinkedList
<TMenu
>(subMenus
);
1442 Collections
.reverse(menus
);
1443 for (TMenu menu
: menus
) {
1444 if (menu
.mouseWouldHit(mouse
)) {
1448 return activeMenu
.mouseWouldHit(mouse
);
1452 * See if we need to switch window or activate the menu based on
1455 * @param mouse mouse event
1457 private void checkSwitchFocus(final TMouseEvent mouse
) {
1459 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1460 && (activeMenu
!= null)
1461 && (mouse
.getAbsoluteY() != 0)
1462 && (!mouseOnMenu(mouse
))
1464 // They clicked outside the active menu, turn it off
1465 activeMenu
.setActive(false);
1467 for (TMenu menu
: subMenus
) {
1468 menu
.setActive(false);
1474 // See if they hit the menu bar
1475 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1476 && (mouse
.isMouse1())
1477 && (!modalWindowActive())
1478 && (mouse
.getAbsoluteY() == 0)
1481 for (TMenu menu
: subMenus
) {
1482 menu
.setActive(false);
1486 // They selected the menu, go activate it
1487 for (TMenu menu
: menus
) {
1488 if ((mouse
.getAbsoluteX() >= menu
.getX())
1489 && (mouse
.getAbsoluteX() < menu
.getX()
1490 + menu
.getTitle().length() + 2)
1492 menu
.setActive(true);
1495 menu
.setActive(false);
1501 // See if they hit the menu bar
1502 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1503 && (mouse
.isMouse1())
1504 && (activeMenu
!= null)
1505 && (mouse
.getAbsoluteY() == 0)
1508 TMenu oldMenu
= activeMenu
;
1509 for (TMenu menu
: subMenus
) {
1510 menu
.setActive(false);
1514 // See if we should switch menus
1515 for (TMenu menu
: menus
) {
1516 if ((mouse
.getAbsoluteX() >= menu
.getX())
1517 && (mouse
.getAbsoluteX() < menu
.getX()
1518 + menu
.getTitle().length() + 2)
1520 menu
.setActive(true);
1524 if (oldMenu
!= activeMenu
) {
1525 // They switched menus
1526 oldMenu
.setActive(false);
1531 // Only switch if there are multiple windows
1532 if (windows
.size() < 2) {
1536 // Switch on the upclick
1537 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
) {
1541 synchronized (windows
) {
1542 Collections
.sort(windows
);
1543 if (windows
.get(0).isModal()) {
1544 // Modal windows don't switch
1548 for (TWindow window
: windows
) {
1549 assert (!window
.isModal());
1550 if (window
.mouseWouldHit(mouse
)) {
1551 if (window
== windows
.get(0)) {
1552 // Clicked on the same window, nothing to do
1556 // We will be switching to another window
1557 assert (windows
.get(0).isActive());
1558 assert (!window
.isActive());
1559 windows
.get(0).onUnfocus();
1560 windows
.get(0).setActive(false);
1561 windows
.get(0).setZ(window
.getZ());
1563 window
.setActive(true);
1570 // Clicked on the background, nothing to do
1575 * Turn off the menu.
1577 public final void closeMenu() {
1578 if (activeMenu
!= null) {
1579 activeMenu
.setActive(false);
1581 for (TMenu menu
: subMenus
) {
1582 menu
.setActive(false);
1589 * Turn off a sub-menu.
1591 public final void closeSubMenu() {
1592 assert (activeMenu
!= null);
1593 TMenu item
= subMenus
.get(subMenus
.size() - 1);
1594 assert (item
!= null);
1595 item
.setActive(false);
1596 subMenus
.remove(subMenus
.size() - 1);
1600 * Switch to the next menu.
1602 * @param forward if true, then switch to the next menu in the list,
1603 * otherwise switch to the previous menu in the list
1605 public final void switchMenu(final boolean forward
) {
1606 assert (activeMenu
!= null);
1608 for (TMenu menu
: subMenus
) {
1609 menu
.setActive(false);
1613 for (int i
= 0; i
< menus
.size(); i
++) {
1614 if (activeMenu
== menus
.get(i
)) {
1616 if (i
< menus
.size() - 1) {
1624 activeMenu
.setActive(false);
1625 activeMenu
= menus
.get(i
);
1626 activeMenu
.setActive(true);
1633 * Add a menu item to the global list. If it has a keyboard accelerator,
1634 * that will be added the global hash.
1636 * @param item the menu item
1638 public final void addMenuItem(final TMenuItem item
) {
1639 menuItems
.add(item
);
1641 TKeypress key
= item
.getKey();
1643 synchronized (accelerators
) {
1644 assert (accelerators
.get(key
) == null);
1645 accelerators
.put(key
.toLowerCase(), item
);
1651 * Disable one menu item.
1653 * @param id the menu item ID
1655 public final void disableMenuItem(final int id
) {
1656 for (TMenuItem item
: menuItems
) {
1657 if (item
.getId() == id
) {
1658 item
.setEnabled(false);
1664 * Disable the range of menu items with ID's between lower and upper,
1667 * @param lower the lowest menu item ID
1668 * @param upper the highest menu item ID
1670 public final void disableMenuItems(final int lower
, final int upper
) {
1671 for (TMenuItem item
: menuItems
) {
1672 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
1673 item
.setEnabled(false);
1679 * Enable one menu item.
1681 * @param id the menu item ID
1683 public final void enableMenuItem(final int id
) {
1684 for (TMenuItem item
: menuItems
) {
1685 if (item
.getId() == id
) {
1686 item
.setEnabled(true);
1692 * Enable the range of menu items with ID's between lower and upper,
1695 * @param lower the lowest menu item ID
1696 * @param upper the highest menu item ID
1698 public final void enableMenuItems(final int lower
, final int upper
) {
1699 for (TMenuItem item
: menuItems
) {
1700 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
1701 item
.setEnabled(true);
1707 * Recompute menu x positions based on their title length.
1709 public final void recomputeMenuX() {
1711 for (TMenu menu
: menus
) {
1713 x
+= menu
.getTitle().length() + 2;
1718 * Post an event to process and turn off the menu.
1720 * @param event new event to add to the queue
1722 public final void postMenuEvent(final TInputEvent event
) {
1723 synchronized (fillEventQueue
) {
1724 fillEventQueue
.add(event
);
1730 * Add a sub-menu to the list of open sub-menus.
1732 * @param menu sub-menu
1734 public final void addSubMenu(final TMenu menu
) {
1739 * Convenience function to add a top-level menu.
1741 * @param title menu title
1742 * @return the new menu
1744 public final TMenu
addMenu(final String title
) {
1747 TMenu menu
= new TMenu(this, x
, y
, title
);
1754 * Convenience function to add a default "File" menu.
1756 * @return the new menu
1758 public final TMenu
addFileMenu() {
1759 TMenu fileMenu
= addMenu("&File");
1760 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
1761 fileMenu
.addSeparator();
1762 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
1763 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
1764 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management " +
1765 "commands (Open, Save, Print, etc.)");
1766 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
1771 * Convenience function to add a default "Edit" menu.
1773 * @return the new menu
1775 public final TMenu
addEditMenu() {
1776 TMenu editMenu
= addMenu("&Edit");
1777 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
1778 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
1779 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
1780 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
1781 TStatusBar statusBar
= editMenu
.newStatusBar("Editor operations, " +
1782 "undo, and Clipboard access");
1783 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
1788 * Convenience function to add a default "Window" menu.
1790 * @return the new menu
1792 public final TMenu
addWindowMenu() {
1793 TMenu windowMenu
= addMenu("&Window");
1794 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
1795 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
1796 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
1797 windowMenu
.addSeparator();
1798 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
1799 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
1800 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
1801 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
1802 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
1803 TStatusBar statusBar
= windowMenu
.newStatusBar("Open, arrange, and " +
1805 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
1810 * Convenience function to add a default "Help" menu.
1812 * @return the new menu
1814 public final TMenu
addHelpMenu() {
1815 TMenu helpMenu
= addMenu("&Help");
1816 helpMenu
.addDefaultItem(TMenu
.MID_HELP_CONTENTS
);
1817 helpMenu
.addDefaultItem(TMenu
.MID_HELP_INDEX
);
1818 helpMenu
.addDefaultItem(TMenu
.MID_HELP_SEARCH
);
1819 helpMenu
.addDefaultItem(TMenu
.MID_HELP_PREVIOUS
);
1820 helpMenu
.addDefaultItem(TMenu
.MID_HELP_HELP
);
1821 helpMenu
.addDefaultItem(TMenu
.MID_HELP_ACTIVE_FILE
);
1822 helpMenu
.addSeparator();
1823 helpMenu
.addDefaultItem(TMenu
.MID_ABOUT
);
1824 TStatusBar statusBar
= helpMenu
.newStatusBar("Access online help");
1825 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
1829 // ------------------------------------------------------------------------
1830 // Event handlers ---------------------------------------------------------
1831 // ------------------------------------------------------------------------
1834 * Method that TApplication subclasses can override to handle menu or
1835 * posted command events.
1837 * @param command command event
1838 * @return if true, this event was consumed
1840 protected boolean onCommand(final TCommandEvent command
) {
1841 // Default: handle cmExit
1842 if (command
.equals(cmExit
)) {
1843 if (messageBox("Confirmation", "Exit application?",
1844 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
1850 if (command
.equals(cmShell
)) {
1851 openTerminal(0, 0, TWindow
.RESIZABLE
);
1855 if (command
.equals(cmTile
)) {
1859 if (command
.equals(cmCascade
)) {
1863 if (command
.equals(cmCloseAll
)) {
1872 * Method that TApplication subclasses can override to handle menu
1875 * @param menu menu event
1876 * @return if true, this event was consumed
1878 protected boolean onMenu(final TMenuEvent menu
) {
1880 // Default: handle MID_EXIT
1881 if (menu
.getId() == TMenu
.MID_EXIT
) {
1882 if (messageBox("Confirmation", "Exit application?",
1883 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
1889 if (menu
.getId() == TMenu
.MID_SHELL
) {
1890 openTerminal(0, 0, TWindow
.RESIZABLE
);
1894 if (menu
.getId() == TMenu
.MID_TILE
) {
1898 if (menu
.getId() == TMenu
.MID_CASCADE
) {
1902 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
1906 if (menu
.getId() == TMenu
.MID_ABOUT
) {
1914 * Method that TApplication subclasses can override to handle keystrokes.
1916 * @param keypress keystroke event
1917 * @return if true, this event was consumed
1919 protected boolean onKeypress(final TKeypressEvent keypress
) {
1920 // Default: only menu shortcuts
1922 // Process Alt-F, Alt-E, etc. menu shortcut keys
1923 if (!keypress
.getKey().isFnKey()
1924 && keypress
.getKey().isAlt()
1925 && !keypress
.getKey().isCtrl()
1926 && (activeMenu
== null)
1927 && !modalWindowActive()
1930 assert (subMenus
.size() == 0);
1932 for (TMenu menu
: menus
) {
1933 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
1934 == Character
.toLowerCase(keypress
.getKey().getChar())
1937 menu
.setActive(true);
1946 // ------------------------------------------------------------------------
1947 // TTimer management ------------------------------------------------------
1948 // ------------------------------------------------------------------------
1951 * Get the amount of time I can sleep before missing a Timer tick.
1953 * @param timeout = initial (maximum) timeout in millis
1954 * @return number of milliseconds between now and the next timer event
1956 private long getSleepTime(final long timeout
) {
1957 Date now
= new Date();
1958 long nowTime
= now
.getTime();
1959 long sleepTime
= timeout
;
1960 for (TTimer timer
: timers
) {
1961 long nextTickTime
= timer
.getNextTick().getTime();
1962 if (nextTickTime
< nowTime
) {
1966 long timeDifference
= nextTickTime
- nowTime
;
1967 if (timeDifference
< sleepTime
) {
1968 sleepTime
= timeDifference
;
1971 assert (sleepTime
>= 0);
1972 assert (sleepTime
<= timeout
);
1977 * Convenience function to add a timer.
1979 * @param duration number of milliseconds to wait between ticks
1980 * @param recurring if true, re-schedule this timer after every tick
1981 * @param action function to call when button is pressed
1984 public final TTimer
addTimer(final long duration
, final boolean recurring
,
1985 final TAction action
) {
1987 TTimer timer
= new TTimer(duration
, recurring
, action
);
1988 synchronized (timers
) {
1995 * Convenience function to remove a timer.
1997 * @param timer timer to remove
1999 public final void removeTimer(final TTimer timer
) {
2000 synchronized (timers
) {
2001 timers
.remove(timer
);
2005 // ------------------------------------------------------------------------
2006 // Other TWindow constructors ---------------------------------------------
2007 // ------------------------------------------------------------------------
2010 * Convenience function to spawn a message box.
2012 * @param title window title, will be centered along the top border
2013 * @param caption message to display. Use embedded newlines to get a
2015 * @return the new message box
2017 public final TMessageBox
messageBox(final String title
,
2018 final String caption
) {
2020 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
2024 * Convenience function to spawn a message box.
2026 * @param title window title, will be centered along the top border
2027 * @param caption message to display. Use embedded newlines to get a
2029 * @param type one of the TMessageBox.Type constants. Default is
2031 * @return the new message box
2033 public final TMessageBox
messageBox(final String title
,
2034 final String caption
, final TMessageBox
.Type type
) {
2036 return new TMessageBox(this, title
, caption
, type
);
2040 * Convenience function to spawn an input box.
2042 * @param title window title, will be centered along the top border
2043 * @param caption message to display. Use embedded newlines to get a
2045 * @return the new input box
2047 public final TInputBox
inputBox(final String title
, final String caption
) {
2049 return new TInputBox(this, title
, caption
);
2053 * Convenience function to spawn an input box.
2055 * @param title window title, will be centered along the top border
2056 * @param caption message to display. Use embedded newlines to get a
2058 * @param text initial text to seed the field with
2059 * @return the new input box
2061 public final TInputBox
inputBox(final String title
, final String caption
,
2062 final String text
) {
2064 return new TInputBox(this, title
, caption
, text
);
2068 * Convenience function to open a terminal window.
2070 * @param x column relative to parent
2071 * @param y row relative to parent
2072 * @return the terminal new window
2074 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
2075 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
2079 * Convenience function to open a terminal window.
2081 * @param x column relative to parent
2082 * @param y row relative to parent
2083 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2084 * @return the terminal new window
2086 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2089 return new TTerminalWindow(this, x
, y
, flags
);
2093 * Convenience function to spawn an file open box.
2095 * @param path path of selected file
2096 * @return the result of the new file open box
2097 * @throws IOException if java.io operation throws
2099 public final String
fileOpenBox(final String path
) throws IOException
{
2101 TFileOpenBox box
= new TFileOpenBox(this, path
, TFileOpenBox
.Type
.OPEN
);
2102 return box
.getFilename();
2106 * Convenience function to spawn an file open box.
2108 * @param path path of selected file
2109 * @param type one of the Type constants
2110 * @return the result of the new file open box
2111 * @throws IOException if java.io operation throws
2113 public final String
fileOpenBox(final String path
,
2114 final TFileOpenBox
.Type type
) throws IOException
{
2116 TFileOpenBox box
= new TFileOpenBox(this, path
, type
);
2117 return box
.getFilename();