2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.io
.InputStream
;
34 import java
.io
.OutputStream
;
35 import java
.io
.UnsupportedEncodingException
;
36 import java
.util
.Collections
;
37 import java
.util
.Date
;
38 import java
.util
.HashMap
;
39 import java
.util
.ArrayList
;
40 import java
.util
.LinkedList
;
41 import java
.util
.List
;
44 import jexer
.bits
.CellAttributes
;
45 import jexer
.bits
.ColorTheme
;
46 import jexer
.bits
.GraphicsChars
;
47 import jexer
.event
.TCommandEvent
;
48 import jexer
.event
.TInputEvent
;
49 import jexer
.event
.TKeypressEvent
;
50 import jexer
.event
.TMenuEvent
;
51 import jexer
.event
.TMouseEvent
;
52 import jexer
.event
.TResizeEvent
;
53 import jexer
.backend
.Backend
;
54 import jexer
.backend
.AWTBackend
;
55 import jexer
.backend
.ECMA48Backend
;
56 import jexer
.io
.Screen
;
57 import jexer
.menu
.TMenu
;
58 import jexer
.menu
.TMenuItem
;
59 import static jexer
.TCommand
.*;
60 import static jexer
.TKeypress
.*;
63 * TApplication sets up a full Text User Interface application.
65 public class TApplication
{
68 * If true, emit thread stuff to System.err.
70 private static final boolean debugThreads
= false;
73 * If true, emit events being processed to System.err.
75 private static final boolean debugEvents
= false;
78 * WidgetEventHandler is the main event consumer loop. There are at most
79 * two such threads in existence: the primary for normal case and a
80 * secondary that is used for TMessageBox, TInputBox, and similar.
82 private class WidgetEventHandler
implements Runnable
{
84 * The main application.
86 private TApplication application
;
89 * Whether or not this WidgetEventHandler is the primary or secondary
92 private boolean primary
= true;
97 * @param application the main application
98 * @param primary if true, this is the primary event handler thread
100 public WidgetEventHandler(final TApplication application
,
101 final boolean primary
) {
103 this.application
= application
;
104 this.primary
= primary
;
113 while (!application
.quit
) {
115 // Wait until application notifies me
116 while (!application
.quit
) {
118 synchronized (application
.drainEventQueue
) {
119 if (application
.drainEventQueue
.size() > 0) {
124 synchronized (this) {
126 System
.err
.printf("%s %s sleep\n", this,
127 primary ?
"primary" : "secondary");
133 System
.err
.printf("%s %s AWAKE\n", this,
134 primary ?
"primary" : "secondary");
138 && (application
.secondaryEventReceiver
== null)
140 // Secondary thread, emergency exit. If we
141 // got here then something went wrong with
142 // the handoff between yield() and
144 synchronized (application
.primaryEventHandler
) {
145 application
.primaryEventHandler
.notify();
147 application
.secondaryEventHandler
= null;
148 throw new RuntimeException(
149 "secondary exited at wrong time");
153 } catch (InterruptedException e
) {
158 // Pull all events off the queue
160 TInputEvent event
= null;
161 synchronized (application
.drainEventQueue
) {
162 if (application
.drainEventQueue
.size() == 0) {
165 event
= application
.drainEventQueue
.remove(0);
167 // Wait for drawAll() or doIdle() to be done, then handle
169 boolean oldLock
= lockHandleEvent();
170 assert (oldLock
== false);
171 application
.repaint
= true;
173 primaryHandleEvent(event
);
175 secondaryHandleEvent(event
);
178 && (application
.secondaryEventReceiver
== null)
180 // Secondary thread, time to exit.
182 // DO NOT UNLOCK. Primary thread just came back from
183 // primaryHandleEvent() and will unlock in the else
184 // block below. Just wake it up.
185 synchronized (application
.primaryEventHandler
) {
186 application
.primaryEventHandler
.notify();
188 // Now eliminate my reference so that
189 // wakeEventHandler() resumes working on the primary.
190 application
.secondaryEventHandler
= null;
195 // Unlock. Either I am primary thread, or I am
196 // secondary thread and still running.
197 oldLock
= unlockHandleEvent();
198 assert (oldLock
== true);
202 // I have done some work of some kind. Tell the main run()
203 // loop to wake up now.
204 synchronized (application
) {
205 application
.notify();
208 } // while (true) (main runnable loop)
213 * The primary event handler thread.
215 private volatile WidgetEventHandler primaryEventHandler
;
218 * The secondary event handler thread.
220 private volatile WidgetEventHandler secondaryEventHandler
;
223 * The widget receiving events from the secondary event handler thread.
225 private volatile TWidget secondaryEventReceiver
;
228 * Spinlock for the primary and secondary event handlers.
229 * WidgetEventHandler.run() is responsible for setting this value.
231 private volatile boolean insideHandleEvent
= false;
234 * Wake the sleeping active event handler.
236 private void wakeEventHandler() {
237 if (secondaryEventHandler
!= null) {
238 synchronized (secondaryEventHandler
) {
239 secondaryEventHandler
.notify();
242 assert (primaryEventHandler
!= null);
243 synchronized (primaryEventHandler
) {
244 primaryEventHandler
.notify();
250 * Set the insideHandleEvent flag to true. lockoutEventHandlers() will
251 * spin indefinitely until unlockHandleEvent() is called.
253 * @return the old value of insideHandleEvent
255 private boolean lockHandleEvent() {
257 System
.err
.printf(" >> lockHandleEvent(): oldValue %s",
260 boolean oldValue
= true;
262 synchronized (this) {
263 // Wait for TApplication.run() to finish using the global state
264 // before allowing further event processing.
265 while (lockoutHandleEvent
== true) {}
267 oldValue
= insideHandleEvent
;
268 insideHandleEvent
= true;
272 System
.err
.printf(" ***\n");
278 * Set the insideHandleEvent flag to false. lockoutEventHandlers() will
279 * spin indefinitely until unlockHandleEvent() is called.
281 * @return the old value of insideHandleEvent
283 private boolean unlockHandleEvent() {
285 System
.err
.printf(" << unlockHandleEvent(): oldValue %s\n",
288 synchronized (this) {
289 boolean oldValue
= insideHandleEvent
;
290 insideHandleEvent
= false;
296 * Spinlock for the primary and secondary event handlers. When true, the
297 * event handlers will spinlock wait before calling handleEvent().
299 private volatile boolean lockoutHandleEvent
= false;
302 * TApplication.run() needs to be able rely on the global data structures
303 * being intact when calling doIdle() and drawAll(). Tell the event
304 * handlers to wait for an unlock before handling their events.
306 private void stopEventHandlers() {
308 System
.err
.printf(">> stopEventHandlers()");
311 lockoutHandleEvent
= true;
312 // Wait for the last event to finish processing before returning
313 // control to TApplication.run().
314 while (insideHandleEvent
== true) {}
317 System
.err
.printf(" XXX\n");
322 * TApplication.run() needs to be able rely on the global data structures
323 * being intact when calling doIdle() and drawAll(). Tell the event
324 * handlers that it is now OK to handle their events.
326 private void startEventHandlers() {
328 System
.err
.printf("<< startEventHandlers()\n");
330 lockoutHandleEvent
= false;
334 * Access to the physical screen, keyboard, and mouse.
336 private Backend backend
;
343 public final Screen
getScreen() {
344 return backend
.getScreen();
348 * Actual mouse coordinate X.
353 * Actual mouse coordinate Y.
358 * Old version of mouse coordinate X.
360 private int oldMouseX
;
363 * Old version mouse coordinate Y.
365 private int oldMouseY
;
368 * Event queue that is filled by run().
370 private List
<TInputEvent
> fillEventQueue
;
373 * Event queue that will be drained by either primary or secondary
376 private List
<TInputEvent
> drainEventQueue
;
379 * Top-level menus in this application.
381 private List
<TMenu
> menus
;
384 * Stack of activated sub-menus in this application.
386 private List
<TMenu
> subMenus
;
389 * The currently acive menu.
391 private TMenu activeMenu
= null;
394 * Active keyboard accelerators.
396 private Map
<TKeypress
, TMenuItem
> accelerators
;
399 * Windows and widgets pull colors from this ColorTheme.
401 private ColorTheme theme
;
404 * Get the color theme.
408 public final ColorTheme
getTheme() {
413 * The top-level windows (but not menus).
415 private List
<TWindow
> windows
;
418 * Timers that are being ticked.
420 private List
<TTimer
> timers
;
423 * When true, exit the application.
425 private volatile boolean quit
= false;
428 * When true, repaint the entire screen.
430 private volatile boolean repaint
= true;
433 * Y coordinate of the top edge of the desktop. For now this is a
434 * constant. Someday it would be nice to have a multi-line menu or
437 private static final int desktopTop
= 1;
440 * Get Y coordinate of the top edge of the desktop.
442 * @return Y coordinate of the top edge of the desktop
444 public final int getDesktopTop() {
449 * Y coordinate of the bottom edge of the desktop.
451 private int desktopBottom
;
454 * Get Y coordinate of the bottom edge of the desktop.
456 * @return Y coordinate of the bottom edge of the desktop
458 public final int getDesktopBottom() {
459 return desktopBottom
;
463 * Public constructor.
465 * @param input an InputStream connected to the remote user, or null for
466 * System.in. If System.in is used, then on non-Windows systems it will
467 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
468 * mode. input is always converted to a Reader with UTF-8 encoding.
469 * @param output an OutputStream connected to the remote user, or null
470 * for System.out. output is always converted to a Writer with UTF-8
472 * @throws UnsupportedEncodingException if an exception is thrown when
473 * creating the InputStreamReader
475 public TApplication(final InputStream input
,
476 final OutputStream output
) throws UnsupportedEncodingException
{
478 // AWT is the default backend on Windows unless explicitly overridden
480 boolean useAWT
= false;
481 if (System
.getProperty("os.name").startsWith("Windows")) {
484 if (System
.getProperty("jexer.AWT") != null) {
485 if (System
.getProperty("jexer.AWT", "false").equals("true")) {
494 backend
= new AWTBackend(this);
496 backend
= new ECMA48Backend(this, input
, output
);
498 theme
= new ColorTheme();
499 desktopBottom
= getScreen().getHeight() - 1;
500 fillEventQueue
= new ArrayList
<TInputEvent
>();
501 drainEventQueue
= new ArrayList
<TInputEvent
>();
502 windows
= new LinkedList
<TWindow
>();
503 menus
= new LinkedList
<TMenu
>();
504 subMenus
= new LinkedList
<TMenu
>();
505 timers
= new LinkedList
<TTimer
>();
506 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
508 // Setup the main consumer thread
509 primaryEventHandler
= new WidgetEventHandler(this, true);
510 (new Thread(primaryEventHandler
)).start();
514 * Invert the cell color at a position. This is used to track the mouse.
516 * @param x column position
517 * @param y row position
519 private void invertCell(final int x
, final int y
) {
520 synchronized (getScreen()) {
521 CellAttributes attr
= getScreen().getAttrXY(x
, y
);
522 attr
.setForeColor(attr
.getForeColor().invert());
523 attr
.setBackColor(attr
.getBackColor().invert());
524 getScreen().putAttrXY(x
, y
, attr
, false);
531 public final void drawAll() {
533 System
.err
.printf("drawAll() enter\n");
537 synchronized (getScreen()) {
538 if ((oldMouseX
!= mouseX
) || (oldMouseY
!= mouseY
)) {
539 // The only thing that has happened is the mouse moved.
540 // Clear the old position and draw the new position.
541 invertCell(oldMouseX
, oldMouseY
);
542 invertCell(mouseX
, mouseY
);
547 if (getScreen().isDirty()) {
548 backend
.flushScreen();
554 System
.err
.printf("drawAll() REDRAW\n");
557 // If true, the cursor is not visible
558 boolean cursor
= false;
560 // Start with a clean screen
563 // Draw the background
564 CellAttributes background
= theme
.getColor("tapplication.background");
565 getScreen().putAll(GraphicsChars
.HATCH
, background
);
567 // Draw each window in reverse Z order
568 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
569 Collections
.sort(sorted
);
570 Collections
.reverse(sorted
);
571 for (TWindow window
: sorted
) {
572 window
.drawChildren();
575 // Draw the blank menubar line - reset the screen clipping first so
576 // it won't trim it out.
577 getScreen().resetClipping();
578 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
579 theme
.getColor("tmenu"));
580 // Now draw the menus.
582 for (TMenu menu
: menus
) {
583 CellAttributes menuColor
;
584 CellAttributes menuMnemonicColor
;
585 if (menu
.getActive()) {
586 menuColor
= theme
.getColor("tmenu.highlighted");
587 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
589 menuColor
= theme
.getColor("tmenu");
590 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
592 // Draw the menu title
593 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
595 getScreen().putStrXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
596 // Draw the highlight character
597 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
598 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
600 if (menu
.getActive()) {
602 // Reset the screen clipping so we can draw the next title.
603 getScreen().resetClipping();
605 x
+= menu
.getTitle().length() + 2;
608 for (TMenu menu
: subMenus
) {
609 // Reset the screen clipping so we can draw the next sub-menu.
610 getScreen().resetClipping();
614 // Draw the mouse pointer
615 invertCell(mouseX
, mouseY
);
617 // Place the cursor if it is visible
618 TWidget activeWidget
= null;
619 if (sorted
.size() > 0) {
620 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
621 if (activeWidget
.visibleCursor()) {
622 getScreen().putCursor(true, activeWidget
.getCursorAbsoluteX(),
623 activeWidget
.getCursorAbsoluteY());
630 getScreen().hideCursor();
633 // Flush the screen contents
634 backend
.flushScreen();
640 * Run this application until it exits.
642 public final void run() {
644 // Timeout is in milliseconds, so default timeout after 1 second
648 // If I've got no updates to render, wait for something from the
649 // backend or a timer.
651 && ((mouseX
== oldMouseX
) && (mouseY
== oldMouseY
))
653 // Never sleep longer than 100 millis, to get windows with
654 // background tasks an opportunity to update the display.
655 timeout
= getSleepTime(100);
657 // See if there are any definitely events waiting to be
658 // processed. If so, do not wait -- either there is I/O
659 // coming in or the primary/secondary threads are still
661 synchronized (drainEventQueue
) {
662 if (drainEventQueue
.size() > 0) {
666 synchronized (fillEventQueue
) {
667 if (fillEventQueue
.size() > 0) {
674 // As of now, I've got nothing to do: no I/O, nothing from
675 // the consumer threads, no timers that need to run ASAP. So
676 // wait until either the backend or the consumer threads have
679 synchronized (this) {
682 } catch (InterruptedException e
) {
683 // I'm awake and don't care why, let's see what's going
689 // Pull any pending I/O events
690 backend
.getEvents(fillEventQueue
);
692 // Dispatch each event to the appropriate handler, one at a time.
694 TInputEvent event
= null;
695 synchronized (fillEventQueue
) {
696 if (fillEventQueue
.size() == 0) {
699 event
= fillEventQueue
.remove(0);
701 metaHandleEvent(event
);
704 // Wake a consumer thread if we have any pending events.
705 synchronized (drainEventQueue
) {
706 if (drainEventQueue
.size() > 0) {
711 // Prevent stepping on the primary or secondary event handler.
714 // Process timers and call doIdle()'s
718 synchronized (getScreen()) {
722 // Let the event handlers run again.
723 startEventHandlers();
727 // Shutdown the event consumer threads
728 if (secondaryEventHandler
!= null) {
729 synchronized (secondaryEventHandler
) {
730 secondaryEventHandler
.notify();
733 if (primaryEventHandler
!= null) {
734 synchronized (primaryEventHandler
) {
735 primaryEventHandler
.notify();
739 // Shutdown the user I/O thread(s)
742 // Close all the windows. This gives them an opportunity to release
749 * Peek at certain application-level events, add to eventQueue, and wake
750 * up the consuming Thread.
752 * @param event the input event to consume
754 private void metaHandleEvent(final TInputEvent event
) {
757 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
758 event
)); System
.err
.flush();
762 // Do no more processing if the application is already trying
767 // Special application-wide events -------------------------------
770 if (event
instanceof TCommandEvent
) {
771 TCommandEvent command
= (TCommandEvent
) event
;
772 if (command
.getCmd().equals(cmAbort
)) {
779 if (event
instanceof TResizeEvent
) {
780 TResizeEvent resize
= (TResizeEvent
) event
;
781 synchronized (getScreen()) {
782 getScreen().setDimensions(resize
.getWidth(),
784 desktopBottom
= getScreen().getHeight() - 1;
793 // Peek at the mouse position
794 if (event
instanceof TMouseEvent
) {
795 TMouseEvent mouse
= (TMouseEvent
) event
;
796 synchronized (getScreen()) {
797 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
800 mouseX
= mouse
.getX();
801 mouseY
= mouse
.getY();
806 // Put into the main queue
807 synchronized (drainEventQueue
) {
808 drainEventQueue
.add(event
);
813 * Dispatch one event to the appropriate widget or application-level
814 * event handler. This is the primary event handler, it has the normal
815 * application-wide event handling.
817 * @param event the input event to consume
818 * @see #secondaryHandleEvent(TInputEvent event)
820 private void primaryHandleEvent(final TInputEvent event
) {
823 System
.err
.printf("Handle event: %s\n", event
);
826 // Special application-wide events -----------------------------------
828 // Peek at the mouse position
829 if (event
instanceof TMouseEvent
) {
830 // See if we need to switch focus to another window or the menu
831 checkSwitchFocus((TMouseEvent
) event
);
834 // Handle menu events
835 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
836 TMenu menu
= activeMenu
;
838 if (event
instanceof TMouseEvent
) {
839 TMouseEvent mouse
= (TMouseEvent
) event
;
841 while (subMenus
.size() > 0) {
842 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
843 if (subMenu
.mouseWouldHit(mouse
)) {
846 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
847 && (!mouse
.getMouse1())
848 && (!mouse
.getMouse2())
849 && (!mouse
.getMouse3())
850 && (!mouse
.getMouseWheelUp())
851 && (!mouse
.getMouseWheelDown())
855 // We navigated away from a sub-menu, so close it
859 // Convert the mouse relative x/y to menu coordinates
860 assert (mouse
.getX() == mouse
.getAbsoluteX());
861 assert (mouse
.getY() == mouse
.getAbsoluteY());
862 if (subMenus
.size() > 0) {
863 menu
= subMenus
.get(subMenus
.size() - 1);
865 mouse
.setX(mouse
.getX() - menu
.getX());
866 mouse
.setY(mouse
.getY() - menu
.getY());
868 menu
.handleEvent(event
);
872 if (event
instanceof TKeypressEvent
) {
873 TKeypressEvent keypress
= (TKeypressEvent
) event
;
875 // See if this key matches an accelerator, and if so dispatch the
877 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
878 TMenuItem item
= null;
879 synchronized (accelerators
) {
880 item
= accelerators
.get(keypressLowercase
);
883 if (item
.getEnabled()) {
884 // Let the menu item dispatch
889 // Handle the keypress
890 if (onKeypress(keypress
)) {
895 if (event
instanceof TCommandEvent
) {
896 if (onCommand((TCommandEvent
) event
)) {
901 if (event
instanceof TMenuEvent
) {
902 if (onMenu((TMenuEvent
) event
)) {
907 // Dispatch events to the active window -------------------------------
908 for (TWindow window
: windows
) {
909 if (window
.getActive()) {
910 if (event
instanceof TMouseEvent
) {
911 TMouseEvent mouse
= (TMouseEvent
) event
;
912 // Convert the mouse relative x/y to window coordinates
913 assert (mouse
.getX() == mouse
.getAbsoluteX());
914 assert (mouse
.getY() == mouse
.getAbsoluteY());
915 mouse
.setX(mouse
.getX() - window
.getX());
916 mouse
.setY(mouse
.getY() - window
.getY());
919 System
.err
.printf("TApplication dispatch event: %s\n",
922 window
.handleEvent(event
);
928 * Dispatch one event to the appropriate widget or application-level
929 * event handler. This is the secondary event handler used by certain
930 * special dialogs (currently TMessageBox and TFileOpenBox).
932 * @param event the input event to consume
933 * @see #primaryHandleEvent(TInputEvent event)
935 private void secondaryHandleEvent(final TInputEvent event
) {
936 secondaryEventReceiver
.handleEvent(event
);
940 * Enable a widget to override the primary event thread.
942 * @param widget widget that will receive events
944 public final void enableSecondaryEventReceiver(final TWidget widget
) {
945 assert (secondaryEventReceiver
== null);
946 assert (secondaryEventHandler
== null);
947 assert (widget
instanceof TMessageBox
);
948 secondaryEventReceiver
= widget
;
949 secondaryEventHandler
= new WidgetEventHandler(this, false);
950 (new Thread(secondaryEventHandler
)).start();
954 * Yield to the secondary thread.
956 public final void yield() {
957 assert (secondaryEventReceiver
!= null);
958 // This is where we handoff the event handler lock from the primary
959 // to secondary thread. We unlock here, and in a future loop the
960 // secondary thread locks again. When it gives up, we have the
962 boolean oldLock
= unlockHandleEvent();
963 assert (oldLock
== true);
965 while (secondaryEventReceiver
!= null) {
966 synchronized (primaryEventHandler
) {
968 primaryEventHandler
.wait();
969 } catch (InterruptedException e
) {
977 * Do stuff when there is no user input.
979 private void doIdle() {
981 System
.err
.printf("doIdle()\n");
984 // Now run any timers that have timed out
985 Date now
= new Date();
986 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
987 for (TTimer timer
: timers
) {
988 if (timer
.getNextTick().getTime() <= now
.getTime()) {
990 if (timer
.recurring
) {
991 keepTimers
.add(timer
);
994 keepTimers
.add(timer
);
1000 for (TWindow window
: windows
) {
1006 * Get the amount of time I can sleep before missing a Timer tick.
1008 * @param timeout = initial (maximum) timeout in millis
1009 * @return number of milliseconds between now and the next timer event
1011 private long getSleepTime(final long timeout
) {
1012 Date now
= new Date();
1013 long nowTime
= now
.getTime();
1014 long sleepTime
= timeout
;
1015 for (TTimer timer
: timers
) {
1016 long nextTickTime
= timer
.getNextTick().getTime();
1017 if (nextTickTime
< nowTime
) {
1021 long timeDifference
= nextTickTime
- nowTime
;
1022 if (timeDifference
< sleepTime
) {
1023 sleepTime
= timeDifference
;
1026 assert (sleepTime
>= 0);
1027 assert (sleepTime
<= timeout
);
1032 * Close window. Note that the window's destructor is NOT called by this
1033 * method, instead the GC is assumed to do the cleanup.
1035 * @param window the window to remove
1037 public final void closeWindow(final TWindow window
) {
1038 synchronized (windows
) {
1039 int z
= window
.getZ();
1041 Collections
.sort(windows
);
1043 TWindow activeWindow
= null;
1044 for (TWindow w
: windows
) {
1046 w
.setZ(w
.getZ() - 1);
1047 if (w
.getZ() == 0) {
1049 assert (activeWindow
== null);
1058 // Perform window cleanup
1061 // Check if we are closing a TMessageBox or similar
1062 if (secondaryEventReceiver
!= null) {
1063 assert (secondaryEventHandler
!= null);
1065 // Do not send events to the secondaryEventReceiver anymore, the
1066 // window is closed.
1067 secondaryEventReceiver
= null;
1069 // Wake the secondary thread, it will wake the primary as it
1071 synchronized (secondaryEventHandler
) {
1072 secondaryEventHandler
.notify();
1078 * Switch to the next window.
1080 * @param forward if true, then switch to the next window in the list,
1081 * otherwise switch to the previous window in the list
1083 public final void switchWindow(final boolean forward
) {
1084 // Only switch if there are multiple windows
1085 if (windows
.size() < 2) {
1089 synchronized (windows
) {
1091 // Swap z/active between active window and the next in the list
1092 int activeWindowI
= -1;
1093 for (int i
= 0; i
< windows
.size(); i
++) {
1094 if (windows
.get(i
).getActive()) {
1099 assert (activeWindowI
>= 0);
1101 // Do not switch if a window is modal
1102 if (windows
.get(activeWindowI
).isModal()) {
1108 nextWindowI
= (activeWindowI
+ 1) % windows
.size();
1110 if (activeWindowI
== 0) {
1111 nextWindowI
= windows
.size() - 1;
1113 nextWindowI
= activeWindowI
- 1;
1116 windows
.get(activeWindowI
).setActive(false);
1117 windows
.get(activeWindowI
).setZ(windows
.get(nextWindowI
).getZ());
1118 windows
.get(nextWindowI
).setZ(0);
1119 windows
.get(nextWindowI
).setActive(true);
1121 } // synchronized (windows)
1126 * Add a window to my window list and make it active.
1128 * @param window new window to add
1130 public final void addWindow(final TWindow window
) {
1131 synchronized (windows
) {
1132 // Do not allow a modal window to spawn a non-modal window
1133 if ((windows
.size() > 0) && (windows
.get(0).isModal())) {
1134 assert (window
.isModal());
1136 for (TWindow w
: windows
) {
1138 w
.setZ(w
.getZ() + 1);
1140 windows
.add(window
);
1141 window
.setActive(true);
1147 * Check if there is a system-modal window on top.
1149 * @return true if the active window is modal
1151 private boolean modalWindowActive() {
1152 if (windows
.size() == 0) {
1155 return windows
.get(windows
.size() - 1).isModal();
1159 * Check if a mouse event would hit either the active menu or any open
1162 * @param mouse mouse event
1163 * @return true if the mouse would hit the active menu or an open
1166 private boolean mouseOnMenu(final TMouseEvent mouse
) {
1167 assert (activeMenu
!= null);
1168 List
<TMenu
> menus
= new LinkedList
<TMenu
>(subMenus
);
1169 Collections
.reverse(menus
);
1170 for (TMenu menu
: menus
) {
1171 if (menu
.mouseWouldHit(mouse
)) {
1175 return activeMenu
.mouseWouldHit(mouse
);
1179 * See if we need to switch window or activate the menu based on
1182 * @param mouse mouse event
1184 private void checkSwitchFocus(final TMouseEvent mouse
) {
1186 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1187 && (activeMenu
!= null)
1188 && (mouse
.getAbsoluteY() != 0)
1189 && (!mouseOnMenu(mouse
))
1191 // They clicked outside the active menu, turn it off
1192 activeMenu
.setActive(false);
1194 for (TMenu menu
: subMenus
) {
1195 menu
.setActive(false);
1201 // See if they hit the menu bar
1202 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1203 && (mouse
.getMouse1())
1204 && (!modalWindowActive())
1205 && (mouse
.getAbsoluteY() == 0)
1208 for (TMenu menu
: subMenus
) {
1209 menu
.setActive(false);
1213 // They selected the menu, go activate it
1214 for (TMenu menu
: menus
) {
1215 if ((mouse
.getAbsoluteX() >= menu
.getX())
1216 && (mouse
.getAbsoluteX() < menu
.getX()
1217 + menu
.getTitle().length() + 2)
1219 menu
.setActive(true);
1222 menu
.setActive(false);
1228 // See if they hit the menu bar
1229 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1230 && (mouse
.getMouse1())
1231 && (activeMenu
!= null)
1232 && (mouse
.getAbsoluteY() == 0)
1235 TMenu oldMenu
= activeMenu
;
1236 for (TMenu menu
: subMenus
) {
1237 menu
.setActive(false);
1241 // See if we should switch menus
1242 for (TMenu menu
: menus
) {
1243 if ((mouse
.getAbsoluteX() >= menu
.getX())
1244 && (mouse
.getAbsoluteX() < menu
.getX()
1245 + menu
.getTitle().length() + 2)
1247 menu
.setActive(true);
1251 if (oldMenu
!= activeMenu
) {
1252 // They switched menus
1253 oldMenu
.setActive(false);
1258 // Only switch if there are multiple windows
1259 if (windows
.size() < 2) {
1263 // Switch on the upclick
1264 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
) {
1268 synchronized (windows
) {
1269 Collections
.sort(windows
);
1270 if (windows
.get(0).isModal()) {
1271 // Modal windows don't switch
1275 for (TWindow window
: windows
) {
1276 assert (!window
.isModal());
1277 if (window
.mouseWouldHit(mouse
)) {
1278 if (window
== windows
.get(0)) {
1279 // Clicked on the same window, nothing to do
1283 // We will be switching to another window
1284 assert (windows
.get(0).getActive());
1285 assert (!window
.getActive());
1286 windows
.get(0).setActive(false);
1287 windows
.get(0).setZ(window
.getZ());
1289 window
.setActive(true);
1295 // Clicked on the background, nothing to do
1300 * Turn off the menu.
1302 public final void closeMenu() {
1303 if (activeMenu
!= null) {
1304 activeMenu
.setActive(false);
1306 for (TMenu menu
: subMenus
) {
1307 menu
.setActive(false);
1314 * Turn off a sub-menu.
1316 public final void closeSubMenu() {
1317 assert (activeMenu
!= null);
1318 TMenu item
= subMenus
.get(subMenus
.size() - 1);
1319 assert (item
!= null);
1320 item
.setActive(false);
1321 subMenus
.remove(subMenus
.size() - 1);
1325 * Switch to the next menu.
1327 * @param forward if true, then switch to the next menu in the list,
1328 * otherwise switch to the previous menu in the list
1330 public final void switchMenu(final boolean forward
) {
1331 assert (activeMenu
!= null);
1333 for (TMenu menu
: subMenus
) {
1334 menu
.setActive(false);
1338 for (int i
= 0; i
< menus
.size(); i
++) {
1339 if (activeMenu
== menus
.get(i
)) {
1341 if (i
< menus
.size() - 1) {
1349 activeMenu
.setActive(false);
1350 activeMenu
= menus
.get(i
);
1351 activeMenu
.setActive(true);
1358 * Method that TApplication subclasses can override to handle menu or
1359 * posted command events.
1361 * @param command command event
1362 * @return if true, this event was consumed
1364 protected boolean onCommand(final TCommandEvent command
) {
1365 // Default: handle cmExit
1366 if (command
.equals(cmExit
)) {
1367 if (messageBox("Confirmation", "Exit application?",
1368 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
1374 if (command
.equals(cmShell
)) {
1375 openTerminal(0, 0, TWindow
.RESIZABLE
);
1379 if (command
.equals(cmTile
)) {
1383 if (command
.equals(cmCascade
)) {
1387 if (command
.equals(cmCloseAll
)) {
1396 * Method that TApplication subclasses can override to handle menu
1399 * @param menu menu event
1400 * @return if true, this event was consumed
1402 protected boolean onMenu(final TMenuEvent menu
) {
1404 // Default: handle MID_EXIT
1405 if (menu
.getId() == TMenu
.MID_EXIT
) {
1406 if (messageBox("Confirmation", "Exit application?",
1407 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
1413 if (menu
.getId() == TMenu
.MID_SHELL
) {
1414 openTerminal(0, 0, TWindow
.RESIZABLE
);
1418 if (menu
.getId() == TMenu
.MID_TILE
) {
1422 if (menu
.getId() == TMenu
.MID_CASCADE
) {
1426 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
1434 * Method that TApplication subclasses can override to handle keystrokes.
1436 * @param keypress keystroke event
1437 * @return if true, this event was consumed
1439 protected boolean onKeypress(final TKeypressEvent keypress
) {
1440 // Default: only menu shortcuts
1442 // Process Alt-F, Alt-E, etc. menu shortcut keys
1443 if (!keypress
.getKey().getIsKey()
1444 && keypress
.getKey().getAlt()
1445 && !keypress
.getKey().getCtrl()
1446 && (activeMenu
== null)
1449 assert (subMenus
.size() == 0);
1451 for (TMenu menu
: menus
) {
1452 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
1453 == Character
.toLowerCase(keypress
.getKey().getCh())
1456 menu
.setActive(true);
1466 * Add a keyboard accelerator to the global hash.
1468 * @param item menu item this accelerator relates to
1469 * @param keypress keypress that will dispatch a TMenuEvent
1471 public final void addAccelerator(final TMenuItem item
,
1472 final TKeypress keypress
) {
1474 synchronized (accelerators
) {
1475 assert (accelerators
.get(keypress
) == null);
1476 accelerators
.put(keypress
, item
);
1481 * Recompute menu x positions based on their title length.
1483 public final void recomputeMenuX() {
1485 for (TMenu menu
: menus
) {
1487 x
+= menu
.getTitle().length() + 2;
1492 * Post an event to process and turn off the menu.
1494 * @param event new event to add to the queue
1496 public final void addMenuEvent(final TInputEvent event
) {
1497 synchronized (fillEventQueue
) {
1498 fillEventQueue
.add(event
);
1504 * Add a sub-menu to the list of open sub-menus.
1506 * @param menu sub-menu
1508 public final void addSubMenu(final TMenu menu
) {
1513 * Convenience function to add a top-level menu.
1515 * @param title menu title
1516 * @return the new menu
1518 public final TMenu
addMenu(final String title
) {
1521 TMenu menu
= new TMenu(this, x
, y
, title
);
1528 * Convenience function to add a default "File" menu.
1530 * @return the new menu
1532 public final TMenu
addFileMenu() {
1533 TMenu fileMenu
= addMenu("&File");
1534 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
1535 fileMenu
.addSeparator();
1536 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
1537 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
1542 * Convenience function to add a default "Edit" menu.
1544 * @return the new menu
1546 public final TMenu
addEditMenu() {
1547 TMenu editMenu
= addMenu("&Edit");
1548 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
1549 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
1550 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
1551 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
1556 * Convenience function to add a default "Window" menu.
1558 * @return the new menu
1560 public final TMenu
addWindowMenu() {
1561 TMenu windowMenu
= addMenu("&Window");
1562 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
1563 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
1564 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
1565 windowMenu
.addSeparator();
1566 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
1567 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
1568 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
1569 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
1570 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
1575 * Close all open windows.
1577 private void closeAllWindows() {
1578 // Don't do anything if we are in the menu
1579 if (activeMenu
!= null) {
1583 synchronized (windows
) {
1584 for (TWindow window
: windows
) {
1585 closeWindow(window
);
1591 * Re-layout the open windows as non-overlapping tiles. This produces
1592 * almost the same results as Turbo Pascal 7.0's IDE.
1594 private void tileWindows() {
1595 synchronized (windows
) {
1596 // Don't do anything if we are in the menu
1597 if (activeMenu
!= null) {
1600 int z
= windows
.size();
1606 a
= (int)(Math
.sqrt(z
));
1610 if (((a
* b
) + c
) == z
) {
1618 int newWidth
= (getScreen().getWidth() / a
);
1619 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
1620 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
1622 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1623 Collections
.sort(sorted
);
1624 Collections
.reverse(sorted
);
1625 for (int i
= 0; i
< sorted
.size(); i
++) {
1626 int logicalX
= i
/ b
;
1627 int logicalY
= i
% b
;
1628 if (i
>= ((a
- 1) * b
)) {
1630 logicalY
= i
- ((a
- 1) * b
);
1633 TWindow w
= sorted
.get(i
);
1634 w
.setX(logicalX
* newWidth
);
1635 w
.setWidth(newWidth
);
1636 if (i
>= ((a
- 1) * b
)) {
1637 w
.setY((logicalY
* newHeight2
) + 1);
1638 w
.setHeight(newHeight2
);
1640 w
.setY((logicalY
* newHeight1
) + 1);
1641 w
.setHeight(newHeight1
);
1648 * Re-layout the open windows as overlapping cascaded windows.
1650 private void cascadeWindows() {
1651 synchronized (windows
) {
1652 // Don't do anything if we are in the menu
1653 if (activeMenu
!= null) {
1658 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1659 Collections
.sort(sorted
);
1660 Collections
.reverse(sorted
);
1661 for (TWindow window
: sorted
) {
1666 if (x
> getScreen().getWidth()) {
1669 if (y
>= getScreen().getHeight()) {
1677 * Convenience function to add a timer.
1679 * @param duration number of milliseconds to wait between ticks
1680 * @param recurring if true, re-schedule this timer after every tick
1681 * @param action function to call when button is pressed
1684 public final TTimer
addTimer(final long duration
, final boolean recurring
,
1685 final TAction action
) {
1687 TTimer timer
= new TTimer(duration
, recurring
, action
);
1688 synchronized (timers
) {
1695 * Convenience function to remove a timer.
1697 * @param timer timer to remove
1699 public final void removeTimer(final TTimer timer
) {
1700 synchronized (timers
) {
1701 timers
.remove(timer
);
1706 * Convenience function to spawn a message box.
1708 * @param title window title, will be centered along the top border
1709 * @param caption message to display. Use embedded newlines to get a
1711 * @return the new message box
1713 public final TMessageBox
messageBox(final String title
,
1714 final String caption
) {
1716 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
1720 * Convenience function to spawn a message box.
1722 * @param title window title, will be centered along the top border
1723 * @param caption message to display. Use embedded newlines to get a
1725 * @param type one of the TMessageBox.Type constants. Default is
1727 * @return the new message box
1729 public final TMessageBox
messageBox(final String title
,
1730 final String caption
, final TMessageBox
.Type type
) {
1732 return new TMessageBox(this, title
, caption
, type
);
1736 * Convenience function to spawn an input box.
1738 * @param title window title, will be centered along the top border
1739 * @param caption message to display. Use embedded newlines to get a
1741 * @return the new input box
1743 public final TInputBox
inputBox(final String title
, final String caption
) {
1745 return new TInputBox(this, title
, caption
);
1749 * Convenience function to spawn an input box.
1751 * @param title window title, will be centered along the top border
1752 * @param caption message to display. Use embedded newlines to get a
1754 * @param text initial text to seed the field with
1755 * @return the new input box
1757 public final TInputBox
inputBox(final String title
, final String caption
,
1758 final String text
) {
1760 return new TInputBox(this, title
, caption
, text
);
1764 * Convenience function to open a terminal window.
1766 * @param x column relative to parent
1767 * @param y row relative to parent
1768 * @return the terminal new window
1770 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
1771 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
1775 * Convenience function to open a terminal window.
1777 * @param x column relative to parent
1778 * @param y row relative to parent
1779 * @param flags mask of CENTERED, MODAL, or RESIZABLE
1780 * @return the terminal new window
1782 public final TTerminalWindow
openTerminal(final int x
, final int y
,
1785 return new TTerminalWindow(this, x
, y
, flags
);