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]
32 import java
.io
.InputStream
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.PrintWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.text
.MessageFormat
;
39 import java
.util
.ArrayList
;
40 import java
.util
.Collections
;
41 import java
.util
.Date
;
42 import java
.util
.HashMap
;
43 import java
.util
.LinkedList
;
44 import java
.util
.List
;
46 import java
.util
.ResourceBundle
;
48 import jexer
.bits
.Cell
;
49 import jexer
.bits
.CellAttributes
;
50 import jexer
.bits
.ColorTheme
;
51 import jexer
.bits
.StringUtils
;
52 import jexer
.event
.TCommandEvent
;
53 import jexer
.event
.TInputEvent
;
54 import jexer
.event
.TKeypressEvent
;
55 import jexer
.event
.TMenuEvent
;
56 import jexer
.event
.TMouseEvent
;
57 import jexer
.event
.TResizeEvent
;
58 import jexer
.backend
.Backend
;
59 import jexer
.backend
.MultiBackend
;
60 import jexer
.backend
.Screen
;
61 import jexer
.backend
.SwingBackend
;
62 import jexer
.backend
.ECMA48Backend
;
63 import jexer
.backend
.TWindowBackend
;
64 import jexer
.menu
.TMenu
;
65 import jexer
.menu
.TMenuItem
;
66 import jexer
.menu
.TSubMenu
;
67 import static jexer
.TCommand
.*;
68 import static jexer
.TKeypress
.*;
71 * TApplication is the main driver class for a full Text User Interface
72 * application. It manages windows, provides a menu bar and status bar, and
73 * processes events received from the user.
75 public class TApplication
implements Runnable
{
80 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TApplication
.class.getName());
82 // ------------------------------------------------------------------------
83 // Constants --------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * If true, emit thread stuff to System.err.
89 private static final boolean debugThreads
= false;
92 * If true, emit events being processed to System.err.
94 private static final boolean debugEvents
= false;
97 * If true, do "smart placement" on new windows that are not specified to
100 private static final boolean smartWindowPlacement
= true;
103 * Two backend types are available.
105 public static enum BackendType
{
112 * An ECMA48 / ANSI X3.64 / XTERM style terminal.
117 * Synonym for ECMA48.
122 // ------------------------------------------------------------------------
123 // Variables --------------------------------------------------------------
124 // ------------------------------------------------------------------------
127 * The primary event handler thread.
129 private volatile WidgetEventHandler primaryEventHandler
;
132 * The secondary event handler thread.
134 private volatile WidgetEventHandler secondaryEventHandler
;
137 * The screen handler thread.
139 private volatile ScreenHandler screenHandler
;
142 * The widget receiving events from the secondary event handler thread.
144 private volatile TWidget secondaryEventReceiver
;
147 * Access to the physical screen, keyboard, and mouse.
149 private Backend backend
;
152 * Actual mouse coordinate X.
157 * Actual mouse coordinate Y.
162 * Old version of mouse coordinate X.
164 private int oldMouseX
;
167 * Old version mouse coordinate Y.
169 private int oldMouseY
;
172 * Old drawn version of mouse coordinate X.
174 private int oldDrawnMouseX
;
177 * Old drawn version mouse coordinate Y.
179 private int oldDrawnMouseY
;
182 * Old drawn version mouse cell.
184 private Cell oldDrawnMouseCell
= new Cell();
187 * The last mouse up click time, used to determine if this is a mouse
190 private long lastMouseUpTime
;
193 * The amount of millis between mouse up events to assume a double-click.
195 private long doubleClickTime
= 250;
198 * Event queue that is filled by run().
200 private List
<TInputEvent
> fillEventQueue
;
203 * Event queue that will be drained by either primary or secondary
206 private List
<TInputEvent
> drainEventQueue
;
209 * Top-level menus in this application.
211 private List
<TMenu
> menus
;
214 * Stack of activated sub-menus in this application.
216 private List
<TMenu
> subMenus
;
219 * The currently active menu.
221 private TMenu activeMenu
= null;
224 * Active keyboard accelerators.
226 private Map
<TKeypress
, TMenuItem
> accelerators
;
231 private List
<TMenuItem
> menuItems
;
234 * Windows and widgets pull colors from this ColorTheme.
236 private ColorTheme theme
;
239 * The top-level windows (but not menus).
241 private List
<TWindow
> windows
;
244 * The currently acive window.
246 private TWindow activeWindow
= null;
249 * Timers that are being ticked.
251 private List
<TTimer
> timers
;
254 * When true, the application has been started.
256 private volatile boolean started
= false;
259 * When true, exit the application.
261 private volatile boolean quit
= false;
264 * When true, repaint the entire screen.
266 private volatile boolean repaint
= true;
269 * Y coordinate of the top edge of the desktop. For now this is a
270 * constant. Someday it would be nice to have a multi-line menu or
273 private static final int desktopTop
= 1;
276 * Y coordinate of the bottom edge of the desktop.
278 private int desktopBottom
;
281 * An optional TDesktop background window that is drawn underneath
284 private TDesktop desktop
;
287 * If true, focus follows mouse: windows automatically raised if the
288 * mouse passes over them.
290 private boolean focusFollowsMouse
= false;
293 * The list of commands to run before the next I/O check.
295 private List
<Runnable
> invokeLaters
= new LinkedList
<Runnable
>();
298 * The last time the screen was resized.
300 private long screenResizeTime
= 0;
303 * WidgetEventHandler is the main event consumer loop. There are at most
304 * two such threads in existence: the primary for normal case and a
305 * secondary that is used for TMessageBox, TInputBox, and similar.
307 private class WidgetEventHandler
implements Runnable
{
309 * The main application.
311 private TApplication application
;
314 * Whether or not this WidgetEventHandler is the primary or secondary
317 private boolean primary
= true;
320 * Public constructor.
322 * @param application the main application
323 * @param primary if true, this is the primary event handler thread
325 public WidgetEventHandler(final TApplication application
,
326 final boolean primary
) {
328 this.application
= application
;
329 this.primary
= primary
;
336 // Wrap everything in a try, so that if we go belly up we can let
337 // the user have their terminal back.
340 } catch (Throwable t
) {
341 this.application
.restoreConsole();
343 this.application
.exit();
350 private void runImpl() {
351 boolean first
= true;
354 while (!application
.quit
) {
356 // Wait until application notifies me
357 while (!application
.quit
) {
359 synchronized (application
.drainEventQueue
) {
360 if (application
.drainEventQueue
.size() > 0) {
369 timeout
= application
.getSleepTime(1000);
373 // A timer needs to fire, break out.
378 System
.err
.printf("%d %s %s %s sleep %d millis\n",
379 System
.currentTimeMillis(), this,
380 primary ?
"primary" : "secondary",
381 Thread
.currentThread(), timeout
);
384 synchronized (this) {
389 System
.err
.printf("%d %s %s %s AWAKE\n",
390 System
.currentTimeMillis(), this,
391 primary ?
"primary" : "secondary",
392 Thread
.currentThread());
396 && (application
.secondaryEventReceiver
== null)
398 // Secondary thread, emergency exit. If we got
399 // here then something went wrong with the
400 // handoff between yield() and closeWindow().
401 synchronized (application
.primaryEventHandler
) {
402 application
.primaryEventHandler
.notify();
404 application
.secondaryEventHandler
= null;
405 throw new RuntimeException("secondary exited " +
409 } catch (InterruptedException e
) {
412 } // while (!application.quit)
414 // Pull all events off the queue
416 TInputEvent event
= null;
417 synchronized (application
.drainEventQueue
) {
418 if (application
.drainEventQueue
.size() == 0) {
421 event
= application
.drainEventQueue
.remove(0);
424 // We will have an event to process, so repaint the
425 // screen at the end.
426 application
.repaint
= true;
429 primaryHandleEvent(event
);
431 secondaryHandleEvent(event
);
434 && (application
.secondaryEventReceiver
== null)
436 // Secondary thread, time to exit.
438 // Eliminate my reference so that wakeEventHandler()
439 // resumes working on the primary.
440 application
.secondaryEventHandler
= null;
442 // We are ready to exit, wake up the primary thread.
443 // Remember that it is currently sleeping inside its
444 // primaryHandleEvent().
445 synchronized (application
.primaryEventHandler
) {
446 application
.primaryEventHandler
.notify();
455 // Fire timers, update screen.
457 application
.finishEventProcessing();
460 } // while (true) (main runnable loop)
465 * ScreenHandler pushes screen updates to the physical device.
467 private class ScreenHandler
implements Runnable
{
469 * The main application.
471 private TApplication application
;
476 private boolean dirty
= false;
479 * Public constructor.
481 * @param application the main application
483 public ScreenHandler(final TApplication application
) {
484 this.application
= application
;
488 * The screen update loop.
491 // Wrap everything in a try, so that if we go belly up we can let
492 // the user have their terminal back.
495 } catch (Throwable t
) {
496 this.application
.restoreConsole();
498 this.application
.exit();
505 private void runImpl() {
508 while (!application
.quit
) {
510 // Wait until application notifies me
511 while (!application
.quit
) {
513 synchronized (this) {
519 // Always check within 50 milliseconds.
522 } catch (InterruptedException e
) {
525 } // while (!application.quit)
527 // Flush the screen contents
529 System
.err
.printf("%d %s backend.flushScreen()\n",
530 System
.currentTimeMillis(), Thread
.currentThread());
532 synchronized (getScreen()) {
533 backend
.flushScreen();
535 } // while (true) (main runnable loop)
537 // Shutdown the user I/O thread(s)
542 * Set the dirty flag.
544 public void setDirty() {
545 synchronized (this) {
552 // ------------------------------------------------------------------------
553 // Constructors -----------------------------------------------------------
554 // ------------------------------------------------------------------------
557 * Public constructor.
559 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
561 * @param windowWidth the number of text columns to start with
562 * @param windowHeight the number of text rows to start with
563 * @param fontSize the size in points
564 * @throws UnsupportedEncodingException if an exception is thrown when
565 * creating the InputStreamReader
567 public TApplication(final BackendType backendType
, final int windowWidth
,
568 final int windowHeight
, final int fontSize
)
569 throws UnsupportedEncodingException
{
571 switch (backendType
) {
573 backend
= new SwingBackend(this, windowWidth
, windowHeight
,
579 backend
= new ECMA48Backend(this, null, null, windowWidth
,
580 windowHeight
, fontSize
);
583 throw new IllegalArgumentException("Invalid backend type: "
590 * Public constructor.
592 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
594 * @throws UnsupportedEncodingException if an exception is thrown when
595 * creating the InputStreamReader
597 public TApplication(final BackendType backendType
)
598 throws UnsupportedEncodingException
{
600 switch (backendType
) {
602 // The default SwingBackend is 80x25, 20 pt font. If you want to
603 // change that, you can pass the extra arguments to the
604 // SwingBackend constructor here. For example, if you wanted
605 // 90x30, 16 pt font:
607 // backend = new SwingBackend(this, 90, 30, 16);
608 backend
= new SwingBackend(this);
613 backend
= new ECMA48Backend(this, null, null);
616 throw new IllegalArgumentException("Invalid backend type: "
623 * Public constructor. The backend type will be BackendType.ECMA48.
625 * @param input an InputStream connected to the remote user, or null for
626 * System.in. If System.in is used, then on non-Windows systems it will
627 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
628 * mode. input is always converted to a Reader with UTF-8 encoding.
629 * @param output an OutputStream connected to the remote user, or null
630 * for System.out. output is always converted to a Writer with UTF-8
632 * @throws UnsupportedEncodingException if an exception is thrown when
633 * creating the InputStreamReader
635 public TApplication(final InputStream input
,
636 final OutputStream output
) throws UnsupportedEncodingException
{
638 backend
= new ECMA48Backend(this, input
, output
);
643 * Public constructor. The backend type will be BackendType.ECMA48.
645 * @param input the InputStream underlying 'reader'. Its available()
646 * method is used to determine if reader.read() will block or not.
647 * @param reader a Reader connected to the remote user.
648 * @param writer a PrintWriter connected to the remote user.
649 * @param setRawMode if true, set System.in into raw mode with stty.
650 * This should in general not be used. It is here solely for Demo3,
651 * which uses System.in.
652 * @throws IllegalArgumentException if input, reader, or writer are null.
654 public TApplication(final InputStream input
, final Reader reader
,
655 final PrintWriter writer
, final boolean setRawMode
) {
657 backend
= new ECMA48Backend(this, input
, reader
, writer
, setRawMode
);
662 * Public constructor. The backend type will be BackendType.ECMA48.
664 * @param input the InputStream underlying 'reader'. Its available()
665 * method is used to determine if reader.read() will block or not.
666 * @param reader a Reader connected to the remote user.
667 * @param writer a PrintWriter connected to the remote user.
668 * @throws IllegalArgumentException if input, reader, or writer are null.
670 public TApplication(final InputStream input
, final Reader reader
,
671 final PrintWriter writer
) {
673 this(input
, reader
, writer
, false);
677 * Public constructor. This hook enables use with new non-Jexer
680 * @param backend a Backend that is already ready to go.
682 public TApplication(final Backend backend
) {
683 this.backend
= backend
;
684 backend
.setListener(this);
689 * Finish construction once the backend is set.
691 private void TApplicationImpl() {
692 theme
= new ColorTheme();
693 desktopBottom
= getScreen().getHeight() - 1;
694 fillEventQueue
= new LinkedList
<TInputEvent
>();
695 drainEventQueue
= new LinkedList
<TInputEvent
>();
696 windows
= new LinkedList
<TWindow
>();
697 menus
= new ArrayList
<TMenu
>();
698 subMenus
= new ArrayList
<TMenu
>();
699 timers
= new LinkedList
<TTimer
>();
700 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
701 menuItems
= new LinkedList
<TMenuItem
>();
702 desktop
= new TDesktop(this);
704 // Special case: the Swing backend needs to have a timer to drive its
706 if ((backend
instanceof SwingBackend
)
707 || (backend
instanceof MultiBackend
)
709 // Default to 500 millis, unless a SwingBackend has its own
712 if (backend
instanceof SwingBackend
) {
713 millis
= ((SwingBackend
) backend
).getBlinkMillis();
716 addTimer(millis
, true,
719 TApplication
.this.doRepaint();
727 // ------------------------------------------------------------------------
728 // Runnable ---------------------------------------------------------------
729 // ------------------------------------------------------------------------
732 * Run this application until it exits.
735 // System.err.println("*** TApplication.run() begins ***");
737 // Start the screen updater thread
738 screenHandler
= new ScreenHandler(this);
739 (new Thread(screenHandler
)).start();
741 // Start the main consumer thread
742 primaryEventHandler
= new WidgetEventHandler(this, true);
743 (new Thread(primaryEventHandler
)).start();
748 synchronized (this) {
749 boolean doWait
= false;
751 if (!backend
.hasEvents()) {
752 synchronized (fillEventQueue
) {
753 if (fillEventQueue
.size() == 0) {
760 // No I/O to dispatch, so wait until the backend
764 System
.err
.println(System
.currentTimeMillis() +
765 " " + Thread
.currentThread() + " MAIN sleep");
771 System
.err
.println(System
.currentTimeMillis() +
772 " " + Thread
.currentThread() + " MAIN AWAKE");
774 } catch (InterruptedException e
) {
775 // I'm awake and don't care why, let's see what's
776 // going on out there.
780 } // synchronized (this)
782 synchronized (fillEventQueue
) {
783 // Pull any pending I/O events
784 backend
.getEvents(fillEventQueue
);
786 // Dispatch each event to the appropriate handler, one at a
789 TInputEvent event
= null;
790 if (fillEventQueue
.size() == 0) {
793 event
= fillEventQueue
.remove(0);
794 metaHandleEvent(event
);
798 // Wake a consumer thread if we have any pending events.
799 if (drainEventQueue
.size() > 0) {
805 // Shutdown the event consumer threads
806 if (secondaryEventHandler
!= null) {
807 synchronized (secondaryEventHandler
) {
808 secondaryEventHandler
.notify();
811 if (primaryEventHandler
!= null) {
812 synchronized (primaryEventHandler
) {
813 primaryEventHandler
.notify();
817 // Close all the windows. This gives them an opportunity to release
821 // Give the overarching application an opportunity to release
825 // System.err.println("*** TApplication.run() exits ***");
828 // ------------------------------------------------------------------------
829 // Event handlers ---------------------------------------------------------
830 // ------------------------------------------------------------------------
833 * Method that TApplication subclasses can override to handle menu or
834 * posted command events.
836 * @param command command event
837 * @return if true, this event was consumed
839 protected boolean onCommand(final TCommandEvent command
) {
840 // Default: handle cmExit
841 if (command
.equals(cmExit
)) {
842 if (messageBox(i18n
.getString("exitDialogTitle"),
843 i18n
.getString("exitDialogText"),
844 TMessageBox
.Type
.YESNO
).isYes()) {
851 if (command
.equals(cmShell
)) {
852 openTerminal(0, 0, TWindow
.RESIZABLE
);
856 if (command
.equals(cmTile
)) {
860 if (command
.equals(cmCascade
)) {
864 if (command
.equals(cmCloseAll
)) {
869 if (command
.equals(cmMenu
)) {
870 if (!modalWindowActive() && (activeMenu
== null)) {
871 if (menus
.size() > 0) {
872 menus
.get(0).setActive(true);
873 activeMenu
= menus
.get(0);
883 * Method that TApplication subclasses can override to handle menu
886 * @param menu menu event
887 * @return if true, this event was consumed
889 protected boolean onMenu(final TMenuEvent menu
) {
891 // Default: handle MID_EXIT
892 if (menu
.getId() == TMenu
.MID_EXIT
) {
893 if (messageBox(i18n
.getString("exitDialogTitle"),
894 i18n
.getString("exitDialogText"),
895 TMessageBox
.Type
.YESNO
).isYes()) {
902 if (menu
.getId() == TMenu
.MID_SHELL
) {
903 openTerminal(0, 0, TWindow
.RESIZABLE
);
907 if (menu
.getId() == TMenu
.MID_TILE
) {
911 if (menu
.getId() == TMenu
.MID_CASCADE
) {
915 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
919 if (menu
.getId() == TMenu
.MID_ABOUT
) {
923 if (menu
.getId() == TMenu
.MID_REPAINT
) {
924 getScreen().clearPhysical();
928 if (menu
.getId() == TMenu
.MID_VIEW_IMAGE
) {
932 if (menu
.getId() == TMenu
.MID_SCREEN_OPTIONS
) {
933 new TFontChooserWindow(this);
940 * Method that TApplication subclasses can override to handle keystrokes.
942 * @param keypress keystroke event
943 * @return if true, this event was consumed
945 protected boolean onKeypress(final TKeypressEvent keypress
) {
946 // Default: only menu shortcuts
948 // Process Alt-F, Alt-E, etc. menu shortcut keys
949 if (!keypress
.getKey().isFnKey()
950 && keypress
.getKey().isAlt()
951 && !keypress
.getKey().isCtrl()
952 && (activeMenu
== null)
953 && !modalWindowActive()
956 assert (subMenus
.size() == 0);
958 for (TMenu menu
: menus
) {
959 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
960 == Character
.toLowerCase(keypress
.getKey().getChar())
963 menu
.setActive(true);
973 * Process background events, and update the screen.
975 private void finishEventProcessing() {
977 System
.err
.printf(System
.currentTimeMillis() + " " +
978 Thread
.currentThread() + " finishEventProcessing()\n");
981 // Process timers and call doIdle()'s
985 synchronized (getScreen()) {
989 // Wake up the screen repainter
993 System
.err
.printf(System
.currentTimeMillis() + " " +
994 Thread
.currentThread() + " finishEventProcessing() END\n");
999 * Peek at certain application-level events, add to eventQueue, and wake
1000 * up the consuming Thread.
1002 * @param event the input event to consume
1004 private void metaHandleEvent(final TInputEvent event
) {
1007 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
1008 event
)); System
.err
.flush();
1012 // Do no more processing if the application is already trying
1017 // Special application-wide events -------------------------------
1020 if (event
instanceof TCommandEvent
) {
1021 TCommandEvent command
= (TCommandEvent
) event
;
1022 if (command
.equals(cmAbort
)) {
1028 synchronized (drainEventQueue
) {
1030 if (event
instanceof TResizeEvent
) {
1031 TResizeEvent resize
= (TResizeEvent
) event
;
1032 synchronized (getScreen()) {
1033 if ((System
.currentTimeMillis() - screenResizeTime
>= 15)
1034 || (resize
.getWidth() < getScreen().getWidth())
1035 || (resize
.getHeight() < getScreen().getHeight())
1037 getScreen().setDimensions(resize
.getWidth(),
1038 resize
.getHeight());
1039 screenResizeTime
= System
.currentTimeMillis();
1041 desktopBottom
= getScreen().getHeight() - 1;
1047 if (desktop
!= null) {
1048 desktop
.setDimensions(0, 0, resize
.getWidth(),
1049 resize
.getHeight() - 1);
1050 desktop
.onResize(resize
);
1053 // Change menu edges if needed.
1056 // We are dirty, redraw the screen.
1060 System.err.println("New screen: " + resize.getWidth() +
1061 " x " + resize.getHeight());
1066 // Put into the main queue
1067 drainEventQueue
.add(event
);
1072 * Dispatch one event to the appropriate widget or application-level
1073 * event handler. This is the primary event handler, it has the normal
1074 * application-wide event handling.
1076 * @param event the input event to consume
1077 * @see #secondaryHandleEvent(TInputEvent event)
1079 private void primaryHandleEvent(final TInputEvent event
) {
1082 System
.err
.printf("%s primaryHandleEvent: %s\n",
1083 Thread
.currentThread(), event
);
1085 TMouseEvent doubleClick
= null;
1087 // Special application-wide events -----------------------------------
1089 // Peek at the mouse position
1090 if (event
instanceof TMouseEvent
) {
1091 TMouseEvent mouse
= (TMouseEvent
) event
;
1092 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1095 mouseX
= mouse
.getX();
1096 mouseY
= mouse
.getY();
1098 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1099 && (!mouse
.isMouseWheelUp())
1100 && (!mouse
.isMouseWheelDown())
1102 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
1105 // This is a double-click.
1106 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
1108 mouse
.getX(), mouse
.getY(),
1109 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
1110 mouse
.isMouse1(), mouse
.isMouse2(),
1112 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
1115 // The first click of a potential double-click.
1116 lastMouseUpTime
= mouse
.getTime().getTime();
1121 // See if we need to switch focus to another window or the menu
1122 checkSwitchFocus((TMouseEvent
) event
);
1125 // Handle menu events
1126 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
1127 TMenu menu
= activeMenu
;
1129 if (event
instanceof TMouseEvent
) {
1130 TMouseEvent mouse
= (TMouseEvent
) event
;
1132 while (subMenus
.size() > 0) {
1133 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
1134 if (subMenu
.mouseWouldHit(mouse
)) {
1137 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1138 && (!mouse
.isMouse1())
1139 && (!mouse
.isMouse2())
1140 && (!mouse
.isMouse3())
1141 && (!mouse
.isMouseWheelUp())
1142 && (!mouse
.isMouseWheelDown())
1146 // We navigated away from a sub-menu, so close it
1150 // Convert the mouse relative x/y to menu coordinates
1151 assert (mouse
.getX() == mouse
.getAbsoluteX());
1152 assert (mouse
.getY() == mouse
.getAbsoluteY());
1153 if (subMenus
.size() > 0) {
1154 menu
= subMenus
.get(subMenus
.size() - 1);
1156 mouse
.setX(mouse
.getX() - menu
.getX());
1157 mouse
.setY(mouse
.getY() - menu
.getY());
1159 menu
.handleEvent(event
);
1163 if (event
instanceof TKeypressEvent
) {
1164 TKeypressEvent keypress
= (TKeypressEvent
) event
;
1166 // See if this key matches an accelerator, and is not being
1167 // shortcutted by the active window, and if so dispatch the menu
1169 boolean windowWillShortcut
= false;
1170 if (activeWindow
!= null) {
1171 assert (activeWindow
.isShown());
1172 if (activeWindow
.isShortcutKeypress(keypress
.getKey())) {
1173 // We do not process this key, it will be passed to the
1175 windowWillShortcut
= true;
1179 if (!windowWillShortcut
&& !modalWindowActive()) {
1180 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
1181 TMenuItem item
= null;
1182 synchronized (accelerators
) {
1183 item
= accelerators
.get(keypressLowercase
);
1186 if (item
.isEnabled()) {
1187 // Let the menu item dispatch
1193 // Handle the keypress
1194 if (onKeypress(keypress
)) {
1200 if (event
instanceof TCommandEvent
) {
1201 if (onCommand((TCommandEvent
) event
)) {
1206 if (event
instanceof TMenuEvent
) {
1207 if (onMenu((TMenuEvent
) event
)) {
1212 // Dispatch events to the active window -------------------------------
1213 boolean dispatchToDesktop
= true;
1214 TWindow window
= activeWindow
;
1215 if (window
!= null) {
1216 assert (window
.isActive());
1217 assert (window
.isShown());
1218 if (event
instanceof TMouseEvent
) {
1219 TMouseEvent mouse
= (TMouseEvent
) event
;
1220 // Convert the mouse relative x/y to window coordinates
1221 assert (mouse
.getX() == mouse
.getAbsoluteX());
1222 assert (mouse
.getY() == mouse
.getAbsoluteY());
1223 mouse
.setX(mouse
.getX() - window
.getX());
1224 mouse
.setY(mouse
.getY() - window
.getY());
1226 if (doubleClick
!= null) {
1227 doubleClick
.setX(doubleClick
.getX() - window
.getX());
1228 doubleClick
.setY(doubleClick
.getY() - window
.getY());
1231 if (window
.mouseWouldHit(mouse
)) {
1232 dispatchToDesktop
= false;
1234 } else if (event
instanceof TKeypressEvent
) {
1235 dispatchToDesktop
= false;
1239 System
.err
.printf("TApplication dispatch event: %s\n",
1242 window
.handleEvent(event
);
1243 if (doubleClick
!= null) {
1244 window
.handleEvent(doubleClick
);
1247 if (dispatchToDesktop
) {
1248 // This event is fair game for the desktop to process.
1249 if (desktop
!= null) {
1250 desktop
.handleEvent(event
);
1251 if (doubleClick
!= null) {
1252 desktop
.handleEvent(doubleClick
);
1259 * Dispatch one event to the appropriate widget or application-level
1260 * event handler. This is the secondary event handler used by certain
1261 * special dialogs (currently TMessageBox and TFileOpenBox).
1263 * @param event the input event to consume
1264 * @see #primaryHandleEvent(TInputEvent event)
1266 private void secondaryHandleEvent(final TInputEvent event
) {
1267 TMouseEvent doubleClick
= null;
1270 System
.err
.printf("%s secondaryHandleEvent: %s\n",
1271 Thread
.currentThread(), event
);
1274 // Peek at the mouse position
1275 if (event
instanceof TMouseEvent
) {
1276 TMouseEvent mouse
= (TMouseEvent
) event
;
1277 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1280 mouseX
= mouse
.getX();
1281 mouseY
= mouse
.getY();
1283 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1284 && (!mouse
.isMouseWheelUp())
1285 && (!mouse
.isMouseWheelDown())
1287 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
1290 // This is a double-click.
1291 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
1293 mouse
.getX(), mouse
.getY(),
1294 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
1295 mouse
.isMouse1(), mouse
.isMouse2(),
1297 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
1300 // The first click of a potential double-click.
1301 lastMouseUpTime
= mouse
.getTime().getTime();
1307 secondaryEventReceiver
.handleEvent(event
);
1308 // Note that it is possible for secondaryEventReceiver to be null
1309 // now, because its handleEvent() might have finished out on the
1310 // secondary thread. So put any extra processing inside a null
1312 if (secondaryEventReceiver
!= null) {
1313 if (doubleClick
!= null) {
1314 secondaryEventReceiver
.handleEvent(doubleClick
);
1320 * Enable a widget to override the primary event thread.
1322 * @param widget widget that will receive events
1324 public final void enableSecondaryEventReceiver(final TWidget widget
) {
1326 System
.err
.println(System
.currentTimeMillis() +
1327 " enableSecondaryEventReceiver()");
1330 assert (secondaryEventReceiver
== null);
1331 assert (secondaryEventHandler
== null);
1332 assert ((widget
instanceof TMessageBox
)
1333 || (widget
instanceof TFileOpenBox
));
1334 secondaryEventReceiver
= widget
;
1335 secondaryEventHandler
= new WidgetEventHandler(this, false);
1337 (new Thread(secondaryEventHandler
)).start();
1341 * Yield to the secondary thread.
1343 public final void yield() {
1345 System
.err
.printf(System
.currentTimeMillis() + " " +
1346 Thread
.currentThread() + " yield()\n");
1349 assert (secondaryEventReceiver
!= null);
1351 while (secondaryEventReceiver
!= null) {
1352 synchronized (primaryEventHandler
) {
1354 primaryEventHandler
.wait();
1355 } catch (InterruptedException e
) {
1363 * Do stuff when there is no user input.
1365 private void doIdle() {
1367 System
.err
.printf(System
.currentTimeMillis() + " " +
1368 Thread
.currentThread() + " doIdle()\n");
1371 synchronized (timers
) {
1374 System
.err
.printf(System
.currentTimeMillis() + " " +
1375 Thread
.currentThread() + " doIdle() 2\n");
1378 // Run any timers that have timed out
1379 Date now
= new Date();
1380 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
1381 for (TTimer timer
: timers
) {
1382 if (timer
.getNextTick().getTime() <= now
.getTime()) {
1383 // Something might change, so repaint the screen.
1386 if (timer
.recurring
) {
1387 keepTimers
.add(timer
);
1390 keepTimers
.add(timer
);
1394 timers
.addAll(keepTimers
);
1398 for (TWindow window
: windows
) {
1401 if (desktop
!= null) {
1405 // Run any invokeLaters
1406 synchronized (invokeLaters
) {
1407 for (Runnable invoke
: invokeLaters
) {
1410 invokeLaters
.clear();
1416 * Wake the sleeping active event handler.
1418 private void wakeEventHandler() {
1423 if (secondaryEventHandler
!= null) {
1424 synchronized (secondaryEventHandler
) {
1425 secondaryEventHandler
.notify();
1428 assert (primaryEventHandler
!= null);
1429 synchronized (primaryEventHandler
) {
1430 primaryEventHandler
.notify();
1436 * Wake the sleeping screen handler.
1438 private void wakeScreenHandler() {
1443 synchronized (screenHandler
) {
1444 screenHandler
.notify();
1448 // ------------------------------------------------------------------------
1449 // TApplication -----------------------------------------------------------
1450 // ------------------------------------------------------------------------
1453 * Place a command on the run queue, and run it before the next round of
1456 * @param command the command to run later
1458 public void invokeLater(final Runnable command
) {
1459 synchronized (invokeLaters
) {
1460 invokeLaters
.add(command
);
1466 * Restore the console to sane defaults. This is meant to be used for
1467 * improper exits (e.g. a caught exception in main()), and should not be
1468 * necessary for normal program termination.
1470 public void restoreConsole() {
1471 if (backend
!= null) {
1472 if (backend
instanceof ECMA48Backend
) {
1481 * @return the Backend
1483 public final Backend
getBackend() {
1490 * @return the Screen
1492 public final Screen
getScreen() {
1493 if (backend
instanceof TWindowBackend
) {
1494 // We are being rendered to a TWindow. We can't use its
1495 // getScreen() method because that is how it is rendering to a
1496 // hardware backend somewhere. Instead use its getOtherScreen()
1498 return ((TWindowBackend
) backend
).getOtherScreen();
1500 return backend
.getScreen();
1505 * Get the color theme.
1509 public final ColorTheme
getTheme() {
1514 * Repaint the screen on the next update.
1516 public void doRepaint() {
1522 * Get Y coordinate of the top edge of the desktop.
1524 * @return Y coordinate of the top edge of the desktop
1526 public final int getDesktopTop() {
1531 * Get Y coordinate of the bottom edge of the desktop.
1533 * @return Y coordinate of the bottom edge of the desktop
1535 public final int getDesktopBottom() {
1536 return desktopBottom
;
1540 * Set the TDesktop instance.
1542 * @param desktop a TDesktop instance, or null to remove the one that is
1545 public final void setDesktop(final TDesktop desktop
) {
1546 if (this.desktop
!= null) {
1547 this.desktop
.onClose();
1549 this.desktop
= desktop
;
1553 * Get the TDesktop instance.
1555 * @return the desktop, or null if it is not set
1557 public final TDesktop
getDesktop() {
1562 * Get the current active window.
1564 * @return the active window, or null if it is not set
1566 public final TWindow
getActiveWindow() {
1567 return activeWindow
;
1571 * Get a (shallow) copy of the window list.
1573 * @return a copy of the list of windows for this application
1575 public final List
<TWindow
> getAllWindows() {
1576 List
<TWindow
> result
= new ArrayList
<TWindow
>();
1577 result
.addAll(windows
);
1582 * Get focusFollowsMouse flag.
1584 * @return true if focus follows mouse: windows automatically raised if
1585 * the mouse passes over them
1587 public boolean getFocusFollowsMouse() {
1588 return focusFollowsMouse
;
1592 * Set focusFollowsMouse flag.
1594 * @param focusFollowsMouse if true, focus follows mouse: windows
1595 * automatically raised if the mouse passes over them
1597 public void setFocusFollowsMouse(final boolean focusFollowsMouse
) {
1598 this.focusFollowsMouse
= focusFollowsMouse
;
1602 * Display the about dialog.
1604 protected void showAboutDialog() {
1605 String version
= getClass().getPackage().getImplementationVersion();
1606 if (version
== null) {
1607 // This is Java 9+, use a hardcoded string here.
1610 messageBox(i18n
.getString("aboutDialogTitle"),
1611 MessageFormat
.format(i18n
.getString("aboutDialogText"), version
),
1612 TMessageBox
.Type
.OK
);
1616 * Handle the Tool | Open image menu item.
1618 private void openImage() {
1620 List
<String
> filters
= new ArrayList
<String
>();
1621 filters
.add("^.*\\.[Jj][Pp][Gg]$");
1622 filters
.add("^.*\\.[Jj][Pp][Ee][Gg]$");
1623 filters
.add("^.*\\.[Pp][Nn][Gg]$");
1624 filters
.add("^.*\\.[Gg][Ii][Ff]$");
1625 filters
.add("^.*\\.[Bb][Mm][Pp]$");
1626 String filename
= fileOpenBox(".", TFileOpenBox
.Type
.OPEN
, filters
);
1627 if (filename
!= null) {
1628 new TImageWindow(this, new File(filename
));
1630 } catch (IOException e
) {
1631 // Show this exception to the user.
1632 new TExceptionDialog(this, e
);
1637 * Check if application is still running.
1639 * @return true if the application is running
1641 public final boolean isRunning() {
1648 // ------------------------------------------------------------------------
1649 // Screen refresh loop ----------------------------------------------------
1650 // ------------------------------------------------------------------------
1653 * Invert the cell color at a position. This is used to track the mouse.
1655 * @param x column position
1656 * @param y row position
1658 private void invertCell(final int x
, final int y
) {
1659 invertCell(x
, y
, false);
1663 * Invert the cell color at a position. This is used to track the mouse.
1665 * @param x column position
1666 * @param y row position
1667 * @param onlyThisCell if true, only invert this cell
1669 private void invertCell(final int x
, final int y
,
1670 final boolean onlyThisCell
) {
1673 System
.err
.printf("%d %s invertCell() %d %d\n",
1674 System
.currentTimeMillis(), Thread
.currentThread(), x
, y
);
1676 if (activeWindow
!= null) {
1677 System
.err
.println("activeWindow.hasHiddenMouse() " +
1678 activeWindow
.hasHiddenMouse());
1682 // If this cell is on top of a visible window that has requested a
1683 // hidden mouse, bail out.
1684 if ((activeWindow
!= null) && (activeMenu
== null)) {
1685 if ((activeWindow
.hasHiddenMouse() == true)
1686 && (x
> activeWindow
.getX())
1687 && (x
< activeWindow
.getX() + activeWindow
.getWidth() - 1)
1688 && (y
> activeWindow
.getY())
1689 && (y
< activeWindow
.getY() + activeWindow
.getHeight() - 1)
1695 Cell cell
= getScreen().getCharXY(x
, y
);
1696 if (cell
.isImage()) {
1699 if (cell
.getForeColorRGB() < 0) {
1700 cell
.setForeColor(cell
.getForeColor().invert());
1702 cell
.setForeColorRGB(cell
.getForeColorRGB() ^
0x00ffffff);
1704 if (cell
.getBackColorRGB() < 0) {
1705 cell
.setBackColor(cell
.getBackColor().invert());
1707 cell
.setBackColorRGB(cell
.getBackColorRGB() ^
0x00ffffff);
1709 getScreen().putCharXY(x
, y
, cell
);
1710 if ((onlyThisCell
== true) || (cell
.getWidth() == Cell
.Width
.SINGLE
)) {
1714 // This cell is one half of a fullwidth glyph. Invert the other
1716 if (cell
.getWidth() == Cell
.Width
.LEFT
) {
1717 if (x
< getScreen().getWidth() - 1) {
1718 Cell rightHalf
= getScreen().getCharXY(x
+ 1, y
);
1719 if (rightHalf
.getWidth() == Cell
.Width
.RIGHT
) {
1720 invertCell(x
+ 1, y
, true);
1725 if (cell
.getWidth() == Cell
.Width
.RIGHT
) {
1727 Cell leftHalf
= getScreen().getCharXY(x
- 1, y
);
1728 if (leftHalf
.getWidth() == Cell
.Width
.LEFT
) {
1729 invertCell(x
- 1, y
, true);
1738 private void drawAll() {
1739 boolean menuIsActive
= false;
1742 System
.err
.printf("%d %s drawAll() enter\n",
1743 System
.currentTimeMillis(), Thread
.currentThread());
1746 // I don't think this does anything useful anymore...
1749 System
.err
.printf("%d %s drawAll() !repaint\n",
1750 System
.currentTimeMillis(), Thread
.currentThread());
1752 if ((oldDrawnMouseX
!= mouseX
) || (oldDrawnMouseY
!= mouseY
)) {
1754 System
.err
.printf("%d %s drawAll() !repaint MOUSE\n",
1755 System
.currentTimeMillis(), Thread
.currentThread());
1758 // The only thing that has happened is the mouse moved.
1760 // Redraw the old cell at that position, and save the cell at
1761 // the new mouse position.
1763 System
.err
.printf("%d %s restoreImage() %d %d\n",
1764 System
.currentTimeMillis(), Thread
.currentThread(),
1765 oldDrawnMouseX
, oldDrawnMouseY
);
1767 oldDrawnMouseCell
.restoreImage();
1768 getScreen().putCharXY(oldDrawnMouseX
, oldDrawnMouseY
,
1770 oldDrawnMouseCell
= getScreen().getCharXY(mouseX
, mouseY
);
1771 if (backend
instanceof ECMA48Backend
) {
1772 // Special case: the entire row containing the mouse has
1773 // to be re-drawn if it has any image data, AND any rows
1775 if (oldDrawnMouseY
!= mouseY
) {
1776 for (int i
= oldDrawnMouseY
; ;) {
1777 getScreen().unsetImageRow(i
);
1781 if (oldDrawnMouseY
< mouseY
) {
1788 getScreen().unsetImageRow(mouseY
);
1792 // Draw mouse at the new position.
1793 invertCell(mouseX
, mouseY
);
1795 oldDrawnMouseX
= mouseX
;
1796 oldDrawnMouseY
= mouseY
;
1798 if (getScreen().isDirty()) {
1799 screenHandler
.setDirty();
1805 System
.err
.printf("%d %s drawAll() REDRAW\n",
1806 System
.currentTimeMillis(), Thread
.currentThread());
1809 // If true, the cursor is not visible
1810 boolean cursor
= false;
1812 // Start with a clean screen
1813 getScreen().clear();
1816 if (desktop
!= null) {
1817 desktop
.drawChildren();
1820 // Draw each window in reverse Z order
1821 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
1822 Collections
.sort(sorted
);
1823 TWindow topLevel
= null;
1824 if (sorted
.size() > 0) {
1825 topLevel
= sorted
.get(0);
1827 Collections
.reverse(sorted
);
1828 for (TWindow window
: sorted
) {
1829 if (window
.isShown()) {
1830 window
.drawChildren();
1834 // Draw the blank menubar line - reset the screen clipping first so
1835 // it won't trim it out.
1836 getScreen().resetClipping();
1837 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
1838 theme
.getColor("tmenu"));
1839 // Now draw the menus.
1841 for (TMenu menu
: menus
) {
1842 CellAttributes menuColor
;
1843 CellAttributes menuMnemonicColor
;
1844 if (menu
.isActive()) {
1845 menuIsActive
= true;
1846 menuColor
= theme
.getColor("tmenu.highlighted");
1847 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
1850 menuColor
= theme
.getColor("tmenu");
1851 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
1853 // Draw the menu title
1854 getScreen().hLineXY(x
, 0, StringUtils
.width(menu
.getTitle()) + 2, ' ',
1856 getScreen().putStringXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
1857 // Draw the highlight character
1858 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getScreenShortcutIdx(),
1859 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
1861 if (menu
.isActive()) {
1862 ((TWindow
) menu
).drawChildren();
1863 // Reset the screen clipping so we can draw the next title.
1864 getScreen().resetClipping();
1866 x
+= StringUtils
.width(menu
.getTitle()) + 2;
1869 for (TMenu menu
: subMenus
) {
1870 // Reset the screen clipping so we can draw the next sub-menu.
1871 getScreen().resetClipping();
1872 ((TWindow
) menu
).drawChildren();
1874 getScreen().resetClipping();
1876 // Draw the status bar of the top-level window
1877 TStatusBar statusBar
= null;
1878 if (topLevel
!= null) {
1879 statusBar
= topLevel
.getStatusBar();
1881 if (statusBar
!= null) {
1882 getScreen().resetClipping();
1883 statusBar
.setWidth(getScreen().getWidth());
1884 statusBar
.setY(getScreen().getHeight() - topLevel
.getY());
1887 CellAttributes barColor
= new CellAttributes();
1888 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
1889 getScreen().hLineXY(0, desktopBottom
, getScreen().getWidth(), ' ',
1893 // Draw the mouse pointer
1895 System
.err
.printf("%d %s restoreImage() %d %d\n",
1896 System
.currentTimeMillis(), Thread
.currentThread(),
1897 oldDrawnMouseX
, oldDrawnMouseY
);
1899 oldDrawnMouseCell
= getScreen().getCharXY(mouseX
, mouseY
);
1900 if (backend
instanceof ECMA48Backend
) {
1901 // Special case: the entire row containing the mouse has to be
1902 // re-drawn if it has any image data, AND any rows in between.
1903 if (oldDrawnMouseY
!= mouseY
) {
1904 for (int i
= oldDrawnMouseY
; ;) {
1905 getScreen().unsetImageRow(i
);
1909 if (oldDrawnMouseY
< mouseY
) {
1916 getScreen().unsetImageRow(mouseY
);
1919 invertCell(mouseX
, mouseY
);
1920 oldDrawnMouseX
= mouseX
;
1921 oldDrawnMouseY
= mouseY
;
1923 // Place the cursor if it is visible
1924 if (!menuIsActive
) {
1925 TWidget activeWidget
= null;
1926 if (sorted
.size() > 0) {
1927 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
1928 if (activeWidget
.isCursorVisible()) {
1929 if ((activeWidget
.getCursorAbsoluteY() < desktopBottom
)
1930 && (activeWidget
.getCursorAbsoluteY() > desktopTop
)
1932 getScreen().putCursor(true,
1933 activeWidget
.getCursorAbsoluteX(),
1934 activeWidget
.getCursorAbsoluteY());
1937 // Turn off the cursor. Also place it at 0,0.
1938 getScreen().putCursor(false, 0, 0);
1947 getScreen().hideCursor();
1950 if (getScreen().isDirty()) {
1951 screenHandler
.setDirty();
1957 * Force this application to exit.
1959 public void exit() {
1961 synchronized (this) {
1967 * Subclasses can use this hook to cleanup resources. Called as the last
1968 * step of TApplication.run().
1970 public void onExit() {
1971 // Default does nothing.
1974 // ------------------------------------------------------------------------
1975 // TWindow management -----------------------------------------------------
1976 // ------------------------------------------------------------------------
1979 * Return the total number of windows.
1981 * @return the total number of windows
1983 public final int windowCount() {
1984 return windows
.size();
1988 * Return the number of windows that are showing.
1990 * @return the number of windows that are showing on screen
1992 public final int shownWindowCount() {
1994 for (TWindow w
: windows
) {
2003 * Return the number of windows that are hidden.
2005 * @return the number of windows that are hidden
2007 public final int hiddenWindowCount() {
2009 for (TWindow w
: windows
) {
2018 * Check if a window instance is in this application's window list.
2020 * @param window window to look for
2021 * @return true if this window is in the list
2023 public final boolean hasWindow(final TWindow window
) {
2024 if (windows
.size() == 0) {
2027 for (TWindow w
: windows
) {
2029 assert (window
.getApplication() == this);
2037 * Activate a window: bring it to the top and have it receive events.
2039 * @param window the window to become the new active window
2041 public void activateWindow(final TWindow window
) {
2042 if (hasWindow(window
) == false) {
2044 * Someone has a handle to a window I don't have. Ignore this
2050 // Whatever window might be moving/dragging, stop it now.
2051 for (TWindow w
: windows
) {
2052 if (w
.inMovements()) {
2057 assert (windows
.size() > 0);
2059 if (window
.isHidden()) {
2060 // Unhiding will also activate.
2064 assert (window
.isShown());
2066 if (windows
.size() == 1) {
2067 assert (window
== windows
.get(0));
2068 if (activeWindow
== null) {
2069 activeWindow
= window
;
2071 activeWindow
.setActive(true);
2072 activeWindow
.onFocus();
2075 assert (window
.isActive());
2076 assert (activeWindow
== window
);
2080 if (activeWindow
== window
) {
2081 assert (window
.isActive());
2083 // Window is already active, do nothing.
2087 assert (!window
.isActive());
2088 if (activeWindow
!= null) {
2089 // TODO: see if this assertion is really necessary.
2090 // assert (activeWindow.getZ() == 0);
2092 activeWindow
.setActive(false);
2094 // Increment every window Z that is on top of window
2095 for (TWindow w
: windows
) {
2099 if (w
.getZ() < window
.getZ()) {
2100 w
.setZ(w
.getZ() + 1);
2104 // Unset activeWindow now before unfocus, so that a window
2105 // lifecycle change inside onUnfocus() doesn't call
2106 // switchWindow() and lead to a stack overflow.
2107 TWindow oldActiveWindow
= activeWindow
;
2108 activeWindow
= null;
2109 oldActiveWindow
.onUnfocus();
2111 activeWindow
= window
;
2112 activeWindow
.setZ(0);
2113 activeWindow
.setActive(true);
2114 activeWindow
.onFocus();
2121 * @param window the window to hide
2123 public void hideWindow(final TWindow window
) {
2124 if (hasWindow(window
) == false) {
2126 * Someone has a handle to a window I don't have. Ignore this
2132 // Whatever window might be moving/dragging, stop it now.
2133 for (TWindow w
: windows
) {
2134 if (w
.inMovements()) {
2139 assert (windows
.size() > 0);
2141 if (!window
.hidden
) {
2142 if (window
== activeWindow
) {
2143 if (shownWindowCount() > 1) {
2146 activeWindow
= null;
2147 window
.setActive(false);
2151 window
.hidden
= true;
2159 * @param window the window to show
2161 public void showWindow(final TWindow window
) {
2162 if (hasWindow(window
) == false) {
2164 * Someone has a handle to a window I don't have. Ignore this
2170 // Whatever window might be moving/dragging, stop it now.
2171 for (TWindow w
: windows
) {
2172 if (w
.inMovements()) {
2177 assert (windows
.size() > 0);
2179 if (window
.hidden
) {
2180 window
.hidden
= false;
2182 activateWindow(window
);
2187 * Close window. Note that the window's destructor is NOT called by this
2188 * method, instead the GC is assumed to do the cleanup.
2190 * @param window the window to remove
2192 public final void closeWindow(final TWindow window
) {
2193 if (hasWindow(window
) == false) {
2195 * Someone has a handle to a window I don't have. Ignore this
2201 // Let window know that it is about to be closed, while it is still
2202 // visible on screen.
2203 window
.onPreClose();
2205 synchronized (windows
) {
2206 // Whatever window might be moving/dragging, stop it now.
2207 for (TWindow w
: windows
) {
2208 if (w
.inMovements()) {
2213 int z
= window
.getZ();
2216 windows
.remove(window
);
2217 Collections
.sort(windows
);
2218 activeWindow
= null;
2220 boolean foundNextWindow
= false;
2222 for (TWindow w
: windows
) {
2226 // Do not activate a hidden window.
2231 if (foundNextWindow
== false) {
2232 foundNextWindow
= true;
2235 assert (activeWindow
== null);
2247 // Perform window cleanup
2250 // Check if we are closing a TMessageBox or similar
2251 if (secondaryEventReceiver
!= null) {
2252 assert (secondaryEventHandler
!= null);
2254 // Do not send events to the secondaryEventReceiver anymore, the
2255 // window is closed.
2256 secondaryEventReceiver
= null;
2258 // Wake the secondary thread, it will wake the primary as it
2260 synchronized (secondaryEventHandler
) {
2261 secondaryEventHandler
.notify();
2265 // Permit desktop to be active if it is the only thing left.
2266 if (desktop
!= null) {
2267 if (windows
.size() == 0) {
2268 desktop
.setActive(true);
2274 * Switch to the next window.
2276 * @param forward if true, then switch to the next window in the list,
2277 * otherwise switch to the previous window in the list
2279 public final void switchWindow(final boolean forward
) {
2280 // Only switch if there are multiple visible windows
2281 if (shownWindowCount() < 2) {
2284 assert (activeWindow
!= null);
2286 synchronized (windows
) {
2287 // Whatever window might be moving/dragging, stop it now.
2288 for (TWindow w
: windows
) {
2289 if (w
.inMovements()) {
2294 // Swap z/active between active window and the next in the list
2295 int activeWindowI
= -1;
2296 for (int i
= 0; i
< windows
.size(); i
++) {
2297 if (windows
.get(i
) == activeWindow
) {
2298 assert (activeWindow
.isActive());
2302 assert (!windows
.get(0).isActive());
2305 assert (activeWindowI
>= 0);
2307 // Do not switch if a window is modal
2308 if (activeWindow
.isModal()) {
2312 int nextWindowI
= activeWindowI
;
2316 nextWindowI
%= windows
.size();
2319 if (nextWindowI
< 0) {
2320 nextWindowI
= windows
.size() - 1;
2324 if (windows
.get(nextWindowI
).isShown()) {
2325 activateWindow(windows
.get(nextWindowI
));
2329 } // synchronized (windows)
2334 * Add a window to my window list and make it active. Note package
2337 * @param window new window to add
2339 final void addWindowToApplication(final TWindow window
) {
2341 // Do not add menu windows to the window list.
2342 if (window
instanceof TMenu
) {
2346 // Do not add the desktop to the window list.
2347 if (window
instanceof TDesktop
) {
2351 synchronized (windows
) {
2352 if (windows
.contains(window
)) {
2353 throw new IllegalArgumentException("Window " + window
+
2354 " is already in window list");
2357 // Whatever window might be moving/dragging, stop it now.
2358 for (TWindow w
: windows
) {
2359 if (w
.inMovements()) {
2364 // Do not allow a modal window to spawn a non-modal window. If a
2365 // modal window is active, then this window will become modal
2367 if (modalWindowActive()) {
2368 window
.flags
|= TWindow
.MODAL
;
2369 window
.flags
|= TWindow
.CENTERED
;
2370 window
.hidden
= false;
2372 if (window
.isShown()) {
2373 for (TWindow w
: windows
) {
2378 w
.setZ(w
.getZ() + 1);
2381 windows
.add(window
);
2382 if (window
.isShown()) {
2383 activeWindow
= window
;
2384 activeWindow
.setZ(0);
2385 activeWindow
.setActive(true);
2386 activeWindow
.onFocus();
2389 if (((window
.flags
& TWindow
.CENTERED
) == 0)
2390 && ((window
.flags
& TWindow
.ABSOLUTEXY
) == 0)
2391 && (smartWindowPlacement
== true)
2394 doSmartPlacement(window
);
2398 // Desktop cannot be active over any other window.
2399 if (desktop
!= null) {
2400 desktop
.setActive(false);
2405 * Check if there is a system-modal window on top.
2407 * @return true if the active window is modal
2409 private boolean modalWindowActive() {
2410 if (windows
.size() == 0) {
2414 for (TWindow w
: windows
) {
2424 * Check if there is a window with overridden menu flag on top.
2426 * @return true if the active window is overriding the menu
2428 private boolean overrideMenuWindowActive() {
2429 if (activeWindow
!= null) {
2430 if (activeWindow
.hasOverriddenMenu()) {
2439 * Close all open windows.
2441 private void closeAllWindows() {
2442 // Don't do anything if we are in the menu
2443 if (activeMenu
!= null) {
2446 while (windows
.size() > 0) {
2447 closeWindow(windows
.get(0));
2452 * Re-layout the open windows as non-overlapping tiles. This produces
2453 * almost the same results as Turbo Pascal 7.0's IDE.
2455 private void tileWindows() {
2456 synchronized (windows
) {
2457 // Don't do anything if we are in the menu
2458 if (activeMenu
!= null) {
2461 int z
= windows
.size();
2467 a
= (int)(Math
.sqrt(z
));
2471 if (((a
* b
) + c
) == z
) {
2479 int newWidth
= (getScreen().getWidth() / a
);
2480 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
2481 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
2483 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
2484 Collections
.sort(sorted
);
2485 Collections
.reverse(sorted
);
2486 for (int i
= 0; i
< sorted
.size(); i
++) {
2487 int logicalX
= i
/ b
;
2488 int logicalY
= i
% b
;
2489 if (i
>= ((a
- 1) * b
)) {
2491 logicalY
= i
- ((a
- 1) * b
);
2494 TWindow w
= sorted
.get(i
);
2495 int oldWidth
= w
.getWidth();
2496 int oldHeight
= w
.getHeight();
2498 w
.setX(logicalX
* newWidth
);
2499 w
.setWidth(newWidth
);
2500 if (i
>= ((a
- 1) * b
)) {
2501 w
.setY((logicalY
* newHeight2
) + 1);
2502 w
.setHeight(newHeight2
);
2504 w
.setY((logicalY
* newHeight1
) + 1);
2505 w
.setHeight(newHeight1
);
2507 if ((w
.getWidth() != oldWidth
)
2508 || (w
.getHeight() != oldHeight
)
2510 w
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
2511 w
.getWidth(), w
.getHeight()));
2518 * Re-layout the open windows as overlapping cascaded windows.
2520 private void cascadeWindows() {
2521 synchronized (windows
) {
2522 // Don't do anything if we are in the menu
2523 if (activeMenu
!= null) {
2528 List
<TWindow
> sorted
= new ArrayList
<TWindow
>(windows
);
2529 Collections
.sort(sorted
);
2530 Collections
.reverse(sorted
);
2531 for (TWindow window
: sorted
) {
2536 if (x
> getScreen().getWidth()) {
2539 if (y
>= getScreen().getHeight()) {
2547 * Place a window to minimize its overlap with other windows.
2549 * @param window the window to place
2551 public final void doSmartPlacement(final TWindow window
) {
2552 // This is a pretty dumb algorithm, but seems to work. The hardest
2553 // part is computing these "overlap" values seeking a minimum average
2556 int yMin
= desktopTop
;
2557 int xMax
= getScreen().getWidth() - window
.getWidth() + 1;
2558 int yMax
= desktopBottom
- window
.getHeight() + 1;
2566 if ((xMin
== xMax
) && (yMin
== yMax
)) {
2567 // No work to do, bail out.
2571 // Compute the overlap matrix without the new window.
2572 int width
= getScreen().getWidth();
2573 int height
= getScreen().getHeight();
2574 int overlapMatrix
[][] = new int[width
][height
];
2575 for (TWindow w
: windows
) {
2579 for (int x
= w
.getX(); x
< w
.getX() + w
.getWidth(); x
++) {
2586 for (int y
= w
.getY(); y
< w
.getY() + w
.getHeight(); y
++) {
2593 overlapMatrix
[x
][y
]++;
2598 long oldOverlapTotal
= 0;
2599 long oldOverlapN
= 0;
2600 for (int x
= 0; x
< width
; x
++) {
2601 for (int y
= 0; y
< height
; y
++) {
2602 oldOverlapTotal
+= overlapMatrix
[x
][y
];
2603 if (overlapMatrix
[x
][y
] > 0) {
2610 double oldOverlapAvg
= (double) oldOverlapTotal
/ (double) oldOverlapN
;
2611 boolean first
= true;
2612 int windowX
= window
.getX();
2613 int windowY
= window
.getY();
2615 // For each possible (x, y) position for the new window, compute a
2616 // new overlap matrix.
2617 for (int x
= xMin
; x
< xMax
; x
++) {
2618 for (int y
= yMin
; y
< yMax
; y
++) {
2620 // Start with the matrix minus this window.
2621 int newMatrix
[][] = new int[width
][height
];
2622 for (int mx
= 0; mx
< width
; mx
++) {
2623 for (int my
= 0; my
< height
; my
++) {
2624 newMatrix
[mx
][my
] = overlapMatrix
[mx
][my
];
2628 // Add this window's values to the new overlap matrix.
2629 long newOverlapTotal
= 0;
2630 long newOverlapN
= 0;
2631 // Start by adding each new cell.
2632 for (int wx
= x
; wx
< x
+ window
.getWidth(); wx
++) {
2636 for (int wy
= y
; wy
< y
+ window
.getHeight(); wy
++) {
2640 newMatrix
[wx
][wy
]++;
2643 // Now figure out the new value for total coverage.
2644 for (int mx
= 0; mx
< width
; mx
++) {
2645 for (int my
= 0; my
< height
; my
++) {
2646 newOverlapTotal
+= newMatrix
[x
][y
];
2647 if (newMatrix
[mx
][my
] > 0) {
2652 double newOverlapAvg
= (double) newOverlapTotal
/ (double) newOverlapN
;
2655 // First time: just record what we got.
2656 oldOverlapAvg
= newOverlapAvg
;
2659 // All other times: pick a new best (x, y) and save the
2661 if (newOverlapAvg
< oldOverlapAvg
) {
2664 oldOverlapAvg
= newOverlapAvg
;
2668 } // for (int x = xMin; x < xMax; x++)
2670 } // for (int y = yMin; y < yMax; y++)
2672 // Finally, set the window's new coordinates.
2673 window
.setX(windowX
);
2674 window
.setY(windowY
);
2677 // ------------------------------------------------------------------------
2678 // TMenu management -------------------------------------------------------
2679 // ------------------------------------------------------------------------
2682 * Check if a mouse event would hit either the active menu or any open
2685 * @param mouse mouse event
2686 * @return true if the mouse would hit the active menu or an open
2689 private boolean mouseOnMenu(final TMouseEvent mouse
) {
2690 assert (activeMenu
!= null);
2691 List
<TMenu
> menus
= new ArrayList
<TMenu
>(subMenus
);
2692 Collections
.reverse(menus
);
2693 for (TMenu menu
: menus
) {
2694 if (menu
.mouseWouldHit(mouse
)) {
2698 return activeMenu
.mouseWouldHit(mouse
);
2702 * See if we need to switch window or activate the menu based on
2705 * @param mouse mouse event
2707 private void checkSwitchFocus(final TMouseEvent mouse
) {
2709 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2710 && (activeMenu
!= null)
2711 && (mouse
.getAbsoluteY() != 0)
2712 && (!mouseOnMenu(mouse
))
2714 // They clicked outside the active menu, turn it off
2715 activeMenu
.setActive(false);
2717 for (TMenu menu
: subMenus
) {
2718 menu
.setActive(false);
2724 // See if they hit the menu bar
2725 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2726 && (mouse
.isMouse1())
2727 && (!modalWindowActive())
2728 && (!overrideMenuWindowActive())
2729 && (mouse
.getAbsoluteY() == 0)
2732 for (TMenu menu
: subMenus
) {
2733 menu
.setActive(false);
2737 // They selected the menu, go activate it
2738 for (TMenu menu
: menus
) {
2739 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2740 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2741 + StringUtils
.width(menu
.getTitle()) + 2)
2743 menu
.setActive(true);
2746 menu
.setActive(false);
2752 // See if they hit the menu bar
2753 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
2754 && (mouse
.isMouse1())
2755 && (activeMenu
!= null)
2756 && (mouse
.getAbsoluteY() == 0)
2759 TMenu oldMenu
= activeMenu
;
2760 for (TMenu menu
: subMenus
) {
2761 menu
.setActive(false);
2765 // See if we should switch menus
2766 for (TMenu menu
: menus
) {
2767 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2768 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2769 + StringUtils
.width(menu
.getTitle()) + 2)
2771 menu
.setActive(true);
2775 if (oldMenu
!= activeMenu
) {
2776 // They switched menus
2777 oldMenu
.setActive(false);
2782 // If a menu is still active, don't switch windows
2783 if (activeMenu
!= null) {
2787 // Only switch if there are multiple windows
2788 if (windows
.size() < 2) {
2792 if (((focusFollowsMouse
== true)
2793 && (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
))
2794 || (mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2796 synchronized (windows
) {
2797 Collections
.sort(windows
);
2798 if (windows
.get(0).isModal()) {
2799 // Modal windows don't switch
2803 for (TWindow window
: windows
) {
2804 assert (!window
.isModal());
2806 if (window
.isHidden()) {
2807 assert (!window
.isActive());
2811 if (window
.mouseWouldHit(mouse
)) {
2812 if (window
== windows
.get(0)) {
2813 // Clicked on the same window, nothing to do
2814 assert (window
.isActive());
2818 // We will be switching to another window
2819 assert (windows
.get(0).isActive());
2820 assert (windows
.get(0) == activeWindow
);
2821 assert (!window
.isActive());
2822 if (activeWindow
!= null) {
2823 activeWindow
.onUnfocus();
2824 activeWindow
.setActive(false);
2825 activeWindow
.setZ(window
.getZ());
2827 activeWindow
= window
;
2829 window
.setActive(true);
2836 // Clicked on the background, nothing to do
2840 // Nothing to do: this isn't a mouse up, or focus isn't following
2846 * Turn off the menu.
2848 public final void closeMenu() {
2849 if (activeMenu
!= null) {
2850 activeMenu
.setActive(false);
2852 for (TMenu menu
: subMenus
) {
2853 menu
.setActive(false);
2860 * Get a (shallow) copy of the menu list.
2862 * @return a copy of the menu list
2864 public final List
<TMenu
> getAllMenus() {
2865 return new ArrayList
<TMenu
>(menus
);
2869 * Add a top-level menu to the list.
2871 * @param menu the menu to add
2872 * @throws IllegalArgumentException if the menu is already used in
2873 * another TApplication
2875 public final void addMenu(final TMenu menu
) {
2876 if ((menu
.getApplication() != null)
2877 && (menu
.getApplication() != this)
2879 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2880 "part of application " + menu
.getApplication());
2888 * Remove a top-level menu from the list.
2890 * @param menu the menu to remove
2891 * @throws IllegalArgumentException if the menu is already used in
2892 * another TApplication
2894 public final void removeMenu(final TMenu menu
) {
2895 if ((menu
.getApplication() != null)
2896 && (menu
.getApplication() != this)
2898 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2899 "part of application " + menu
.getApplication());
2907 * Turn off a sub-menu.
2909 public final void closeSubMenu() {
2910 assert (activeMenu
!= null);
2911 TMenu item
= subMenus
.get(subMenus
.size() - 1);
2912 assert (item
!= null);
2913 item
.setActive(false);
2914 subMenus
.remove(subMenus
.size() - 1);
2918 * Switch to the next menu.
2920 * @param forward if true, then switch to the next menu in the list,
2921 * otherwise switch to the previous menu in the list
2923 public final void switchMenu(final boolean forward
) {
2924 assert (activeMenu
!= null);
2926 for (TMenu menu
: subMenus
) {
2927 menu
.setActive(false);
2931 for (int i
= 0; i
< menus
.size(); i
++) {
2932 if (activeMenu
== menus
.get(i
)) {
2934 if (i
< menus
.size() - 1) {
2943 i
= menus
.size() - 1;
2946 activeMenu
.setActive(false);
2947 activeMenu
= menus
.get(i
);
2948 activeMenu
.setActive(true);
2955 * Add a menu item to the global list. If it has a keyboard accelerator,
2956 * that will be added the global hash.
2958 * @param item the menu item
2960 public final void addMenuItem(final TMenuItem item
) {
2961 menuItems
.add(item
);
2963 TKeypress key
= item
.getKey();
2965 synchronized (accelerators
) {
2966 assert (accelerators
.get(key
) == null);
2967 accelerators
.put(key
.toLowerCase(), item
);
2973 * Disable one menu item.
2975 * @param id the menu item ID
2977 public final void disableMenuItem(final int id
) {
2978 for (TMenuItem item
: menuItems
) {
2979 if (item
.getId() == id
) {
2980 item
.setEnabled(false);
2986 * Disable the range of menu items with ID's between lower and upper,
2989 * @param lower the lowest menu item ID
2990 * @param upper the highest menu item ID
2992 public final void disableMenuItems(final int lower
, final int upper
) {
2993 for (TMenuItem item
: menuItems
) {
2994 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2995 item
.setEnabled(false);
2996 item
.getParent().activate(0);
3002 * Enable one menu item.
3004 * @param id the menu item ID
3006 public final void enableMenuItem(final int id
) {
3007 for (TMenuItem item
: menuItems
) {
3008 if (item
.getId() == id
) {
3009 item
.setEnabled(true);
3010 item
.getParent().activate(0);
3016 * Enable the range of menu items with ID's between lower and upper,
3019 * @param lower the lowest menu item ID
3020 * @param upper the highest menu item ID
3022 public final void enableMenuItems(final int lower
, final int upper
) {
3023 for (TMenuItem item
: menuItems
) {
3024 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
3025 item
.setEnabled(true);
3026 item
.getParent().activate(0);
3032 * Get the menu item associated with this ID.
3034 * @param id the menu item ID
3035 * @return the menu item, or null if not found
3037 public final TMenuItem
getMenuItem(final int id
) {
3038 for (TMenuItem item
: menuItems
) {
3039 if (item
.getId() == id
) {
3047 * Recompute menu x positions based on their title length.
3049 public final void recomputeMenuX() {
3051 for (TMenu menu
: menus
) {
3054 x
+= StringUtils
.width(menu
.getTitle()) + 2;
3056 // Don't let the menu window exceed the screen width
3057 int rightEdge
= menu
.getX() + menu
.getWidth();
3058 if (rightEdge
> getScreen().getWidth()) {
3059 menu
.setX(getScreen().getWidth() - menu
.getWidth());
3065 * Post an event to process.
3067 * @param event new event to add to the queue
3069 public final void postEvent(final TInputEvent event
) {
3070 synchronized (this) {
3071 synchronized (fillEventQueue
) {
3072 fillEventQueue
.add(event
);
3075 System
.err
.println(System
.currentTimeMillis() + " " +
3076 Thread
.currentThread() + " postEvent() wake up main");
3083 * Post an event to process and turn off the menu.
3085 * @param event new event to add to the queue
3087 public final void postMenuEvent(final TInputEvent event
) {
3088 synchronized (this) {
3089 synchronized (fillEventQueue
) {
3090 fillEventQueue
.add(event
);
3093 System
.err
.println(System
.currentTimeMillis() + " " +
3094 Thread
.currentThread() + " postMenuEvent() wake up main");
3102 * Add a sub-menu to the list of open sub-menus.
3104 * @param menu sub-menu
3106 public final void addSubMenu(final TMenu menu
) {
3111 * Convenience function to add a top-level menu.
3113 * @param title menu title
3114 * @return the new menu
3116 public final TMenu
addMenu(final String title
) {
3119 TMenu menu
= new TMenu(this, x
, y
, title
);
3126 * Convenience function to add a default tools (hamburger) menu.
3128 * @return the new menu
3130 public final TMenu
addToolMenu() {
3131 TMenu toolMenu
= addMenu(i18n
.getString("toolMenuTitle"));
3132 toolMenu
.addDefaultItem(TMenu
.MID_REPAINT
);
3133 toolMenu
.addDefaultItem(TMenu
.MID_VIEW_IMAGE
);
3134 toolMenu
.addDefaultItem(TMenu
.MID_SCREEN_OPTIONS
);
3135 TStatusBar toolStatusBar
= toolMenu
.newStatusBar(i18n
.
3136 getString("toolMenuStatus"));
3137 toolStatusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3142 * Convenience function to add a default "File" menu.
3144 * @return the new menu
3146 public final TMenu
addFileMenu() {
3147 TMenu fileMenu
= addMenu(i18n
.getString("fileMenuTitle"));
3148 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
3149 fileMenu
.addSeparator();
3150 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
3151 TStatusBar statusBar
= fileMenu
.newStatusBar(i18n
.
3152 getString("fileMenuStatus"));
3153 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3158 * Convenience function to add a default "Edit" menu.
3160 * @return the new menu
3162 public final TMenu
addEditMenu() {
3163 TMenu editMenu
= addMenu(i18n
.getString("editMenuTitle"));
3164 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
3165 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
3166 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
3167 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
3168 TStatusBar statusBar
= editMenu
.newStatusBar(i18n
.
3169 getString("editMenuStatus"));
3170 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3175 * Convenience function to add a default "Window" menu.
3177 * @return the new menu
3179 public final TMenu
addWindowMenu() {
3180 TMenu windowMenu
= addMenu(i18n
.getString("windowMenuTitle"));
3181 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
3182 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
3183 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
3184 windowMenu
.addSeparator();
3185 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
3186 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
3187 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
3188 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
3189 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
3190 TStatusBar statusBar
= windowMenu
.newStatusBar(i18n
.
3191 getString("windowMenuStatus"));
3192 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3197 * Convenience function to add a default "Help" menu.
3199 * @return the new menu
3201 public final TMenu
addHelpMenu() {
3202 TMenu helpMenu
= addMenu(i18n
.getString("helpMenuTitle"));
3203 helpMenu
.addDefaultItem(TMenu
.MID_HELP_CONTENTS
);
3204 helpMenu
.addDefaultItem(TMenu
.MID_HELP_INDEX
);
3205 helpMenu
.addDefaultItem(TMenu
.MID_HELP_SEARCH
);
3206 helpMenu
.addDefaultItem(TMenu
.MID_HELP_PREVIOUS
);
3207 helpMenu
.addDefaultItem(TMenu
.MID_HELP_HELP
);
3208 helpMenu
.addDefaultItem(TMenu
.MID_HELP_ACTIVE_FILE
);
3209 helpMenu
.addSeparator();
3210 helpMenu
.addDefaultItem(TMenu
.MID_ABOUT
);
3211 TStatusBar statusBar
= helpMenu
.newStatusBar(i18n
.
3212 getString("helpMenuStatus"));
3213 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3218 * Convenience function to add a default "Table" menu.
3220 * @return the new menu
3222 public final TMenu
addTableMenu() {
3223 TMenu tableMenu
= addMenu(i18n
.getString("tableMenuTitle"));
3224 tableMenu
.addDefaultItem(TMenu
.MID_TABLE_RENAME_COLUMN
, false);
3225 tableMenu
.addDefaultItem(TMenu
.MID_TABLE_RENAME_ROW
, false);
3226 tableMenu
.addSeparator();
3228 TSubMenu viewMenu
= tableMenu
.addSubMenu(i18n
.
3229 getString("tableSubMenuView"));
3230 viewMenu
.addDefaultItem(TMenu
.MID_TABLE_VIEW_ROW_LABELS
, false);
3231 viewMenu
.addDefaultItem(TMenu
.MID_TABLE_VIEW_COLUMN_LABELS
, false);
3232 viewMenu
.addDefaultItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_ROW
, false);
3233 viewMenu
.addDefaultItem(TMenu
.MID_TABLE_VIEW_HIGHLIGHT_COLUMN
, false);
3235 TSubMenu borderMenu
= tableMenu
.addSubMenu(i18n
.
3236 getString("tableSubMenuBorders"));
3237 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_NONE
, false);
3238 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_ALL
, false);
3239 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_CELL_NONE
, false);
3240 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_CELL_ALL
, false);
3241 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_RIGHT
, false);
3242 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_LEFT
, false);
3243 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_TOP
, false);
3244 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_BOTTOM
, false);
3245 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
, false);
3246 borderMenu
.addDefaultItem(TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
, false);
3247 TSubMenu deleteMenu
= tableMenu
.addSubMenu(i18n
.
3248 getString("tableSubMenuDelete"));
3249 deleteMenu
.addDefaultItem(TMenu
.MID_TABLE_DELETE_LEFT
, false);
3250 deleteMenu
.addDefaultItem(TMenu
.MID_TABLE_DELETE_UP
, false);
3251 deleteMenu
.addDefaultItem(TMenu
.MID_TABLE_DELETE_ROW
, false);
3252 deleteMenu
.addDefaultItem(TMenu
.MID_TABLE_DELETE_COLUMN
, false);
3253 TSubMenu insertMenu
= tableMenu
.addSubMenu(i18n
.
3254 getString("tableSubMenuInsert"));
3255 insertMenu
.addDefaultItem(TMenu
.MID_TABLE_INSERT_LEFT
, false);
3256 insertMenu
.addDefaultItem(TMenu
.MID_TABLE_INSERT_RIGHT
, false);
3257 insertMenu
.addDefaultItem(TMenu
.MID_TABLE_INSERT_ABOVE
, false);
3258 insertMenu
.addDefaultItem(TMenu
.MID_TABLE_INSERT_BELOW
, false);
3259 TSubMenu columnMenu
= tableMenu
.addSubMenu(i18n
.
3260 getString("tableSubMenuColumn"));
3261 columnMenu
.addDefaultItem(TMenu
.MID_TABLE_COLUMN_NARROW
, false);
3262 columnMenu
.addDefaultItem(TMenu
.MID_TABLE_COLUMN_WIDEN
, false);
3263 TSubMenu fileMenu
= tableMenu
.addSubMenu(i18n
.
3264 getString("tableSubMenuFile"));
3265 fileMenu
.addDefaultItem(TMenu
.MID_TABLE_FILE_OPEN_CSV
, false);
3266 fileMenu
.addDefaultItem(TMenu
.MID_TABLE_FILE_SAVE_CSV
, false);
3267 fileMenu
.addDefaultItem(TMenu
.MID_TABLE_FILE_SAVE_TEXT
, false);
3269 TStatusBar statusBar
= tableMenu
.newStatusBar(i18n
.
3270 getString("tableMenuStatus"));
3271 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
3275 // ------------------------------------------------------------------------
3276 // TTimer management ------------------------------------------------------
3277 // ------------------------------------------------------------------------
3280 * Get the amount of time I can sleep before missing a Timer tick.
3282 * @param timeout = initial (maximum) timeout in millis
3283 * @return number of milliseconds between now and the next timer event
3285 private long getSleepTime(final long timeout
) {
3286 Date now
= new Date();
3287 long nowTime
= now
.getTime();
3288 long sleepTime
= timeout
;
3290 synchronized (timers
) {
3291 for (TTimer timer
: timers
) {
3292 long nextTickTime
= timer
.getNextTick().getTime();
3293 if (nextTickTime
< nowTime
) {
3297 long timeDifference
= nextTickTime
- nowTime
;
3298 if (timeDifference
< sleepTime
) {
3299 sleepTime
= timeDifference
;
3304 assert (sleepTime
>= 0);
3305 assert (sleepTime
<= timeout
);
3310 * Convenience function to add a timer.
3312 * @param duration number of milliseconds to wait between ticks
3313 * @param recurring if true, re-schedule this timer after every tick
3314 * @param action function to call when button is pressed
3317 public final TTimer
addTimer(final long duration
, final boolean recurring
,
3318 final TAction action
) {
3320 TTimer timer
= new TTimer(duration
, recurring
, action
);
3321 synchronized (timers
) {
3328 * Convenience function to remove a timer.
3330 * @param timer timer to remove
3332 public final void removeTimer(final TTimer timer
) {
3333 synchronized (timers
) {
3334 timers
.remove(timer
);
3338 // ------------------------------------------------------------------------
3339 // Other TWindow constructors ---------------------------------------------
3340 // ------------------------------------------------------------------------
3343 * Convenience function to spawn a message box.
3345 * @param title window title, will be centered along the top border
3346 * @param caption message to display. Use embedded newlines to get a
3348 * @return the new message box
3350 public final TMessageBox
messageBox(final String title
,
3351 final String caption
) {
3353 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
3357 * Convenience function to spawn a message box.
3359 * @param title window title, will be centered along the top border
3360 * @param caption message to display. Use embedded newlines to get a
3362 * @param type one of the TMessageBox.Type constants. Default is
3364 * @return the new message box
3366 public final TMessageBox
messageBox(final String title
,
3367 final String caption
, final TMessageBox
.Type type
) {
3369 return new TMessageBox(this, title
, caption
, type
);
3373 * Convenience function to spawn an input box.
3375 * @param title window title, will be centered along the top border
3376 * @param caption message to display. Use embedded newlines to get a
3378 * @return the new input box
3380 public final TInputBox
inputBox(final String title
, final String caption
) {
3382 return new TInputBox(this, title
, caption
);
3386 * Convenience function to spawn an input box.
3388 * @param title window title, will be centered along the top border
3389 * @param caption message to display. Use embedded newlines to get a
3391 * @param text initial text to seed the field with
3392 * @return the new input box
3394 public final TInputBox
inputBox(final String title
, final String caption
,
3395 final String text
) {
3397 return new TInputBox(this, title
, caption
, text
);
3401 * Convenience function to spawn an input box.
3403 * @param title window title, will be centered along the top border
3404 * @param caption message to display. Use embedded newlines to get a
3406 * @param text initial text to seed the field with
3407 * @param type one of the Type constants. Default is Type.OK.
3408 * @return the new input box
3410 public final TInputBox
inputBox(final String title
, final String caption
,
3411 final String text
, final TInputBox
.Type type
) {
3413 return new TInputBox(this, title
, caption
, text
, type
);
3417 * Convenience function to open a terminal window.
3419 * @param x column relative to parent
3420 * @param y row relative to parent
3421 * @return the terminal new window
3423 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
3424 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
3428 * Convenience function to open a terminal window.
3430 * @param x column relative to parent
3431 * @param y row relative to parent
3432 * @param closeOnExit if true, close the window when the command exits
3433 * @return the terminal new window
3435 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3436 final boolean closeOnExit
) {
3438 return openTerminal(x
, y
, TWindow
.RESIZABLE
, closeOnExit
);
3442 * Convenience function to open a terminal window.
3444 * @param x column relative to parent
3445 * @param y row relative to parent
3446 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3447 * @return the terminal new window
3449 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3452 return new TTerminalWindow(this, x
, y
, flags
);
3456 * Convenience function to open a terminal window.
3458 * @param x column relative to parent
3459 * @param y row relative to parent
3460 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3461 * @param closeOnExit if true, close the window when the command exits
3462 * @return the terminal new window
3464 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3465 final int flags
, final boolean closeOnExit
) {
3467 return new TTerminalWindow(this, x
, y
, flags
, closeOnExit
);
3471 * Convenience function to open a terminal window and execute a custom
3472 * command line inside it.
3474 * @param x column relative to parent
3475 * @param y row relative to parent
3476 * @param commandLine the command line to execute
3477 * @return the terminal new window
3479 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3480 final String commandLine
) {
3482 return openTerminal(x
, y
, TWindow
.RESIZABLE
, commandLine
);
3486 * Convenience function to open a terminal window and execute a custom
3487 * command line inside it.
3489 * @param x column relative to parent
3490 * @param y row relative to parent
3491 * @param commandLine the command line to execute
3492 * @param closeOnExit if true, close the window when the command exits
3493 * @return the terminal new window
3495 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3496 final String commandLine
, final boolean closeOnExit
) {
3498 return openTerminal(x
, y
, TWindow
.RESIZABLE
, commandLine
, closeOnExit
);
3502 * Convenience function to open a terminal window and execute a custom
3503 * command line inside it.
3505 * @param x column relative to parent
3506 * @param y row relative to parent
3507 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3508 * @param command the command line to execute
3509 * @return the terminal new window
3511 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3512 final int flags
, final String
[] command
) {
3514 return new TTerminalWindow(this, x
, y
, flags
, command
);
3518 * Convenience function to open a terminal window and execute a custom
3519 * command line inside it.
3521 * @param x column relative to parent
3522 * @param y row relative to parent
3523 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3524 * @param command the command line to execute
3525 * @param closeOnExit if true, close the window when the command exits
3526 * @return the terminal new window
3528 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3529 final int flags
, final String
[] command
, final boolean closeOnExit
) {
3531 return new TTerminalWindow(this, x
, y
, flags
, command
, closeOnExit
);
3535 * Convenience function to open a terminal window and execute a custom
3536 * command line inside it.
3538 * @param x column relative to parent
3539 * @param y row relative to parent
3540 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3541 * @param commandLine the command line to execute
3542 * @return the terminal new window
3544 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3545 final int flags
, final String commandLine
) {
3547 return new TTerminalWindow(this, x
, y
, flags
, commandLine
.split("\\s+"));
3551 * Convenience function to open a terminal window and execute a custom
3552 * command line inside it.
3554 * @param x column relative to parent
3555 * @param y row relative to parent
3556 * @param flags mask of CENTERED, MODAL, or RESIZABLE
3557 * @param commandLine the command line to execute
3558 * @param closeOnExit if true, close the window when the command exits
3559 * @return the terminal new window
3561 public final TTerminalWindow
openTerminal(final int x
, final int y
,
3562 final int flags
, final String commandLine
, final boolean closeOnExit
) {
3564 return new TTerminalWindow(this, x
, y
, flags
, commandLine
.split("\\s+"),
3569 * Convenience function to spawn an file open box.
3571 * @param path path of selected file
3572 * @return the result of the new file open box
3573 * @throws IOException if java.io operation throws
3575 public final String
fileOpenBox(final String path
) throws IOException
{
3577 TFileOpenBox box
= new TFileOpenBox(this, path
, TFileOpenBox
.Type
.OPEN
);
3578 return box
.getFilename();
3582 * Convenience function to spawn an file open box.
3584 * @param path path of selected file
3585 * @param type one of the Type constants
3586 * @return the result of the new file open box
3587 * @throws IOException if java.io operation throws
3589 public final String
fileOpenBox(final String path
,
3590 final TFileOpenBox
.Type type
) throws IOException
{
3592 TFileOpenBox box
= new TFileOpenBox(this, path
, type
);
3593 return box
.getFilename();
3597 * Convenience function to spawn a file open box.
3599 * @param path path of selected file
3600 * @param type one of the Type constants
3601 * @param filter a string that files must match to be displayed
3602 * @return the result of the new file open box
3603 * @throws IOException of a java.io operation throws
3605 public final String
fileOpenBox(final String path
,
3606 final TFileOpenBox
.Type type
, final String filter
) throws IOException
{
3608 ArrayList
<String
> filters
= new ArrayList
<String
>();
3609 filters
.add(filter
);
3611 TFileOpenBox box
= new TFileOpenBox(this, path
, type
, filters
);
3612 return box
.getFilename();
3616 * Convenience function to spawn a file open box.
3618 * @param path path of selected file
3619 * @param type one of the Type constants
3620 * @param filters a list of strings that files must match to be displayed
3621 * @return the result of the new file open box
3622 * @throws IOException of a java.io operation throws
3624 public final String
fileOpenBox(final String path
,
3625 final TFileOpenBox
.Type type
,
3626 final List
<String
> filters
) throws IOException
{
3628 TFileOpenBox box
= new TFileOpenBox(this, path
, type
, filters
);
3629 return box
.getFilename();
3633 * Convenience function to create a new window and make it active.
3634 * Window will be located at (0, 0).
3636 * @param title window title, will be centered along the top border
3637 * @param width width of window
3638 * @param height height of window
3639 * @return the new window
3641 public final TWindow
addWindow(final String title
, final int width
,
3644 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
);
3649 * Convenience function to create a new window and make it active.
3650 * Window will be located at (0, 0).
3652 * @param title window title, will be centered along the top border
3653 * @param width width of window
3654 * @param height height of window
3655 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
3656 * @return the new window
3658 public final TWindow
addWindow(final String title
,
3659 final int width
, final int height
, final int flags
) {
3661 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
, flags
);
3666 * Convenience function to create a new window and make it active.
3668 * @param title window title, will be centered along the top border
3669 * @param x column relative to parent
3670 * @param y row relative to parent
3671 * @param width width of window
3672 * @param height height of window
3673 * @return the new window
3675 public final TWindow
addWindow(final String title
,
3676 final int x
, final int y
, final int width
, final int height
) {
3678 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
);
3683 * Convenience function to create a new window and make it active.
3685 * @param title window title, will be centered along the top border
3686 * @param x column relative to parent
3687 * @param y row relative to parent
3688 * @param width width of window
3689 * @param height height of window
3690 * @param flags mask of RESIZABLE, CENTERED, or MODAL
3691 * @return the new window
3693 public final TWindow
addWindow(final String title
,
3694 final int x
, final int y
, final int width
, final int height
,
3697 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
, flags
);