2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
.ArrayList
;
38 import java
.util
.Collections
;
39 import java
.util
.Date
;
40 import java
.util
.HashMap
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
44 import java
.util
.ResourceBundle
;
46 import jexer
.bits
.Cell
;
47 import jexer
.bits
.CellAttributes
;
48 import jexer
.bits
.ColorTheme
;
49 import jexer
.event
.TCommandEvent
;
50 import jexer
.event
.TInputEvent
;
51 import jexer
.event
.TKeypressEvent
;
52 import jexer
.event
.TMenuEvent
;
53 import jexer
.event
.TMouseEvent
;
54 import jexer
.event
.TResizeEvent
;
55 import jexer
.backend
.Backend
;
56 import jexer
.backend
.MultiBackend
;
57 import jexer
.backend
.Screen
;
58 import jexer
.backend
.SwingBackend
;
59 import jexer
.backend
.ECMA48Backend
;
60 import jexer
.backend
.TWindowBackend
;
61 import jexer
.menu
.TMenu
;
62 import jexer
.menu
.TMenuItem
;
63 import static jexer
.TCommand
.*;
64 import static jexer
.TKeypress
.*;
67 * TApplication is the main driver class for a full Text User Interface
68 * application. It manages windows, provides a menu bar and status bar, and
69 * processes events received from the user.
71 public class TApplication
implements Runnable
{
76 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TApplication
.class.getName());
78 // ------------------------------------------------------------------------
79 // Constants --------------------------------------------------------------
80 // ------------------------------------------------------------------------
83 * If true, emit thread stuff to System.err.
85 private static final boolean debugThreads
= false;
88 * If true, emit events being processed to System.err.
90 private static final boolean debugEvents
= false;
93 * If true, do "smart placement" on new windows that are not specified to
96 private static final boolean smartWindowPlacement
= true;
99 * Two backend types are available.
101 public static enum BackendType
{
108 * An ECMA48 / ANSI X3.64 / XTERM style terminal.
113 * Synonym for ECMA48.
118 // ------------------------------------------------------------------------
119 // Variables --------------------------------------------------------------
120 // ------------------------------------------------------------------------
123 * The primary event handler thread.
125 private volatile WidgetEventHandler primaryEventHandler
;
128 * The secondary event handler thread.
130 private volatile WidgetEventHandler secondaryEventHandler
;
133 * The widget receiving events from the secondary event handler thread.
135 private volatile TWidget secondaryEventReceiver
;
138 * Access to the physical screen, keyboard, and mouse.
140 private Backend backend
;
143 * Actual mouse coordinate X.
148 * Actual mouse coordinate Y.
153 * Old version of mouse coordinate X.
155 private int oldMouseX
;
158 * Old version mouse coordinate Y.
160 private int oldMouseY
;
163 * Old drawn version of mouse coordinate X.
165 private int oldDrawnMouseX
;
168 * Old drawn version mouse coordinate Y.
170 private int oldDrawnMouseY
;
173 * Old drawn version mouse cell.
175 private Cell oldDrawnMouseCell
= new Cell();
178 * The last mouse up click time, used to determine if this is a mouse
181 private long lastMouseUpTime
;
184 * The amount of millis between mouse up events to assume a double-click.
186 private long doubleClickTime
= 250;
189 * Event queue that is filled by run().
191 private List
<TInputEvent
> fillEventQueue
;
194 * Event queue that will be drained by either primary or secondary
197 private List
<TInputEvent
> drainEventQueue
;
200 * Top-level menus in this application.
202 private List
<TMenu
> menus
;
205 * Stack of activated sub-menus in this application.
207 private List
<TMenu
> subMenus
;
210 * The currently active menu.
212 private TMenu activeMenu
= null;
215 * Active keyboard accelerators.
217 private Map
<TKeypress
, TMenuItem
> accelerators
;
222 private List
<TMenuItem
> menuItems
;
225 * Windows and widgets pull colors from this ColorTheme.
227 private ColorTheme theme
;
230 * The top-level windows (but not menus).
232 private List
<TWindow
> windows
;
235 * The currently acive window.
237 private TWindow activeWindow
= null;
240 * Timers that are being ticked.
242 private List
<TTimer
> timers
;
245 * When true, the application has been started.
247 private volatile boolean started
= false;
250 * When true, exit the application.
252 private volatile boolean quit
= false;
255 * When true, repaint the entire screen.
257 private volatile boolean repaint
= true;
260 * Y coordinate of the top edge of the desktop. For now this is a
261 * constant. Someday it would be nice to have a multi-line menu or
264 private static final int desktopTop
= 1;
267 * Y coordinate of the bottom edge of the desktop.
269 private int desktopBottom
;
272 * An optional TDesktop background window that is drawn underneath
275 private TDesktop desktop
;
278 * If true, focus follows mouse: windows automatically raised if the
279 * mouse passes over them.
281 private boolean focusFollowsMouse
= false;
284 * The images that might be displayed. Note package private access.
286 private List
<TImage
> images
;
289 * The list of commands to run before the next I/O check.
291 private List
<Runnable
> invokeLaters
= new LinkedList
<Runnable
>();
294 * WidgetEventHandler is the main event consumer loop. There are at most
295 * two such threads in existence: the primary for normal case and a
296 * secondary that is used for TMessageBox, TInputBox, and similar.
298 private class WidgetEventHandler
implements Runnable
{
300 * The main application.
302 private TApplication application
;
305 * Whether or not this WidgetEventHandler is the primary or secondary
308 private boolean primary
= true;
311 * Public constructor.
313 * @param application the main application
314 * @param primary if true, this is the primary event handler thread
316 public WidgetEventHandler(final TApplication application
,
317 final boolean primary
) {
319 this.application
= application
;
320 this.primary
= primary
;
327 // Wrap everything in a try, so that if we go belly up we can let
328 // the user have their terminal back.
331 } catch (Throwable t
) {
332 this.application
.restoreConsole();
334 this.application
.exit();
341 private void runImpl() {
342 boolean first
= true;
345 while (!application
.quit
) {
347 // Wait until application notifies me
348 while (!application
.quit
) {
350 synchronized (application
.drainEventQueue
) {
351 if (application
.drainEventQueue
.size() > 0) {
360 timeout
= application
.getSleepTime(1000);
364 // A timer needs to fire, break out.
369 System
.err
.printf("%d %s %s %s sleep %d millis\n",
370 System
.currentTimeMillis(), this,
371 primary ?
"primary" : "secondary",
372 Thread
.currentThread(), timeout
);
375 synchronized (this) {
380 System
.err
.printf("%d %s %s %s AWAKE\n",
381 System
.currentTimeMillis(), this,
382 primary ?
"primary" : "secondary",
383 Thread
.currentThread());
387 && (application
.secondaryEventReceiver
== null)
389 // Secondary thread, emergency exit. If we got
390 // here then something went wrong with the
391 // handoff between yield() and closeWindow().
392 synchronized (application
.primaryEventHandler
) {
393 application
.primaryEventHandler
.notify();
395 application
.secondaryEventHandler
= null;
396 throw new RuntimeException("secondary exited " +
400 } catch (InterruptedException e
) {
403 } // while (!application.quit)
405 // Pull all events off the queue
407 TInputEvent event
= null;
408 synchronized (application
.drainEventQueue
) {
409 if (application
.drainEventQueue
.size() == 0) {
412 event
= application
.drainEventQueue
.remove(0);
415 // We will have an event to process, so repaint the
416 // screen at the end.
417 application
.repaint
= true;
420 primaryHandleEvent(event
);
422 secondaryHandleEvent(event
);
425 && (application
.secondaryEventReceiver
== null)
427 // Secondary thread, time to exit.
429 // Eliminate my reference so that wakeEventHandler()
430 // resumes working on the primary.
431 application
.secondaryEventHandler
= null;
433 // DO NOT UNLOCK. Primary thread just came back from
434 // primaryHandleEvent() and will unlock in the else
435 // block below. Just wake it up.
436 synchronized (application
.primaryEventHandler
) {
437 application
.primaryEventHandler
.notify();
446 // Fire timers, update screen.
448 application
.finishEventProcessing();
451 } // while (true) (main runnable loop)
455 // ------------------------------------------------------------------------
456 // Constructors -----------------------------------------------------------
457 // ------------------------------------------------------------------------
460 * Public constructor.
462 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
464 * @param windowWidth the number of text columns to start with
465 * @param windowHeight the number of text rows to start with
466 * @param fontSize the size in points
467 * @throws UnsupportedEncodingException if an exception is thrown when
468 * creating the InputStreamReader
470 public TApplication(final BackendType backendType
, final int windowWidth
,
471 final int windowHeight
, final int fontSize
)
472 throws UnsupportedEncodingException
{
474 switch (backendType
) {
476 backend
= new SwingBackend(this, windowWidth
, windowHeight
,
482 backend
= new ECMA48Backend(this, null, null, windowWidth
,
483 windowHeight
, fontSize
);
486 throw new IllegalArgumentException("Invalid backend type: "
493 * Public constructor.
495 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
497 * @throws UnsupportedEncodingException if an exception is thrown when
498 * creating the InputStreamReader
500 public TApplication(final BackendType backendType
)
501 throws UnsupportedEncodingException
{
503 switch (backendType
) {
505 // The default SwingBackend is 80x25, 20 pt font. If you want to
506 // change that, you can pass the extra arguments to the
507 // SwingBackend constructor here. For example, if you wanted
508 // 90x30, 16 pt font:
510 // backend = new SwingBackend(this, 90, 30, 16);
511 backend
= new SwingBackend(this);
516 backend
= new ECMA48Backend(this, null, null);
519 throw new IllegalArgumentException("Invalid backend type: "
526 * Public constructor. The backend type will be BackendType.ECMA48.
528 * @param input an InputStream connected to the remote user, or null for
529 * System.in. If System.in is used, then on non-Windows systems it will
530 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
531 * mode. input is always converted to a Reader with UTF-8 encoding.
532 * @param output an OutputStream connected to the remote user, or null
533 * for System.out. output is always converted to a Writer with UTF-8
535 * @throws UnsupportedEncodingException if an exception is thrown when
536 * creating the InputStreamReader
538 public TApplication(final InputStream input
,
539 final OutputStream output
) throws UnsupportedEncodingException
{
541 backend
= new ECMA48Backend(this, input
, output
);
546 * Public constructor. The backend type will be BackendType.ECMA48.
548 * @param input the InputStream underlying 'reader'. Its available()
549 * method is used to determine if reader.read() will block or not.
550 * @param reader a Reader connected to the remote user.
551 * @param writer a PrintWriter connected to the remote user.
552 * @param setRawMode if true, set System.in into raw mode with stty.
553 * This should in general not be used. It is here solely for Demo3,
554 * which uses System.in.
555 * @throws IllegalArgumentException if input, reader, or writer are null.
557 public TApplication(final InputStream input
, final Reader reader
,
558 final PrintWriter writer
, final boolean setRawMode
) {
560 backend
= new ECMA48Backend(this, input
, reader
, writer
, setRawMode
);
565 * Public constructor. The backend type will be BackendType.ECMA48.
567 * @param input the InputStream underlying 'reader'. Its available()
568 * method is used to determine if reader.read() will block or not.
569 * @param reader a Reader connected to the remote user.
570 * @param writer a PrintWriter connected to the remote user.
571 * @throws IllegalArgumentException if input, reader, or writer are null.
573 public TApplication(final InputStream input
, final Reader reader
,
574 final PrintWriter writer
) {
576 this(input
, reader
, writer
, false);
580 * Public constructor. This hook enables use with new non-Jexer
583 * @param backend a Backend that is already ready to go.
585 public TApplication(final Backend backend
) {
586 this.backend
= backend
;
587 backend
.setListener(this);
592 * Finish construction once the backend is set.
594 private void TApplicationImpl() {
595 theme
= new ColorTheme();
596 desktopBottom
= getScreen().getHeight() - 1;
597 fillEventQueue
= new LinkedList
<TInputEvent
>();
598 drainEventQueue
= new LinkedList
<TInputEvent
>();
599 windows
= new LinkedList
<TWindow
>();
600 menus
= new ArrayList
<TMenu
>();
601 subMenus
= new ArrayList
<TMenu
>();
602 timers
= new LinkedList
<TTimer
>();
603 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
604 menuItems
= new LinkedList
<TMenuItem
>();
605 desktop
= new TDesktop(this);
606 images
= new LinkedList
<TImage
>();
608 // Special case: the Swing backend needs to have a timer to drive its
610 if ((backend
instanceof SwingBackend
)
611 || (backend
instanceof MultiBackend
)
613 // Default to 500 millis, unless a SwingBackend has its own
616 if (backend
instanceof SwingBackend
) {
617 millis
= ((SwingBackend
) backend
).getBlinkMillis();
620 addTimer(millis
, true,
623 TApplication
.this.doRepaint();
631 // ------------------------------------------------------------------------
632 // Runnable ---------------------------------------------------------------
633 // ------------------------------------------------------------------------
636 * Run this application until it exits.
639 // Start the main consumer thread
640 primaryEventHandler
= new WidgetEventHandler(this, true);
641 (new Thread(primaryEventHandler
)).start();
646 synchronized (this) {
647 boolean doWait
= false;
649 if (!backend
.hasEvents()) {
650 synchronized (fillEventQueue
) {
651 if (fillEventQueue
.size() == 0) {
658 // No I/O to dispatch, so wait until the backend
662 System
.err
.println(System
.currentTimeMillis() +
663 " " + Thread
.currentThread() + " MAIN sleep");
669 System
.err
.println(System
.currentTimeMillis() +
670 " " + Thread
.currentThread() + " MAIN AWAKE");
672 } catch (InterruptedException e
) {
673 // I'm awake and don't care why, let's see what's
674 // going on out there.
678 } // synchronized (this)
680 synchronized (fillEventQueue
) {
681 // Pull any pending I/O events
682 backend
.getEvents(fillEventQueue
);
684 // Dispatch each event to the appropriate handler, one at a
687 TInputEvent event
= null;
688 if (fillEventQueue
.size() == 0) {
691 event
= fillEventQueue
.remove(0);
692 metaHandleEvent(event
);
696 // Wake a consumer thread if we have any pending events.
697 if (drainEventQueue
.size() > 0) {
703 // Shutdown the event consumer threads
704 if (secondaryEventHandler
!= null) {
705 synchronized (secondaryEventHandler
) {
706 secondaryEventHandler
.notify();
709 if (primaryEventHandler
!= null) {
710 synchronized (primaryEventHandler
) {
711 primaryEventHandler
.notify();
715 // Shutdown the user I/O thread(s)
718 // Close all the windows. This gives them an opportunity to release
724 // ------------------------------------------------------------------------
725 // Event handlers ---------------------------------------------------------
726 // ------------------------------------------------------------------------
729 * Method that TApplication subclasses can override to handle menu or
730 * posted command events.
732 * @param command command event
733 * @return if true, this event was consumed
735 protected boolean onCommand(final TCommandEvent command
) {
736 // Default: handle cmExit
737 if (command
.equals(cmExit
)) {
738 if (messageBox(i18n
.getString("exitDialogTitle"),
739 i18n
.getString("exitDialogText"),
740 TMessageBox
.Type
.YESNO
).isYes()) {
747 if (command
.equals(cmShell
)) {
748 openTerminal(0, 0, TWindow
.RESIZABLE
);
752 if (command
.equals(cmTile
)) {
756 if (command
.equals(cmCascade
)) {
760 if (command
.equals(cmCloseAll
)) {
765 if (command
.equals(cmMenu
)) {
766 if (!modalWindowActive() && (activeMenu
== null)) {
767 if (menus
.size() > 0) {
768 menus
.get(0).setActive(true);
769 activeMenu
= menus
.get(0);
779 * Method that TApplication subclasses can override to handle menu
782 * @param menu menu event
783 * @return if true, this event was consumed
785 protected boolean onMenu(final TMenuEvent menu
) {
787 // Default: handle MID_EXIT
788 if (menu
.getId() == TMenu
.MID_EXIT
) {
789 if (messageBox(i18n
.getString("exitDialogTitle"),
790 i18n
.getString("exitDialogText"),
791 TMessageBox
.Type
.YESNO
).isYes()) {
798 if (menu
.getId() == TMenu
.MID_SHELL
) {
799 openTerminal(0, 0, TWindow
.RESIZABLE
);
803 if (menu
.getId() == TMenu
.MID_TILE
) {
807 if (menu
.getId() == TMenu
.MID_CASCADE
) {
811 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
815 if (menu
.getId() == TMenu
.MID_REPAINT
) {
816 getScreen().clearPhysical();
824 * Method that TApplication subclasses can override to handle keystrokes.
826 * @param keypress keystroke event
827 * @return if true, this event was consumed
829 protected boolean onKeypress(final TKeypressEvent keypress
) {
830 // Default: only menu shortcuts
832 // Process Alt-F, Alt-E, etc. menu shortcut keys
833 if (!keypress
.getKey().isFnKey()
834 && keypress
.getKey().isAlt()
835 && !keypress
.getKey().isCtrl()
836 && (activeMenu
== null)
837 && !modalWindowActive()
840 assert (subMenus
.size() == 0);
842 for (TMenu menu
: menus
) {
843 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
844 == Character
.toLowerCase(keypress
.getKey().getChar())
847 menu
.setActive(true);
857 * Process background events, and update the screen.
859 private void finishEventProcessing() {
861 System
.err
.printf(System
.currentTimeMillis() + " " +
862 Thread
.currentThread() + " finishEventProcessing()\n");
865 // Process timers and call doIdle()'s
869 synchronized (getScreen()) {
874 System
.err
.printf(System
.currentTimeMillis() + " " +
875 Thread
.currentThread() + " finishEventProcessing() END\n");
880 * Peek at certain application-level events, add to eventQueue, and wake
881 * up the consuming Thread.
883 * @param event the input event to consume
885 private void metaHandleEvent(final TInputEvent event
) {
888 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
889 event
)); System
.err
.flush();
893 // Do no more processing if the application is already trying
898 // Special application-wide events -------------------------------
901 if (event
instanceof TCommandEvent
) {
902 TCommandEvent command
= (TCommandEvent
) event
;
903 if (command
.getCmd().equals(cmAbort
)) {
909 synchronized (drainEventQueue
) {
911 if (event
instanceof TResizeEvent
) {
912 TResizeEvent resize
= (TResizeEvent
) event
;
913 synchronized (getScreen()) {
914 getScreen().setDimensions(resize
.getWidth(),
916 desktopBottom
= getScreen().getHeight() - 1;
922 if (desktop
!= null) {
923 desktop
.setDimensions(0, 0, resize
.getWidth(),
924 resize
.getHeight() - 1);
927 // Change menu edges if needed.
930 // We are dirty, redraw the screen.
934 System.err.println("New screen: " + resize.getWidth() +
935 " x " + resize.getHeight());
940 // Put into the main queue
941 drainEventQueue
.add(event
);
946 * Dispatch one event to the appropriate widget or application-level
947 * event handler. This is the primary event handler, it has the normal
948 * application-wide event handling.
950 * @param event the input event to consume
951 * @see #secondaryHandleEvent(TInputEvent event)
953 private void primaryHandleEvent(final TInputEvent event
) {
956 System
.err
.printf("%s primaryHandleEvent: %s\n",
957 Thread
.currentThread(), event
);
959 TMouseEvent doubleClick
= null;
961 // Special application-wide events -----------------------------------
963 // Peek at the mouse position
964 if (event
instanceof TMouseEvent
) {
965 TMouseEvent mouse
= (TMouseEvent
) event
;
966 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
969 mouseX
= mouse
.getX();
970 mouseY
= mouse
.getY();
972 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
973 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
976 // This is a double-click.
977 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
979 mouse
.getX(), mouse
.getY(),
980 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
981 mouse
.isMouse1(), mouse
.isMouse2(),
983 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
986 // The first click of a potential double-click.
987 lastMouseUpTime
= mouse
.getTime().getTime();
992 // See if we need to switch focus to another window or the menu
993 checkSwitchFocus((TMouseEvent
) event
);
996 // Handle menu events
997 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
998 TMenu menu
= activeMenu
;
1000 if (event
instanceof TMouseEvent
) {
1001 TMouseEvent mouse
= (TMouseEvent
) event
;
1003 while (subMenus
.size() > 0) {
1004 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
1005 if (subMenu
.mouseWouldHit(mouse
)) {
1008 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1009 && (!mouse
.isMouse1())
1010 && (!mouse
.isMouse2())
1011 && (!mouse
.isMouse3())
1012 && (!mouse
.isMouseWheelUp())
1013 && (!mouse
.isMouseWheelDown())
1017 // We navigated away from a sub-menu, so close it
1021 // Convert the mouse relative x/y to menu coordinates
1022 assert (mouse
.getX() == mouse
.getAbsoluteX());
1023 assert (mouse
.getY() == mouse
.getAbsoluteY());
1024 if (subMenus
.size() > 0) {
1025 menu
= subMenus
.get(subMenus
.size() - 1);
1027 mouse
.setX(mouse
.getX() - menu
.getX());
1028 mouse
.setY(mouse
.getY() - menu
.getY());
1030 menu
.handleEvent(event
);
1034 if (event
instanceof TKeypressEvent
) {
1035 TKeypressEvent keypress
= (TKeypressEvent
) event
;
1037 // See if this key matches an accelerator, and is not being
1038 // shortcutted by the active window, and if so dispatch the menu
1040 boolean windowWillShortcut
= false;
1041 if (activeWindow
!= null) {
1042 assert (activeWindow
.isShown());
1043 if (activeWindow
.isShortcutKeypress(keypress
.getKey())) {
1044 // We do not process this key, it will be passed to the
1046 windowWillShortcut
= true;
1050 if (!windowWillShortcut
&& !modalWindowActive()) {
1051 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
1052 TMenuItem item
= null;
1053 synchronized (accelerators
) {
1054 item
= accelerators
.get(keypressLowercase
);
1057 if (item
.isEnabled()) {
1058 // Let the menu item dispatch
1064 // Handle the keypress
1065 if (onKeypress(keypress
)) {
1071 if (event
instanceof TCommandEvent
) {
1072 if (onCommand((TCommandEvent
) event
)) {
1077 if (event
instanceof TMenuEvent
) {
1078 if (onMenu((TMenuEvent
) event
)) {
1083 // Dispatch events to the active window -------------------------------
1084 boolean dispatchToDesktop
= true;
1085 TWindow window
= activeWindow
;
1086 if (window
!= null) {
1087 assert (window
.isActive());
1088 assert (window
.isShown());
1089 if (event
instanceof TMouseEvent
) {
1090 TMouseEvent mouse
= (TMouseEvent
) event
;
1091 // Convert the mouse relative x/y to window coordinates
1092 assert (mouse
.getX() == mouse
.getAbsoluteX());
1093 assert (mouse
.getY() == mouse
.getAbsoluteY());
1094 mouse
.setX(mouse
.getX() - window
.getX());
1095 mouse
.setY(mouse
.getY() - window
.getY());
1097 if (doubleClick
!= null) {
1098 doubleClick
.setX(doubleClick
.getX() - window
.getX());
1099 doubleClick
.setY(doubleClick
.getY() - window
.getY());
1102 if (window
.mouseWouldHit(mouse
)) {
1103 dispatchToDesktop
= false;
1105 } else if (event
instanceof TKeypressEvent
) {
1106 dispatchToDesktop
= false;
1110 System
.err
.printf("TApplication dispatch event: %s\n",
1113 window
.handleEvent(event
);
1114 if (doubleClick
!= null) {
1115 window
.handleEvent(doubleClick
);
1118 if (dispatchToDesktop
) {
1119 // This event is fair game for the desktop to process.
1120 if (desktop
!= null) {
1121 desktop
.handleEvent(event
);
1122 if (doubleClick
!= null) {
1123 desktop
.handleEvent(doubleClick
);
1130 * Dispatch one event to the appropriate widget or application-level
1131 * event handler. This is the secondary event handler used by certain
1132 * special dialogs (currently TMessageBox and TFileOpenBox).
1134 * @param event the input event to consume
1135 * @see #primaryHandleEvent(TInputEvent event)
1137 private void secondaryHandleEvent(final TInputEvent event
) {
1138 TMouseEvent doubleClick
= null;
1141 System
.err
.printf("%s secondaryHandleEvent: %s\n",
1142 Thread
.currentThread(), event
);
1145 // Peek at the mouse position
1146 if (event
instanceof TMouseEvent
) {
1147 TMouseEvent mouse
= (TMouseEvent
) event
;
1148 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1151 mouseX
= mouse
.getX();
1152 mouseY
= mouse
.getY();
1154 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1155 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
1158 // This is a double-click.
1159 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
1161 mouse
.getX(), mouse
.getY(),
1162 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
1163 mouse
.isMouse1(), mouse
.isMouse2(),
1165 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
1168 // The first click of a potential double-click.
1169 lastMouseUpTime
= mouse
.getTime().getTime();
1175 secondaryEventReceiver
.handleEvent(event
);
1176 // Note that it is possible for secondaryEventReceiver to be null
1177 // now, because its handleEvent() might have finished out on the
1178 // secondary thread. So put any extra processing inside a null
1180 if (secondaryEventReceiver
!= null) {
1181 if (doubleClick
!= null) {
1182 secondaryEventReceiver
.handleEvent(doubleClick
);
1188 * Enable a widget to override the primary event thread.
1190 * @param widget widget that will receive events
1192 public final void enableSecondaryEventReceiver(final TWidget widget
) {
1194 System
.err
.println(System
.currentTimeMillis() +
1195 " enableSecondaryEventReceiver()");
1198 assert (secondaryEventReceiver
== null);
1199 assert (secondaryEventHandler
== null);
1200 assert ((widget
instanceof TMessageBox
)
1201 || (widget
instanceof TFileOpenBox
));
1202 secondaryEventReceiver
= widget
;
1203 secondaryEventHandler
= new WidgetEventHandler(this, false);
1205 (new Thread(secondaryEventHandler
)).start();
1209 * Yield to the secondary thread.
1211 public final void yield() {
1213 System
.err
.printf(System
.currentTimeMillis() + " " +
1214 Thread
.currentThread() + " yield()\n");
1217 assert (secondaryEventReceiver
!= null);
1219 while (secondaryEventReceiver
!= null) {
1220 synchronized (primaryEventHandler
) {
1222 primaryEventHandler
.wait();
1223 } catch (InterruptedException e
) {
1231 * Do stuff when there is no user input.
1233 private void doIdle() {
1235 System
.err
.printf(System
.currentTimeMillis() + " " +
1236 Thread
.currentThread() + " doIdle()\n");
1239 synchronized (timers
) {
1242 System
.err
.printf(System
.currentTimeMillis() + " " +
1243 Thread
.currentThread() + " doIdle() 2\n");
1246 // Run any timers that have timed out
1247 Date now
= new Date();
1248 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
1249 for (TTimer timer
: timers
) {
1250 if (timer
.getNextTick().getTime() <= now
.getTime()) {
1251 // Something might change, so repaint the screen.
1254 if (timer
.recurring
) {
1255 keepTimers
.add(timer
);
1258 keepTimers
.add(timer
);
1262 timers
.addAll(keepTimers
);
1266 for (TWindow window
: windows
) {
1269 if (desktop
!= null) {
1273 // Run any invokeLaters
1274 synchronized (invokeLaters
) {
1275 for (Runnable invoke
: invokeLaters
) {
1278 invokeLaters
.clear();
1284 * Wake the sleeping active event handler.
1286 private void wakeEventHandler() {
1291 if (secondaryEventHandler
!= null) {
1292 synchronized (secondaryEventHandler
) {
1293 secondaryEventHandler
.notify();
1296 assert (primaryEventHandler
!= null);
1297 synchronized (primaryEventHandler
) {
1298 primaryEventHandler
.notify();
1303 // ------------------------------------------------------------------------
1304 // TApplication -----------------------------------------------------------
1305 // ------------------------------------------------------------------------
1308 * Place a command on the run queue, and run it before the next round of
1311 * @param command the command to run later
1313 public void invokeLater(final Runnable command
) {
1314 synchronized (invokeLaters
) {
1315 invokeLaters
.add(command
);
1320 * Restore the console to sane defaults. This is meant to be used for
1321 * improper exits (e.g. a caught exception in main()), and should not be
1322 * necessary for normal program termination.
1324 public void restoreConsole() {
1325 if (backend
!= null) {
1326 if (backend
instanceof ECMA48Backend
) {
1335 * @return the Backend
1337 public final Backend
getBackend() {
1344 * @return the Screen
1346 public final Screen
getScreen() {
1347 if (backend
instanceof TWindowBackend
) {
1348 // We are being rendered to a TWindow. We can't use its
1349 // getScreen() method because that is how it is rendering to a
1350 // hardware backend somewhere. Instead use its getOtherScreen()
1352 return ((TWindowBackend
) backend
).getOtherScreen();
1354 return backend
.getScreen();
1359 * Get the color theme.
1363 public final ColorTheme
getTheme() {
1368 * Repaint the screen on the next update.
1370 public void doRepaint() {
1376 * Get Y coordinate of the top edge of the desktop.
1378 * @return Y coordinate of the top edge of the desktop
1380 public final int getDesktopTop() {
1385 * Get Y coordinate of the bottom edge of the desktop.
1387 * @return Y coordinate of the bottom edge of the desktop
1389 public final int getDesktopBottom() {
1390 return desktopBottom
;
1394 * Set the TDesktop instance.
1396 * @param desktop a TDesktop instance, or null to remove the one that is
1399 public final void setDesktop(final TDesktop desktop
) {
1400 if (this.desktop
!= null) {
1401 this.desktop
.onClose();
1403 this.desktop
= desktop
;
1407 * Get the TDesktop instance.
1409 * @return the desktop, or null if it is not set
1411 public final TDesktop
getDesktop() {
1416 * Get the current active window.
1418 * @return the active window, or null if it is not set
1420 public final TWindow
getActiveWindow() {
1421 return activeWindow
;
1425 * Get a (shallow) copy of the window list.
1427 * @return a copy of the list of windows for this application
1429 public final List
<TWindow
> getAllWindows() {
1430 List
<TWindow
> result
= new ArrayList
<TWindow
>();
1431 result
.addAll(windows
);
1436 * Get focusFollowsMouse flag.
1438 * @return true if focus follows mouse: windows automatically raised if
1439 * the mouse passes over them
1441 public boolean getFocusFollowsMouse() {
1442 return focusFollowsMouse
;
1446 * Set focusFollowsMouse flag.
1448 * @param focusFollowsMouse if true, focus follows mouse: windows
1449 * automatically raised if the mouse passes over them
1451 public void setFocusFollowsMouse(final boolean focusFollowsMouse
) {
1452 this.focusFollowsMouse
= focusFollowsMouse
;
1455 // ------------------------------------------------------------------------
1456 // Screen refresh loop ----------------------------------------------------
1457 // ------------------------------------------------------------------------
1460 * Invert the cell color at a position. This is used to track the mouse.
1462 * @param x column position
1463 * @param y row position
1465 private void invertCell(final int x
, final int y
) {
1467 System
.err
.printf("%d %s invertCell() %d %d\n",
1468 System
.currentTimeMillis(), Thread
.currentThread(), x
, y
);
1470 Cell cell
= getScreen().getCharXY(x
, y
);
1471 if (cell
.isImage()) {
1474 if (cell
.getForeColorRGB() < 0) {
1475 cell
.setForeColor(cell
.getForeColor().invert());
1477 cell
.setForeColorRGB(cell
.getForeColorRGB() ^
0x00ffffff);
1479 if (cell
.getBackColorRGB() < 0) {
1480 cell
.setBackColor(cell
.getBackColor().invert());
1482 cell
.setBackColorRGB(cell
.getBackColorRGB() ^
0x00ffffff);
1485 getScreen().putCharXY(x
, y
, cell
);
1491 private void drawAll() {
1492 boolean menuIsActive
= false;
1495 System
.err
.printf("%d %s drawAll() enter\n",
1496 System
.currentTimeMillis(), Thread
.currentThread());
1499 // I don't think this does anything useful anymore...
1502 System
.err
.printf("%d %s drawAll() !repaint\n",
1503 System
.currentTimeMillis(), Thread
.currentThread());
1505 if ((oldDrawnMouseX
!= mouseX
) || (oldDrawnMouseY
!= mouseY
)) {
1507 System
.err
.printf("%d %s drawAll() !repaint MOUSE\n",
1508 System
.currentTimeMillis(), Thread
.currentThread());
1511 // The only thing that has happened is the mouse moved.
1513 // Redraw the old cell at that position, and save the cell at
1514 // the new mouse position.
1516 System
.err
.printf("%d %s restoreImage() %d %d\n",
1517 System
.currentTimeMillis(), Thread
.currentThread(),
1518 oldDrawnMouseX
, oldDrawnMouseY
);
1520 oldDrawnMouseCell
.restoreImage();
1521 getScreen().putCharXY(oldDrawnMouseX
, oldDrawnMouseY
,
1523 oldDrawnMouseCell
= getScreen().getCharXY(mouseX
, mouseY
);
1524 if ((images
.size() > 0) && (backend
instanceof ECMA48Backend
)) {
1525 // Special case: the entire row containing the mouse has
1526 // to be re-drawn if it has any image data, AND any rows
1528 if (oldDrawnMouseY
!= mouseY
) {
1529 for (int i
= oldDrawnMouseY
; ;) {
1530 getScreen().unsetImageRow(i
);
1534 if (oldDrawnMouseY
< mouseY
) {
1541 getScreen().unsetImageRow(mouseY
);
1545 // Draw mouse at the new position.
1546 invertCell(mouseX
, mouseY
);
1548 oldDrawnMouseX
= mouseX
;
1549 oldDrawnMouseY
= mouseY
;
1551 if ((images
.size() > 0) || getScreen().isDirty()) {
1552 backend
.flushScreen();
1558 System
.err
.printf("%d %s drawAll() REDRAW\n",
1559 System
.currentTimeMillis(), Thread
.currentThread());
1562 // If true, the cursor is not visible
1563 boolean cursor
= false;
1565 // Start with a clean screen
1566 getScreen().clear();
1569 if (desktop
!= null) {
1570 desktop
.drawChildren();
1573 // Draw each window in reverse Z order
1574 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
1575 Collections
.sort(sorted
);
1576 TWindow topLevel
= null;
1577 if (sorted
.size() > 0) {
1578 topLevel
= sorted
.get(0);
1580 Collections
.reverse(sorted
);
1581 for (TWindow window
: sorted
) {
1582 if (window
.isShown()) {
1583 window
.drawChildren();
1587 // Draw the blank menubar line - reset the screen clipping first so
1588 // it won't trim it out.
1589 getScreen().resetClipping();
1590 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
1591 theme
.getColor("tmenu"));
1592 // Now draw the menus.
1594 for (TMenu menu
: menus
) {
1595 CellAttributes menuColor
;
1596 CellAttributes menuMnemonicColor
;
1597 if (menu
.isActive()) {
1598 menuIsActive
= true;
1599 menuColor
= theme
.getColor("tmenu.highlighted");
1600 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
1603 menuColor
= theme
.getColor("tmenu");
1604 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
1606 // Draw the menu title
1607 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
1609 getScreen().putStringXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
1610 // Draw the highlight character
1611 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
1612 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
1614 if (menu
.isActive()) {
1615 ((TWindow
) menu
).drawChildren();
1616 // Reset the screen clipping so we can draw the next title.
1617 getScreen().resetClipping();
1619 x
+= menu
.getTitle().length() + 2;
1622 for (TMenu menu
: subMenus
) {
1623 // Reset the screen clipping so we can draw the next sub-menu.
1624 getScreen().resetClipping();
1625 ((TWindow
) menu
).drawChildren();
1627 getScreen().resetClipping();
1629 // Draw the status bar of the top-level window
1630 TStatusBar statusBar
= null;
1631 if (topLevel
!= null) {
1632 statusBar
= topLevel
.getStatusBar();
1634 if (statusBar
!= null) {
1635 getScreen().resetClipping();
1636 statusBar
.setWidth(getScreen().getWidth());
1637 statusBar
.setY(getScreen().getHeight() - topLevel
.getY());
1640 CellAttributes barColor
= new CellAttributes();
1641 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
1642 getScreen().hLineXY(0, desktopBottom
, getScreen().getWidth(), ' ',
1646 // Draw the mouse pointer
1648 System
.err
.printf("%d %s restoreImage() %d %d\n",
1649 System
.currentTimeMillis(), Thread
.currentThread(),
1650 oldDrawnMouseX
, oldDrawnMouseY
);
1652 oldDrawnMouseCell
= getScreen().getCharXY(mouseX
, mouseY
);
1653 if ((images
.size() > 0) && (backend
instanceof ECMA48Backend
)) {
1654 // Special case: the entire row containing the mouse has to be
1655 // re-drawn if it has any image data, AND any rows in between.
1656 if (oldDrawnMouseY
!= mouseY
) {
1657 for (int i
= oldDrawnMouseY
; ;) {
1658 getScreen().unsetImageRow(i
);
1662 if (oldDrawnMouseY
< mouseY
) {
1669 getScreen().unsetImageRow(mouseY
);
1672 invertCell(mouseX
, mouseY
);
1673 oldDrawnMouseX
= mouseX
;
1674 oldDrawnMouseY
= mouseY
;
1676 // Place the cursor if it is visible
1677 if (!menuIsActive
) {
1678 TWidget activeWidget
= null;
1679 if (sorted
.size() > 0) {
1680 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
1681 if (activeWidget
.isCursorVisible()) {
1682 if ((activeWidget
.getCursorAbsoluteY() < desktopBottom
)
1683 && (activeWidget
.getCursorAbsoluteY() > desktopTop
)
1685 getScreen().putCursor(true,
1686 activeWidget
.getCursorAbsoluteX(),
1687 activeWidget
.getCursorAbsoluteY());
1690 // Turn off the cursor. Also place it at 0,0.
1691 getScreen().putCursor(false, 0, 0);
1700 getScreen().hideCursor();
1703 // Flush the screen contents
1704 if ((images
.size() > 0) || getScreen().isDirty()) {
1705 backend
.flushScreen();
1712 * Force this application to exit.
1714 public void exit() {
1716 synchronized (this) {
1721 // ------------------------------------------------------------------------
1722 // TWindow management -----------------------------------------------------
1723 // ------------------------------------------------------------------------
1726 * Return the total number of windows.
1728 * @return the total number of windows
1730 public final int windowCount() {
1731 return windows
.size();
1735 * Return the number of windows that are showing.
1737 * @return the number of windows that are showing on screen
1739 public final int shownWindowCount() {
1741 for (TWindow w
: windows
) {
1750 * Return the number of windows that are hidden.
1752 * @return the number of windows that are hidden
1754 public final int hiddenWindowCount() {
1756 for (TWindow w
: windows
) {
1765 * Check if a window instance is in this application's window list.
1767 * @param window window to look for
1768 * @return true if this window is in the list
1770 public final boolean hasWindow(final TWindow window
) {
1771 if (windows
.size() == 0) {
1774 for (TWindow w
: windows
) {
1776 assert (window
.getApplication() == this);
1784 * Activate a window: bring it to the top and have it receive events.
1786 * @param window the window to become the new active window
1788 public void activateWindow(final TWindow window
) {
1789 if (hasWindow(window
) == false) {
1791 * Someone has a handle to a window I don't have. Ignore this
1797 // Whatever window might be moving/dragging, stop it now.
1798 for (TWindow w
: windows
) {
1799 if (w
.inMovements()) {
1804 assert (windows
.size() > 0);
1806 if (window
.isHidden()) {
1807 // Unhiding will also activate.
1811 assert (window
.isShown());
1813 if (windows
.size() == 1) {
1814 assert (window
== windows
.get(0));
1815 if (activeWindow
== null) {
1816 activeWindow
= window
;
1818 activeWindow
.setActive(true);
1819 activeWindow
.onFocus();
1822 assert (window
.isActive());
1823 assert (activeWindow
== window
);
1827 if (activeWindow
== window
) {
1828 assert (window
.isActive());
1830 // Window is already active, do nothing.
1834 assert (!window
.isActive());
1835 if (activeWindow
!= null) {
1836 assert (activeWindow
.getZ() == 0);
1838 activeWindow
.setActive(false);
1840 // Increment every window Z that is on top of window
1841 for (TWindow w
: windows
) {
1845 if (w
.getZ() < window
.getZ()) {
1846 w
.setZ(w
.getZ() + 1);
1850 // Unset activeWindow now before unfocus, so that a window
1851 // lifecycle change inside onUnfocus() doesn't call
1852 // switchWindow() and lead to a stack overflow.
1853 TWindow oldActiveWindow
= activeWindow
;
1854 activeWindow
= null;
1855 oldActiveWindow
.onUnfocus();
1857 activeWindow
= window
;
1858 activeWindow
.setZ(0);
1859 activeWindow
.setActive(true);
1860 activeWindow
.onFocus();
1867 * @param window the window to hide
1869 public void hideWindow(final TWindow window
) {
1870 if (hasWindow(window
) == false) {
1872 * Someone has a handle to a window I don't have. Ignore this
1878 // Whatever window might be moving/dragging, stop it now.
1879 for (TWindow w
: windows
) {
1880 if (w
.inMovements()) {
1885 assert (windows
.size() > 0);
1887 if (!window
.hidden
) {
1888 if (window
== activeWindow
) {
1889 if (shownWindowCount() > 1) {
1892 activeWindow
= null;
1893 window
.setActive(false);
1897 window
.hidden
= true;
1905 * @param window the window to show
1907 public void showWindow(final TWindow window
) {
1908 if (hasWindow(window
) == false) {
1910 * Someone has a handle to a window I don't have. Ignore this
1916 // Whatever window might be moving/dragging, stop it now.
1917 for (TWindow w
: windows
) {
1918 if (w
.inMovements()) {
1923 assert (windows
.size() > 0);
1925 if (window
.hidden
) {
1926 window
.hidden
= false;
1928 activateWindow(window
);
1933 * Close window. Note that the window's destructor is NOT called by this
1934 * method, instead the GC is assumed to do the cleanup.
1936 * @param window the window to remove
1938 public final void closeWindow(final TWindow window
) {
1939 if (hasWindow(window
) == false) {
1941 * Someone has a handle to a window I don't have. Ignore this
1947 // Let window know that it is about to be closed, while it is still
1948 // visible on screen.
1949 window
.onPreClose();
1951 synchronized (windows
) {
1952 // Whatever window might be moving/dragging, stop it now.
1953 for (TWindow w
: windows
) {
1954 if (w
.inMovements()) {
1959 int z
= window
.getZ();
1962 Collections
.sort(windows
);
1964 activeWindow
= null;
1965 for (TWindow w
: windows
) {
1967 // Do not activate a hidden window.
1973 w
.setZ(w
.getZ() - 1);
1974 if (w
.getZ() == 0) {
1977 assert (activeWindow
== null);
1989 // Perform window cleanup
1992 // Check if we are closing a TMessageBox or similar
1993 if (secondaryEventReceiver
!= null) {
1994 assert (secondaryEventHandler
!= null);
1996 // Do not send events to the secondaryEventReceiver anymore, the
1997 // window is closed.
1998 secondaryEventReceiver
= null;
2000 // Wake the secondary thread, it will wake the primary as it
2002 synchronized (secondaryEventHandler
) {
2003 secondaryEventHandler
.notify();
2007 // Permit desktop to be active if it is the only thing left.
2008 if (desktop
!= null) {
2009 if (windows
.size() == 0) {
2010 desktop
.setActive(true);
2016 * Switch to the next window.
2018 * @param forward if true, then switch to the next window in the list,
2019 * otherwise switch to the previous window in the list
2021 public final void switchWindow(final boolean forward
) {
2022 // Only switch if there are multiple visible windows
2023 if (shownWindowCount() < 2) {
2026 assert (activeWindow
!= null);
2028 synchronized (windows
) {
2029 // Whatever window might be moving/dragging, stop it now.
2030 for (TWindow w
: windows
) {
2031 if (w
.inMovements()) {
2036 // Swap z/active between active window and the next in the list
2037 int activeWindowI
= -1;
2038 for (int i
= 0; i
< windows
.size(); i
++) {
2039 if (windows
.get(i
) == activeWindow
) {
2040 assert (activeWindow
.isActive());
2044 assert (!windows
.get(0).isActive());
2047 assert (activeWindowI
>= 0);
2049 // Do not switch if a window is modal
2050 if (activeWindow
.isModal()) {
2054 int nextWindowI
= activeWindowI
;
2058 nextWindowI
%= windows
.size();
2061 if (nextWindowI
< 0) {
2062 nextWindowI
= windows
.size() - 1;
2066 if (windows
.get(nextWindowI
).isShown()) {
2067 activateWindow(windows
.get(nextWindowI
));
2071 } // synchronized (windows)
2076 * Add a window to my window list and make it active. Note package
2079 * @param window new window to add
2081 final void addWindowToApplication(final TWindow window
) {
2083 // Do not add menu windows to the window list.
2084 if (window
instanceof TMenu
) {
2088 // Do not add the desktop to the window list.
2089 if (window
instanceof TDesktop
) {
2093 synchronized (windows
) {
2094 if (windows
.contains(window
)) {
2095 throw new IllegalArgumentException("Window " + window
+
2096 " is already in window list");
2099 // Whatever window might be moving/dragging, stop it now.
2100 for (TWindow w
: windows
) {
2101 if (w
.inMovements()) {
2106 // Do not allow a modal window to spawn a non-modal window. If a
2107 // modal window is active, then this window will become modal
2109 if (modalWindowActive()) {
2110 window
.flags
|= TWindow
.MODAL
;
2111 window
.flags
|= TWindow
.CENTERED
;
2112 window
.hidden
= false;
2114 if (window
.isShown()) {
2115 for (TWindow w
: windows
) {
2120 w
.setZ(w
.getZ() + 1);
2123 windows
.add(window
);
2124 if (window
.isShown()) {
2125 activeWindow
= window
;
2126 activeWindow
.setZ(0);
2127 activeWindow
.setActive(true);
2128 activeWindow
.onFocus();
2131 if (((window
.flags
& TWindow
.CENTERED
) == 0)
2132 && ((window
.flags
& TWindow
.ABSOLUTEXY
) == 0)
2133 && (smartWindowPlacement
== true)
2136 doSmartPlacement(window
);
2140 // Desktop cannot be active over any other window.
2141 if (desktop
!= null) {
2142 desktop
.setActive(false);
2147 * Check if there is a system-modal window on top.
2149 * @return true if the active window is modal
2151 private boolean modalWindowActive() {
2152 if (windows
.size() == 0) {
2156 for (TWindow w
: windows
) {
2166 * Close all open windows.
2168 private void closeAllWindows() {
2169 // Don't do anything if we are in the menu
2170 if (activeMenu
!= null) {
2173 while (windows
.size() > 0) {
2174 closeWindow(windows
.get(0));
2179 * Re-layout the open windows as non-overlapping tiles. This produces
2180 * almost the same results as Turbo Pascal 7.0's IDE.
2182 private void tileWindows() {
2183 synchronized (windows
) {
2184 // Don't do anything if we are in the menu
2185 if (activeMenu
!= null) {
2188 int z
= windows
.size();
2194 a
= (int)(Math
.sqrt(z
));
2198 if (((a
* b
) + c
) == z
) {
2206 int newWidth
= (getScreen().getWidth() / a
);
2207 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
2208 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
2210 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
2211 Collections
.sort(sorted
);
2212 Collections
.reverse(sorted
);
2213 for (int i
= 0; i
< sorted
.size(); i
++) {
2214 int logicalX
= i
/ b
;
2215 int logicalY
= i
% b
;
2216 if (i
>= ((a
- 1) * b
)) {
2218 logicalY
= i
- ((a
- 1) * b
);
2221 TWindow w
= sorted
.get(i
);
2222 int oldWidth
= w
.getWidth();
2223 int oldHeight
= w
.getHeight();
2225 w
.setX(logicalX
* newWidth
);
2226 w
.setWidth(newWidth
);
2227 if (i
>= ((a
- 1) * b
)) {
2228 w
.setY((logicalY
* newHeight2
) + 1);
2229 w
.setHeight(newHeight2
);
2231 w
.setY((logicalY
* newHeight1
) + 1);
2232 w
.setHeight(newHeight1
);
2234 if ((w
.getWidth() != oldWidth
)
2235 || (w
.getHeight() != oldHeight
)
2237 w
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
2238 w
.getWidth(), w
.getHeight()));
2245 * Re-layout the open windows as overlapping cascaded windows.
2247 private void cascadeWindows() {
2248 synchronized (windows
) {
2249 // Don't do anything if we are in the menu
2250 if (activeMenu
!= null) {
2255 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
2256 Collections
.sort(sorted
);
2257 Collections
.reverse(sorted
);
2258 for (TWindow window
: sorted
) {
2263 if (x
> getScreen().getWidth()) {
2266 if (y
>= getScreen().getHeight()) {
2274 * Place a window to minimize its overlap with other windows.
2276 * @param window the window to place
2278 public final void doSmartPlacement(final TWindow window
) {
2279 // This is a pretty dumb algorithm, but seems to work. The hardest
2280 // part is computing these "overlap" values seeking a minimum average
2283 int yMin
= desktopTop
;
2284 int xMax
= getScreen().getWidth() - window
.getWidth() + 1;
2285 int yMax
= desktopBottom
- window
.getHeight() + 1;
2293 if ((xMin
== xMax
) && (yMin
== yMax
)) {
2294 // No work to do, bail out.
2298 // Compute the overlap matrix without the new window.
2299 int width
= getScreen().getWidth();
2300 int height
= getScreen().getHeight();
2301 int overlapMatrix
[][] = new int[width
][height
];
2302 for (TWindow w
: windows
) {
2306 for (int x
= w
.getX(); x
< w
.getX() + w
.getWidth(); x
++) {
2313 for (int y
= w
.getY(); y
< w
.getY() + w
.getHeight(); y
++) {
2320 overlapMatrix
[x
][y
]++;
2325 long oldOverlapTotal
= 0;
2326 long oldOverlapN
= 0;
2327 for (int x
= 0; x
< width
; x
++) {
2328 for (int y
= 0; y
< height
; y
++) {
2329 oldOverlapTotal
+= overlapMatrix
[x
][y
];
2330 if (overlapMatrix
[x
][y
] > 0) {
2337 double oldOverlapAvg
= (double) oldOverlapTotal
/ (double) oldOverlapN
;
2338 boolean first
= true;
2339 int windowX
= window
.getX();
2340 int windowY
= window
.getY();
2342 // For each possible (x, y) position for the new window, compute a
2343 // new overlap matrix.
2344 for (int x
= xMin
; x
< xMax
; x
++) {
2345 for (int y
= yMin
; y
< yMax
; y
++) {
2347 // Start with the matrix minus this window.
2348 int newMatrix
[][] = new int[width
][height
];
2349 for (int mx
= 0; mx
< width
; mx
++) {
2350 for (int my
= 0; my
< height
; my
++) {
2351 newMatrix
[mx
][my
] = overlapMatrix
[mx
][my
];
2355 // Add this window's values to the new overlap matrix.
2356 long newOverlapTotal
= 0;
2357 long newOverlapN
= 0;
2358 // Start by adding each new cell.
2359 for (int wx
= x
; wx
< x
+ window
.getWidth(); wx
++) {
2363 for (int wy
= y
; wy
< y
+ window
.getHeight(); wy
++) {
2367 newMatrix
[wx
][wy
]++;
2370 // Now figure out the new value for total coverage.
2371 for (int mx
= 0; mx
< width
; mx
++) {
2372 for (int my
= 0; my
< height
; my
++) {
2373 newOverlapTotal
+= newMatrix
[x
][y
];
2374 if (newMatrix
[mx
][my
] > 0) {
2379 double newOverlapAvg
= (double) newOverlapTotal
/ (double) newOverlapN
;
2382 // First time: just record what we got.
2383 oldOverlapAvg
= newOverlapAvg
;
2386 // All other times: pick a new best (x, y) and save the
2388 if (newOverlapAvg
< oldOverlapAvg
) {
2391 oldOverlapAvg
= newOverlapAvg
;
2395 } // for (int x = xMin; x < xMax; x++)
2397 } // for (int y = yMin; y < yMax; y++)
2399 // Finally, set the window's new coordinates.
2400 window
.setX(windowX
);
2401 window
.setY(windowY
);
2404 // ------------------------------------------------------------------------
2405 // TImage management ------------------------------------------------------
2406 // ------------------------------------------------------------------------
2409 * Add an image to the list. Note package private access.
2411 * @param image the image to add
2412 * @throws IllegalArgumentException if the image is already used in
2413 * another TApplication
2415 final void addImage(final TImage image
) {
2416 if ((image
.getApplication() != null)
2417 && (image
.getApplication() != this)
2419 throw new IllegalArgumentException("Image " + image
+
2420 " is already " + "part of application " +
2421 image
.getApplication());
2427 * Remove an image from the list. Note package private access.
2429 * @param image the image to remove
2430 * @throws IllegalArgumentException if the image is already used in
2431 * another TApplication
2433 final void removeImage(final TImage image
) {
2434 if ((image
.getApplication() != null)
2435 && (image
.getApplication() != this)
2437 throw new IllegalArgumentException("Image " + image
+
2438 " is already " + "part of application " +
2439 image
.getApplication());
2441 images
.remove(image
);
2444 // ------------------------------------------------------------------------
2445 // TMenu management -------------------------------------------------------
2446 // ------------------------------------------------------------------------
2449 * Check if a mouse event would hit either the active menu or any open
2452 * @param mouse mouse event
2453 * @return true if the mouse would hit the active menu or an open
2456 private boolean mouseOnMenu(final TMouseEvent mouse
) {
2457 assert (activeMenu
!= null);
2458 List
<TMenu
> menus
= new ArrayList
<TMenu
>(subMenus
);
2459 Collections
.reverse(menus
);
2460 for (TMenu menu
: menus
) {
2461 if (menu
.mouseWouldHit(mouse
)) {
2465 return activeMenu
.mouseWouldHit(mouse
);
2469 * See if we need to switch window or activate the menu based on
2472 * @param mouse mouse event
2474 private void checkSwitchFocus(final TMouseEvent mouse
) {
2476 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2477 && (activeMenu
!= null)
2478 && (mouse
.getAbsoluteY() != 0)
2479 && (!mouseOnMenu(mouse
))
2481 // They clicked outside the active menu, turn it off
2482 activeMenu
.setActive(false);
2484 for (TMenu menu
: subMenus
) {
2485 menu
.setActive(false);
2491 // See if they hit the menu bar
2492 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2493 && (mouse
.isMouse1())
2494 && (!modalWindowActive())
2495 && (mouse
.getAbsoluteY() == 0)
2498 for (TMenu menu
: subMenus
) {
2499 menu
.setActive(false);
2503 // They selected the menu, go activate it
2504 for (TMenu menu
: menus
) {
2505 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2506 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2507 + menu
.getTitle().length() + 2)
2509 menu
.setActive(true);
2512 menu
.setActive(false);
2518 // See if they hit the menu bar
2519 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
2520 && (mouse
.isMouse1())
2521 && (activeMenu
!= null)
2522 && (mouse
.getAbsoluteY() == 0)
2525 TMenu oldMenu
= activeMenu
;
2526 for (TMenu menu
: subMenus
) {
2527 menu
.setActive(false);
2531 // See if we should switch menus
2532 for (TMenu menu
: menus
) {
2533 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2534 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2535 + menu
.getTitle().length() + 2)
2537 menu
.setActive(true);
2541 if (oldMenu
!= activeMenu
) {
2542 // They switched menus
2543 oldMenu
.setActive(false);
2548 // If a menu is still active, don't switch windows
2549 if (activeMenu
!= null) {
2553 // Only switch if there are multiple windows
2554 if (windows
.size() < 2) {
2558 if (((focusFollowsMouse
== true)
2559 && (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
))
2560 || (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
)
2562 synchronized (windows
) {
2563 Collections
.sort(windows
);
2564 if (windows
.get(0).isModal()) {
2565 // Modal windows don't switch
2569 for (TWindow window
: windows
) {
2570 assert (!window
.isModal());
2572 if (window
.isHidden()) {
2573 assert (!window
.isActive());
2577 if (window
.mouseWouldHit(mouse
)) {
2578 if (window
== windows
.get(0)) {
2579 // Clicked on the same window, nothing to do
2580 assert (window
.isActive());
2584 // We will be switching to another window
2585 assert (windows
.get(0).isActive());
2586 assert (windows
.get(0) == activeWindow
);
2587 assert (!window
.isActive());
2588 if (activeWindow
!= null) {
2589 activeWindow
.onUnfocus();
2590 activeWindow
.setActive(false);
2591 activeWindow
.setZ(window
.getZ());
2593 activeWindow
= window
;
2595 window
.setActive(true);
2602 // Clicked on the background, nothing to do
2606 // Nothing to do: this isn't a mouse up, or focus isn't following
2612 * Turn off the menu.
2614 public final void closeMenu() {
2615 if (activeMenu
!= null) {
2616 activeMenu
.setActive(false);
2618 for (TMenu menu
: subMenus
) {
2619 menu
.setActive(false);
2626 * Get a (shallow) copy of the menu list.
2628 * @return a copy of the menu list
2630 public final List
<TMenu
> getAllMenus() {
2631 return new ArrayList
<TMenu
>(menus
);
2635 * Add a top-level menu to the list.
2637 * @param menu the menu to add
2638 * @throws IllegalArgumentException if the menu is already used in
2639 * another TApplication
2641 public final void addMenu(final TMenu menu
) {
2642 if ((menu
.getApplication() != null)
2643 && (menu
.getApplication() != this)
2645 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2646 "part of application " + menu
.getApplication());
2654 * Remove a top-level menu from the list.
2656 * @param menu the menu to remove
2657 * @throws IllegalArgumentException if the menu is already used in
2658 * another TApplication
2660 public final void removeMenu(final TMenu menu
) {
2661 if ((menu
.getApplication() != null)
2662 && (menu
.getApplication() != this)
2664 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2665 "part of application " + menu
.getApplication());
2673 * Turn off a sub-menu.
2675 public final void closeSubMenu() {
2676 assert (activeMenu
!= null);
2677 TMenu item
= subMenus
.get(subMenus
.size() - 1);
2678 assert (item
!= null);
2679 item
.setActive(false);
2680 subMenus
.remove(subMenus
.size() - 1);
2684 * Switch to the next menu.
2686 * @param forward if true, then switch to the next menu in the list,
2687 * otherwise switch to the previous menu in the list
2689 public final void switchMenu(final boolean forward
) {
2690 assert (activeMenu
!= null);
2692 for (TMenu menu
: subMenus
) {
2693 menu
.setActive(false);
2697 for (int i
= 0; i
< menus
.size(); i
++) {
2698 if (activeMenu
== menus
.get(i
)) {
2700 if (i
< menus
.size() - 1) {
2709 i
= menus
.size() - 1;
2712 activeMenu
.setActive(false);
2713 activeMenu
= menus
.get(i
);
2714 activeMenu
.setActive(true);
2721 * Add a menu item to the global list. If it has a keyboard accelerator,
2722 * that will be added the global hash.
2724 * @param item the menu item
2726 public final void addMenuItem(final TMenuItem item
) {
2727 menuItems
.add(item
);
2729 TKeypress key
= item
.getKey();
2731 synchronized (accelerators
) {
2732 assert (accelerators
.get(key
) == null);
2733 accelerators
.put(key
.toLowerCase(), item
);
2739 * Disable one menu item.
2741 * @param id the menu item ID
2743 public final void disableMenuItem(final int id
) {
2744 for (TMenuItem item
: menuItems
) {
2745 if (item
.getId() == id
) {
2746 item
.setEnabled(false);
2752 * Disable the range of menu items with ID's between lower and upper,
2755 * @param lower the lowest menu item ID
2756 * @param upper the highest menu item ID
2758 public final void disableMenuItems(final int lower
, final int upper
) {
2759 for (TMenuItem item
: menuItems
) {
2760 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2761 item
.setEnabled(false);
2762 item
.getParent().activate(0);
2768 * Enable one menu item.
2770 * @param id the menu item ID
2772 public final void enableMenuItem(final int id
) {
2773 for (TMenuItem item
: menuItems
) {
2774 if (item
.getId() == id
) {
2775 item
.setEnabled(true);
2776 item
.getParent().activate(0);
2782 * Enable the range of menu items with ID's between lower and upper,
2785 * @param lower the lowest menu item ID
2786 * @param upper the highest menu item ID
2788 public final void enableMenuItems(final int lower
, final int upper
) {
2789 for (TMenuItem item
: menuItems
) {
2790 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2791 item
.setEnabled(true);
2792 item
.getParent().activate(0);
2798 * Recompute menu x positions based on their title length.
2800 public final void recomputeMenuX() {
2802 for (TMenu menu
: menus
) {
2805 x
+= menu
.getTitle().length() + 2;
2807 // Don't let the menu window exceed the screen width
2808 int rightEdge
= menu
.getX() + menu
.getWidth();
2809 if (rightEdge
> getScreen().getWidth()) {
2810 menu
.setX(getScreen().getWidth() - menu
.getWidth());
2816 * Post an event to process.
2818 * @param event new event to add to the queue
2820 public final void postEvent(final TInputEvent event
) {
2821 synchronized (this) {
2822 synchronized (fillEventQueue
) {
2823 fillEventQueue
.add(event
);
2826 System
.err
.println(System
.currentTimeMillis() + " " +
2827 Thread
.currentThread() + " postEvent() wake up main");
2834 * Post an event to process and turn off the menu.
2836 * @param event new event to add to the queue
2838 public final void postMenuEvent(final TInputEvent event
) {
2839 synchronized (this) {
2840 synchronized (fillEventQueue
) {
2841 fillEventQueue
.add(event
);
2844 System
.err
.println(System
.currentTimeMillis() + " " +
2845 Thread
.currentThread() + " postMenuEvent() wake up main");
2853 * Add a sub-menu to the list of open sub-menus.
2855 * @param menu sub-menu
2857 public final void addSubMenu(final TMenu menu
) {
2862 * Convenience function to add a top-level menu.
2864 * @param title menu title
2865 * @return the new menu
2867 public final TMenu
addMenu(final String title
) {
2870 TMenu menu
= new TMenu(this, x
, y
, title
);
2877 * Convenience function to add a default "File" menu.
2879 * @return the new menu
2881 public final TMenu
addFileMenu() {
2882 TMenu fileMenu
= addMenu(i18n
.getString("fileMenuTitle"));
2883 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
2884 fileMenu
.addSeparator();
2885 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
2886 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
2887 TStatusBar statusBar
= fileMenu
.newStatusBar(i18n
.
2888 getString("fileMenuStatus"));
2889 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2894 * Convenience function to add a default "Edit" menu.
2896 * @return the new menu
2898 public final TMenu
addEditMenu() {
2899 TMenu editMenu
= addMenu(i18n
.getString("editMenuTitle"));
2900 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
2901 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
2902 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
2903 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
2904 TStatusBar statusBar
= editMenu
.newStatusBar(i18n
.
2905 getString("editMenuStatus"));
2906 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2911 * Convenience function to add a default "Window" menu.
2913 * @return the new menu
2915 public final TMenu
addWindowMenu() {
2916 TMenu windowMenu
= addMenu(i18n
.getString("windowMenuTitle"));
2917 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
2918 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
2919 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
2920 windowMenu
.addSeparator();
2921 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
2922 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
2923 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
2924 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
2925 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
2926 TStatusBar statusBar
= windowMenu
.newStatusBar(i18n
.
2927 getString("windowMenuStatus"));
2928 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2933 * Convenience function to add a default "Help" menu.
2935 * @return the new menu
2937 public final TMenu
addHelpMenu() {
2938 TMenu helpMenu
= addMenu(i18n
.getString("helpMenuTitle"));
2939 helpMenu
.addDefaultItem(TMenu
.MID_HELP_CONTENTS
);
2940 helpMenu
.addDefaultItem(TMenu
.MID_HELP_INDEX
);
2941 helpMenu
.addDefaultItem(TMenu
.MID_HELP_SEARCH
);
2942 helpMenu
.addDefaultItem(TMenu
.MID_HELP_PREVIOUS
);
2943 helpMenu
.addDefaultItem(TMenu
.MID_HELP_HELP
);
2944 helpMenu
.addDefaultItem(TMenu
.MID_HELP_ACTIVE_FILE
);
2945 helpMenu
.addSeparator();
2946 helpMenu
.addDefaultItem(TMenu
.MID_ABOUT
);
2947 TStatusBar statusBar
= helpMenu
.newStatusBar(i18n
.
2948 getString("helpMenuStatus"));
2949 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2953 // ------------------------------------------------------------------------
2954 // TTimer management ------------------------------------------------------
2955 // ------------------------------------------------------------------------
2958 * Get the amount of time I can sleep before missing a Timer tick.
2960 * @param timeout = initial (maximum) timeout in millis
2961 * @return number of milliseconds between now and the next timer event
2963 private long getSleepTime(final long timeout
) {
2964 Date now
= new Date();
2965 long nowTime
= now
.getTime();
2966 long sleepTime
= timeout
;
2968 synchronized (timers
) {
2969 for (TTimer timer
: timers
) {
2970 long nextTickTime
= timer
.getNextTick().getTime();
2971 if (nextTickTime
< nowTime
) {
2975 long timeDifference
= nextTickTime
- nowTime
;
2976 if (timeDifference
< sleepTime
) {
2977 sleepTime
= timeDifference
;
2982 assert (sleepTime
>= 0);
2983 assert (sleepTime
<= timeout
);
2988 * Convenience function to add a timer.
2990 * @param duration number of milliseconds to wait between ticks
2991 * @param recurring if true, re-schedule this timer after every tick
2992 * @param action function to call when button is pressed
2995 public final TTimer
addTimer(final long duration
, final boolean recurring
,
2996 final TAction action
) {
2998 TTimer timer
= new TTimer(duration
, recurring
, action
);
2999 synchronized (timers
) {
3006 * Convenience function to remove a timer.
3008 * @param timer timer to remove
3010 public final void removeTimer(final TTimer timer
) {
3011 synchronized (timers
) {
3012 timers
.remove(timer
);
3016 // ------------------------------------------------------------------------
3017 // Other TWindow constructors ---------------------------------------------
3018 // ------------------------------------------------------------------------
3021 * Convenience function to spawn a message box.
3023 * @param title window title, will be centered along the top border
3024 * @param caption message to display. Use embedded newlines to get a
3026 * @return the new message box
3028 public final TMessageBox
messageBox(final String title
,
3029 final String caption
) {
3031 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
3035 * Convenience function to spawn a message box.
3037 * @param title window title, will be centered along the top border
3038 * @param caption message to display. Use embedded newlines to get a
3040 * @param type one of the TMessageBox.Type constants. Default is
3042 * @return the new message box
3044 public final TMessageBox
messageBox(final String title
,
3045 final String caption
, final TMessageBox
.Type type
) {
3047 return new TMessageBox(this, title
, caption
, type
);
3051 * Convenience function to spawn an input box.
3053 * @param title window title, will be centered along the top border
3054 * @param caption message to display. Use embedded newlines to get a
3056 * @return the new input box
3058 public final TInputBox
inputBox(final String title
, final String caption
) {
3060 return new TInputBox(this, title
, caption
);
3064 * Convenience function to spawn an input box.
3066 * @param title window title, will be centered along the top border
3067 * @param caption message to display. Use embedded newlines to get a
3069 * @param text initial text to seed the field with
3070 * @return the new input box
3072 public final TInputBox
inputBox(final String title
, final String caption
,
3073 final String text
) {
3075 return new TInputBox(this, title
, caption
, text
);
3079 * Convenience function to spawn an input box.
3081 * @param title window title, will be centered along the top border
3082 * @param caption message to display. Use embedded newlines to get a
3084 * @param text initial text to seed the field with
3085 * @param type one of the Type constants. Default is Type.OK.
3086 * @return the new input box
3088 public final TInputBox
inputBox(final String title
, final String caption
,
3089 final String text
, final TInputBox
.Type type
) {
3091 return new TInputBox(this, title
, caption
, text
, type
);
3095 * Convenience function to open a terminal window.
3097 * @param x column relative to parent
3098 * @param y row relative to parent
3099 * @return the terminal new window
3101 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
3102 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
3106 * Convenience function to open a terminal window.
3108 * @param x column relative to parent
3109 * @param y row relative to parent
3110 * @param closeOnExit if true, close the window when the command exits
3111 * @return the terminal new window
3113 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3114 final boolean closeOnExit
) {
3116 return openTerminal(x
, y
, TWindow
.RESIZABLE
, closeOnExit
);
3120 * Convenience function to open a terminal window.
3122 * @param x column relative to parent
3123 * @param y row relative to parent
3124 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3125 * @return the terminal new window
3127 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3130 return new TTerminalWindow(this, x
, y
, flags
);
3134 * Convenience function to open a terminal window.
3136 * @param x column relative to parent
3137 * @param y row relative to parent
3138 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3139 * @param closeOnExit if true, close the window when the command exits
3140 * @return the terminal new window
3142 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3143 final int flags
, final boolean closeOnExit
) {
3145 return new TTerminalWindow(this, x
, y
, flags
, closeOnExit
);
3149 * Convenience function to open a terminal window and execute a custom
3150 * command line inside it.
3152 * @param x column relative to parent
3153 * @param y row relative to parent
3154 * @param commandLine the command line to execute
3155 * @return the terminal new window
3157 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3158 final String commandLine
) {
3160 return openTerminal(x
, y
, TWindow
.RESIZABLE
, commandLine
);
3164 * Convenience function to open a terminal window and execute a custom
3165 * command line inside it.
3167 * @param x column relative to parent
3168 * @param y row relative to parent
3169 * @param commandLine the command line to execute
3170 * @param closeOnExit if true, close the window when the command exits
3171 * @return the terminal new window
3173 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3174 final String commandLine
, final boolean closeOnExit
) {
3176 return openTerminal(x
, y
, TWindow
.RESIZABLE
, commandLine
, closeOnExit
);
3180 * Convenience function to open a terminal window and execute a custom
3181 * command line inside it.
3183 * @param x column relative to parent
3184 * @param y row relative to parent
3185 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3186 * @param command the command line to execute
3187 * @return the terminal new window
3189 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3190 final int flags
, final String
[] command
) {
3192 return new TTerminalWindow(this, x
, y
, flags
, command
);
3196 * Convenience function to open a terminal window and execute a custom
3197 * command line inside it.
3199 * @param x column relative to parent
3200 * @param y row relative to parent
3201 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3202 * @param command the command line to execute
3203 * @param closeOnExit if true, close the window when the command exits
3204 * @return the terminal new window
3206 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3207 final int flags
, final String
[] command
, final boolean closeOnExit
) {
3209 return new TTerminalWindow(this, x
, y
, flags
, command
, closeOnExit
);
3213 * Convenience function to open a terminal window and execute a custom
3214 * command line inside it.
3216 * @param x column relative to parent
3217 * @param y row relative to parent
3218 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3219 * @param commandLine the command line to execute
3220 * @return the terminal new window
3222 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3223 final int flags
, final String commandLine
) {
3225 return new TTerminalWindow(this, x
, y
, flags
, commandLine
.split("\\s"));
3229 * Convenience function to open a terminal window and execute a custom
3230 * command line inside it.
3232 * @param x column relative to parent
3233 * @param y row relative to parent
3234 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3235 * @param commandLine the command line to execute
3236 * @param closeOnExit if true, close the window when the command exits
3237 * @return the terminal new window
3239 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3240 final int flags
, final String commandLine
, final boolean closeOnExit
) {
3242 return new TTerminalWindow(this, x
, y
, flags
, commandLine
.split("\\s"),
3247 * Convenience function to spawn an file open box.
3249 * @param path path of selected file
3250 * @return the result of the new file open box
3251 * @throws IOException if java.io operation throws
3253 public final String
fileOpenBox(final String path
) throws IOException
{
3255 TFileOpenBox box
= new TFileOpenBox(this, path
, TFileOpenBox
.Type
.OPEN
);
3256 return box
.getFilename();
3260 * Convenience function to spawn an file open box.
3262 * @param path path of selected file
3263 * @param type one of the Type constants
3264 * @return the result of the new file open box
3265 * @throws IOException if java.io operation throws
3267 public final String
fileOpenBox(final String path
,
3268 final TFileOpenBox
.Type type
) throws IOException
{
3270 TFileOpenBox box
= new TFileOpenBox(this, path
, type
);
3271 return box
.getFilename();
3275 * Convenience function to spawn a file open box.
3277 * @param path path of selected file
3278 * @param type one of the Type constants
3279 * @param filter a string that files must match to be displayed
3280 * @return the result of the new file open box
3281 * @throws IOException of a java.io operation throws
3283 public final String
fileOpenBox(final String path
,
3284 final TFileOpenBox
.Type type
, final String filter
) throws IOException
{
3286 ArrayList
<String
> filters
= new ArrayList
<String
>();
3287 filters
.add(filter
);
3289 TFileOpenBox box
= new TFileOpenBox(this, path
, type
, filters
);
3290 return box
.getFilename();
3294 * Convenience function to spawn a file open box.
3296 * @param path path of selected file
3297 * @param type one of the Type constants
3298 * @param filters a list of strings that files must match to be displayed
3299 * @return the result of the new file open box
3300 * @throws IOException of a java.io operation throws
3302 public final String
fileOpenBox(final String path
,
3303 final TFileOpenBox
.Type type
,
3304 final List
<String
> filters
) throws IOException
{
3306 TFileOpenBox box
= new TFileOpenBox(this, path
, type
, filters
);
3307 return box
.getFilename();
3311 * Convenience function to create a new window and make it active.
3312 * Window will be located at (0, 0).
3314 * @param title window title, will be centered along the top border
3315 * @param width width of window
3316 * @param height height of window
3317 * @return the new window
3319 public final TWindow
addWindow(final String title
, final int width
,
3322 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
);
3327 * Convenience function to create a new window and make it active.
3328 * Window will be located at (0, 0).
3330 * @param title window title, will be centered along the top border
3331 * @param width width of window
3332 * @param height height of window
3333 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
3334 * @return the new window
3336 public final TWindow
addWindow(final String title
,
3337 final int width
, final int height
, final int flags
) {
3339 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
, flags
);
3344 * Convenience function to create a new window and make it active.
3346 * @param title window title, will be centered along the top border
3347 * @param x column relative to parent
3348 * @param y row relative to parent
3349 * @param width width of window
3350 * @param height height of window
3351 * @return the new window
3353 public final TWindow
addWindow(final String title
,
3354 final int x
, final int y
, final int width
, final int height
) {
3356 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
);
3361 * Convenience function to create a new window and make it active.
3363 * @param title window title, will be centered along the top border
3364 * @param x column relative to parent
3365 * @param y row relative to parent
3366 * @param width width of window
3367 * @param height height of window
3368 * @param flags mask of RESIZABLE, CENTERED, or MODAL
3369 * @return the new window
3371 public final TWindow
addWindow(final String title
,
3372 final int x
, final int y
, final int width
, final int height
,
3375 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
, flags
);