2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.image
.BufferedImage
;
32 import java
.io
.IOException
;
33 import java
.util
.List
;
34 import java
.util
.ArrayList
;
36 import jexer
.backend
.Screen
;
37 import jexer
.bits
.Cell
;
38 import jexer
.bits
.CellAttributes
;
39 import jexer
.bits
.ColorTheme
;
40 import jexer
.event
.TCommandEvent
;
41 import jexer
.event
.TInputEvent
;
42 import jexer
.event
.TKeypressEvent
;
43 import jexer
.event
.TMenuEvent
;
44 import jexer
.event
.TMouseEvent
;
45 import jexer
.event
.TResizeEvent
;
46 import jexer
.layout
.LayoutManager
;
47 import jexer
.menu
.TMenu
;
48 import jexer
.ttree
.TTreeItem
;
49 import jexer
.ttree
.TTreeView
;
50 import jexer
.ttree
.TTreeViewWidget
;
51 import static jexer
.TKeypress
.*;
54 * TWidget is the base class of all objects that can be drawn on screen or
55 * handle user input events.
57 public abstract class TWidget
implements Comparable
<TWidget
> {
59 // ------------------------------------------------------------------------
60 // Variables --------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * Every widget has a parent widget that it may be "contained" in. For
65 * example, a TWindow might contain several TFields, or a TComboBox may
66 * contain a TList that itself contains a TVScroller.
68 private TWidget parent
= null;
71 * Child widgets that this widget contains.
73 private List
<TWidget
> children
;
76 * The currently active child widget that will receive keypress events.
78 private TWidget activeChild
= null;
81 * If true, this widget will receive events.
83 private boolean active
= false;
86 * The window that this widget draws to.
88 private TWindow window
= null;
91 * Absolute X position of the top-left corner.
96 * Absolute Y position of the top-left corner.
103 private int width
= 0;
108 private int height
= 0;
111 * My tab order inside a window or containing widget.
113 private int tabOrder
= 0;
116 * If true, this widget can be tabbed to or receive events.
118 private boolean enabled
= true;
121 * If true, this widget will be rendered.
123 private boolean visible
= true;
126 * If true, this widget has a cursor.
128 private boolean cursorVisible
= false;
131 * Cursor column position in relative coordinates.
133 private int cursorX
= 0;
136 * Cursor row position in relative coordinates.
138 private int cursorY
= 0;
143 private LayoutManager layout
= null;
145 // ------------------------------------------------------------------------
146 // Constructors -----------------------------------------------------------
147 // ------------------------------------------------------------------------
150 * Default constructor for subclasses.
152 protected TWidget() {
153 children
= new ArrayList
<TWidget
>();
157 * Protected constructor.
159 * @param parent parent widget
161 protected TWidget(final TWidget parent
) {
166 * Protected constructor.
168 * @param parent parent widget
169 * @param x column relative to parent
170 * @param y row relative to parent
171 * @param width width of widget
172 * @param height height of widget
174 protected TWidget(final TWidget parent
, final int x
, final int y
,
175 final int width
, final int height
) {
177 this(parent
, true, x
, y
, width
, height
);
181 * Protected constructor used by subclasses that are disabled by default.
183 * @param parent parent widget
184 * @param enabled if true assume enabled
186 protected TWidget(final TWidget parent
, final boolean enabled
) {
187 this(parent
, enabled
, 0, 0, 0, 0);
191 * Protected constructor used by subclasses that are disabled by default.
193 * @param parent parent widget
194 * @param enabled if true assume enabled
195 * @param x column relative to parent
196 * @param y row relative to parent
197 * @param width width of widget
198 * @param height height of widget
200 protected TWidget(final TWidget parent
, final boolean enabled
,
201 final int x
, final int y
, final int width
, final int height
) {
204 throw new IllegalArgumentException("width cannot be negative");
207 throw new IllegalArgumentException("height cannot be negative");
210 this.enabled
= enabled
;
211 this.parent
= parent
;
212 children
= new ArrayList
<TWidget
>();
217 this.height
= height
;
219 if (parent
!= null) {
220 this.window
= parent
.window
;
221 parent
.addChild(this);
226 * Backdoor access for TWindow's constructor. ONLY TWindow USES THIS.
228 * @param window the top-level window
229 * @param x column relative to parent
230 * @param y row relative to parent
231 * @param width width of window
232 * @param height height of window
234 protected final void setupForTWindow(final TWindow window
,
235 final int x
, final int y
, final int width
, final int height
) {
238 throw new IllegalArgumentException("width cannot be negative");
241 throw new IllegalArgumentException("height cannot be negative");
244 this.parent
= window
;
245 this.window
= window
;
249 this.height
= height
;
252 // ------------------------------------------------------------------------
253 // Event handlers ---------------------------------------------------------
254 // ------------------------------------------------------------------------
257 * Subclasses should override this method to cleanup resources. This is
258 * called by TWindow.onClose().
260 protected void close() {
261 // Default: call close() on children.
262 for (TWidget w
: getChildren()) {
268 * Check if a mouse press/release event coordinate is contained in this
271 * @param mouse a mouse-based event
272 * @return whether or not a mouse click would be sent to this widget
274 public final boolean mouseWouldHit(final TMouseEvent mouse
) {
280 if ((this instanceof TTreeItem
)
281 && ((y
< 0) || (y
> parent
.getHeight() - 1))
286 if ((mouse
.getAbsoluteX() >= getAbsoluteX())
287 && (mouse
.getAbsoluteX() < getAbsoluteX() + width
)
288 && (mouse
.getAbsoluteY() >= getAbsoluteY())
289 && (mouse
.getAbsoluteY() < getAbsoluteY() + height
)
297 * Method that subclasses can override to handle keystrokes.
299 * @param keypress keystroke event
301 public void onKeypress(final TKeypressEvent keypress
) {
302 assert (parent
!= null);
304 if ((children
.size() == 0)
305 || (this instanceof TTreeView
)
306 || (this instanceof TText
)
307 || (this instanceof TComboBox
)
311 // tab / shift-tab - switch to next/previous widget
312 // left-arrow or up-arrow: same as shift-tab
313 if ((keypress
.equals(kbTab
))
314 || (keypress
.equals(kbDown
) && !(this instanceof TComboBox
))
316 parent
.switchWidget(true);
318 } else if ((keypress
.equals(kbShiftTab
))
319 || (keypress
.equals(kbBackTab
))
320 || (keypress
.equals(kbUp
) && !(this instanceof TComboBox
))
322 parent
.switchWidget(false);
327 if ((children
.size() == 0)
328 && !(this instanceof TTreeView
)
332 // right-arrow or down-arrow: same as tab
333 if (keypress
.equals(kbRight
)) {
334 parent
.switchWidget(true);
336 } else if (keypress
.equals(kbLeft
)) {
337 parent
.switchWidget(false);
342 // If I have any buttons on me AND this is an Alt-key that matches
343 // its mnemonic, send it an Enter keystroke.
344 for (TWidget widget
: children
) {
345 if (widget
instanceof TButton
) {
346 TButton button
= (TButton
) widget
;
347 if (button
.isEnabled()
348 && !keypress
.getKey().isFnKey()
349 && keypress
.getKey().isAlt()
350 && !keypress
.getKey().isCtrl()
351 && (Character
.toLowerCase(button
.getMnemonic().getShortcut())
352 == Character
.toLowerCase(keypress
.getKey().getChar()))
355 widget
.onKeypress(new TKeypressEvent(kbEnter
));
361 // If I have any labels on me AND this is an Alt-key that matches
362 // its mnemonic, call its action.
363 for (TWidget widget
: children
) {
364 if (widget
instanceof TLabel
) {
365 TLabel label
= (TLabel
) widget
;
366 if (!keypress
.getKey().isFnKey()
367 && keypress
.getKey().isAlt()
368 && !keypress
.getKey().isCtrl()
369 && (Character
.toLowerCase(label
.getMnemonic().getShortcut())
370 == Character
.toLowerCase(keypress
.getKey().getChar()))
379 // If I have any radiobuttons on me AND this is an Alt-key that
380 // matches its mnemonic, select it and send a Space to it.
381 for (TWidget widget
: children
) {
382 if (widget
instanceof TRadioButton
) {
383 TRadioButton button
= (TRadioButton
) widget
;
384 if (button
.isEnabled()
385 && !keypress
.getKey().isFnKey()
386 && keypress
.getKey().isAlt()
387 && !keypress
.getKey().isCtrl()
388 && (Character
.toLowerCase(button
.getMnemonic().getShortcut())
389 == Character
.toLowerCase(keypress
.getKey().getChar()))
392 widget
.onKeypress(new TKeypressEvent(kbSpace
));
396 if (widget
instanceof TRadioGroup
) {
397 for (TWidget child
: widget
.getChildren()) {
398 if (child
instanceof TRadioButton
) {
399 TRadioButton button
= (TRadioButton
) child
;
400 if (button
.isEnabled()
401 && !keypress
.getKey().isFnKey()
402 && keypress
.getKey().isAlt()
403 && !keypress
.getKey().isCtrl()
404 && (Character
.toLowerCase(button
.getMnemonic().getShortcut())
405 == Character
.toLowerCase(keypress
.getKey().getChar()))
408 widget
.activate(child
);
409 child
.onKeypress(new TKeypressEvent(kbSpace
));
417 // If I have any checkboxes on me AND this is an Alt-key that matches
418 // its mnemonic, select it and set it to checked.
419 for (TWidget widget
: children
) {
420 if (widget
instanceof TCheckBox
) {
421 TCheckBox checkBox
= (TCheckBox
) widget
;
422 if (checkBox
.isEnabled()
423 && !keypress
.getKey().isFnKey()
424 && keypress
.getKey().isAlt()
425 && !keypress
.getKey().isCtrl()
426 && (Character
.toLowerCase(checkBox
.getMnemonic().getShortcut())
427 == Character
.toLowerCase(keypress
.getKey().getChar()))
430 checkBox
.setChecked(true);
436 // Dispatch the keypress to an active widget
437 for (TWidget widget
: children
) {
439 widget
.onKeypress(keypress
);
446 * Method that subclasses can override to handle mouse button presses.
448 * @param mouse mouse button event
450 public void onMouseDown(final TMouseEvent mouse
) {
451 // Default: do nothing, pass to children instead
452 if (activeChild
!= null) {
453 if (activeChild
.mouseWouldHit(mouse
)) {
454 // Dispatch to the active child
456 // Set x and y relative to the child's coordinates
457 mouse
.setX(mouse
.getAbsoluteX() - activeChild
.getAbsoluteX());
458 mouse
.setY(mouse
.getAbsoluteY() - activeChild
.getAbsoluteY());
459 activeChild
.onMouseDown(mouse
);
463 for (int i
= children
.size() - 1 ; i
>= 0 ; i
--) {
464 TWidget widget
= children
.get(i
);
465 if (widget
.mouseWouldHit(mouse
)) {
466 // Dispatch to this child, also activate it
469 // Set x and y relative to the child's coordinates
470 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
471 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
472 widget
.onMouseDown(mouse
);
479 * Method that subclasses can override to handle mouse button releases.
481 * @param mouse mouse button event
483 public void onMouseUp(final TMouseEvent mouse
) {
484 // Default: do nothing, pass to children instead
485 if (activeChild
!= null) {
486 if (activeChild
.mouseWouldHit(mouse
)) {
487 // Dispatch to the active child
489 // Set x and y relative to the child's coordinates
490 mouse
.setX(mouse
.getAbsoluteX() - activeChild
.getAbsoluteX());
491 mouse
.setY(mouse
.getAbsoluteY() - activeChild
.getAbsoluteY());
492 activeChild
.onMouseUp(mouse
);
496 for (int i
= children
.size() - 1 ; i
>= 0 ; i
--) {
497 TWidget widget
= children
.get(i
);
498 if (widget
.mouseWouldHit(mouse
)) {
499 // Dispatch to this child, also activate it
502 // Set x and y relative to the child's coordinates
503 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
504 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
505 widget
.onMouseUp(mouse
);
512 * Method that subclasses can override to handle mouse movements.
514 * @param mouse mouse motion event
516 public void onMouseMotion(final TMouseEvent mouse
) {
517 // Default: do nothing, pass it on to ALL of my children. This way
518 // the children can see the mouse "leaving" their area.
519 for (TWidget widget
: children
) {
520 // Set x and y relative to the child's coordinates
521 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
522 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
523 widget
.onMouseMotion(mouse
);
528 * Method that subclasses can override to handle mouse button
531 * @param mouse mouse button event
533 public void onMouseDoubleClick(final TMouseEvent mouse
) {
534 // Default: do nothing, pass to children instead
535 if (activeChild
!= null) {
536 if (activeChild
.mouseWouldHit(mouse
)) {
537 // Dispatch to the active child
539 // Set x and y relative to the child's coordinates
540 mouse
.setX(mouse
.getAbsoluteX() - activeChild
.getAbsoluteX());
541 mouse
.setY(mouse
.getAbsoluteY() - activeChild
.getAbsoluteY());
542 activeChild
.onMouseDoubleClick(mouse
);
546 for (int i
= children
.size() - 1 ; i
>= 0 ; i
--) {
547 TWidget widget
= children
.get(i
);
548 if (widget
.mouseWouldHit(mouse
)) {
549 // Dispatch to this child, also activate it
552 // Set x and y relative to the child's coordinates
553 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
554 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
555 widget
.onMouseDoubleClick(mouse
);
562 * Method that subclasses can override to handle window/screen resize
565 * @param resize resize event
567 public void onResize(final TResizeEvent resize
) {
568 // Default: change my width/height.
569 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
570 width
= resize
.getWidth();
571 height
= resize
.getHeight();
572 if (layout
!= null) {
573 if (this instanceof TWindow
) {
574 layout
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
575 width
- 2, height
- 2));
577 layout
.onResize(resize
);
581 // Let children see the screen resize
582 for (TWidget widget
: children
) {
583 widget
.onResize(resize
);
589 * Method that subclasses can override to handle posted command events.
591 * @param command command event
593 public void onCommand(final TCommandEvent command
) {
594 // Default: do nothing, pass to children instead
595 for (TWidget widget
: children
) {
596 widget
.onCommand(command
);
601 * Method that subclasses can override to handle menu or posted menu
604 * @param menu menu event
606 public void onMenu(final TMenuEvent menu
) {
607 // Default: do nothing, pass to children instead
608 for (TWidget widget
: children
) {
614 * Method that subclasses can override to do processing when the UI is
615 * idle. Note that repainting is NOT assumed. To get a refresh after
616 * onIdle, call doRepaint().
618 public void onIdle() {
619 // Default: do nothing, pass to children instead
620 for (TWidget widget
: children
) {
626 * Consume event. Subclasses that want to intercept all events in one go
627 * can override this method.
629 * @param event keyboard, mouse, resize, command, or menu event
631 public void handleEvent(final TInputEvent event
) {
633 System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
639 // System.err.println(" -- discard --");
643 if (event
instanceof TKeypressEvent
) {
644 onKeypress((TKeypressEvent
) event
);
645 } else if (event
instanceof TMouseEvent
) {
647 TMouseEvent mouse
= (TMouseEvent
) event
;
649 switch (mouse
.getType()) {
660 onMouseMotion(mouse
);
663 case MOUSE_DOUBLE_CLICK
:
664 onMouseDoubleClick(mouse
);
668 throw new IllegalArgumentException("Invalid mouse event type: "
671 } else if (event
instanceof TResizeEvent
) {
672 onResize((TResizeEvent
) event
);
673 } else if (event
instanceof TCommandEvent
) {
674 onCommand((TCommandEvent
) event
);
675 } else if (event
instanceof TMenuEvent
) {
676 onMenu((TMenuEvent
) event
);
683 // ------------------------------------------------------------------------
684 // TWidget ----------------------------------------------------------------
685 // ------------------------------------------------------------------------
690 * @return parent widget
692 public final TWidget
getParent() {
697 * Get the list of child widgets that this widget contains.
699 * @return the list of child widgets
701 public List
<TWidget
> getChildren() {
706 * Remove this widget from its parent container. close() will be called
707 * before it is removed.
709 public final void remove() {
714 * Remove this widget from its parent container.
716 * @param doClose if true, call the close() method before removing the
719 public final void remove(final boolean doClose
) {
720 if (parent
!= null) {
721 parent
.remove(this, doClose
);
726 * Remove a child widget from this container.
728 * @param child the child widget to remove
730 public final void remove(final TWidget child
) {
735 * Remove a child widget from this container.
737 * @param child the child widget to remove
738 * @param doClose if true, call the close() method before removing the
741 public final void remove(final TWidget child
, final boolean doClose
) {
742 if (!children
.contains(child
)) {
743 throw new IndexOutOfBoundsException("child widget is not in " +
744 "list of children of this parent");
749 children
.remove(child
);
752 if (layout
!= null) {
758 * Set this widget's parent to a different widget.
760 * @param newParent new parent widget
761 * @param doClose if true, call the close() method before removing the
762 * child from its existing parent widget
764 public final void setParent(final TWidget newParent
,
765 final boolean doClose
) {
767 if (parent
!= null) {
768 parent
.remove(this, doClose
);
771 assert (parent
== null);
772 assert (window
== null);
774 setWindow(parent
.window
);
775 parent
.addChild(this);
779 * Set this widget's window to a specific window.
781 * Having a null parent with a specified window is only used within Jexer
782 * by TStatusBar because TApplication routes events directly to it and
783 * calls its draw() method. Any other non-parented widgets will require
784 * similar special case functionality to receive events or be drawn to
787 * @param window the window to use
789 public final void setWindow(final TWindow window
) {
790 this.window
= window
;
791 for (TWidget child
: getChildren()) {
792 child
.setWindow(window
);
797 * Remove a child widget from this container, and all of its children
798 * recursively from their parent containers.
800 * @param child the child widget to remove
801 * @param doClose if true, call the close() method before removing each
804 public final void removeAll(final TWidget child
, final boolean doClose
) {
805 remove(child
, doClose
);
806 for (TWidget w
: child
.children
) {
807 child
.removeAll(w
, doClose
);
814 * @return if true, this widget will receive events
816 public final boolean isActive() {
823 * @param active if true, this widget will receive events
825 public final void setActive(final boolean active
) {
826 this.active
= active
;
830 * Get the window this widget is on.
834 public final TWindow
getWindow() {
841 * @return absolute X position of the top-left corner
843 public final int getX() {
850 * @param x absolute X position of the top-left corner
852 public final void setX(final int x
) {
859 * @return absolute Y position of the top-left corner
861 public final int getY() {
868 * @param y absolute Y position of the top-left corner
870 public final void setY(final int y
) {
877 * @return widget width
879 public int getWidth() {
886 * @param width new widget width
888 public void setWidth(final int width
) {
890 if (layout
!= null) {
891 layout
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
899 * @return widget height
901 public int getHeight() {
908 * @param height new widget height
910 public void setHeight(final int height
) {
911 this.height
= height
;
912 if (layout
!= null) {
913 layout
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
919 * Change the dimensions.
921 * @param x absolute X position of the top-left corner
922 * @param y absolute Y position of the top-left corner
923 * @param width new widget width
924 * @param height new widget height
926 public final void setDimensions(final int x
, final int y
, final int width
,
931 // Call the functions so that subclasses can choose how to handle it.
934 if (layout
!= null) {
935 layout
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
941 * Get the layout manager.
943 * @return the layout manager, or null if not set
945 public LayoutManager
getLayoutManager() {
950 * Set the layout manager.
952 * @param layout the new layout manager
954 public void setLayoutManager(LayoutManager layout
) {
955 if (this.layout
!= null) {
956 for (TWidget w
: children
) {
957 this.layout
.remove(w
);
961 this.layout
= layout
;
962 if (this.layout
!= null) {
963 for (TWidget w
: children
) {
972 * @return if true, this widget can be tabbed to or receive events
974 public final boolean isEnabled() {
981 * @param enabled if true, this widget can be tabbed to or receive events
983 public final void setEnabled(final boolean enabled
) {
984 this.enabled
= enabled
;
987 // See if there are any active siblings to switch to
988 boolean foundSibling
= false;
989 if (parent
!= null) {
990 for (TWidget w
: parent
.children
) {
992 && !(this instanceof THScroller
)
993 && !(this instanceof TVScroller
)
1000 if (!foundSibling
) {
1001 parent
.activeChild
= null;
1010 * @param visible if true, this widget will be drawn
1012 public final void setVisible(final boolean visible
) {
1013 this.visible
= visible
;
1017 * See if this widget is visible.
1019 * @return if true, this widget will be drawn
1021 public final boolean isVisible() {
1026 * Set visible cursor flag.
1028 * @param cursorVisible if true, this widget has a cursor
1030 public final void setCursorVisible(final boolean cursorVisible
) {
1031 this.cursorVisible
= cursorVisible
;
1035 * See if this widget has a visible cursor.
1037 * @return if true, this widget has a visible cursor
1039 public final boolean isCursorVisible() {
1040 // If cursor is out of my bounds, it is not visible.
1041 if ((cursorX
>= width
)
1043 || (cursorY
>= height
)
1049 assert (window
!= null);
1051 if (window
instanceof TDesktop
) {
1052 // Desktop doesn't have a window border.
1053 return cursorVisible
;
1056 // If cursor is out of my window's bounds, it is not visible.
1057 if ((getCursorAbsoluteX() >= window
.getAbsoluteX()
1058 + window
.getWidth() - 1)
1059 || (getCursorAbsoluteX() < 0)
1060 || (getCursorAbsoluteY() >= window
.getAbsoluteY()
1061 + window
.getHeight() - 1)
1062 || (getCursorAbsoluteY() < 0)
1066 return cursorVisible
;
1070 * Get cursor X value.
1072 * @return cursor column position in relative coordinates
1074 public final int getCursorX() {
1079 * Set cursor X value.
1081 * @param cursorX column position in relative coordinates
1083 public final void setCursorX(final int cursorX
) {
1084 this.cursorX
= cursorX
;
1088 * Get cursor Y value.
1090 * @return cursor row position in relative coordinates
1092 public final int getCursorY() {
1097 * Set cursor Y value.
1099 * @param cursorY row position in relative coordinates
1101 public final void setCursorY(final int cursorY
) {
1102 this.cursorY
= cursorY
;
1106 * Get this TWidget's parent TApplication.
1108 * @return the parent TApplication, or null if not assigned
1110 public TApplication
getApplication() {
1111 if (window
!= null) {
1112 return window
.getApplication();
1120 * @return the Screen, or null if not assigned
1122 public Screen
getScreen() {
1123 if (window
!= null) {
1124 return window
.getScreen();
1130 * Comparison operator. For various subclasses it sorts on:
1132 * <li>tabOrder for TWidgets</li>
1133 * <li>z for TWindows</li>
1134 * <li>text for TTreeItems</li>
1137 * @param that another TWidget, TWindow, or TTreeItem instance
1138 * @return difference between this.tabOrder and that.tabOrder, or
1139 * difference between this.z and that.z, or String.compareTo(text)
1142 public final int compareTo(final TWidget that
) {
1143 if ((this instanceof TWindow
)
1144 && (that
instanceof TWindow
)
1146 return (((TWindow
) this).getZ() - ((TWindow
) that
).getZ());
1148 if ((this instanceof TTreeItem
)
1149 && (that
instanceof TTreeItem
)
1151 return (((TTreeItem
) this).getText().compareTo(
1152 ((TTreeItem
) that
).getText()));
1154 return (this.tabOrder
- that
.tabOrder
);
1158 * See if this widget should render with the active color.
1160 * @return true if this widget is active and all of its parents are
1163 public final boolean isAbsoluteActive() {
1164 if (parent
== this) {
1167 return (active
&& (parent
== null ?
true : parent
.isAbsoluteActive()));
1171 * Returns the cursor X position.
1173 * @return absolute screen column number for the cursor's X position
1175 public final int getCursorAbsoluteX() {
1176 return getAbsoluteX() + cursorX
;
1180 * Returns the cursor Y position.
1182 * @return absolute screen row number for the cursor's Y position
1184 public final int getCursorAbsoluteY() {
1185 return getAbsoluteY() + cursorY
;
1189 * Compute my absolute X position as the sum of my X plus all my parent's
1192 * @return absolute screen column number for my X position
1194 public final int getAbsoluteX() {
1195 assert (parent
!= null);
1196 if (parent
== this) {
1199 if ((parent
instanceof TWindow
)
1200 && !(parent
instanceof TMenu
)
1201 && !(parent
instanceof TDesktop
)
1203 // Widgets on a TWindow have (0,0) as their top-left, but this is
1204 // actually the TWindow's (1,1).
1205 return parent
.getAbsoluteX() + x
+ 1;
1207 return parent
.getAbsoluteX() + x
;
1211 * Compute my absolute Y position as the sum of my Y plus all my parent's
1214 * @return absolute screen row number for my Y position
1216 public final int getAbsoluteY() {
1217 assert (parent
!= null);
1218 if (parent
== this) {
1221 if ((parent
instanceof TWindow
)
1222 && !(parent
instanceof TMenu
)
1223 && !(parent
instanceof TDesktop
)
1225 // Widgets on a TWindow have (0,0) as their top-left, but this is
1226 // actually the TWindow's (1,1).
1227 return parent
.getAbsoluteY() + y
+ 1;
1229 return parent
.getAbsoluteY() + y
;
1233 * Get the global color theme.
1235 * @return the ColorTheme
1237 protected final ColorTheme
getTheme() {
1238 return window
.getApplication().getTheme();
1242 * See if this widget can be drawn onto a screen.
1244 * @return true if this widget is part of the hierarchy that can draw to
1247 public final boolean isDrawable() {
1248 if ((window
== null)
1249 || (window
.getScreen() == null)
1254 if (parent
== this) {
1257 return (parent
.isDrawable());
1261 * Draw my specific widget. When called, the screen rectangle I draw
1262 * into is already setup (offset and clipping).
1264 public void draw() {
1265 // Default widget draws nothing.
1269 * Called by parent to render to TWindow. Note package private access.
1271 final void drawChildren() {
1272 if (!isDrawable()) {
1276 // Set my clipping rectangle
1277 assert (window
!= null);
1278 assert (getScreen() != null);
1279 Screen screen
= getScreen();
1281 // Special case: TStatusBar is drawn by TApplication, not anything
1283 if (this instanceof TStatusBar
) {
1287 screen
.setClipRight(width
);
1288 screen
.setClipBottom(height
);
1290 int absoluteRightEdge
= window
.getAbsoluteX() + window
.getWidth();
1291 int absoluteBottomEdge
= window
.getAbsoluteY() + window
.getHeight();
1292 if (!(this instanceof TWindow
)
1293 && !(this instanceof TVScroller
)
1294 && !(window
instanceof TDesktop
)
1296 absoluteRightEdge
-= 1;
1298 if (!(this instanceof TWindow
)
1299 && !(this instanceof THScroller
)
1300 && !(window
instanceof TDesktop
)
1302 absoluteBottomEdge
-= 1;
1304 int myRightEdge
= getAbsoluteX() + width
;
1305 int myBottomEdge
= getAbsoluteY() + height
;
1306 if (getAbsoluteX() > absoluteRightEdge
) {
1308 screen
.setClipRight(0);
1309 } else if (myRightEdge
> absoluteRightEdge
) {
1310 screen
.setClipRight(screen
.getClipRight()
1311 - (myRightEdge
- absoluteRightEdge
));
1313 if (getAbsoluteY() > absoluteBottomEdge
) {
1315 screen
.setClipBottom(0);
1316 } else if (myBottomEdge
> absoluteBottomEdge
) {
1317 screen
.setClipBottom(screen
.getClipBottom()
1318 - (myBottomEdge
- absoluteBottomEdge
));
1322 screen
.setOffsetX(getAbsoluteX());
1323 screen
.setOffsetY(getAbsoluteY());
1327 if (!isDrawable()) {
1328 // An action taken by a draw method unhooked me from the UI.
1333 assert (visible
== true);
1335 // Continue down the chain. Draw the active child last so that it
1337 for (TWidget widget
: children
) {
1338 if (widget
.isVisible() && (widget
!= activeChild
)) {
1339 widget
.drawChildren();
1340 if (!isDrawable()) {
1341 // An action taken by a draw method unhooked me from the UI.
1347 if (activeChild
!= null) {
1348 activeChild
.drawChildren();
1353 * Repaint the screen on the next update.
1355 protected final void doRepaint() {
1356 window
.getApplication().doRepaint();
1360 * Add a child widget to my list of children. We set its tabOrder to 0
1361 * and increment the tabOrder of all other children.
1363 * @param child TWidget to add
1365 private void addChild(final TWidget child
) {
1366 children
.add(child
);
1369 && !(child
instanceof THScroller
)
1370 && !(child
instanceof TVScroller
)
1372 for (TWidget widget
: children
) {
1373 widget
.active
= false;
1375 child
.active
= true;
1376 activeChild
= child
;
1378 for (int i
= 0; i
< children
.size(); i
++) {
1379 children
.get(i
).tabOrder
= i
;
1381 if (layout
!= null) {
1387 * Reset the tab order of children to match their position in the list.
1388 * Available so that subclasses can re-order their widgets if needed.
1390 protected void resetTabOrder() {
1391 for (int i
= 0; i
< children
.size(); i
++) {
1392 children
.get(i
).tabOrder
= i
;
1397 * Remove and {@link TWidget#close()} the given child from this {@link TWidget}.
1399 * Will also reorder the tab values of the remaining children.
1401 * @param child the child to remove
1403 * @return TRUE if the child was removed, FALSE if it was not found
1405 public boolean removeChild(final TWidget child
) {
1406 if (children
.remove(child
)) {
1408 child
.parent
= null;
1409 child
.window
= null;
1420 * Switch the active child.
1422 * @param child TWidget to activate
1424 public final void activate(final TWidget child
) {
1425 assert (child
.enabled
);
1426 if ((child
instanceof THScroller
)
1427 || (child
instanceof TVScroller
)
1432 if (children
.size() == 1) {
1433 if (children
.get(0).enabled
== true) {
1434 child
.active
= true;
1435 activeChild
= child
;
1438 if (child
!= activeChild
) {
1439 if (activeChild
!= null) {
1440 activeChild
.active
= false;
1442 child
.active
= true;
1443 activeChild
= child
;
1449 * Switch the active child.
1451 * @param tabOrder tabOrder of the child to activate. If that child
1452 * isn't enabled, then the next enabled child will be activated.
1454 public final void activate(final int tabOrder
) {
1455 if (children
.size() == 1) {
1456 if (children
.get(0).enabled
== true) {
1457 children
.get(0).active
= true;
1458 activeChild
= children
.get(0);
1463 TWidget child
= null;
1464 for (TWidget widget
: children
) {
1465 if ((widget
.enabled
)
1466 && !(widget
instanceof THScroller
)
1467 && !(widget
instanceof TVScroller
)
1468 && (widget
.tabOrder
>= tabOrder
)
1474 if ((child
!= null) && (child
!= activeChild
)) {
1475 if (activeChild
!= null) {
1476 activeChild
.active
= false;
1478 assert (child
.enabled
);
1479 child
.active
= true;
1480 activeChild
= child
;
1485 * Make this widget the active child of its parent. Note that this is
1486 * not final since TWindow overrides activate().
1488 public void activate() {
1490 if (parent
!= null) {
1491 parent
.activate(this);
1497 * Make this widget, all of its parents, the active child.
1499 public final void activateAll() {
1501 if (parent
== this) {
1504 if (parent
!= null) {
1505 parent
.activateAll();
1510 * Switch the active widget with the next in the tab order.
1512 * @param forward if true, then switch to the next enabled widget in the
1513 * list, otherwise switch to the previous enabled widget in the list
1515 public final void switchWidget(final boolean forward
) {
1517 // No children: do nothing.
1518 if (children
.size() == 0) {
1522 assert (parent
!= null);
1524 // If there is only one child, make it active if it is enabled.
1525 if (children
.size() == 1) {
1526 if (children
.get(0).enabled
== true) {
1527 activeChild
= children
.get(0);
1528 activeChild
.active
= true;
1530 children
.get(0).active
= false;
1536 // Two or more children: go forward or backward to the next enabled
1539 if (activeChild
!= null) {
1540 tabOrder
= activeChild
.tabOrder
;
1550 // If at the end, pass the switch to my parent.
1551 if ((!forward
) && (parent
!= this)) {
1552 parent
.switchWidget(forward
);
1556 tabOrder
= children
.size() - 1;
1557 } else if (tabOrder
== children
.size()) {
1558 // If at the end, pass the switch to my parent.
1559 if ((forward
) && (parent
!= this)) {
1560 parent
.switchWidget(forward
);
1566 if (activeChild
== null) {
1567 if (tabOrder
== 0) {
1568 // We wrapped around
1571 } else if (activeChild
.tabOrder
== tabOrder
) {
1572 // We wrapped around
1575 } while ((!children
.get(tabOrder
).enabled
)
1576 && !(children
.get(tabOrder
) instanceof THScroller
)
1577 && !(children
.get(tabOrder
) instanceof TVScroller
));
1579 if (activeChild
!= null) {
1580 assert (children
.get(tabOrder
).enabled
);
1582 activeChild
.active
= false;
1584 if (children
.get(tabOrder
).enabled
== true) {
1585 children
.get(tabOrder
).active
= true;
1586 activeChild
= children
.get(tabOrder
);
1591 * Returns my active widget.
1593 * @return widget that is active, or this if no children
1595 public TWidget
getActiveChild() {
1596 if ((this instanceof THScroller
)
1597 || (this instanceof TVScroller
)
1602 for (TWidget widget
: children
) {
1603 if (widget
.active
) {
1604 return widget
.getActiveChild();
1607 // No active children, return me
1612 * Insert a vertical split between this widget and parent, and optionally
1613 * put another widget in the other side of the split.
1615 * @param newWidgetOnLeft if true, the new widget (if specified) will be
1616 * on the left pane, and this widget will be placed on the right pane
1617 * @param newWidget the new widget to add to the other pane, or null
1618 * @return the new split pane widget
1620 public TSplitPane
splitVertical(final boolean newWidgetOnLeft
,
1621 final TWidget newWidget
) {
1623 TSplitPane splitPane
= new TSplitPane(null, x
, y
, width
, height
, true);
1624 TWidget myParent
= parent
;
1626 if (myParent
instanceof TSplitPane
) {
1627 // TSplitPane has a left/right/top/bottom link to me somewhere,
1628 // replace it with a link to splitPane.
1629 ((TSplitPane
) myParent
).replaceWidget(this, splitPane
);
1631 splitPane
.setParent(myParent
, false);
1632 if (newWidgetOnLeft
) {
1633 splitPane
.setLeft(newWidget
);
1634 splitPane
.setRight(this);
1636 splitPane
.setLeft(this);
1637 splitPane
.setRight(newWidget
);
1639 if (newWidget
!= null) {
1640 newWidget
.activateAll();
1645 assert (parent
!= null);
1646 assert (window
!= null);
1647 assert (splitPane
.getWindow() != null);
1648 assert (splitPane
.getParent() != null);
1649 assert (splitPane
.isActive() == true);
1650 assert (parent
== splitPane
);
1651 if (newWidget
!= null) {
1652 assert (newWidget
.parent
== parent
);
1653 assert (newWidget
.active
== true);
1654 assert (active
== false);
1656 assert (active
== true);
1662 * Insert a horizontal split between this widget and parent, and
1663 * optionally put another widget in the other side of the split.
1665 * @param newWidgetOnTop if true, the new widget (if specified) will be
1666 * on the top pane, and this widget's children will be placed on the
1668 * @param newWidget the new widget to add to the other pane, or null
1669 * @return the new split pane widget
1671 public TSplitPane
splitHorizontal(final boolean newWidgetOnTop
,
1672 final TWidget newWidget
) {
1674 TSplitPane splitPane
= new TSplitPane(null, x
, y
, width
, height
, false);
1675 TWidget myParent
= parent
;
1677 if (myParent
instanceof TSplitPane
) {
1678 // TSplitPane has a left/right/top/bottom link to me somewhere,
1679 // replace it with a link to splitPane.
1680 ((TSplitPane
) myParent
).replaceWidget(this, splitPane
);
1682 splitPane
.setParent(myParent
, false);
1683 if (newWidgetOnTop
) {
1684 splitPane
.setTop(newWidget
);
1685 splitPane
.setBottom(this);
1687 splitPane
.setTop(this);
1688 splitPane
.setBottom(newWidget
);
1690 if (newWidget
!= null) {
1691 newWidget
.activateAll();
1696 assert (parent
!= null);
1697 assert (window
!= null);
1698 assert (splitPane
.getWindow() != null);
1699 assert (splitPane
.getParent() != null);
1700 assert (splitPane
.isActive() == true);
1701 assert (parent
== splitPane
);
1702 if (newWidget
!= null) {
1703 assert (newWidget
.parent
== parent
);
1704 assert (newWidget
.active
== true);
1705 assert (active
== false);
1707 assert (active
== true);
1713 * Generate a human-readable string for this widget.
1715 * @return a human-readable string
1718 public String
toString() {
1719 return String
.format("%s(%8x) position (%d, %d) geometry %dx%d " +
1720 "active %s enabled %s visible %s", getClass().getName(),
1721 hashCode(), x
, y
, width
, height
, active
, enabled
, visible
);
1725 * Generate a string for this widget's hierarchy.
1727 * @param prefix a prefix to use for this widget's place in the hierarchy
1728 * @return a pretty-printable string of this hierarchy
1730 protected String
toPrettyString(final String prefix
) {
1731 StringBuilder sb
= new StringBuilder(prefix
);
1732 sb
.append(toString());
1733 String newPrefix
= "";
1734 for (int i
= 0; i
< prefix
.length(); i
++) {
1737 for (int i
= 0; i
< children
.size(); i
++) {
1738 TWidget child
= children
.get(i
);
1740 if (i
== children
.size() - 1) {
1741 sb
.append(child
.toPrettyString(newPrefix
+ " \u2514\u2500"));
1743 sb
.append(child
.toPrettyString(newPrefix
+ " \u251c\u2500"));
1746 return sb
.toString();
1750 * Generate a string for this widget's hierarchy.
1752 * @return a pretty-printable string of this hierarchy
1754 public String
toPrettyString() {
1755 return toPrettyString("");
1758 // ------------------------------------------------------------------------
1759 // Passthru for Screen functions ------------------------------------------
1760 // ------------------------------------------------------------------------
1763 * Get the attributes at one location.
1765 * @param x column coordinate. 0 is the left-most column.
1766 * @param y row coordinate. 0 is the top-most row.
1767 * @return attributes at (x, y)
1769 protected final CellAttributes
getAttrXY(final int x
, final int y
) {
1770 return getScreen().getAttrXY(x
, y
);
1774 * Set the attributes at one location.
1776 * @param x column coordinate. 0 is the left-most column.
1777 * @param y row coordinate. 0 is the top-most row.
1778 * @param attr attributes to use (bold, foreColor, backColor)
1780 protected final void putAttrXY(final int x
, final int y
,
1781 final CellAttributes attr
) {
1783 getScreen().putAttrXY(x
, y
, attr
);
1787 * Set the attributes at one location.
1789 * @param x column coordinate. 0 is the left-most column.
1790 * @param y row coordinate. 0 is the top-most row.
1791 * @param attr attributes to use (bold, foreColor, backColor)
1792 * @param clip if true, honor clipping/offset
1794 protected final void putAttrXY(final int x
, final int y
,
1795 final CellAttributes attr
, final boolean clip
) {
1797 getScreen().putAttrXY(x
, y
, attr
, clip
);
1801 * Fill the entire screen with one character with attributes.
1803 * @param ch character to draw
1804 * @param attr attributes to use (bold, foreColor, backColor)
1806 protected final void putAll(final int ch
, final CellAttributes attr
) {
1807 getScreen().putAll(ch
, attr
);
1811 * Render one character with attributes.
1813 * @param x column coordinate. 0 is the left-most column.
1814 * @param y row coordinate. 0 is the top-most row.
1815 * @param ch character + attributes to draw
1817 protected final void putCharXY(final int x
, final int y
, final Cell ch
) {
1818 getScreen().putCharXY(x
, y
, ch
);
1822 * Render one character with attributes.
1824 * @param x column coordinate. 0 is the left-most column.
1825 * @param y row coordinate. 0 is the top-most row.
1826 * @param ch character to draw
1827 * @param attr attributes to use (bold, foreColor, backColor)
1829 protected final void putCharXY(final int x
, final int y
, final int ch
,
1830 final CellAttributes attr
) {
1832 getScreen().putCharXY(x
, y
, ch
, attr
);
1836 * Render one character without changing the underlying attributes.
1838 * @param x column coordinate. 0 is the left-most column.
1839 * @param y row coordinate. 0 is the top-most row.
1840 * @param ch character to draw
1842 protected final void putCharXY(final int x
, final int y
, final int ch
) {
1843 getScreen().putCharXY(x
, y
, ch
);
1847 * Render a string. Does not wrap if the string exceeds the line.
1849 * @param x column coordinate. 0 is the left-most column.
1850 * @param y row coordinate. 0 is the top-most row.
1851 * @param str string to draw
1852 * @param attr attributes to use (bold, foreColor, backColor)
1854 protected final void putStringXY(final int x
, final int y
, final String str
,
1855 final CellAttributes attr
) {
1857 getScreen().putStringXY(x
, y
, str
, attr
);
1861 * Render a string without changing the underlying attribute. Does not
1862 * wrap if the string exceeds the line.
1864 * @param x column coordinate. 0 is the left-most column.
1865 * @param y row coordinate. 0 is the top-most row.
1866 * @param str string to draw
1868 protected final void putStringXY(final int x
, final int y
, final String str
) {
1869 getScreen().putStringXY(x
, y
, str
);
1873 * Draw a vertical line from (x, y) to (x, y + n).
1875 * @param x column coordinate. 0 is the left-most column.
1876 * @param y row coordinate. 0 is the top-most row.
1877 * @param n number of characters to draw
1878 * @param ch character to draw
1879 * @param attr attributes to use (bold, foreColor, backColor)
1881 protected final void vLineXY(final int x
, final int y
, final int n
,
1882 final int ch
, final CellAttributes attr
) {
1884 getScreen().vLineXY(x
, y
, n
, ch
, attr
);
1888 * Draw a horizontal line from (x, y) to (x + n, y).
1890 * @param x column coordinate. 0 is the left-most column.
1891 * @param y row coordinate. 0 is the top-most row.
1892 * @param n number of characters to draw
1893 * @param ch character to draw
1894 * @param attr attributes to use (bold, foreColor, backColor)
1896 protected final void hLineXY(final int x
, final int y
, final int n
,
1897 final int ch
, final CellAttributes attr
) {
1899 getScreen().hLineXY(x
, y
, n
, ch
, attr
);
1903 * Draw a box with a border and empty background.
1905 * @param left left column of box. 0 is the left-most row.
1906 * @param top top row of the box. 0 is the top-most row.
1907 * @param right right column of box
1908 * @param bottom bottom row of the box
1909 * @param border attributes to use for the border
1910 * @param background attributes to use for the background
1912 protected final void drawBox(final int left
, final int top
,
1913 final int right
, final int bottom
,
1914 final CellAttributes border
, final CellAttributes background
) {
1916 getScreen().drawBox(left
, top
, right
, bottom
, border
, background
);
1920 * Draw a box with a border and empty background.
1922 * @param left left column of box. 0 is the left-most row.
1923 * @param top top row of the box. 0 is the top-most row.
1924 * @param right right column of box
1925 * @param bottom bottom row of the box
1926 * @param border attributes to use for the border
1927 * @param background attributes to use for the background
1928 * @param borderType if 1, draw a single-line border; if 2, draw a
1929 * double-line border; if 3, draw double-line top/bottom edges and
1930 * single-line left/right edges (like Qmodem)
1931 * @param shadow if true, draw a "shadow" on the box
1933 protected final void drawBox(final int left
, final int top
,
1934 final int right
, final int bottom
,
1935 final CellAttributes border
, final CellAttributes background
,
1936 final int borderType
, final boolean shadow
) {
1938 getScreen().drawBox(left
, top
, right
, bottom
, border
, background
,
1939 borderType
, shadow
);
1943 * Draw a box shadow.
1945 * @param left left column of box. 0 is the left-most row.
1946 * @param top top row of the box. 0 is the top-most row.
1947 * @param right right column of box
1948 * @param bottom bottom row of the box
1950 protected final void drawBoxShadow(final int left
, final int top
,
1951 final int right
, final int bottom
) {
1953 getScreen().drawBoxShadow(left
, top
, right
, bottom
);
1956 // ------------------------------------------------------------------------
1957 // Other TWidget constructors ---------------------------------------------
1958 // ------------------------------------------------------------------------
1961 * Convenience function to add a label to this container/window.
1964 * @param x column relative to parent
1965 * @param y row relative to parent
1966 * @return the new label
1968 public final TLabel
addLabel(final String text
, final int x
, final int y
) {
1969 return addLabel(text
, x
, y
, "tlabel");
1973 * Convenience function to add a label to this container/window.
1976 * @param x column relative to parent
1977 * @param y row relative to parent
1978 * @param action to call when shortcut is pressed
1979 * @return the new label
1981 public final TLabel
addLabel(final String text
, final int x
, final int y
,
1982 final TAction action
) {
1984 return addLabel(text
, x
, y
, "tlabel", action
);
1988 * Convenience function to add a label to this container/window.
1991 * @param x column relative to parent
1992 * @param y row relative to parent
1993 * @param colorKey ColorTheme key color to use for foreground text.
1994 * Default is "tlabel"
1995 * @return the new label
1997 public final TLabel
addLabel(final String text
, final int x
, final int y
,
1998 final String colorKey
) {
2000 return new TLabel(this, text
, x
, y
, colorKey
);
2004 * Convenience function to add a label to this container/window.
2007 * @param x column relative to parent
2008 * @param y row relative to parent
2009 * @param colorKey ColorTheme key color to use for foreground text.
2010 * Default is "tlabel"
2011 * @param action to call when shortcut is pressed
2012 * @return the new label
2014 public final TLabel
addLabel(final String text
, final int x
, final int y
,
2015 final String colorKey
, final TAction action
) {
2017 return new TLabel(this, text
, x
, y
, colorKey
, action
);
2021 * Convenience function to add a label to this container/window.
2024 * @param x column relative to parent
2025 * @param y row relative to parent
2026 * @param colorKey ColorTheme key color to use for foreground text.
2027 * Default is "tlabel"
2028 * @param useWindowBackground if true, use the window's background color
2029 * @return the new label
2031 public final TLabel
addLabel(final String text
, final int x
, final int y
,
2032 final String colorKey
, final boolean useWindowBackground
) {
2034 return new TLabel(this, text
, x
, y
, colorKey
, useWindowBackground
);
2038 * Convenience function to add a label to this container/window.
2041 * @param x column relative to parent
2042 * @param y row relative to parent
2043 * @param colorKey ColorTheme key color to use for foreground text.
2044 * Default is "tlabel"
2045 * @param useWindowBackground if true, use the window's background color
2046 * @param action to call when shortcut is pressed
2047 * @return the new label
2049 public final TLabel
addLabel(final String text
, final int x
, final int y
,
2050 final String colorKey
, final boolean useWindowBackground
,
2051 final TAction action
) {
2053 return new TLabel(this, text
, x
, y
, colorKey
, useWindowBackground
,
2058 * Convenience function to add a button to this container/window.
2060 * @param text label on the button
2061 * @param x column relative to parent
2062 * @param y row relative to parent
2063 * @param action action to call when button is pressed
2064 * @return the new button
2066 public final TButton
addButton(final String text
, final int x
, final int y
,
2067 final TAction action
) {
2069 return new TButton(this, text
, x
, y
, action
);
2073 * Convenience function to add a checkbox to this container/window.
2075 * @param x column relative to parent
2076 * @param y row relative to parent
2077 * @param label label to display next to (right of) the checkbox
2078 * @param checked initial check state
2079 * @return the new checkbox
2081 public final TCheckBox
addCheckBox(final int x
, final int y
,
2082 final String label
, final boolean checked
) {
2084 return new TCheckBox(this, x
, y
, label
, checked
);
2088 * Convenience function to add a combobox to this container/window.
2090 * @param x column relative to parent
2091 * @param y row relative to parent
2092 * @param width visible combobox width, including the down-arrow
2093 * @param values the possible values for the box, shown in the drop-down
2094 * @param valuesIndex the initial index in values, or -1 for no default
2096 * @param maxValuesHeight the maximum height of the values drop-down when
2098 * @param updateAction action to call when a new value is selected from
2099 * the list or enter is pressed in the edit field
2100 * @return the new combobox
2102 public final TComboBox
addComboBox(final int x
, final int y
,
2103 final int width
, final List
<String
> values
, final int valuesIndex
,
2104 final int maxValuesHeight
, final TAction updateAction
) {
2106 return new TComboBox(this, x
, y
, width
, values
, valuesIndex
,
2107 maxValuesHeight
, updateAction
);
2111 * Convenience function to add a spinner to this container/window.
2113 * @param x column relative to parent
2114 * @param y row relative to parent
2115 * @param upAction action to call when the up arrow is clicked or pressed
2116 * @param downAction action to call when the down arrow is clicked or
2118 * @return the new spinner
2120 public final TSpinner
addSpinner(final int x
, final int y
,
2121 final TAction upAction
, final TAction downAction
) {
2123 return new TSpinner(this, x
, y
, upAction
, downAction
);
2127 * Convenience function to add a calendar to this container/window.
2129 * @param x column relative to parent
2130 * @param y row relative to parent
2131 * @param updateAction action to call when the user changes the value of
2133 * @return the new calendar
2135 public final TCalendar
addCalendar(final int x
, final int y
,
2136 final TAction updateAction
) {
2138 return new TCalendar(this, x
, y
, updateAction
);
2142 * Convenience function to add a progress bar to this container/window.
2144 * @param x column relative to parent
2145 * @param y row relative to parent
2146 * @param width width of progress bar
2147 * @param value initial value of percent complete
2148 * @return the new progress bar
2150 public final TProgressBar
addProgressBar(final int x
, final int y
,
2151 final int width
, final int value
) {
2153 return new TProgressBar(this, x
, y
, width
, value
);
2157 * Convenience function to add a radio button group to this
2160 * @param x column relative to parent
2161 * @param y row relative to parent
2162 * @param label label to display on the group box
2163 * @return the new radio button group
2165 public final TRadioGroup
addRadioGroup(final int x
, final int y
,
2166 final String label
) {
2168 return new TRadioGroup(this, x
, y
, label
);
2172 * Convenience function to add a text field to this container/window.
2174 * @param x column relative to parent
2175 * @param y row relative to parent
2176 * @param width visible text width
2177 * @param fixed if true, the text cannot exceed the display width
2178 * @return the new text field
2180 public final TField
addField(final int x
, final int y
,
2181 final int width
, final boolean fixed
) {
2183 return new TField(this, x
, y
, width
, fixed
);
2187 * Convenience function to add a text field to this container/window.
2189 * @param x column relative to parent
2190 * @param y row relative to parent
2191 * @param width visible text width
2192 * @param fixed if true, the text cannot exceed the display width
2193 * @param text initial text, default is empty string
2194 * @return the new text field
2196 public final TField
addField(final int x
, final int y
,
2197 final int width
, final boolean fixed
, final String text
) {
2199 return new TField(this, x
, y
, width
, fixed
, text
);
2203 * Convenience function to add a text field to this container/window.
2205 * @param x column relative to parent
2206 * @param y row relative to parent
2207 * @param width visible text width
2208 * @param fixed if true, the text cannot exceed the display width
2209 * @param text initial text, default is empty string
2210 * @param enterAction function to call when enter key is pressed
2211 * @param updateAction function to call when the text is updated
2212 * @return the new text field
2214 public final TField
addField(final int x
, final int y
,
2215 final int width
, final boolean fixed
, final String text
,
2216 final TAction enterAction
, final TAction updateAction
) {
2218 return new TField(this, x
, y
, width
, fixed
, text
, enterAction
,
2223 * Convenience function to add a scrollable text box to this
2226 * @param text text on the screen
2227 * @param x column relative to parent
2228 * @param y row relative to parent
2229 * @param width width of text area
2230 * @param height height of text area
2231 * @param colorKey ColorTheme key color to use for foreground text
2232 * @return the new text box
2234 public final TText
addText(final String text
, final int x
,
2235 final int y
, final int width
, final int height
, final String colorKey
) {
2237 return new TText(this, text
, x
, y
, width
, height
, colorKey
);
2241 * Convenience function to add a scrollable text box to this
2244 * @param text text on the screen
2245 * @param x column relative to parent
2246 * @param y row relative to parent
2247 * @param width width of text area
2248 * @param height height of text area
2249 * @return the new text box
2251 public final TText
addText(final String text
, final int x
, final int y
,
2252 final int width
, final int height
) {
2254 return new TText(this, text
, x
, y
, width
, height
, "ttext");
2258 * Convenience function to add an editable text area box to this
2261 * @param text text on the screen
2262 * @param x column relative to parent
2263 * @param y row relative to parent
2264 * @param width width of text area
2265 * @param height height of text area
2266 * @return the new text box
2268 public final TEditorWidget
addEditor(final String text
, final int x
,
2269 final int y
, final int width
, final int height
) {
2271 return new TEditorWidget(this, text
, x
, y
, width
, height
);
2275 * Convenience function to spawn a message box.
2277 * @param title window title, will be centered along the top border
2278 * @param caption message to display. Use embedded newlines to get a
2280 * @return the new message box
2282 public final TMessageBox
messageBox(final String title
,
2283 final String caption
) {
2285 return getApplication().messageBox(title
, caption
, TMessageBox
.Type
.OK
);
2289 * Convenience function to spawn a message box.
2291 * @param title window title, will be centered along the top border
2292 * @param caption message to display. Use embedded newlines to get a
2294 * @param type one of the TMessageBox.Type constants. Default is
2296 * @return the new message box
2298 public final TMessageBox
messageBox(final String title
,
2299 final String caption
, final TMessageBox
.Type type
) {
2301 return getApplication().messageBox(title
, caption
, type
);
2305 * Convenience function to spawn an input box.
2307 * @param title window title, will be centered along the top border
2308 * @param caption message to display. Use embedded newlines to get a
2310 * @return the new input box
2312 public final TInputBox
inputBox(final String title
, final String caption
) {
2314 return getApplication().inputBox(title
, caption
);
2318 * Convenience function to spawn an input box.
2320 * @param title window title, will be centered along the top border
2321 * @param caption message to display. Use embedded newlines to get a
2323 * @param text initial text to seed the field with
2324 * @return the new input box
2326 public final TInputBox
inputBox(final String title
, final String caption
,
2327 final String text
) {
2329 return getApplication().inputBox(title
, caption
, text
);
2333 * Convenience function to spawn an input box.
2335 * @param title window title, will be centered along the top border
2336 * @param caption message to display. Use embedded newlines to get a
2338 * @param text initial text to seed the field with
2339 * @param type one of the Type constants. Default is Type.OK.
2340 * @return the new input box
2342 public final TInputBox
inputBox(final String title
, final String caption
,
2343 final String text
, final TInputBox
.Type type
) {
2345 return getApplication().inputBox(title
, caption
, text
, type
);
2349 * Convenience function to add a password text field to this
2352 * @param x column relative to parent
2353 * @param y row relative to parent
2354 * @param width visible text width
2355 * @param fixed if true, the text cannot exceed the display width
2356 * @return the new text field
2358 public final TPasswordField
addPasswordField(final int x
, final int y
,
2359 final int width
, final boolean fixed
) {
2361 return new TPasswordField(this, x
, y
, width
, fixed
);
2365 * Convenience function to add a password text field to this
2368 * @param x column relative to parent
2369 * @param y row relative to parent
2370 * @param width visible text width
2371 * @param fixed if true, the text cannot exceed the display width
2372 * @param text initial text, default is empty string
2373 * @return the new text field
2375 public final TPasswordField
addPasswordField(final int x
, final int y
,
2376 final int width
, final boolean fixed
, final String text
) {
2378 return new TPasswordField(this, x
, y
, width
, fixed
, text
);
2382 * Convenience function to add a password text field to this
2385 * @param x column relative to parent
2386 * @param y row relative to parent
2387 * @param width visible text width
2388 * @param fixed if true, the text cannot exceed the display width
2389 * @param text initial text, default is empty string
2390 * @param enterAction function to call when enter key is pressed
2391 * @param updateAction function to call when the text is updated
2392 * @return the new text field
2394 public final TPasswordField
addPasswordField(final int x
, final int y
,
2395 final int width
, final boolean fixed
, final String text
,
2396 final TAction enterAction
, final TAction updateAction
) {
2398 return new TPasswordField(this, x
, y
, width
, fixed
, text
, enterAction
,
2403 * Convenience function to add a scrollable tree view to this
2406 * @param x column relative to parent
2407 * @param y row relative to parent
2408 * @param width width of tree view
2409 * @param height height of tree view
2410 * @return the new tree view
2412 public final TTreeViewWidget
addTreeViewWidget(final int x
, final int y
,
2413 final int width
, final int height
) {
2415 return new TTreeViewWidget(this, x
, y
, width
, height
);
2419 * Convenience function to add a scrollable tree view to this
2422 * @param x column relative to parent
2423 * @param y row relative to parent
2424 * @param width width of tree view
2425 * @param height height of tree view
2426 * @param action action to perform when an item is selected
2427 * @return the new tree view
2429 public final TTreeViewWidget
addTreeViewWidget(final int x
, final int y
,
2430 final int width
, final int height
, final TAction action
) {
2432 return new TTreeViewWidget(this, x
, y
, width
, height
, action
);
2436 * Convenience function to spawn a file open box.
2438 * @param path path of selected file
2439 * @return the result of the new file open box
2440 * @throws IOException if a java.io operation throws
2442 public final String
fileOpenBox(final String path
) throws IOException
{
2443 return getApplication().fileOpenBox(path
);
2447 * Convenience function to spawn a file save box.
2449 * @param path path of selected file
2450 * @return the result of the new file open box
2451 * @throws IOException if a java.io operation throws
2453 public final String
fileSaveBox(final String path
) throws IOException
{
2454 return getApplication().fileOpenBox(path
, TFileOpenBox
.Type
.SAVE
);
2458 * Convenience function to spawn a file open box.
2460 * @param path path of selected file
2461 * @param type one of the Type constants
2462 * @return the result of the new file open box
2463 * @throws IOException if a java.io operation throws
2465 public final String
fileOpenBox(final String path
,
2466 final TFileOpenBox
.Type type
) throws IOException
{
2468 return getApplication().fileOpenBox(path
, type
);
2472 * Convenience function to spawn a file open box.
2474 * @param path path of selected file
2475 * @param type one of the Type constants
2476 * @param filter a string that files must match to be displayed
2477 * @return the result of the new file open box
2478 * @throws IOException of a java.io operation throws
2480 public final String
fileOpenBox(final String path
,
2481 final TFileOpenBox
.Type type
, final String filter
) throws IOException
{
2483 ArrayList
<String
> filters
= new ArrayList
<String
>();
2484 filters
.add(filter
);
2486 return getApplication().fileOpenBox(path
, type
, filters
);
2490 * Convenience function to spawn a file open box.
2492 * @param path path of selected file
2493 * @param type one of the Type constants
2494 * @param filters a list of strings that files must match to be displayed
2495 * @return the result of the new file open box
2496 * @throws IOException of a java.io operation throws
2498 public final String
fileOpenBox(final String path
,
2499 final TFileOpenBox
.Type type
,
2500 final List
<String
> filters
) throws IOException
{
2502 return getApplication().fileOpenBox(path
, type
, filters
);
2506 * Convenience function to add a directory list to this container/window.
2508 * @param path directory path, must be a directory
2509 * @param x column relative to parent
2510 * @param y row relative to parent
2511 * @param width width of text area
2512 * @param height height of text area
2513 * @return the new directory list
2515 public final TDirectoryList
addDirectoryList(final String path
, final int x
,
2516 final int y
, final int width
, final int height
) {
2518 return new TDirectoryList(this, path
, x
, y
, width
, height
, null);
2522 * Convenience function to add a directory list to this container/window.
2524 * @param path directory path, must be a directory
2525 * @param x column relative to parent
2526 * @param y row relative to parent
2527 * @param width width of text area
2528 * @param height height of text area
2529 * @param action action to perform when an item is selected (enter or
2531 * @return the new directory list
2533 public final TDirectoryList
addDirectoryList(final String path
, final int x
,
2534 final int y
, final int width
, final int height
, final TAction action
) {
2536 return new TDirectoryList(this, path
, x
, y
, width
, height
, action
);
2540 * Convenience function to add a directory list to this container/window.
2542 * @param path directory path, must be a directory
2543 * @param x column relative to parent
2544 * @param y row relative to parent
2545 * @param width width of text area
2546 * @param height height of text area
2547 * @param action action to perform when an item is selected (enter or
2549 * @param singleClickAction action to perform when an item is selected
2551 * @return the new directory list
2553 public final TDirectoryList
addDirectoryList(final String path
, final int x
,
2554 final int y
, final int width
, final int height
, final TAction action
,
2555 final TAction singleClickAction
) {
2557 return new TDirectoryList(this, path
, x
, y
, width
, height
, action
,
2562 * Convenience function to add a directory list to this container/window.
2564 * @param path directory path, must be a directory
2565 * @param x column relative to parent
2566 * @param y row relative to parent
2567 * @param width width of text area
2568 * @param height height of text area
2569 * @param action action to perform when an item is selected (enter or
2571 * @param singleClickAction action to perform when an item is selected
2573 * @param filters a list of strings that files must match to be displayed
2574 * @return the new directory list
2576 public final TDirectoryList
addDirectoryList(final String path
, final int x
,
2577 final int y
, final int width
, final int height
, final TAction action
,
2578 final TAction singleClickAction
, final List
<String
> filters
) {
2580 return new TDirectoryList(this, path
, x
, y
, width
, height
, action
,
2581 singleClickAction
, filters
);
2585 * Convenience function to add a list to this container/window.
2587 * @param strings list of strings to show
2588 * @param x column relative to parent
2589 * @param y row relative to parent
2590 * @param width width of text area
2591 * @param height height of text area
2592 * @return the new directory list
2594 public final TList
addList(final List
<String
> strings
, final int x
,
2595 final int y
, final int width
, final int height
) {
2597 return new TList(this, strings
, x
, y
, width
, height
, null);
2601 * Convenience function to add a list to this container/window.
2603 * @param strings list of strings to show
2604 * @param x column relative to parent
2605 * @param y row relative to parent
2606 * @param width width of text area
2607 * @param height height of text area
2608 * @param enterAction action to perform when an item is selected
2609 * @return the new directory list
2611 public final TList
addList(final List
<String
> strings
, final int x
,
2612 final int y
, final int width
, final int height
,
2613 final TAction enterAction
) {
2615 return new TList(this, strings
, x
, y
, width
, height
, enterAction
);
2619 * Convenience function to add a list to this container/window.
2621 * @param strings list of strings to show
2622 * @param x column relative to parent
2623 * @param y row relative to parent
2624 * @param width width of text area
2625 * @param height height of text area
2626 * @param enterAction action to perform when an item is selected
2627 * @param moveAction action to perform when the user navigates to a new
2628 * item with arrow/page keys
2629 * @return the new directory list
2631 public final TList
addList(final List
<String
> strings
, final int x
,
2632 final int y
, final int width
, final int height
,
2633 final TAction enterAction
, final TAction moveAction
) {
2635 return new TList(this, strings
, x
, y
, width
, height
, enterAction
,
2640 * Convenience function to add a list to this container/window.
2642 * @param strings list of strings to show. This is allowed to be null
2643 * and set later with setList() or by subclasses.
2644 * @param x column relative to parent
2645 * @param y row relative to parent
2646 * @param width width of text area
2647 * @param height height of text area
2648 * @param enterAction action to perform when an item is selected
2649 * @param moveAction action to perform when the user navigates to a new
2650 * item with arrow/page keys
2651 * @param singleClickAction action to perform when the user clicks on an
2654 public TList
addList(final List
<String
> strings
, final int x
,
2655 final int y
, final int width
, final int height
,
2656 final TAction enterAction
, final TAction moveAction
,
2657 final TAction singleClickAction
) {
2659 return new TList(this, strings
, x
, y
, width
, height
, enterAction
,
2660 moveAction
, singleClickAction
);
2665 * Convenience function to add an image to this container/window.
2667 * @param x column relative to parent
2668 * @param y row relative to parent
2669 * @param width number of text cells for width of the image
2670 * @param height number of text cells for height of the image
2671 * @param image the image to display
2672 * @param left left column of the image. 0 is the left-most column.
2673 * @param top top row of the image. 0 is the top-most row.
2675 public final TImage
addImage(final int x
, final int y
,
2676 final int width
, final int height
,
2677 final BufferedImage image
, final int left
, final int top
) {
2679 return new TImage(this, x
, y
, width
, height
, image
, left
, top
);
2683 * Convenience function to add an image to this container/window.
2685 * @param x column relative to parent
2686 * @param y row relative to parent
2687 * @param width number of text cells for width of the image
2688 * @param height number of text cells for height of the image
2689 * @param image the image to display
2690 * @param left left column of the image. 0 is the left-most column.
2691 * @param top top row of the image. 0 is the top-most row.
2692 * @param clickAction function to call when mouse is pressed
2694 public final TImage
addImage(final int x
, final int y
,
2695 final int width
, final int height
,
2696 final BufferedImage image
, final int left
, final int top
,
2697 final TAction clickAction
) {
2699 return new TImage(this, x
, y
, width
, height
, image
, left
, top
,
2704 * Convenience function to add an editable 2D data table to this
2707 * @param x column relative to parent
2708 * @param y row relative to parent
2709 * @param width width of widget
2710 * @param height height of widget
2712 public TTableWidget
addTable(final int x
, final int y
, final int width
,
2715 return new TTableWidget(this, x
, y
, width
, height
);
2719 * Convenience function to add an editable 2D data table to this
2722 * @param x column relative to parent
2723 * @param y row relative to parent
2724 * @param width width of widget
2725 * @param height height of widget
2726 * @param gridColumns number of columns in grid
2727 * @param gridRows number of rows in grid
2729 public TTableWidget
addTable(final int x
, final int y
, final int width
,
2730 final int height
, final int gridColumns
, final int gridRows
) {
2732 return new TTableWidget(this, x
, y
, width
, height
, gridColumns
,
2737 * Convenience function to add a panel to this container/window.
2739 * @param x column relative to parent
2740 * @param y row relative to parent
2741 * @param width width of text area
2742 * @param height height of text area
2743 * @return the new panel
2745 public final TPanel
addPanel(final int x
, final int y
, final int width
,
2748 return new TPanel(this, x
, y
, width
, height
);
2752 * Convenience function to add a split pane to this container/window.
2754 * @param x column relative to parent
2755 * @param y row relative to parent
2756 * @param width width of text area
2757 * @param height height of text area
2758 * @param vertical if true, split vertically
2759 * @return the new split pane
2761 public final TSplitPane
addSplitPane(final int x
, final int y
,
2762 final int width
, final int height
, final boolean vertical
) {
2764 return new TSplitPane(this, x
, y
, width
, height
, vertical
);