+ /**
+ * Re-layout the open windows as overlapping cascaded windows.
+ */
+ private void cascadeWindows() {
+ synchronized (windows) {
+ // Don't do anything if we are in the menu
+ if (activeMenu != null) {
+ return;
+ }
+ int x = 0;
+ int y = 1;
+ List<TWindow> sorted = new LinkedList<TWindow>(windows);
+ Collections.sort(sorted);
+ Collections.reverse(sorted);
+ for (TWindow window: sorted) {
+ window.setX(x);
+ window.setY(y);
+ x++;
+ y++;
+ if (x > getScreen().getWidth()) {
+ x = 0;
+ }
+ if (y >= getScreen().getHeight()) {
+ y = 1;
+ }
+ }
+ }
+ }
+
+ /**
+ * Place a window to minimize its overlap with other windows.
+ *
+ * @param window the window to place
+ */
+ public final void doSmartPlacement(final TWindow window) {
+ // This is a pretty dumb algorithm, but seems to work. The hardest
+ // part is computing these "overlap" values seeking a minimum average
+ // overlap.
+ int xMin = 0;
+ int yMin = desktopTop;
+ int xMax = getScreen().getWidth() - window.getWidth() + 1;
+ int yMax = desktopBottom - window.getHeight() + 1;
+ if (xMax < xMin) {
+ xMax = xMin;
+ }
+ if (yMax < yMin) {
+ yMax = yMin;
+ }
+
+ if ((xMin == xMax) && (yMin == yMax)) {
+ // No work to do, bail out.
+ return;
+ }
+
+ // Compute the overlap matrix without the new window.
+ int width = getScreen().getWidth();
+ int height = getScreen().getHeight();
+ int overlapMatrix[][] = new int[width][height];
+ for (TWindow w: windows) {
+ if (window == w) {
+ continue;
+ }
+ for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) {
+ if (x >= width) {
+ continue;
+ }
+ for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) {
+ if (y >= height) {
+ continue;
+ }
+ overlapMatrix[x][y]++;
+ }
+ }
+ }
+
+ long oldOverlapTotal = 0;
+ long oldOverlapN = 0;
+ for (int x = 0; x < width; x++) {
+ for (int y = 0; y < height; y++) {
+ oldOverlapTotal += overlapMatrix[x][y];
+ if (overlapMatrix[x][y] > 0) {
+ oldOverlapN++;
+ }
+ }
+ }
+
+
+ double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN;
+ boolean first = true;
+ int windowX = window.getX();
+ int windowY = window.getY();
+
+ // For each possible (x, y) position for the new window, compute a
+ // new overlap matrix.
+ for (int x = xMin; x < xMax; x++) {
+ for (int y = yMin; y < yMax; y++) {
+
+ // Start with the matrix minus this window.
+ int newMatrix[][] = new int[width][height];
+ for (int mx = 0; mx < width; mx++) {
+ for (int my = 0; my < height; my++) {
+ newMatrix[mx][my] = overlapMatrix[mx][my];
+ }
+ }
+
+ // Add this window's values to the new overlap matrix.
+ long newOverlapTotal = 0;
+ long newOverlapN = 0;
+ // Start by adding each new cell.
+ for (int wx = x; wx < x + window.getWidth(); wx++) {
+ if (wx >= width) {
+ continue;
+ }
+ for (int wy = y; wy < y + window.getHeight(); wy++) {
+ if (wy >= height) {
+ continue;
+ }
+ newMatrix[wx][wy]++;
+ }
+ }
+ // Now figure out the new value for total coverage.
+ for (int mx = 0; mx < width; mx++) {
+ for (int my = 0; my < height; my++) {
+ newOverlapTotal += newMatrix[x][y];
+ if (newMatrix[mx][my] > 0) {
+ newOverlapN++;
+ }
+ }
+ }
+ double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN;
+
+ if (first) {
+ // First time: just record what we got.
+ oldOverlapAvg = newOverlapAvg;
+ first = false;
+ } else {
+ // All other times: pick a new best (x, y) and save the
+ // overlap value.
+ if (newOverlapAvg < oldOverlapAvg) {
+ windowX = x;
+ windowY = y;
+ oldOverlapAvg = newOverlapAvg;
+ }
+ }
+
+ } // for (int x = xMin; x < xMax; x++)
+
+ } // for (int y = yMin; y < yMax; y++)
+
+ // Finally, set the window's new coordinates.
+ window.setX(windowX);
+ window.setY(windowY);
+ }
+
+ // ------------------------------------------------------------------------
+ // TMenu management -------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Check if a mouse event would hit either the active menu or any open
+ * sub-menus.
+ *
+ * @param mouse mouse event
+ * @return true if the mouse would hit the active menu or an open
+ * sub-menu
+ */
+ private boolean mouseOnMenu(final TMouseEvent mouse) {
+ assert (activeMenu != null);
+ List<TMenu> menus = new LinkedList<TMenu>(subMenus);
+ Collections.reverse(menus);
+ for (TMenu menu: menus) {
+ if (menu.mouseWouldHit(mouse)) {
+ return true;
+ }
+ }
+ return activeMenu.mouseWouldHit(mouse);
+ }
+
+ /**
+ * See if we need to switch window or activate the menu based on
+ * a mouse click.
+ *
+ * @param mouse mouse event
+ */
+ private void checkSwitchFocus(final TMouseEvent mouse) {
+
+ if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
+ && (activeMenu != null)
+ && (mouse.getAbsoluteY() != 0)
+ && (!mouseOnMenu(mouse))
+ ) {
+ // They clicked outside the active menu, turn it off
+ activeMenu.setActive(false);
+ activeMenu = null;
+ for (TMenu menu: subMenus) {
+ menu.setActive(false);
+ }
+ subMenus.clear();
+ // Continue checks
+ }
+
+ // See if they hit the menu bar
+ if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
+ && (mouse.isMouse1())
+ && (!modalWindowActive())
+ && (mouse.getAbsoluteY() == 0)
+ ) {
+
+ for (TMenu menu: subMenus) {
+ menu.setActive(false);
+ }
+ subMenus.clear();
+
+ // They selected the menu, go activate it
+ for (TMenu menu: menus) {
+ if ((mouse.getAbsoluteX() >= menu.getTitleX())
+ && (mouse.getAbsoluteX() < menu.getTitleX()
+ + menu.getTitle().length() + 2)
+ ) {
+ menu.setActive(true);
+ activeMenu = menu;
+ } else {
+ menu.setActive(false);
+ }
+ }
+ return;
+ }
+
+ // See if they hit the menu bar
+ if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)
+ && (mouse.isMouse1())
+ && (activeMenu != null)
+ && (mouse.getAbsoluteY() == 0)
+ ) {
+
+ TMenu oldMenu = activeMenu;
+ for (TMenu menu: subMenus) {
+ menu.setActive(false);
+ }
+ subMenus.clear();
+
+ // See if we should switch menus
+ for (TMenu menu: menus) {
+ if ((mouse.getAbsoluteX() >= menu.getTitleX())
+ && (mouse.getAbsoluteX() < menu.getTitleX()
+ + menu.getTitle().length() + 2)
+ ) {
+ menu.setActive(true);
+ activeMenu = menu;
+ }
+ }
+ if (oldMenu != activeMenu) {
+ // They switched menus
+ oldMenu.setActive(false);
+ }
+ return;
+ }
+
+ // If a menu is still active, don't switch windows
+ if (activeMenu != null) {
+ return;
+ }
+
+ // Only switch if there are multiple windows
+ if (windows.size() < 2) {
+ return;
+ }
+
+ if (((focusFollowsMouse == true)
+ && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION))
+ || (mouse.getType() == TMouseEvent.Type.MOUSE_UP)
+ ) {
+ synchronized (windows) {
+ Collections.sort(windows);
+ if (windows.get(0).isModal()) {
+ // Modal windows don't switch
+ return;
+ }
+
+ for (TWindow window: windows) {
+ assert (!window.isModal());
+
+ if (window.isHidden()) {
+ assert (!window.isActive());
+ continue;
+ }
+
+ if (window.mouseWouldHit(mouse)) {
+ if (window == windows.get(0)) {
+ // Clicked on the same window, nothing to do
+ assert (window.isActive());
+ return;
+ }
+
+ // We will be switching to another window
+ assert (windows.get(0).isActive());
+ assert (windows.get(0) == activeWindow);
+ assert (!window.isActive());
+ activeWindow.onUnfocus();
+ activeWindow.setActive(false);
+ activeWindow.setZ(window.getZ());
+ activeWindow = window;
+ window.setZ(0);
+ window.setActive(true);
+ window.onFocus();
+ return;
+ }
+ }
+ }
+
+ // Clicked on the background, nothing to do
+ return;
+ }
+
+ // Nothing to do: this isn't a mouse up, or focus isn't following
+ // mouse.
+ return;
+ }
+
+ /**
+ * Turn off the menu.
+ */
+ public final void closeMenu() {
+ if (activeMenu != null) {
+ activeMenu.setActive(false);
+ activeMenu = null;
+ for (TMenu menu: subMenus) {
+ menu.setActive(false);
+ }
+ subMenus.clear();
+ }
+ }
+
+ /**
+ * Get a (shallow) copy of the menu list.
+ *
+ * @return a copy of the menu list
+ */
+ public final List<TMenu> getAllMenus() {
+ return new LinkedList<TMenu>(menus);
+ }
+
+ /**
+ * Add a top-level menu to the list.
+ *
+ * @param menu the menu to add
+ * @throws IllegalArgumentException if the menu is already used in
+ * another TApplication
+ */
+ public final void addMenu(final TMenu menu) {
+ if ((menu.getApplication() != null)
+ && (menu.getApplication() != this)
+ ) {
+ throw new IllegalArgumentException("Menu " + menu + " is already " +
+ "part of application " + menu.getApplication());
+ }
+ closeMenu();
+ menus.add(menu);
+ recomputeMenuX();
+ }
+
+ /**
+ * Remove a top-level menu from the list.
+ *
+ * @param menu the menu to remove
+ * @throws IllegalArgumentException if the menu is already used in
+ * another TApplication
+ */
+ public final void removeMenu(final TMenu menu) {
+ if ((menu.getApplication() != null)
+ && (menu.getApplication() != this)
+ ) {
+ throw new IllegalArgumentException("Menu " + menu + " is already " +
+ "part of application " + menu.getApplication());
+ }
+ closeMenu();
+ menus.remove(menu);
+ recomputeMenuX();
+ }
+
+ /**
+ * Turn off a sub-menu.
+ */
+ public final void closeSubMenu() {
+ assert (activeMenu != null);
+ TMenu item = subMenus.get(subMenus.size() - 1);
+ assert (item != null);
+ item.setActive(false);
+ subMenus.remove(subMenus.size() - 1);
+ }
+
+ /**
+ * Switch to the next menu.
+ *
+ * @param forward if true, then switch to the next menu in the list,
+ * otherwise switch to the previous menu in the list
+ */
+ public final void switchMenu(final boolean forward) {
+ assert (activeMenu != null);
+
+ for (TMenu menu: subMenus) {
+ menu.setActive(false);
+ }
+ subMenus.clear();
+
+ for (int i = 0; i < menus.size(); i++) {
+ if (activeMenu == menus.get(i)) {
+ if (forward) {
+ if (i < menus.size() - 1) {
+ i++;
+ }
+ } else {
+ if (i > 0) {
+ i--;
+ }
+ }
+ activeMenu.setActive(false);
+ activeMenu = menus.get(i);
+ activeMenu.setActive(true);
+ return;
+ }
+ }
+ }
+
+ /**
+ * Add a menu item to the global list. If it has a keyboard accelerator,
+ * that will be added the global hash.
+ *
+ * @param item the menu item
+ */
+ public final void addMenuItem(final TMenuItem item) {
+ menuItems.add(item);
+
+ TKeypress key = item.getKey();
+ if (key != null) {
+ synchronized (accelerators) {
+ assert (accelerators.get(key) == null);
+ accelerators.put(key.toLowerCase(), item);
+ }
+ }
+ }
+
+ /**
+ * Disable one menu item.
+ *
+ * @param id the menu item ID
+ */
+ public final void disableMenuItem(final int id) {
+ for (TMenuItem item: menuItems) {
+ if (item.getId() == id) {
+ item.setEnabled(false);
+ }
+ }
+ }
+
+ /**
+ * Disable the range of menu items with ID's between lower and upper,
+ * inclusive.
+ *
+ * @param lower the lowest menu item ID
+ * @param upper the highest menu item ID
+ */
+ public final void disableMenuItems(final int lower, final int upper) {
+ for (TMenuItem item: menuItems) {
+ if ((item.getId() >= lower) && (item.getId() <= upper)) {
+ item.setEnabled(false);
+ }
+ }
+ }
+
+ /**
+ * Enable one menu item.
+ *
+ * @param id the menu item ID
+ */
+ public final void enableMenuItem(final int id) {
+ for (TMenuItem item: menuItems) {
+ if (item.getId() == id) {
+ item.setEnabled(true);
+ }
+ }
+ }
+
+ /**
+ * Enable the range of menu items with ID's between lower and upper,
+ * inclusive.
+ *
+ * @param lower the lowest menu item ID
+ * @param upper the highest menu item ID
+ */
+ public final void enableMenuItems(final int lower, final int upper) {
+ for (TMenuItem item: menuItems) {
+ if ((item.getId() >= lower) && (item.getId() <= upper)) {
+ item.setEnabled(true);
+ }
+ }
+ }
+
+ /**
+ * Recompute menu x positions based on their title length.
+ */
+ public final void recomputeMenuX() {
+ int x = 0;
+ for (TMenu menu: menus) {
+ menu.setX(x);
+ menu.setTitleX(x);
+ x += menu.getTitle().length() + 2;
+
+ // Don't let the menu window exceed the screen width
+ int rightEdge = menu.getX() + menu.getWidth();
+ if (rightEdge > getScreen().getWidth()) {
+ menu.setX(getScreen().getWidth() - menu.getWidth());
+ }
+ }
+ }
+
+ /**
+ * Post an event to process and turn off the menu.
+ *
+ * @param event new event to add to the queue
+ */
+ public final void postMenuEvent(final TInputEvent event) {
+ synchronized (this) {
+ synchronized (fillEventQueue) {
+ fillEventQueue.add(event);
+ }
+ if (debugThreads) {
+ System.err.println(System.currentTimeMillis() + " " +
+ Thread.currentThread() + " postMenuEvent() wake up main");
+ }
+ closeMenu();
+ this.notify();
+ }
+ }
+
+ /**
+ * Add a sub-menu to the list of open sub-menus.
+ *
+ * @param menu sub-menu
+ */
+ public final void addSubMenu(final TMenu menu) {
+ subMenus.add(menu);
+ }
+
+ /**
+ * Convenience function to add a top-level menu.
+ *
+ * @param title menu title
+ * @return the new menu
+ */
+ public final TMenu addMenu(final String title) {
+ int x = 0;
+ int y = 0;
+ TMenu menu = new TMenu(this, x, y, title);
+ menus.add(menu);
+ recomputeMenuX();
+ return menu;
+ }
+
+ /**
+ * Convenience function to add a default "File" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addFileMenu() {
+ TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle"));
+ fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE);
+ fileMenu.addSeparator();
+ fileMenu.addDefaultItem(TMenu.MID_SHELL);
+ fileMenu.addDefaultItem(TMenu.MID_EXIT);
+ TStatusBar statusBar = fileMenu.newStatusBar(i18n.
+ getString("fileMenuStatus"));
+ statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+ return fileMenu;
+ }
+
+ /**
+ * Convenience function to add a default "Edit" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addEditMenu() {
+ TMenu editMenu = addMenu(i18n.getString("editMenuTitle"));
+ editMenu.addDefaultItem(TMenu.MID_CUT);
+ editMenu.addDefaultItem(TMenu.MID_COPY);
+ editMenu.addDefaultItem(TMenu.MID_PASTE);
+ editMenu.addDefaultItem(TMenu.MID_CLEAR);
+ TStatusBar statusBar = editMenu.newStatusBar(i18n.
+ getString("editMenuStatus"));
+ statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+ return editMenu;
+ }
+
+ /**
+ * Convenience function to add a default "Window" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addWindowMenu() {
+ TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle"));
+ windowMenu.addDefaultItem(TMenu.MID_TILE);
+ windowMenu.addDefaultItem(TMenu.MID_CASCADE);
+ windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL);
+ windowMenu.addSeparator();
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE);
+ TStatusBar statusBar = windowMenu.newStatusBar(i18n.
+ getString("windowMenuStatus"));
+ statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+ return windowMenu;
+ }
+
+ /**
+ * Convenience function to add a default "Help" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addHelpMenu() {
+ TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle"));
+ helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS);
+ helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX);
+ helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH);
+ helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS);
+ helpMenu.addDefaultItem(TMenu.MID_HELP_HELP);
+ helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE);
+ helpMenu.addSeparator();
+ helpMenu.addDefaultItem(TMenu.MID_ABOUT);
+ TStatusBar statusBar = helpMenu.newStatusBar(i18n.
+ getString("helpMenuStatus"));
+ statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+ return helpMenu;
+ }
+
+ // ------------------------------------------------------------------------
+ // Event handlers ---------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Method that TApplication subclasses can override to handle menu or
+ * posted command events.
+ *
+ * @param command command event
+ * @return if true, this event was consumed
+ */
+ protected boolean onCommand(final TCommandEvent command) {
+ // Default: handle cmExit
+ if (command.equals(cmExit)) {
+ if (messageBox(i18n.getString("exitDialogTitle"),
+ i18n.getString("exitDialogText"),
+ TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
+ exit();
+ }
+ return true;
+ }
+
+ if (command.equals(cmShell)) {
+ openTerminal(0, 0, TWindow.RESIZABLE);
+ return true;
+ }
+
+ if (command.equals(cmTile)) {
+ tileWindows();
+ return true;
+ }
+ if (command.equals(cmCascade)) {
+ cascadeWindows();
+ return true;
+ }
+ if (command.equals(cmCloseAll)) {
+ closeAllWindows();
+ return true;
+ }
+
+ if (command.equals(cmMenu)) {
+ if (!modalWindowActive() && (activeMenu == null)) {
+ if (menus.size() > 0) {
+ menus.get(0).setActive(true);
+ activeMenu = menus.get(0);
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Method that TApplication subclasses can override to handle menu
+ * events.
+ *
+ * @param menu menu event
+ * @return if true, this event was consumed
+ */
+ protected boolean onMenu(final TMenuEvent menu) {
+
+ // Default: handle MID_EXIT
+ if (menu.getId() == TMenu.MID_EXIT) {
+ if (messageBox(i18n.getString("exitDialogTitle"),
+ i18n.getString("exitDialogText"),
+ TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
+ exit();
+ }
+ return true;
+ }
+
+ if (menu.getId() == TMenu.MID_SHELL) {
+ openTerminal(0, 0, TWindow.RESIZABLE);
+ return true;
+ }
+
+ if (menu.getId() == TMenu.MID_TILE) {
+ tileWindows();
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_CASCADE) {
+ cascadeWindows();
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_CLOSE_ALL) {
+ closeAllWindows();
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_ABOUT) {
+ showAboutDialog();
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_REPAINT) {
+ doRepaint();
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Method that TApplication subclasses can override to handle keystrokes.
+ *
+ * @param keypress keystroke event
+ * @return if true, this event was consumed
+ */
+ protected boolean onKeypress(final TKeypressEvent keypress) {
+ // Default: only menu shortcuts
+
+ // Process Alt-F, Alt-E, etc. menu shortcut keys
+ if (!keypress.getKey().isFnKey()
+ && keypress.getKey().isAlt()
+ && !keypress.getKey().isCtrl()
+ && (activeMenu == null)
+ && !modalWindowActive()
+ ) {
+
+ assert (subMenus.size() == 0);
+
+ for (TMenu menu: menus) {
+ if (Character.toLowerCase(menu.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getChar())
+ ) {
+ activeMenu = menu;
+ menu.setActive(true);
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ // ------------------------------------------------------------------------
+ // TTimer management ------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the amount of time I can sleep before missing a Timer tick.
+ *
+ * @param timeout = initial (maximum) timeout in millis
+ * @return number of milliseconds between now and the next timer event
+ */
+ private long getSleepTime(final long timeout) {
+ Date now = new Date();
+ long nowTime = now.getTime();
+ long sleepTime = timeout;
+
+ synchronized (timers) {
+ for (TTimer timer: timers) {
+ long nextTickTime = timer.getNextTick().getTime();
+ if (nextTickTime < nowTime) {
+ return 0;
+ }
+
+ long timeDifference = nextTickTime - nowTime;
+ if (timeDifference < sleepTime) {
+ sleepTime = timeDifference;
+ }
+ }
+ }
+
+ assert (sleepTime >= 0);
+ assert (sleepTime <= timeout);
+ return sleepTime;
+ }
+
+ /**
+ * Convenience function to add a timer.
+ *
+ * @param duration number of milliseconds to wait between ticks
+ * @param recurring if true, re-schedule this timer after every tick
+ * @param action function to call when button is pressed
+ * @return the timer
+ */
+ public final TTimer addTimer(final long duration, final boolean recurring,
+ final TAction action) {
+
+ TTimer timer = new TTimer(duration, recurring, action);
+ synchronized (timers) {
+ timers.add(timer);
+ }
+ return timer;
+ }
+
+ /**
+ * Convenience function to remove a timer.
+ *
+ * @param timer timer to remove
+ */
+ public final void removeTimer(final TTimer timer) {
+ synchronized (timers) {
+ timers.remove(timer);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ // Other TWindow constructors ---------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Convenience function to spawn a message box.
+ *
+ * @param title window title, will be centered along the top border
+ * @param caption message to display. Use embedded newlines to get a
+ * multi-line box.
+ * @return the new message box
+ */
+ public final TMessageBox messageBox(final String title,
+ final String caption) {
+
+ return new TMessageBox(this, title, caption, TMessageBox.Type.OK);
+ }
+
+ /**
+ * Convenience function to spawn a message box.
+ *
+ * @param title window title, will be centered along the top border
+ * @param caption message to display. Use embedded newlines to get a
+ * multi-line box.
+ * @param type one of the TMessageBox.Type constants. Default is
+ * Type.OK.
+ * @return the new message box
+ */
+ public final TMessageBox messageBox(final String title,
+ final String caption, final TMessageBox.Type type) {
+
+ return new TMessageBox(this, title, caption, type);
+ }
+
+ /**
+ * Convenience function to spawn an input box.
+ *
+ * @param title window title, will be centered along the top border
+ * @param caption message to display. Use embedded newlines to get a
+ * multi-line box.
+ * @return the new input box
+ */
+ public final TInputBox inputBox(final String title, final String caption) {
+
+ return new TInputBox(this, title, caption);
+ }
+
+ /**
+ * Convenience function to spawn an input box.
+ *
+ * @param title window title, will be centered along the top border
+ * @param caption message to display. Use embedded newlines to get a
+ * multi-line box.
+ * @param text initial text to seed the field with
+ * @return the new input box
+ */
+ public final TInputBox inputBox(final String title, final String caption,
+ final String text) {
+
+ return new TInputBox(this, title, caption, text);
+ }
+
+ /**
+ * Convenience function to open a terminal window.
+ *
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @return the terminal new window
+ */
+ public final TTerminalWindow openTerminal(final int x, final int y) {
+ return openTerminal(x, y, TWindow.RESIZABLE);
+ }
+
+ /**
+ * Convenience function to open a terminal window.
+ *
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param flags mask of CENTERED, MODAL, or RESIZABLE
+ * @return the terminal new window
+ */
+ public final TTerminalWindow openTerminal(final int x, final int y,
+ final int flags) {
+
+ return new TTerminalWindow(this, x, y, flags);
+ }
+
+ /**
+ * Convenience function to open a terminal window and execute a custom
+ * command line inside it.
+ *
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param commandLine the command line to execute
+ * @return the terminal new window
+ */
+ public final TTerminalWindow openTerminal(final int x, final int y,
+ final String commandLine) {
+
+ return openTerminal(x, y, TWindow.RESIZABLE, commandLine);
+ }
+
+ /**
+ * Convenience function to open a terminal window and execute a custom
+ * command line inside it.
+ *
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param flags mask of CENTERED, MODAL, or RESIZABLE
+ * @param commandLine the command line to execute
+ * @return the terminal new window
+ */
+ public final TTerminalWindow openTerminal(final int x, final int y,
+ final int flags, final String commandLine) {
+
+ return new TTerminalWindow(this, x, y, flags, commandLine);
+ }
+
+ /**
+ * Convenience function to spawn an file open box.
+ *
+ * @param path path of selected file
+ * @return the result of the new file open box
+ * @throws IOException if java.io operation throws
+ */
+ public final String fileOpenBox(final String path) throws IOException {
+
+ TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN);
+ return box.getFilename();
+ }
+
+ /**
+ * Convenience function to spawn an file open box.
+ *
+ * @param path path of selected file
+ * @param type one of the Type constants
+ * @return the result of the new file open box
+ * @throws IOException if java.io operation throws
+ */
+ public final String fileOpenBox(final String path,
+ final TFileOpenBox.Type type) throws IOException {
+
+ TFileOpenBox box = new TFileOpenBox(this, path, type);
+ return box.getFilename();
+ }
+
+ /**
+ * Convenience function to create a new window and make it active.
+ * Window will be located at (0, 0).
+ *
+ * @param title window title, will be centered along the top border
+ * @param width width of window
+ * @param height height of window
+ */
+ public final TWindow addWindow(final String title, final int width,
+ final int height) {
+
+ TWindow window = new TWindow(this, title, 0, 0, width, height);
+ return window;
+ }
+
+ /**
+ * Convenience function to create a new window and make it active.
+ * Window will be located at (0, 0).
+ *
+ * @param title window title, will be centered along the top border
+ * @param width width of window
+ * @param height height of window
+ * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
+ */
+ public final TWindow addWindow(final String title,
+ final int width, final int height, final int flags) {
+
+ TWindow window = new TWindow(this, title, 0, 0, width, height, flags);
+ return window;
+ }
+
+ /**
+ * Convenience function to create a new window and make it active.
+ *
+ * @param title window title, will be centered along the top border
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param width width of window
+ * @param height height of window
+ */
+ public final TWindow addWindow(final String title,
+ final int x, final int y, final int width, final int height) {
+
+ TWindow window = new TWindow(this, title, x, y, width, height);
+ return window;
+ }
+
+ /**
+ * Convenience function to create a new window and make it active.
+ *
+ * @param title window title, will be centered along the top border
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param width width of window
+ * @param height height of window
+ * @param flags mask of RESIZABLE, CENTERED, or MODAL
+ */
+ public final TWindow addWindow(final String title,
+ final int x, final int y, final int width, final int height,
+ final int flags) {
+
+ TWindow window = new TWindow(this, title, x, y, width, height, flags);
+ return window;
+ }
+
+ /**
+ * Convenience function to open a file in an editor window and make it
+ * active.
+ *
+ * @param file the file to open
+ * @throws IOException if a java.io operation throws
+ */
+ public final TEditorWindow addEditor(final File file) throws IOException {
+
+ TEditorWindow editor = new TEditorWindow(this, file);
+ return editor;
+ }