X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FSwingTerminal.java;h=b20d448a3db2cbf534dabd3960144b40d3642469;hb=24489803a611e99348e26cadedae1141f48c1a6c;hp=c2fbfb437eae6b54f43f4b0a497d703b45f0d950;hpb=42873e30bf487bc0b695d60652dba44f82185dbb;p=fanfix.git diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index c2fbfb4..b20d448 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -36,6 +36,7 @@ import java.awt.Graphics2D; import java.awt.Graphics; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.Toolkit; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.KeyEvent; @@ -50,7 +51,6 @@ import java.awt.event.WindowListener; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.InputStream; -import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -241,11 +241,22 @@ public final class SwingTerminal extends LogicalScreen private CursorStyle cursorStyle = CursorStyle.UNDERLINE; /** - * The number of millis to wait before switching the blink from - * visible to invisible. + * The number of millis to wait before switching the blink from visible + * to invisible. Set to 0 or negative to disable blinking. */ private long blinkMillis = 500; + /** + * Get the number of millis to wait before switching the blink from + * visible to invisible. + * + * @return the number of milli to wait before switching the blink from + * visible to invisible + */ + public long getBlinkMillis() { + return blinkMillis; + } + /** * If true, the cursor should be visible right now based on the blink * time. @@ -500,7 +511,6 @@ public final class SwingTerminal extends LogicalScreen // monospace. textHeight = fm.getMaxAscent() + maxDescent - leading; - // TODO: is this still necessary? if (gotTerminus == true) { textHeight++; } @@ -634,6 +644,8 @@ public final class SwingTerminal extends LogicalScreen private void drawCursor(final Graphics gr) { if (cursorVisible + && (cursorY >= 0) + && (cursorX >= 0) && (cursorY <= height - 1) && (cursorX <= width - 1) && cursorBlinkVisible @@ -658,6 +670,14 @@ public final class SwingTerminal extends LogicalScreen } } + /** + * Reset the blink timer. + */ + private void resetBlinkTimer() { + lastBlinkTime = System.currentTimeMillis(); + cursorBlinkVisible = true; + } + /** * Paint redraws the whole screen. * @@ -667,21 +687,12 @@ public final class SwingTerminal extends LogicalScreen if (gotFontDimensions == false) { // Lazy-load the text width/height - // System.err.println("calling getFontDimensions..."); getFontDimensions(gr); /* System.err.println("textWidth " + textWidth + " textHeight " + textHeight); System.err.println("FONT: " + swing.getFont() + " font " + font); */ - // resizeToScreen(); - } - - // See if it is time to flip the blink time. - long nowTime = (new Date()).getTime(); - if (nowTime > blinkMillis + lastBlinkTime) { - lastBlinkTime = nowTime; - cursorBlinkVisible = !cursorBlinkVisible; } int xCellMin = 0; @@ -751,7 +762,6 @@ public final class SwingTerminal extends LogicalScreen } drawCursor(gr); - dirty = false; reallyCleared = false; } // synchronized (this) } @@ -768,9 +778,39 @@ public final class SwingTerminal extends LogicalScreen */ @Override public void flushPhysical() { + // See if it is time to flip the blink time. + long nowTime = System.currentTimeMillis(); + if (nowTime >= blinkMillis + lastBlinkTime) { + lastBlinkTime = nowTime; + cursorBlinkVisible = !cursorBlinkVisible; + // System.err.println("New lastBlinkTime: " + lastBlinkTime); + } + + if ((swing.getFrame() != null) + && (swing.getBufferStrategy() != null) + ) { + do { + do { + drawToSwing(); + } while (swing.getBufferStrategy().contentsRestored()); + + swing.getBufferStrategy().show(); + Toolkit.getDefaultToolkit().sync(); + } while (swing.getBufferStrategy().contentsLost()); + + } else { + // Non-triple-buffered, call drawToSwing() once + drawToSwing(); + } + } + + /** + * Push the logical screen to the physical device. + */ + private void drawToSwing() { /* - System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n", + System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n", reallyCleared, dirty); */ @@ -784,8 +824,7 @@ public final class SwingTerminal extends LogicalScreen swing.paint(gr); gr.dispose(); swing.getBufferStrategy().show(); - // sync() doesn't seem to help the tearing for me. - // Toolkit.getDefaultToolkit().sync(); + Toolkit.getDefaultToolkit().sync(); return; } else if (((swing.getFrame() != null) && (swing.getBufferStrategy() == null)) @@ -797,19 +836,7 @@ public final class SwingTerminal extends LogicalScreen return; } - // Do nothing if nothing happened. - if (!dirty) { - return; - } - if ((swing.getFrame() != null) && (swing.getBufferStrategy() != null)) { - // See if it is time to flip the blink time. - long nowTime = (new Date()).getTime(); - if (nowTime > blinkMillis + lastBlinkTime) { - lastBlinkTime = nowTime; - cursorBlinkVisible = !cursorBlinkVisible; - } - Graphics gr = swing.getBufferStrategy().getDrawGraphics(); synchronized (this) { @@ -837,8 +864,7 @@ public final class SwingTerminal extends LogicalScreen gr.dispose(); swing.getBufferStrategy().show(); - // sync() doesn't seem to help the tearing for me. - // Toolkit.getDefaultToolkit().sync(); + Toolkit.getDefaultToolkit().sync(); return; } @@ -905,48 +931,13 @@ public final class SwingTerminal extends LogicalScreen swing.paint(gr); gr.dispose(); swing.getBufferStrategy().show(); - // sync() doesn't seem to help the tearing for me. - // Toolkit.getDefaultToolkit().sync(); + Toolkit.getDefaultToolkit().sync(); } else { // Repaint on the Swing thread. swing.repaint(xMin, yMin, xMax - xMin, yMax - yMin); } } - /** - * Put the cursor at (x,y). - * - * @param visible if true, the cursor should be visible - * @param x column coordinate to put the cursor on - * @param y row coordinate to put the cursor on - */ - @Override - public void putCursor(final boolean visible, final int x, final int y) { - - if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) { - // See if it is time to flip the blink time. - long nowTime = (new Date()).getTime(); - if (nowTime < blinkMillis + lastBlinkTime) { - // Nothing has changed, so don't do anything. - return; - } - } - - if (cursorVisible - && (cursorY <= height - 1) - && (cursorX <= width - 1) - ) { - // Make the current cursor position dirty - if (physical[cursorX][cursorY].getChar() == 'Q') { - physical[cursorX][cursorY].setChar('X'); - } else { - physical[cursorX][cursorY].setChar('Q'); - } - } - - super.putCursor(visible, x, y); - } - /** * Convert pixel column position to text cell column position. * @@ -1069,9 +1060,9 @@ public final class SwingTerminal extends LogicalScreen // Pull the system property for triple buffering. if (System.getProperty("jexer.Swing.tripleBuffer") != null) { - if (System.getProperty("jexer.Swing.tripleBuffer"). - equals("false")) { - + if (System.getProperty("jexer.Swing.tripleBuffer").equals("true")) { + SwingComponent.tripleBuffer = true; + } else { SwingComponent.tripleBuffer = false; } } @@ -1142,7 +1133,8 @@ public final class SwingTerminal extends LogicalScreen SwingTerminal.this.sessionInfo = new SwingSessionInfo(SwingTerminal.this.swing, SwingTerminal.this.textWidth, - SwingTerminal.this.textHeight); + SwingTerminal.this.textHeight, + windowWidth, windowHeight); SwingTerminal.this.setDimensions(sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); @@ -1171,7 +1163,7 @@ public final class SwingTerminal extends LogicalScreen } /** - * Public constructor creates a new JFrame to render to. + * Public constructor renders to an existing JComponent. * * @param component the Swing component to render to * @param windowWidth the number of text columns to start with @@ -1251,6 +1243,9 @@ public final class SwingTerminal extends LogicalScreen component.setLayout(new BorderLayout()); component.add(newComponent); + // Allow key events to be received + component.setFocusable(true); + // Get the Swing component SwingTerminal.this.swing = new SwingComponent(component); @@ -1539,9 +1534,12 @@ public final class SwingTerminal extends LogicalScreen // Save it and we are done. synchronized (eventQueue) { eventQueue.add(new TKeypressEvent(keypress)); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1575,9 +1573,12 @@ public final class SwingTerminal extends LogicalScreen // Drop a cmAbort and walk away synchronized (eventQueue) { eventQueue.add(new TCommandEvent(cmAbort)); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1663,9 +1664,12 @@ public final class SwingTerminal extends LogicalScreen TResizeEvent windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN, sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); eventQueue.add(windowResize); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1699,9 +1703,12 @@ public final class SwingTerminal extends LogicalScreen synchronized (eventQueue) { eventQueue.add(mouseEvent); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1725,9 +1732,12 @@ public final class SwingTerminal extends LogicalScreen synchronized (eventQueue) { eventQueue.add(mouseEvent); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1788,9 +1798,12 @@ public final class SwingTerminal extends LogicalScreen synchronized (eventQueue) { eventQueue.add(mouseEvent); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1833,9 +1846,12 @@ public final class SwingTerminal extends LogicalScreen synchronized (eventQueue) { eventQueue.add(mouseEvent); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } } @@ -1877,9 +1893,12 @@ public final class SwingTerminal extends LogicalScreen synchronized (eventQueue) { eventQueue.add(mouseEvent); + resetBlinkTimer(); } - synchronized (listener) { - listener.notifyAll(); + if (listener != null) { + synchronized (listener) { + listener.notifyAll(); + } } }