- public TApplication(final InputStream input,
- final OutputStream output) throws UnsupportedEncodingException {
-
- backend = new ECMA48Backend(this, input, output);
- TApplicationImpl();
- }
-
- /**
- * Public constructor. The backend type will be BackendType.ECMA48.
- *
- * @param input the InputStream underlying 'reader'. Its available()
- * method is used to determine if reader.read() will block or not.
- * @param reader a Reader connected to the remote user.
- * @param writer a PrintWriter connected to the remote user.
- * @param setRawMode if true, set System.in into raw mode with stty.
- * This should in general not be used. It is here solely for Demo3,
- * which uses System.in.
- * @throws IllegalArgumentException if input, reader, or writer are null.
- */
- public TApplication(final InputStream input, final Reader reader,
- final PrintWriter writer, final boolean setRawMode) {
-
- backend = new ECMA48Backend(this, input, reader, writer, setRawMode);
- TApplicationImpl();
- }
-
- /**
- * Public constructor. The backend type will be BackendType.ECMA48.
- *
- * @param input the InputStream underlying 'reader'. Its available()
- * method is used to determine if reader.read() will block or not.
- * @param reader a Reader connected to the remote user.
- * @param writer a PrintWriter connected to the remote user.
- * @throws IllegalArgumentException if input, reader, or writer are null.
- */
- public TApplication(final InputStream input, final Reader reader,
- final PrintWriter writer) {
-
- this(input, reader, writer, false);
- }
-
- /**
- * Public constructor. This hook enables use with new non-Jexer
- * backends.
- *
- * @param backend a Backend that is already ready to go.
- */
- public TApplication(final Backend backend) {
- this.backend = backend;
- backend.setListener(this);
- TApplicationImpl();
- }
-
- /**
- * Finish construction once the backend is set.
- */
- private void TApplicationImpl() {
- theme = new ColorTheme();
- desktopBottom = getScreen().getHeight() - 1;
- fillEventQueue = new ArrayList<TInputEvent>();
- drainEventQueue = new ArrayList<TInputEvent>();
- windows = new LinkedList<TWindow>();
- menus = new LinkedList<TMenu>();
- subMenus = new LinkedList<TMenu>();
- timers = new LinkedList<TTimer>();
- accelerators = new HashMap<TKeypress, TMenuItem>();
- menuItems = new ArrayList<TMenuItem>();
- desktop = new TDesktop(this);
-
- // Special case: the Swing backend needs to have a timer to drive its
- // blink state.
- if ((backend instanceof SwingBackend)
- || (backend instanceof MultiBackend)
- ) {
- // Default to 500 millis, unless a SwingBackend has its own
- // value.
- long millis = 500;
- if (backend instanceof SwingBackend) {
- millis = ((SwingBackend) backend).getBlinkMillis();
- }
- if (millis > 0) {
- addTimer(millis, true,
- new TAction() {
- public void DO() {
- TApplication.this.doRepaint();
- }
- }
- );
- }
- }
- }
-
- // ------------------------------------------------------------------------
- // Screen refresh loop ----------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * Process background events, and update the screen.
- */
- private void finishEventProcessing() {
- if (debugThreads) {
- System.err.printf(System.currentTimeMillis() + " " +
- Thread.currentThread() + " finishEventProcessing()\n");
- }
-
- // Process timers and call doIdle()'s
- doIdle();
-
- // Update the screen
- synchronized (getScreen()) {
- drawAll();
- }
-
- if (debugThreads) {
- System.err.printf(System.currentTimeMillis() + " " +
- Thread.currentThread() + " finishEventProcessing() END\n");
- }
- }
-
- /**
- * Invert the cell color at a position. This is used to track the mouse.
- *
- * @param x column position
- * @param y row position
- */
- private void invertCell(final int x, final int y) {
- if (debugThreads) {
- System.err.printf("%d %s invertCell() %d %d\n",
- System.currentTimeMillis(), Thread.currentThread(), x, y);
- }
- CellAttributes attr = getScreen().getAttrXY(x, y);
- attr.setForeColor(attr.getForeColor().invert());
- attr.setBackColor(attr.getBackColor().invert());
- getScreen().putAttrXY(x, y, attr, false);
- }
-
- /**
- * Draw everything.
- */
- private void drawAll() {
- if (debugThreads) {
- System.err.printf("%d %s drawAll() enter\n",
- System.currentTimeMillis(), Thread.currentThread());
- }
-
- if (!repaint) {
- if (debugThreads) {
- System.err.printf("%d %s drawAll() !repaint\n",
- System.currentTimeMillis(), Thread.currentThread());
- }
- synchronized (getScreen()) {
- if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) {
- // The only thing that has happened is the mouse moved.
- // Clear the old position and draw the new position.
- invertCell(oldMouseX, oldMouseY);
- invertCell(mouseX, mouseY);
- oldMouseX = mouseX;
- oldMouseY = mouseY;
- }
- if (getScreen().isDirty()) {
- backend.flushScreen();
- }
- return;
- }
- }
-
- if (debugThreads) {
- System.err.printf("%d %s drawAll() REDRAW\n",
- System.currentTimeMillis(), Thread.currentThread());
- }
-
- // If true, the cursor is not visible
- boolean cursor = false;
-
- // Start with a clean screen
- getScreen().clear();
-
- // Draw the desktop
- if (desktop != null) {
- desktop.drawChildren();
- }
-
- // Draw each window in reverse Z order
- List<TWindow> sorted = new LinkedList<TWindow>(windows);
- Collections.sort(sorted);
- TWindow topLevel = null;
- if (sorted.size() > 0) {
- topLevel = sorted.get(0);
- }
- Collections.reverse(sorted);
- for (TWindow window: sorted) {
- if (window.isShown()) {
- window.drawChildren();
- }
- }
-
- // Draw the blank menubar line - reset the screen clipping first so
- // it won't trim it out.
- getScreen().resetClipping();
- getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
- theme.getColor("tmenu"));
- // Now draw the menus.
- int x = 1;
- for (TMenu menu: menus) {
- CellAttributes menuColor;
- CellAttributes menuMnemonicColor;
- if (menu.isActive()) {
- menuColor = theme.getColor("tmenu.highlighted");
- menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted");
- topLevel = menu;
- } else {
- menuColor = theme.getColor("tmenu");
- menuMnemonicColor = theme.getColor("tmenu.mnemonic");
- }
- // Draw the menu title
- getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ',
- menuColor);
- getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor);
- // Draw the highlight character
- getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(),
- 0, menu.getMnemonic().getShortcut(), menuMnemonicColor);
-
- if (menu.isActive()) {
- menu.drawChildren();
- // Reset the screen clipping so we can draw the next title.
- getScreen().resetClipping();
- }
- x += menu.getTitle().length() + 2;
- }
-
- for (TMenu menu: subMenus) {
- // Reset the screen clipping so we can draw the next sub-menu.
- getScreen().resetClipping();
- menu.drawChildren();
- }
-
- // Draw the status bar of the top-level window
- TStatusBar statusBar = null;
- if (topLevel != null) {
- statusBar = topLevel.getStatusBar();
- }
- if (statusBar != null) {
- getScreen().resetClipping();
- statusBar.setWidth(getScreen().getWidth());
- statusBar.setY(getScreen().getHeight() - topLevel.getY());
- statusBar.draw();
- } else {
- CellAttributes barColor = new CellAttributes();
- barColor.setTo(getTheme().getColor("tstatusbar.text"));
- getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), ' ',
- barColor);
- }
-
- // Draw the mouse pointer
- invertCell(mouseX, mouseY);
- oldMouseX = mouseX;
- oldMouseY = mouseY;
-
- // Place the cursor if it is visible
- TWidget activeWidget = null;
- if (sorted.size() > 0) {
- activeWidget = sorted.get(sorted.size() - 1).getActiveChild();
- if (activeWidget.isCursorVisible()) {
- if ((activeWidget.getCursorAbsoluteY() < desktopBottom)
- && (activeWidget.getCursorAbsoluteY() > desktopTop)
- ) {
- getScreen().putCursor(true,
- activeWidget.getCursorAbsoluteX(),
- activeWidget.getCursorAbsoluteY());
- cursor = true;
- } else {
- getScreen().putCursor(false,
- activeWidget.getCursorAbsoluteX(),
- activeWidget.getCursorAbsoluteY());
- cursor = false;
- }
- }
- }
-
- // Kill the cursor
- if (!cursor) {
- getScreen().hideCursor();
- }
-
- // Flush the screen contents
- if (getScreen().isDirty()) {
- backend.flushScreen();
- }
-
- repaint = false;
- }
-
- // ------------------------------------------------------------------------
- // Main loop --------------------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * Force this application to exit.
- */
- public void exit() {
- quit = true;
- synchronized (this) {
- this.notify();
- }
- }
-
- /**
- * Run this application until it exits.
- */
- public void run() {
- // Start the main consumer thread
- primaryEventHandler = new WidgetEventHandler(this, true);
- (new Thread(primaryEventHandler)).start();
-
- while (!quit) {
- synchronized (this) {
- boolean doWait = false;
-
- synchronized (fillEventQueue) {
- if (fillEventQueue.size() == 0) {
- doWait = true;
- }
- }
-
- if (doWait) {
- // No I/O to dispatch, so wait until the backend
- // provides new I/O.
- try {
- if (debugThreads) {
- System.err.println(System.currentTimeMillis() +
- " MAIN sleep");
- }
-
- this.wait();
-
- if (debugThreads) {
- System.err.println(System.currentTimeMillis() +
- " MAIN AWAKE");
- }
- } catch (InterruptedException e) {
- // I'm awake and don't care why, let's see what's
- // going on out there.
- }
- }
-
- } // synchronized (this)
-
- synchronized (fillEventQueue) {
- // Pull any pending I/O events
- backend.getEvents(fillEventQueue);
-
- // Dispatch each event to the appropriate handler, one at a
- // time.
- for (;;) {
- TInputEvent event = null;
- if (fillEventQueue.size() == 0) {
- break;
- }
- event = fillEventQueue.remove(0);
- metaHandleEvent(event);
- }
- }
-
- // Wake a consumer thread if we have any pending events.
- if (drainEventQueue.size() > 0) {
- wakeEventHandler();
- }
-
- } // while (!quit)
-
- // Shutdown the event consumer threads
- if (secondaryEventHandler != null) {
- synchronized (secondaryEventHandler) {
- secondaryEventHandler.notify();
- }
- }
- if (primaryEventHandler != null) {
- synchronized (primaryEventHandler) {
- primaryEventHandler.notify();
- }
- }
-
- // Shutdown the user I/O thread(s)
- backend.shutdown();
-
- // Close all the windows. This gives them an opportunity to release
- // resources.
- closeAllWindows();
-
- }
-
- /**
- * Peek at certain application-level events, add to eventQueue, and wake
- * up the consuming Thread.
- *
- * @param event the input event to consume
- */
- private void metaHandleEvent(final TInputEvent event) {
-
- if (debugEvents) {
- System.err.printf(String.format("metaHandleEvents event: %s\n",
- event)); System.err.flush();
- }
-
- if (quit) {
- // Do no more processing if the application is already trying
- // to exit.
- return;
- }
-
- // Special application-wide events -------------------------------
-
- // Abort everything
- if (event instanceof TCommandEvent) {
- TCommandEvent command = (TCommandEvent) event;
- if (command.getCmd().equals(cmAbort)) {
- exit();
- return;
- }
- }
-
- synchronized (drainEventQueue) {
- // Screen resize
- if (event instanceof TResizeEvent) {
- TResizeEvent resize = (TResizeEvent) event;
- synchronized (getScreen()) {
- getScreen().setDimensions(resize.getWidth(),
- resize.getHeight());
- desktopBottom = getScreen().getHeight() - 1;
- mouseX = 0;
- mouseY = 0;
- oldMouseX = 0;
- oldMouseY = 0;
- }
- if (desktop != null) {
- desktop.setDimensions(0, 0, resize.getWidth(),
- resize.getHeight() - 1);
- }
- return;
- }
-
- // Put into the main queue
- drainEventQueue.add(event);
- }
- }
-
- /**
- * Dispatch one event to the appropriate widget or application-level
- * event handler. This is the primary event handler, it has the normal
- * application-wide event handling.
- *
- * @param event the input event to consume
- * @see #secondaryHandleEvent(TInputEvent event)
- */
- private void primaryHandleEvent(final TInputEvent event) {