2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.io
.InputStream
;
32 import java
.io
.IOException
;
33 import java
.io
.OutputStream
;
34 import java
.io
.PrintWriter
;
35 import java
.io
.Reader
;
36 import java
.io
.UnsupportedEncodingException
;
37 import java
.util
.Collections
;
38 import java
.util
.Date
;
39 import java
.util
.HashMap
;
40 import java
.util
.ArrayList
;
41 import java
.util
.LinkedList
;
42 import java
.util
.List
;
45 import jexer
.bits
.CellAttributes
;
46 import jexer
.bits
.ColorTheme
;
47 import jexer
.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
.Screen
;
55 import jexer
.backend
.MultiBackend
;
56 import jexer
.backend
.SwingBackend
;
57 import jexer
.backend
.ECMA48Backend
;
58 import jexer
.backend
.TWindowBackend
;
59 import jexer
.menu
.TMenu
;
60 import jexer
.menu
.TMenuItem
;
61 import static jexer
.TCommand
.*;
62 import static jexer
.TKeypress
.*;
65 * TApplication is the main driver class for a full Text User Interface
66 * application. It manages windows, provides a menu bar and status bar, and
67 * processes events received from the user.
69 public class TApplication
implements Runnable
{
71 // ------------------------------------------------------------------------
72 // Public constants -------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * If true, emit thread stuff to System.err.
78 private static final boolean debugThreads
= false;
81 * If true, emit events being processed to System.err.
83 private static final boolean debugEvents
= false;
86 * If true, do "smart placement" on new windows that are not specified to
89 private static final boolean smartWindowPlacement
= true;
92 * Two backend types are available.
94 public static enum BackendType
{
101 * An ECMA48 / ANSI X3.64 / XTERM style terminal.
106 * Synonym for ECMA48.
111 // ------------------------------------------------------------------------
112 // Primary/secondary event handlers ---------------------------------------
113 // ------------------------------------------------------------------------
116 * WidgetEventHandler is the main event consumer loop. There are at most
117 * two such threads in existence: the primary for normal case and a
118 * secondary that is used for TMessageBox, TInputBox, and similar.
120 private class WidgetEventHandler
implements Runnable
{
122 * The main application.
124 private TApplication application
;
127 * Whether or not this WidgetEventHandler is the primary or secondary
130 private boolean primary
= true;
133 * Public constructor.
135 * @param application the main application
136 * @param primary if true, this is the primary event handler thread
138 public WidgetEventHandler(final TApplication application
,
139 final boolean primary
) {
141 this.application
= application
;
142 this.primary
= primary
;
149 boolean first
= true;
152 while (!application
.quit
) {
154 // Wait until application notifies me
155 while (!application
.quit
) {
157 synchronized (application
.drainEventQueue
) {
158 if (application
.drainEventQueue
.size() > 0) {
167 timeout
= application
.getSleepTime(1000);
171 // A timer needs to fire, break out.
176 System
.err
.printf("%d %s %s sleep %d millis\n",
177 System
.currentTimeMillis(), this,
178 primary ?
"primary" : "secondary", timeout
);
181 synchronized (this) {
186 System
.err
.printf("%d %s %s AWAKE\n",
187 System
.currentTimeMillis(), this,
188 primary ?
"primary" : "secondary");
192 && (application
.secondaryEventReceiver
== null)
194 // Secondary thread, emergency exit. If we got
195 // here then something went wrong with the
196 // handoff between yield() and closeWindow().
197 synchronized (application
.primaryEventHandler
) {
198 application
.primaryEventHandler
.notify();
200 application
.secondaryEventHandler
= null;
201 throw new RuntimeException("secondary exited " +
205 } catch (InterruptedException e
) {
208 } // while (!application.quit)
210 // Pull all events off the queue
212 TInputEvent event
= null;
213 synchronized (application
.drainEventQueue
) {
214 if (application
.drainEventQueue
.size() == 0) {
217 event
= application
.drainEventQueue
.remove(0);
220 // We will have an event to process, so repaint the
221 // screen at the end.
222 application
.repaint
= true;
225 primaryHandleEvent(event
);
227 secondaryHandleEvent(event
);
230 && (application
.secondaryEventReceiver
== null)
232 // Secondary thread, time to exit.
234 // DO NOT UNLOCK. Primary thread just came back from
235 // primaryHandleEvent() and will unlock in the else
236 // block below. Just wake it up.
237 synchronized (application
.primaryEventHandler
) {
238 application
.primaryEventHandler
.notify();
240 // Now eliminate my reference so that
241 // wakeEventHandler() resumes working on the primary.
242 application
.secondaryEventHandler
= null;
250 // Fire timers, update screen.
252 application
.finishEventProcessing();
255 } // while (true) (main runnable loop)
260 * The primary event handler thread.
262 private volatile WidgetEventHandler primaryEventHandler
;
265 * The secondary event handler thread.
267 private volatile WidgetEventHandler secondaryEventHandler
;
270 * The widget receiving events from the secondary event handler thread.
272 private volatile TWidget secondaryEventReceiver
;
275 * Wake the sleeping active event handler.
277 private void wakeEventHandler() {
278 if (secondaryEventHandler
!= null) {
279 synchronized (secondaryEventHandler
) {
280 secondaryEventHandler
.notify();
283 assert (primaryEventHandler
!= null);
284 synchronized (primaryEventHandler
) {
285 primaryEventHandler
.notify();
290 // ------------------------------------------------------------------------
291 // TApplication attributes ------------------------------------------------
292 // ------------------------------------------------------------------------
295 * Access to the physical screen, keyboard, and mouse.
297 private Backend backend
;
302 * @return the Backend
304 public final Backend
getBackend() {
313 public final Screen
getScreen() {
314 if (backend
instanceof TWindowBackend
) {
315 // We are being rendered to a TWindow. We can't use its
316 // getScreen() method because that is how it is rendering to a
317 // hardware backend somewhere. Instead use its getOtherScreen()
319 return ((TWindowBackend
) backend
).getOtherScreen();
321 return backend
.getScreen();
326 * Actual mouse coordinate X.
331 * Actual mouse coordinate Y.
336 * Old version of mouse coordinate X.
338 private int oldMouseX
;
341 * Old version mouse coordinate Y.
343 private int oldMouseY
;
346 * Event queue that is filled by run().
348 private List
<TInputEvent
> fillEventQueue
;
351 * Event queue that will be drained by either primary or secondary
354 private List
<TInputEvent
> drainEventQueue
;
357 * Top-level menus in this application.
359 private List
<TMenu
> menus
;
362 * Stack of activated sub-menus in this application.
364 private List
<TMenu
> subMenus
;
367 * The currently active menu.
369 private TMenu activeMenu
= null;
372 * Active keyboard accelerators.
374 private Map
<TKeypress
, TMenuItem
> accelerators
;
379 private List
<TMenuItem
> menuItems
;
382 * Windows and widgets pull colors from this ColorTheme.
384 private ColorTheme theme
;
387 * Get the color theme.
391 public final ColorTheme
getTheme() {
396 * The top-level windows (but not menus).
398 private List
<TWindow
> windows
;
401 * The currently acive window.
403 private TWindow activeWindow
= null;
406 * Timers that are being ticked.
408 private List
<TTimer
> timers
;
411 * When true, exit the application.
413 private volatile boolean quit
= false;
416 * When true, repaint the entire screen.
418 private volatile boolean repaint
= true;
421 * Repaint the screen on the next update.
423 public void doRepaint() {
429 * Y coordinate of the top edge of the desktop. For now this is a
430 * constant. Someday it would be nice to have a multi-line menu or
433 private static final int desktopTop
= 1;
436 * Get Y coordinate of the top edge of the desktop.
438 * @return Y coordinate of the top edge of the desktop
440 public final int getDesktopTop() {
445 * Y coordinate of the bottom edge of the desktop.
447 private int desktopBottom
;
450 * Get Y coordinate of the bottom edge of the desktop.
452 * @return Y coordinate of the bottom edge of the desktop
454 public final int getDesktopBottom() {
455 return desktopBottom
;
459 * An optional TDesktop background window that is drawn underneath
462 private TDesktop desktop
;
465 * Set the TDesktop instance.
467 * @param desktop a TDesktop instance, or null to remove the one that is
470 public final void setDesktop(final TDesktop desktop
) {
471 if (this.desktop
!= null) {
472 this.desktop
.onClose();
474 this.desktop
= desktop
;
478 * Get the TDesktop instance.
480 * @return the desktop, or null if it is not set
482 public final TDesktop
getDesktop() {
487 * Get the current active window.
489 * @return the active window, or null if it is not set
491 public final TWindow
getActiveWindow() {
496 * Get a (shallow) copy of the window list.
498 * @return a copy of the list of windows for this application
500 public final List
<TWindow
> getAllWindows() {
501 List
<TWindow
> result
= new LinkedList
<TWindow
>();
502 result
.addAll(windows
);
507 * If true, focus follows mouse: windows automatically raised if the
508 * mouse passes over them.
510 private boolean focusFollowsMouse
= false;
513 * Get focusFollowsMouse flag.
515 * @return true if focus follows mouse: windows automatically raised if
516 * the mouse passes over them
518 public boolean getFocusFollowsMouse() {
519 return focusFollowsMouse
;
523 * Set focusFollowsMouse flag.
525 * @param focusFollowsMouse if true, focus follows mouse: windows
526 * automatically raised if the mouse passes over them
528 public void setFocusFollowsMouse(final boolean focusFollowsMouse
) {
529 this.focusFollowsMouse
= focusFollowsMouse
;
532 // ------------------------------------------------------------------------
533 // General behavior -------------------------------------------------------
534 // ------------------------------------------------------------------------
537 * Display the about dialog.
539 protected void showAboutDialog() {
540 messageBox("About", "Jexer Version " +
541 this.getClass().getPackage().getImplementationVersion(),
542 TMessageBox
.Type
.OK
);
545 // ------------------------------------------------------------------------
546 // Constructors -----------------------------------------------------------
547 // ------------------------------------------------------------------------
550 * Public constructor.
552 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
554 * @throws UnsupportedEncodingException if an exception is thrown when
555 * creating the InputStreamReader
557 public TApplication(final BackendType backendType
)
558 throws UnsupportedEncodingException
{
560 switch (backendType
) {
562 // The default SwingBackend is 80x25, 20 pt font. If you want to
563 // change that, you can pass the extra arguments to the
564 // SwingBackend constructor here. For example, if you wanted
565 // 90x30, 16 pt font:
567 // backend = new SwingBackend(this, 90, 30, 16);
568 backend
= new SwingBackend(this);
573 backend
= new ECMA48Backend(this, null, null);
576 throw new IllegalArgumentException("Invalid backend type: "
583 * Public constructor. The backend type will be BackendType.ECMA48.
585 * @param input an InputStream connected to the remote user, or null for
586 * System.in. If System.in is used, then on non-Windows systems it will
587 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
588 * mode. input is always converted to a Reader with UTF-8 encoding.
589 * @param output an OutputStream connected to the remote user, or null
590 * for System.out. output is always converted to a Writer with UTF-8
592 * @throws UnsupportedEncodingException if an exception is thrown when
593 * creating the InputStreamReader
595 public TApplication(final InputStream input
,
596 final OutputStream output
) throws UnsupportedEncodingException
{
598 backend
= new ECMA48Backend(this, input
, output
);
603 * Public constructor. The backend type will be BackendType.ECMA48.
605 * @param input the InputStream underlying 'reader'. Its available()
606 * method is used to determine if reader.read() will block or not.
607 * @param reader a Reader connected to the remote user.
608 * @param writer a PrintWriter connected to the remote user.
609 * @param setRawMode if true, set System.in into raw mode with stty.
610 * This should in general not be used. It is here solely for Demo3,
611 * which uses System.in.
612 * @throws IllegalArgumentException if input, reader, or writer are null.
614 public TApplication(final InputStream input
, final Reader reader
,
615 final PrintWriter writer
, final boolean setRawMode
) {
617 backend
= new ECMA48Backend(this, input
, reader
, writer
, setRawMode
);
622 * Public constructor. The backend type will be BackendType.ECMA48.
624 * @param input the InputStream underlying 'reader'. Its available()
625 * method is used to determine if reader.read() will block or not.
626 * @param reader a Reader connected to the remote user.
627 * @param writer a PrintWriter connected to the remote user.
628 * @throws IllegalArgumentException if input, reader, or writer are null.
630 public TApplication(final InputStream input
, final Reader reader
,
631 final PrintWriter writer
) {
633 this(input
, reader
, writer
, false);
637 * Public constructor. This hook enables use with new non-Jexer
640 * @param backend a Backend that is already ready to go.
642 public TApplication(final Backend backend
) {
643 this.backend
= backend
;
644 backend
.setListener(this);
649 * Finish construction once the backend is set.
651 private void TApplicationImpl() {
652 theme
= new ColorTheme();
653 desktopBottom
= getScreen().getHeight() - 1;
654 fillEventQueue
= new ArrayList
<TInputEvent
>();
655 drainEventQueue
= new ArrayList
<TInputEvent
>();
656 windows
= new LinkedList
<TWindow
>();
657 menus
= new LinkedList
<TMenu
>();
658 subMenus
= new LinkedList
<TMenu
>();
659 timers
= new LinkedList
<TTimer
>();
660 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
661 menuItems
= new ArrayList
<TMenuItem
>();
662 desktop
= new TDesktop(this);
664 // Special case: the Swing backend needs to have a timer to drive its
666 if ((backend
instanceof SwingBackend
)
667 || (backend
instanceof MultiBackend
)
669 // Default to 500 millis, unless a SwingBackend has its own
672 if (backend
instanceof SwingBackend
) {
673 millis
= ((SwingBackend
) backend
).getBlinkMillis();
676 addTimer(millis
, true,
679 TApplication
.this.doRepaint();
687 // ------------------------------------------------------------------------
688 // Screen refresh loop ----------------------------------------------------
689 // ------------------------------------------------------------------------
692 * Process background events, and update the screen.
694 private void finishEventProcessing() {
696 System
.err
.printf(System
.currentTimeMillis() + " " +
697 Thread
.currentThread() + " finishEventProcessing()\n");
700 // Process timers and call doIdle()'s
704 synchronized (getScreen()) {
709 System
.err
.printf(System
.currentTimeMillis() + " " +
710 Thread
.currentThread() + " finishEventProcessing() END\n");
715 * Invert the cell color at a position. This is used to track the mouse.
717 * @param x column position
718 * @param y row position
720 private void invertCell(final int x
, final int y
) {
722 System
.err
.printf("%d %s invertCell() %d %d\n",
723 System
.currentTimeMillis(), Thread
.currentThread(), x
, y
);
725 CellAttributes attr
= getScreen().getAttrXY(x
, y
);
726 attr
.setForeColor(attr
.getForeColor().invert());
727 attr
.setBackColor(attr
.getBackColor().invert());
728 getScreen().putAttrXY(x
, y
, attr
, false);
734 private void drawAll() {
736 System
.err
.printf("%d %s drawAll() enter\n",
737 System
.currentTimeMillis(), Thread
.currentThread());
742 System
.err
.printf("%d %s drawAll() !repaint\n",
743 System
.currentTimeMillis(), Thread
.currentThread());
745 synchronized (getScreen()) {
746 if ((oldMouseX
!= mouseX
) || (oldMouseY
!= mouseY
)) {
747 // The only thing that has happened is the mouse moved.
748 // Clear the old position and draw the new position.
749 invertCell(oldMouseX
, oldMouseY
);
750 invertCell(mouseX
, mouseY
);
754 if (getScreen().isDirty()) {
755 backend
.flushScreen();
762 System
.err
.printf("%d %s drawAll() REDRAW\n",
763 System
.currentTimeMillis(), Thread
.currentThread());
766 // If true, the cursor is not visible
767 boolean cursor
= false;
769 // Start with a clean screen
773 if (desktop
!= null) {
774 desktop
.drawChildren();
777 // Draw each window in reverse Z order
778 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
779 Collections
.sort(sorted
);
780 TWindow topLevel
= null;
781 if (sorted
.size() > 0) {
782 topLevel
= sorted
.get(0);
784 Collections
.reverse(sorted
);
785 for (TWindow window
: sorted
) {
786 if (window
.isShown()) {
787 window
.drawChildren();
791 // Draw the blank menubar line - reset the screen clipping first so
792 // it won't trim it out.
793 getScreen().resetClipping();
794 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
795 theme
.getColor("tmenu"));
796 // Now draw the menus.
798 for (TMenu menu
: menus
) {
799 CellAttributes menuColor
;
800 CellAttributes menuMnemonicColor
;
801 if (menu
.isActive()) {
802 menuColor
= theme
.getColor("tmenu.highlighted");
803 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
806 menuColor
= theme
.getColor("tmenu");
807 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
809 // Draw the menu title
810 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
812 getScreen().putStringXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
813 // Draw the highlight character
814 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
815 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
817 if (menu
.isActive()) {
819 // Reset the screen clipping so we can draw the next title.
820 getScreen().resetClipping();
822 x
+= menu
.getTitle().length() + 2;
825 for (TMenu menu
: subMenus
) {
826 // Reset the screen clipping so we can draw the next sub-menu.
827 getScreen().resetClipping();
831 // Draw the status bar of the top-level window
832 TStatusBar statusBar
= null;
833 if (topLevel
!= null) {
834 statusBar
= topLevel
.getStatusBar();
836 if (statusBar
!= null) {
837 getScreen().resetClipping();
838 statusBar
.setWidth(getScreen().getWidth());
839 statusBar
.setY(getScreen().getHeight() - topLevel
.getY());
842 CellAttributes barColor
= new CellAttributes();
843 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
844 getScreen().hLineXY(0, desktopBottom
, getScreen().getWidth(), ' ',
848 // Draw the mouse pointer
849 invertCell(mouseX
, mouseY
);
853 // Place the cursor if it is visible
854 TWidget activeWidget
= null;
855 if (sorted
.size() > 0) {
856 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
857 if (activeWidget
.isCursorVisible()) {
858 if ((activeWidget
.getCursorAbsoluteY() < desktopBottom
)
859 && (activeWidget
.getCursorAbsoluteY() > desktopTop
)
861 getScreen().putCursor(true,
862 activeWidget
.getCursorAbsoluteX(),
863 activeWidget
.getCursorAbsoluteY());
866 getScreen().putCursor(false,
867 activeWidget
.getCursorAbsoluteX(),
868 activeWidget
.getCursorAbsoluteY());
876 getScreen().hideCursor();
879 // Flush the screen contents
880 if (getScreen().isDirty()) {
881 backend
.flushScreen();
887 // ------------------------------------------------------------------------
888 // Main loop --------------------------------------------------------------
889 // ------------------------------------------------------------------------
892 * Force this application to exit.
896 synchronized (this) {
902 * Run this application until it exits.
905 // Start the main consumer thread
906 primaryEventHandler
= new WidgetEventHandler(this, true);
907 (new Thread(primaryEventHandler
)).start();
910 synchronized (this) {
911 boolean doWait
= false;
913 synchronized (fillEventQueue
) {
914 if (fillEventQueue
.size() == 0) {
920 // No I/O to dispatch, so wait until the backend
924 System
.err
.println(System
.currentTimeMillis() +
931 System
.err
.println(System
.currentTimeMillis() +
934 } catch (InterruptedException e
) {
935 // I'm awake and don't care why, let's see what's
936 // going on out there.
940 } // synchronized (this)
942 synchronized (fillEventQueue
) {
943 // Pull any pending I/O events
944 backend
.getEvents(fillEventQueue
);
946 // Dispatch each event to the appropriate handler, one at a
949 TInputEvent event
= null;
950 if (fillEventQueue
.size() == 0) {
953 event
= fillEventQueue
.remove(0);
954 metaHandleEvent(event
);
958 // Wake a consumer thread if we have any pending events.
959 if (drainEventQueue
.size() > 0) {
965 // Shutdown the event consumer threads
966 if (secondaryEventHandler
!= null) {
967 synchronized (secondaryEventHandler
) {
968 secondaryEventHandler
.notify();
971 if (primaryEventHandler
!= null) {
972 synchronized (primaryEventHandler
) {
973 primaryEventHandler
.notify();
977 // Shutdown the user I/O thread(s)
980 // Close all the windows. This gives them an opportunity to release
987 * Peek at certain application-level events, add to eventQueue, and wake
988 * up the consuming Thread.
990 * @param event the input event to consume
992 private void metaHandleEvent(final TInputEvent event
) {
995 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
996 event
)); System
.err
.flush();
1000 // Do no more processing if the application is already trying
1005 // Special application-wide events -------------------------------
1008 if (event
instanceof TCommandEvent
) {
1009 TCommandEvent command
= (TCommandEvent
) event
;
1010 if (command
.getCmd().equals(cmAbort
)) {
1016 synchronized (drainEventQueue
) {
1018 if (event
instanceof TResizeEvent
) {
1019 TResizeEvent resize
= (TResizeEvent
) event
;
1020 synchronized (getScreen()) {
1021 getScreen().setDimensions(resize
.getWidth(),
1022 resize
.getHeight());
1023 desktopBottom
= getScreen().getHeight() - 1;
1029 if (desktop
!= null) {
1030 desktop
.setDimensions(0, 0, resize
.getWidth(),
1031 resize
.getHeight() - 1);
1036 // Put into the main queue
1037 drainEventQueue
.add(event
);
1042 * Dispatch one event to the appropriate widget or application-level
1043 * event handler. This is the primary event handler, it has the normal
1044 * application-wide event handling.
1046 * @param event the input event to consume
1047 * @see #secondaryHandleEvent(TInputEvent event)
1049 private void primaryHandleEvent(final TInputEvent event
) {
1052 System
.err
.printf("Handle event: %s\n", event
);
1055 // Special application-wide events -----------------------------------
1057 // Peek at the mouse position
1058 if (event
instanceof TMouseEvent
) {
1059 TMouseEvent mouse
= (TMouseEvent
) event
;
1060 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1063 mouseX
= mouse
.getX();
1064 mouseY
= mouse
.getY();
1067 // See if we need to switch focus to another window or the menu
1068 checkSwitchFocus((TMouseEvent
) event
);
1071 // Handle menu events
1072 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
1073 TMenu menu
= activeMenu
;
1075 if (event
instanceof TMouseEvent
) {
1076 TMouseEvent mouse
= (TMouseEvent
) event
;
1078 while (subMenus
.size() > 0) {
1079 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
1080 if (subMenu
.mouseWouldHit(mouse
)) {
1083 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
1084 && (!mouse
.isMouse1())
1085 && (!mouse
.isMouse2())
1086 && (!mouse
.isMouse3())
1087 && (!mouse
.isMouseWheelUp())
1088 && (!mouse
.isMouseWheelDown())
1092 // We navigated away from a sub-menu, so close it
1096 // Convert the mouse relative x/y to menu coordinates
1097 assert (mouse
.getX() == mouse
.getAbsoluteX());
1098 assert (mouse
.getY() == mouse
.getAbsoluteY());
1099 if (subMenus
.size() > 0) {
1100 menu
= subMenus
.get(subMenus
.size() - 1);
1102 mouse
.setX(mouse
.getX() - menu
.getX());
1103 mouse
.setY(mouse
.getY() - menu
.getY());
1105 menu
.handleEvent(event
);
1109 if (event
instanceof TKeypressEvent
) {
1110 TKeypressEvent keypress
= (TKeypressEvent
) event
;
1112 // See if this key matches an accelerator, and is not being
1113 // shortcutted by the active window, and if so dispatch the menu
1115 boolean windowWillShortcut
= false;
1116 if (activeWindow
!= null) {
1117 assert (activeWindow
.isShown());
1118 if (activeWindow
.isShortcutKeypress(keypress
.getKey())) {
1119 // We do not process this key, it will be passed to the
1121 windowWillShortcut
= true;
1125 if (!windowWillShortcut
&& !modalWindowActive()) {
1126 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
1127 TMenuItem item
= null;
1128 synchronized (accelerators
) {
1129 item
= accelerators
.get(keypressLowercase
);
1132 if (item
.isEnabled()) {
1133 // Let the menu item dispatch
1139 // Handle the keypress
1140 if (onKeypress(keypress
)) {
1146 if (event
instanceof TCommandEvent
) {
1147 if (onCommand((TCommandEvent
) event
)) {
1152 if (event
instanceof TMenuEvent
) {
1153 if (onMenu((TMenuEvent
) event
)) {
1158 // Dispatch events to the active window -------------------------------
1159 boolean dispatchToDesktop
= true;
1160 TWindow window
= activeWindow
;
1161 if (window
!= null) {
1162 assert (window
.isActive());
1163 assert (window
.isShown());
1164 if (event
instanceof TMouseEvent
) {
1165 TMouseEvent mouse
= (TMouseEvent
) event
;
1166 // Convert the mouse relative x/y to window coordinates
1167 assert (mouse
.getX() == mouse
.getAbsoluteX());
1168 assert (mouse
.getY() == mouse
.getAbsoluteY());
1169 mouse
.setX(mouse
.getX() - window
.getX());
1170 mouse
.setY(mouse
.getY() - window
.getY());
1172 if (window
.mouseWouldHit(mouse
)) {
1173 dispatchToDesktop
= false;
1175 } else if (event
instanceof TKeypressEvent
) {
1176 dispatchToDesktop
= false;
1180 System
.err
.printf("TApplication dispatch event: %s\n",
1183 window
.handleEvent(event
);
1185 if (dispatchToDesktop
) {
1186 // This event is fair game for the desktop to process.
1187 if (desktop
!= null) {
1188 desktop
.handleEvent(event
);
1194 * Dispatch one event to the appropriate widget or application-level
1195 * event handler. This is the secondary event handler used by certain
1196 * special dialogs (currently TMessageBox and TFileOpenBox).
1198 * @param event the input event to consume
1199 * @see #primaryHandleEvent(TInputEvent event)
1201 private void secondaryHandleEvent(final TInputEvent event
) {
1202 // Peek at the mouse position
1203 if (event
instanceof TMouseEvent
) {
1204 TMouseEvent mouse
= (TMouseEvent
) event
;
1205 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1208 mouseX
= mouse
.getX();
1209 mouseY
= mouse
.getY();
1213 secondaryEventReceiver
.handleEvent(event
);
1217 * Enable a widget to override the primary event thread.
1219 * @param widget widget that will receive events
1221 public final void enableSecondaryEventReceiver(final TWidget widget
) {
1223 System
.err
.println(System
.currentTimeMillis() +
1224 " enableSecondaryEventReceiver()");
1227 assert (secondaryEventReceiver
== null);
1228 assert (secondaryEventHandler
== null);
1229 assert ((widget
instanceof TMessageBox
)
1230 || (widget
instanceof TFileOpenBox
));
1231 secondaryEventReceiver
= widget
;
1232 secondaryEventHandler
= new WidgetEventHandler(this, false);
1234 (new Thread(secondaryEventHandler
)).start();
1238 * Yield to the secondary thread.
1240 public final void yield() {
1241 assert (secondaryEventReceiver
!= null);
1243 while (secondaryEventReceiver
!= null) {
1244 synchronized (primaryEventHandler
) {
1246 primaryEventHandler
.wait();
1247 } catch (InterruptedException e
) {
1255 * Do stuff when there is no user input.
1257 private void doIdle() {
1259 System
.err
.printf(System
.currentTimeMillis() + " " +
1260 Thread
.currentThread() + " doIdle()\n");
1263 synchronized (timers
) {
1266 System
.err
.printf(System
.currentTimeMillis() + " " +
1267 Thread
.currentThread() + " doIdle() 2\n");
1270 // Run any timers that have timed out
1271 Date now
= new Date();
1272 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
1273 for (TTimer timer
: timers
) {
1274 if (timer
.getNextTick().getTime() <= now
.getTime()) {
1275 // Something might change, so repaint the screen.
1278 if (timer
.recurring
) {
1279 keepTimers
.add(timer
);
1282 keepTimers
.add(timer
);
1285 timers
= keepTimers
;
1289 for (TWindow window
: windows
) {
1292 if (desktop
!= null) {
1297 // ------------------------------------------------------------------------
1298 // TWindow management -----------------------------------------------------
1299 // ------------------------------------------------------------------------
1302 * Return the total number of windows.
1304 * @return the total number of windows
1306 public final int windowCount() {
1307 return windows
.size();
1311 * Return the number of windows that are showing.
1313 * @return the number of windows that are showing on screen
1315 public final int shownWindowCount() {
1317 for (TWindow w
: windows
) {
1326 * Return the number of windows that are hidden.
1328 * @return the number of windows that are hidden
1330 public final int hiddenWindowCount() {
1332 for (TWindow w
: windows
) {
1341 * Check if a window instance is in this application's window list.
1343 * @param window window to look for
1344 * @return true if this window is in the list
1346 public final boolean hasWindow(final TWindow window
) {
1347 if (windows
.size() == 0) {
1350 for (TWindow w
: windows
) {
1352 assert (window
.getApplication() == this);
1360 * Activate a window: bring it to the top and have it receive events.
1362 * @param window the window to become the new active window
1364 public void activateWindow(final TWindow window
) {
1365 if (hasWindow(window
) == false) {
1367 * Someone has a handle to a window I don't have. Ignore this
1373 // Whatever window might be moving/dragging, stop it now.
1374 for (TWindow w
: windows
) {
1375 if (w
.inMovements()) {
1380 assert (windows
.size() > 0);
1382 if (window
.isHidden()) {
1383 // Unhiding will also activate.
1387 assert (window
.isShown());
1389 if (windows
.size() == 1) {
1390 assert (window
== windows
.get(0));
1391 if (activeWindow
== null) {
1392 activeWindow
= window
;
1394 activeWindow
.setActive(true);
1395 activeWindow
.onFocus();
1398 assert (window
.isActive());
1399 assert (activeWindow
== window
);
1403 if (activeWindow
== window
) {
1404 assert (window
.isActive());
1406 // Window is already active, do nothing.
1410 assert (!window
.isActive());
1411 if (activeWindow
!= null) {
1412 assert (activeWindow
.getZ() == 0);
1414 activeWindow
.onUnfocus();
1415 activeWindow
.setActive(false);
1416 activeWindow
.setZ(window
.getZ());
1418 activeWindow
= window
;
1419 activeWindow
.setZ(0);
1420 activeWindow
.setActive(true);
1421 activeWindow
.onFocus();
1428 * @param window the window to hide
1430 public void hideWindow(final TWindow window
) {
1431 if (hasWindow(window
) == false) {
1433 * Someone has a handle to a window I don't have. Ignore this
1439 // Whatever window might be moving/dragging, stop it now.
1440 for (TWindow w
: windows
) {
1441 if (w
.inMovements()) {
1446 assert (windows
.size() > 0);
1448 if (!window
.hidden
) {
1449 if (window
== activeWindow
) {
1450 if (shownWindowCount() > 1) {
1453 activeWindow
= null;
1454 window
.setActive(false);
1458 window
.hidden
= true;
1466 * @param window the window to show
1468 public void showWindow(final TWindow window
) {
1469 if (hasWindow(window
) == false) {
1471 * Someone has a handle to a window I don't have. Ignore this
1477 // Whatever window might be moving/dragging, stop it now.
1478 for (TWindow w
: windows
) {
1479 if (w
.inMovements()) {
1484 assert (windows
.size() > 0);
1486 if (window
.hidden
) {
1487 window
.hidden
= false;
1489 activateWindow(window
);
1494 * Close window. Note that the window's destructor is NOT called by this
1495 * method, instead the GC is assumed to do the cleanup.
1497 * @param window the window to remove
1499 public final void closeWindow(final TWindow window
) {
1500 if (hasWindow(window
) == false) {
1502 * Someone has a handle to a window I don't have. Ignore this
1508 synchronized (windows
) {
1509 // Whatever window might be moving/dragging, stop it now.
1510 for (TWindow w
: windows
) {
1511 if (w
.inMovements()) {
1516 int z
= window
.getZ();
1519 Collections
.sort(windows
);
1521 activeWindow
= null;
1522 for (TWindow w
: windows
) {
1524 w
.setZ(w
.getZ() - 1);
1525 if (w
.getZ() == 0) {
1528 assert (activeWindow
== null);
1540 // Perform window cleanup
1543 // Check if we are closing a TMessageBox or similar
1544 if (secondaryEventReceiver
!= null) {
1545 assert (secondaryEventHandler
!= null);
1547 // Do not send events to the secondaryEventReceiver anymore, the
1548 // window is closed.
1549 secondaryEventReceiver
= null;
1551 // Wake the secondary thread, it will wake the primary as it
1553 synchronized (secondaryEventHandler
) {
1554 secondaryEventHandler
.notify();
1558 // Permit desktop to be active if it is the only thing left.
1559 if (desktop
!= null) {
1560 if (windows
.size() == 0) {
1561 desktop
.setActive(true);
1567 * Switch to the next window.
1569 * @param forward if true, then switch to the next window in the list,
1570 * otherwise switch to the previous window in the list
1572 public final void switchWindow(final boolean forward
) {
1573 // Only switch if there are multiple visible windows
1574 if (shownWindowCount() < 2) {
1577 assert (activeWindow
!= null);
1579 synchronized (windows
) {
1580 // Whatever window might be moving/dragging, stop it now.
1581 for (TWindow w
: windows
) {
1582 if (w
.inMovements()) {
1587 // Swap z/active between active window and the next in the list
1588 int activeWindowI
= -1;
1589 for (int i
= 0; i
< windows
.size(); i
++) {
1590 if (windows
.get(i
) == activeWindow
) {
1591 assert (activeWindow
.isActive());
1595 assert (!windows
.get(0).isActive());
1598 assert (activeWindowI
>= 0);
1600 // Do not switch if a window is modal
1601 if (activeWindow
.isModal()) {
1605 int nextWindowI
= activeWindowI
;
1609 nextWindowI
%= windows
.size();
1612 if (nextWindowI
< 0) {
1613 nextWindowI
= windows
.size() - 1;
1617 if (windows
.get(nextWindowI
).isShown()) {
1618 activateWindow(windows
.get(nextWindowI
));
1622 } // synchronized (windows)
1627 * Add a window to my window list and make it active.
1629 * @param window new window to add
1631 public final void addWindow(final TWindow window
) {
1633 // Do not add menu windows to the window list.
1634 if (window
instanceof TMenu
) {
1638 // Do not add the desktop to the window list.
1639 if (window
instanceof TDesktop
) {
1643 synchronized (windows
) {
1644 // Whatever window might be moving/dragging, stop it now.
1645 for (TWindow w
: windows
) {
1646 if (w
.inMovements()) {
1651 // Do not allow a modal window to spawn a non-modal window. If a
1652 // modal window is active, then this window will become modal
1654 if (modalWindowActive()) {
1655 window
.flags
|= TWindow
.MODAL
;
1656 window
.flags
|= TWindow
.CENTERED
;
1657 window
.hidden
= false;
1659 if (window
.isShown()) {
1660 for (TWindow w
: windows
) {
1665 w
.setZ(w
.getZ() + 1);
1668 windows
.add(window
);
1669 if (window
.isShown()) {
1670 activeWindow
= window
;
1671 activeWindow
.setZ(0);
1672 activeWindow
.setActive(true);
1673 activeWindow
.onFocus();
1676 if (((window
.flags
& TWindow
.CENTERED
) == 0)
1677 && smartWindowPlacement
) {
1679 doSmartPlacement(window
);
1683 // Desktop cannot be active over any other window.
1684 if (desktop
!= null) {
1685 desktop
.setActive(false);
1690 * Check if there is a system-modal window on top.
1692 * @return true if the active window is modal
1694 private boolean modalWindowActive() {
1695 if (windows
.size() == 0) {
1699 for (TWindow w
: windows
) {
1709 * Close all open windows.
1711 private void closeAllWindows() {
1712 // Don't do anything if we are in the menu
1713 if (activeMenu
!= null) {
1716 while (windows
.size() > 0) {
1717 closeWindow(windows
.get(0));
1722 * Re-layout the open windows as non-overlapping tiles. This produces
1723 * almost the same results as Turbo Pascal 7.0's IDE.
1725 private void tileWindows() {
1726 synchronized (windows
) {
1727 // Don't do anything if we are in the menu
1728 if (activeMenu
!= null) {
1731 int z
= windows
.size();
1737 a
= (int)(Math
.sqrt(z
));
1741 if (((a
* b
) + c
) == z
) {
1749 int newWidth
= (getScreen().getWidth() / a
);
1750 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
1751 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
1753 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1754 Collections
.sort(sorted
);
1755 Collections
.reverse(sorted
);
1756 for (int i
= 0; i
< sorted
.size(); i
++) {
1757 int logicalX
= i
/ b
;
1758 int logicalY
= i
% b
;
1759 if (i
>= ((a
- 1) * b
)) {
1761 logicalY
= i
- ((a
- 1) * b
);
1764 TWindow w
= sorted
.get(i
);
1765 w
.setX(logicalX
* newWidth
);
1766 w
.setWidth(newWidth
);
1767 if (i
>= ((a
- 1) * b
)) {
1768 w
.setY((logicalY
* newHeight2
) + 1);
1769 w
.setHeight(newHeight2
);
1771 w
.setY((logicalY
* newHeight1
) + 1);
1772 w
.setHeight(newHeight1
);
1779 * Re-layout the open windows as overlapping cascaded windows.
1781 private void cascadeWindows() {
1782 synchronized (windows
) {
1783 // Don't do anything if we are in the menu
1784 if (activeMenu
!= null) {
1789 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1790 Collections
.sort(sorted
);
1791 Collections
.reverse(sorted
);
1792 for (TWindow window
: sorted
) {
1797 if (x
> getScreen().getWidth()) {
1800 if (y
>= getScreen().getHeight()) {
1808 * Place a window to minimize its overlap with other windows.
1810 * @param window the window to place
1812 public final void doSmartPlacement(final TWindow window
) {
1813 // This is a pretty dumb algorithm, but seems to work. The hardest
1814 // part is computing these "overlap" values seeking a minimum average
1817 int yMin
= desktopTop
;
1818 int xMax
= getScreen().getWidth() - window
.getWidth() + 1;
1819 int yMax
= desktopBottom
- window
.getHeight() + 1;
1827 if ((xMin
== xMax
) && (yMin
== yMax
)) {
1828 // No work to do, bail out.
1832 // Compute the overlap matrix without the new window.
1833 int width
= getScreen().getWidth();
1834 int height
= getScreen().getHeight();
1835 int overlapMatrix
[][] = new int[width
][height
];
1836 for (TWindow w
: windows
) {
1840 for (int x
= w
.getX(); x
< w
.getX() + w
.getWidth(); x
++) {
1844 for (int y
= w
.getY(); y
< w
.getY() + w
.getHeight(); y
++) {
1848 overlapMatrix
[x
][y
]++;
1853 long oldOverlapTotal
= 0;
1854 long oldOverlapN
= 0;
1855 for (int x
= 0; x
< width
; x
++) {
1856 for (int y
= 0; y
< height
; y
++) {
1857 oldOverlapTotal
+= overlapMatrix
[x
][y
];
1858 if (overlapMatrix
[x
][y
] > 0) {
1865 double oldOverlapAvg
= (double) oldOverlapTotal
/ (double) oldOverlapN
;
1866 boolean first
= true;
1867 int windowX
= window
.getX();
1868 int windowY
= window
.getY();
1870 // For each possible (x, y) position for the new window, compute a
1871 // new overlap matrix.
1872 for (int x
= xMin
; x
< xMax
; x
++) {
1873 for (int y
= yMin
; y
< yMax
; y
++) {
1875 // Start with the matrix minus this window.
1876 int newMatrix
[][] = new int[width
][height
];
1877 for (int mx
= 0; mx
< width
; mx
++) {
1878 for (int my
= 0; my
< height
; my
++) {
1879 newMatrix
[mx
][my
] = overlapMatrix
[mx
][my
];
1883 // Add this window's values to the new overlap matrix.
1884 long newOverlapTotal
= 0;
1885 long newOverlapN
= 0;
1886 // Start by adding each new cell.
1887 for (int wx
= x
; wx
< x
+ window
.getWidth(); wx
++) {
1891 for (int wy
= y
; wy
< y
+ window
.getHeight(); wy
++) {
1895 newMatrix
[wx
][wy
]++;
1898 // Now figure out the new value for total coverage.
1899 for (int mx
= 0; mx
< width
; mx
++) {
1900 for (int my
= 0; my
< height
; my
++) {
1901 newOverlapTotal
+= newMatrix
[x
][y
];
1902 if (newMatrix
[mx
][my
] > 0) {
1907 double newOverlapAvg
= (double) newOverlapTotal
/ (double) newOverlapN
;
1910 // First time: just record what we got.
1911 oldOverlapAvg
= newOverlapAvg
;
1914 // All other times: pick a new best (x, y) and save the
1916 if (newOverlapAvg
< oldOverlapAvg
) {
1919 oldOverlapAvg
= newOverlapAvg
;
1923 } // for (int x = xMin; x < xMax; x++)
1925 } // for (int y = yMin; y < yMax; y++)
1927 // Finally, set the window's new coordinates.
1928 window
.setX(windowX
);
1929 window
.setY(windowY
);
1932 // ------------------------------------------------------------------------
1933 // TMenu management -------------------------------------------------------
1934 // ------------------------------------------------------------------------
1937 * Check if a mouse event would hit either the active menu or any open
1940 * @param mouse mouse event
1941 * @return true if the mouse would hit the active menu or an open
1944 private boolean mouseOnMenu(final TMouseEvent mouse
) {
1945 assert (activeMenu
!= null);
1946 List
<TMenu
> menus
= new LinkedList
<TMenu
>(subMenus
);
1947 Collections
.reverse(menus
);
1948 for (TMenu menu
: menus
) {
1949 if (menu
.mouseWouldHit(mouse
)) {
1953 return activeMenu
.mouseWouldHit(mouse
);
1957 * See if we need to switch window or activate the menu based on
1960 * @param mouse mouse event
1962 private void checkSwitchFocus(final TMouseEvent mouse
) {
1964 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1965 && (activeMenu
!= null)
1966 && (mouse
.getAbsoluteY() != 0)
1967 && (!mouseOnMenu(mouse
))
1969 // They clicked outside the active menu, turn it off
1970 activeMenu
.setActive(false);
1972 for (TMenu menu
: subMenus
) {
1973 menu
.setActive(false);
1979 // See if they hit the menu bar
1980 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
1981 && (mouse
.isMouse1())
1982 && (!modalWindowActive())
1983 && (mouse
.getAbsoluteY() == 0)
1986 for (TMenu menu
: subMenus
) {
1987 menu
.setActive(false);
1991 // They selected the menu, go activate it
1992 for (TMenu menu
: menus
) {
1993 if ((mouse
.getAbsoluteX() >= menu
.getX())
1994 && (mouse
.getAbsoluteX() < menu
.getX()
1995 + menu
.getTitle().length() + 2)
1997 menu
.setActive(true);
2000 menu
.setActive(false);
2006 // See if they hit the menu bar
2007 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
2008 && (mouse
.isMouse1())
2009 && (activeMenu
!= null)
2010 && (mouse
.getAbsoluteY() == 0)
2013 TMenu oldMenu
= activeMenu
;
2014 for (TMenu menu
: subMenus
) {
2015 menu
.setActive(false);
2019 // See if we should switch menus
2020 for (TMenu menu
: menus
) {
2021 if ((mouse
.getAbsoluteX() >= menu
.getX())
2022 && (mouse
.getAbsoluteX() < menu
.getX()
2023 + menu
.getTitle().length() + 2)
2025 menu
.setActive(true);
2029 if (oldMenu
!= activeMenu
) {
2030 // They switched menus
2031 oldMenu
.setActive(false);
2036 // If a menu is still active, don't switch windows
2037 if (activeMenu
!= null) {
2041 // Only switch if there are multiple windows
2042 if (windows
.size() < 2) {
2046 if (((focusFollowsMouse
== true)
2047 && (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
))
2048 || (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
)
2050 synchronized (windows
) {
2051 Collections
.sort(windows
);
2052 if (windows
.get(0).isModal()) {
2053 // Modal windows don't switch
2057 for (TWindow window
: windows
) {
2058 assert (!window
.isModal());
2060 if (window
.isHidden()) {
2061 assert (!window
.isActive());
2065 if (window
.mouseWouldHit(mouse
)) {
2066 if (window
== windows
.get(0)) {
2067 // Clicked on the same window, nothing to do
2068 assert (window
.isActive());
2072 // We will be switching to another window
2073 assert (windows
.get(0).isActive());
2074 assert (windows
.get(0) == activeWindow
);
2075 assert (!window
.isActive());
2076 activeWindow
.onUnfocus();
2077 activeWindow
.setActive(false);
2078 activeWindow
.setZ(window
.getZ());
2079 activeWindow
= window
;
2081 window
.setActive(true);
2088 // Clicked on the background, nothing to do
2092 // Nothing to do: this isn't a mouse up, or focus isn't following
2098 * Turn off the menu.
2100 public final void closeMenu() {
2101 if (activeMenu
!= null) {
2102 activeMenu
.setActive(false);
2104 for (TMenu menu
: subMenus
) {
2105 menu
.setActive(false);
2112 * Get a (shallow) copy of the menu list.
2114 * @return a copy of the menu list
2116 public final List
<TMenu
> getAllMenus() {
2117 return new LinkedList
<TMenu
>(menus
);
2121 * Add a top-level menu to the list.
2123 * @param menu the menu to add
2124 * @throws IllegalArgumentException if the menu is already used in
2125 * another TApplication
2127 public final void addMenu(final TMenu menu
) {
2128 if ((menu
.getApplication() != null)
2129 && (menu
.getApplication() != this)
2131 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2132 "part of application " + menu
.getApplication());
2140 * Remove a top-level menu from the list.
2142 * @param menu the menu to remove
2143 * @throws IllegalArgumentException if the menu is already used in
2144 * another TApplication
2146 public final void removeMenu(final TMenu menu
) {
2147 if ((menu
.getApplication() != null)
2148 && (menu
.getApplication() != this)
2150 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2151 "part of application " + menu
.getApplication());
2159 * Turn off a sub-menu.
2161 public final void closeSubMenu() {
2162 assert (activeMenu
!= null);
2163 TMenu item
= subMenus
.get(subMenus
.size() - 1);
2164 assert (item
!= null);
2165 item
.setActive(false);
2166 subMenus
.remove(subMenus
.size() - 1);
2170 * Switch to the next menu.
2172 * @param forward if true, then switch to the next menu in the list,
2173 * otherwise switch to the previous menu in the list
2175 public final void switchMenu(final boolean forward
) {
2176 assert (activeMenu
!= null);
2178 for (TMenu menu
: subMenus
) {
2179 menu
.setActive(false);
2183 for (int i
= 0; i
< menus
.size(); i
++) {
2184 if (activeMenu
== menus
.get(i
)) {
2186 if (i
< menus
.size() - 1) {
2194 activeMenu
.setActive(false);
2195 activeMenu
= menus
.get(i
);
2196 activeMenu
.setActive(true);
2203 * Add a menu item to the global list. If it has a keyboard accelerator,
2204 * that will be added the global hash.
2206 * @param item the menu item
2208 public final void addMenuItem(final TMenuItem item
) {
2209 menuItems
.add(item
);
2211 TKeypress key
= item
.getKey();
2213 synchronized (accelerators
) {
2214 assert (accelerators
.get(key
) == null);
2215 accelerators
.put(key
.toLowerCase(), item
);
2221 * Disable one menu item.
2223 * @param id the menu item ID
2225 public final void disableMenuItem(final int id
) {
2226 for (TMenuItem item
: menuItems
) {
2227 if (item
.getId() == id
) {
2228 item
.setEnabled(false);
2234 * Disable the range of menu items with ID's between lower and upper,
2237 * @param lower the lowest menu item ID
2238 * @param upper the highest menu item ID
2240 public final void disableMenuItems(final int lower
, final int upper
) {
2241 for (TMenuItem item
: menuItems
) {
2242 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2243 item
.setEnabled(false);
2249 * Enable one menu item.
2251 * @param id the menu item ID
2253 public final void enableMenuItem(final int id
) {
2254 for (TMenuItem item
: menuItems
) {
2255 if (item
.getId() == id
) {
2256 item
.setEnabled(true);
2262 * Enable the range of menu items with ID's between lower and upper,
2265 * @param lower the lowest menu item ID
2266 * @param upper the highest menu item ID
2268 public final void enableMenuItems(final int lower
, final int upper
) {
2269 for (TMenuItem item
: menuItems
) {
2270 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2271 item
.setEnabled(true);
2277 * Recompute menu x positions based on their title length.
2279 public final void recomputeMenuX() {
2281 for (TMenu menu
: menus
) {
2283 x
+= menu
.getTitle().length() + 2;
2288 * Post an event to process and turn off the menu.
2290 * @param event new event to add to the queue
2292 public final void postMenuEvent(final TInputEvent event
) {
2293 synchronized (this) {
2294 synchronized (fillEventQueue
) {
2295 fillEventQueue
.add(event
);
2298 System
.err
.println(System
.currentTimeMillis() + " " +
2299 Thread
.currentThread() + " postMenuEvent() wake up main");
2307 * Add a sub-menu to the list of open sub-menus.
2309 * @param menu sub-menu
2311 public final void addSubMenu(final TMenu menu
) {
2316 * Convenience function to add a top-level menu.
2318 * @param title menu title
2319 * @return the new menu
2321 public final TMenu
addMenu(final String title
) {
2324 TMenu menu
= new TMenu(this, x
, y
, title
);
2331 * Convenience function to add a default "File" menu.
2333 * @return the new menu
2335 public final TMenu
addFileMenu() {
2336 TMenu fileMenu
= addMenu("&File");
2337 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
2338 fileMenu
.addSeparator();
2339 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
2340 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
2341 TStatusBar statusBar
= fileMenu
.newStatusBar("File-management " +
2342 "commands (Open, Save, Print, etc.)");
2343 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
2348 * Convenience function to add a default "Edit" menu.
2350 * @return the new menu
2352 public final TMenu
addEditMenu() {
2353 TMenu editMenu
= addMenu("&Edit");
2354 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
2355 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
2356 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
2357 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
2358 TStatusBar statusBar
= editMenu
.newStatusBar("Editor operations, " +
2359 "undo, and Clipboard access");
2360 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
2365 * Convenience function to add a default "Window" menu.
2367 * @return the new menu
2369 public final TMenu
addWindowMenu() {
2370 TMenu windowMenu
= addMenu("&Window");
2371 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
2372 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
2373 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
2374 windowMenu
.addSeparator();
2375 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
2376 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
2377 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
2378 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
2379 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
2380 TStatusBar statusBar
= windowMenu
.newStatusBar("Open, arrange, and " +
2382 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
2387 * Convenience function to add a default "Help" menu.
2389 * @return the new menu
2391 public final TMenu
addHelpMenu() {
2392 TMenu helpMenu
= addMenu("&Help");
2393 helpMenu
.addDefaultItem(TMenu
.MID_HELP_CONTENTS
);
2394 helpMenu
.addDefaultItem(TMenu
.MID_HELP_INDEX
);
2395 helpMenu
.addDefaultItem(TMenu
.MID_HELP_SEARCH
);
2396 helpMenu
.addDefaultItem(TMenu
.MID_HELP_PREVIOUS
);
2397 helpMenu
.addDefaultItem(TMenu
.MID_HELP_HELP
);
2398 helpMenu
.addDefaultItem(TMenu
.MID_HELP_ACTIVE_FILE
);
2399 helpMenu
.addSeparator();
2400 helpMenu
.addDefaultItem(TMenu
.MID_ABOUT
);
2401 TStatusBar statusBar
= helpMenu
.newStatusBar("Access online help");
2402 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
2406 // ------------------------------------------------------------------------
2407 // Event handlers ---------------------------------------------------------
2408 // ------------------------------------------------------------------------
2411 * Method that TApplication subclasses can override to handle menu or
2412 * posted command events.
2414 * @param command command event
2415 * @return if true, this event was consumed
2417 protected boolean onCommand(final TCommandEvent command
) {
2418 // Default: handle cmExit
2419 if (command
.equals(cmExit
)) {
2420 if (messageBox("Confirmation", "Exit application?",
2421 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
2427 if (command
.equals(cmShell
)) {
2428 openTerminal(0, 0, TWindow
.RESIZABLE
);
2432 if (command
.equals(cmTile
)) {
2436 if (command
.equals(cmCascade
)) {
2440 if (command
.equals(cmCloseAll
)) {
2445 if (command
.equals(cmMenu
)) {
2446 if (!modalWindowActive() && (activeMenu
== null)) {
2447 if (menus
.size() > 0) {
2448 menus
.get(0).setActive(true);
2449 activeMenu
= menus
.get(0);
2459 * Method that TApplication subclasses can override to handle menu
2462 * @param menu menu event
2463 * @return if true, this event was consumed
2465 protected boolean onMenu(final TMenuEvent menu
) {
2467 // Default: handle MID_EXIT
2468 if (menu
.getId() == TMenu
.MID_EXIT
) {
2469 if (messageBox("Confirmation", "Exit application?",
2470 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
2476 if (menu
.getId() == TMenu
.MID_SHELL
) {
2477 openTerminal(0, 0, TWindow
.RESIZABLE
);
2481 if (menu
.getId() == TMenu
.MID_TILE
) {
2485 if (menu
.getId() == TMenu
.MID_CASCADE
) {
2489 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
2493 if (menu
.getId() == TMenu
.MID_ABOUT
) {
2501 * Method that TApplication subclasses can override to handle keystrokes.
2503 * @param keypress keystroke event
2504 * @return if true, this event was consumed
2506 protected boolean onKeypress(final TKeypressEvent keypress
) {
2507 // Default: only menu shortcuts
2509 // Process Alt-F, Alt-E, etc. menu shortcut keys
2510 if (!keypress
.getKey().isFnKey()
2511 && keypress
.getKey().isAlt()
2512 && !keypress
.getKey().isCtrl()
2513 && (activeMenu
== null)
2514 && !modalWindowActive()
2517 assert (subMenus
.size() == 0);
2519 for (TMenu menu
: menus
) {
2520 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
2521 == Character
.toLowerCase(keypress
.getKey().getChar())
2524 menu
.setActive(true);
2533 // ------------------------------------------------------------------------
2534 // TTimer management ------------------------------------------------------
2535 // ------------------------------------------------------------------------
2538 * Get the amount of time I can sleep before missing a Timer tick.
2540 * @param timeout = initial (maximum) timeout in millis
2541 * @return number of milliseconds between now and the next timer event
2543 private long getSleepTime(final long timeout
) {
2544 Date now
= new Date();
2545 long nowTime
= now
.getTime();
2546 long sleepTime
= timeout
;
2548 synchronized (timers
) {
2549 for (TTimer timer
: timers
) {
2550 long nextTickTime
= timer
.getNextTick().getTime();
2551 if (nextTickTime
< nowTime
) {
2555 long timeDifference
= nextTickTime
- nowTime
;
2556 if (timeDifference
< sleepTime
) {
2557 sleepTime
= timeDifference
;
2562 assert (sleepTime
>= 0);
2563 assert (sleepTime
<= timeout
);
2568 * Convenience function to add a timer.
2570 * @param duration number of milliseconds to wait between ticks
2571 * @param recurring if true, re-schedule this timer after every tick
2572 * @param action function to call when button is pressed
2575 public final TTimer
addTimer(final long duration
, final boolean recurring
,
2576 final TAction action
) {
2578 TTimer timer
= new TTimer(duration
, recurring
, action
);
2579 synchronized (timers
) {
2586 * Convenience function to remove a timer.
2588 * @param timer timer to remove
2590 public final void removeTimer(final TTimer timer
) {
2591 synchronized (timers
) {
2592 timers
.remove(timer
);
2596 // ------------------------------------------------------------------------
2597 // Other TWindow constructors ---------------------------------------------
2598 // ------------------------------------------------------------------------
2601 * Convenience function to spawn a message box.
2603 * @param title window title, will be centered along the top border
2604 * @param caption message to display. Use embedded newlines to get a
2606 * @return the new message box
2608 public final TMessageBox
messageBox(final String title
,
2609 final String caption
) {
2611 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
2615 * Convenience function to spawn a message box.
2617 * @param title window title, will be centered along the top border
2618 * @param caption message to display. Use embedded newlines to get a
2620 * @param type one of the TMessageBox.Type constants. Default is
2622 * @return the new message box
2624 public final TMessageBox
messageBox(final String title
,
2625 final String caption
, final TMessageBox
.Type type
) {
2627 return new TMessageBox(this, title
, caption
, type
);
2631 * Convenience function to spawn an input box.
2633 * @param title window title, will be centered along the top border
2634 * @param caption message to display. Use embedded newlines to get a
2636 * @return the new input box
2638 public final TInputBox
inputBox(final String title
, final String caption
) {
2640 return new TInputBox(this, title
, caption
);
2644 * Convenience function to spawn an input box.
2646 * @param title window title, will be centered along the top border
2647 * @param caption message to display. Use embedded newlines to get a
2649 * @param text initial text to seed the field with
2650 * @return the new input box
2652 public final TInputBox
inputBox(final String title
, final String caption
,
2653 final String text
) {
2655 return new TInputBox(this, title
, caption
, text
);
2659 * Convenience function to open a terminal window.
2661 * @param x column relative to parent
2662 * @param y row relative to parent
2663 * @return the terminal new window
2665 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
2666 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
2670 * Convenience function to open a terminal window.
2672 * @param x column relative to parent
2673 * @param y row relative to parent
2674 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2675 * @return the terminal new window
2677 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2680 return new TTerminalWindow(this, x
, y
, flags
);
2684 * Convenience function to spawn an file open box.
2686 * @param path path of selected file
2687 * @return the result of the new file open box
2688 * @throws IOException if java.io operation throws
2690 public final String
fileOpenBox(final String path
) throws IOException
{
2692 TFileOpenBox box
= new TFileOpenBox(this, path
, TFileOpenBox
.Type
.OPEN
);
2693 return box
.getFilename();
2697 * Convenience function to spawn an file open box.
2699 * @param path path of selected file
2700 * @param type one of the Type constants
2701 * @return the result of the new file open box
2702 * @throws IOException if java.io operation throws
2704 public final String
fileOpenBox(final String path
,
2705 final TFileOpenBox
.Type type
) throws IOException
{
2707 TFileOpenBox box
= new TFileOpenBox(this, path
, type
);
2708 return box
.getFilename();
2712 * Convenience function to create a new window and make it active.
2713 * Window will be located at (0, 0).
2715 * @param title window title, will be centered along the top border
2716 * @param width width of window
2717 * @param height height of window
2719 public final TWindow
addWindow(final String title
, final int width
,
2722 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
);
2726 * Convenience function to create a new window and make it active.
2727 * Window will be located at (0, 0).
2729 * @param title window title, will be centered along the top border
2730 * @param width width of window
2731 * @param height height of window
2732 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
2734 public final TWindow
addWindow(final String title
,
2735 final int width
, final int height
, final int flags
) {
2737 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
, flags
);
2742 * Convenience function to create a new window and make it active.
2744 * @param title window title, will be centered along the top border
2745 * @param x column relative to parent
2746 * @param y row relative to parent
2747 * @param width width of window
2748 * @param height height of window
2750 public final TWindow
addWindow(final String title
,
2751 final int x
, final int y
, final int width
, final int height
) {
2753 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
);
2758 * Convenience function to create a new window and make it active.
2760 * @param title window title, will be centered along the top border
2761 * @param x column relative to parent
2762 * @param y row relative to parent
2763 * @param width width of window
2764 * @param height height of window
2765 * @param flags mask of RESIZABLE, CENTERED, or MODAL
2767 public final TWindow
addWindow(final String title
,
2768 final int x
, final int y
, final int width
, final int height
,
2771 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
, flags
);