2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.awt
.Color
;
34 import java
.awt
.Cursor
;
36 import java
.awt
.FontMetrics
;
37 import java
.awt
.Graphics
;
38 import java
.awt
.Insets
;
39 import java
.awt
.Point
;
40 import java
.awt
.Rectangle
;
41 import java
.awt
.Toolkit
;
42 import java
.awt
.geom
.Rectangle2D
;
43 import java
.awt
.image
.BufferedImage
;
44 import java
.awt
.image
.BufferStrategy
;
45 import java
.io
.InputStream
;
46 import java
.util
.Date
;
47 import javax
.swing
.JFrame
;
48 import javax
.swing
.SwingUtilities
;
50 import jexer
.bits
.Cell
;
51 import jexer
.bits
.CellAttributes
;
52 import jexer
.session
.SwingSessionInfo
;
55 * This Screen implementation draws to a Java Swing JFrame.
57 public final class SwingScreen
extends Screen
{
60 * If true, use double buffering thread.
62 private static final boolean doubleBuffer
= true;
65 * Cursor style to draw.
67 public enum CursorStyle
{
69 * Use an underscore for the cursor.
74 * Use a solid block for the cursor.
79 * Use an outlined block for the cursor.
84 private static Color MYBLACK
;
85 private static Color MYRED
;
86 private static Color MYGREEN
;
87 private static Color MYYELLOW
;
88 private static Color MYBLUE
;
89 private static Color MYMAGENTA
;
90 private static Color MYCYAN
;
91 private static Color MYWHITE
;
93 private static Color MYBOLD_BLACK
;
94 private static Color MYBOLD_RED
;
95 private static Color MYBOLD_GREEN
;
96 private static Color MYBOLD_YELLOW
;
97 private static Color MYBOLD_BLUE
;
98 private static Color MYBOLD_MAGENTA
;
99 private static Color MYBOLD_CYAN
;
100 private static Color MYBOLD_WHITE
;
102 private static boolean dosColors
= false;
105 * Setup Swing colors to match DOS color palette.
107 private static void setDOSColors() {
111 MYBLACK
= new Color(0x00, 0x00, 0x00);
112 MYRED
= new Color(0xa8, 0x00, 0x00);
113 MYGREEN
= new Color(0x00, 0xa8, 0x00);
114 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
115 MYBLUE
= new Color(0x00, 0x00, 0xa8);
116 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
117 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
118 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
119 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
120 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
121 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
122 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
123 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
124 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
125 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
126 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
132 * SwingFrame is our top-level hook into the Swing system.
134 class SwingFrame
extends JFrame
{
137 * Serializable version.
139 private static final long serialVersionUID
= 1;
142 * The terminus font resource filename.
144 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
147 * The BufferStrategy object needed for double-buffering.
149 private BufferStrategy bufferStrategy
;
152 * The TUI Screen data.
157 * Width of a character cell.
159 private int textWidth
= 1;
162 * Height of a character cell.
164 private int textHeight
= 1;
167 * Descent of a character cell.
169 private int maxDescent
= 0;
172 * System-dependent Y adjustment for text in the character cell.
174 private int textAdjustY
= 0;
177 * System-dependent X adjustment for text in the character cell.
179 private int textAdjustX
= 0;
182 * Top pixel absolute location.
184 private int top
= 30;
187 * Left pixel absolute location.
189 private int left
= 30;
192 * The cursor style to draw.
194 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
197 * The number of millis to wait before switching the blink from
198 * visible to invisible.
200 private long blinkMillis
= 500;
203 * If true, the cursor should be visible right now based on the blink
206 private boolean cursorBlinkVisible
= true;
209 * The time that the blink last flipped from visible to invisible or
210 * from invisible to visible.
212 private long lastBlinkTime
= 0;
215 * Convert a CellAttributes foreground color to an Swing Color.
217 * @param attr the text attributes
218 * @return the Swing Color
220 private Color
attrToForegroundColor(final CellAttributes attr
) {
222 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
224 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
226 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
228 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
230 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
231 return MYBOLD_YELLOW
;
232 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
234 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
235 return MYBOLD_MAGENTA
;
236 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
240 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
242 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
244 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
246 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
248 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
250 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
252 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
254 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
258 throw new IllegalArgumentException("Invalid color: " + attr
.getForeColor().getValue());
262 * Convert a CellAttributes background color to an Swing Color.
264 * @param attr the text attributes
265 * @return the Swing Color
267 private Color
attrToBackgroundColor(final CellAttributes attr
) {
268 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
270 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
272 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
274 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
276 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
278 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
280 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
282 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
285 throw new IllegalArgumentException("Invalid color: " + attr
.getBackColor().getValue());
289 * Public constructor.
291 * @param screen the Screen that Backend talks to
293 public SwingFrame(final SwingScreen screen
) {
294 this.screen
= screen
;
297 // Figure out my cursor style
298 String cursorStyleString
= System
.getProperty("jexer.Swing.cursorStyle",
299 "underline").toLowerCase();
301 if (cursorStyleString
.equals("underline")) {
302 cursorStyle
= CursorStyle
.UNDERLINE
;
303 } else if (cursorStyleString
.equals("outline")) {
304 cursorStyle
= CursorStyle
.OUTLINE
;
305 } else if (cursorStyleString
.equals("block")) {
306 cursorStyle
= CursorStyle
.BLOCK
;
309 setTitle("Jexer Application");
310 setBackground(Color
.black
);
313 // Always try to use Terminus, the one decent font.
314 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
315 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
316 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
317 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 22);
319 } catch (Exception e
) {
321 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
322 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
326 // Kill the X11 cursor
327 // Transparent 16 x 16 pixel cursor image.
328 BufferedImage cursorImg
= new BufferedImage(16, 16,
329 BufferedImage
.TYPE_INT_ARGB
);
330 // Create a new blank cursor.
331 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
332 cursorImg
, new Point(0, 0), "blank cursor");
333 setCursor(blankCursor
);
335 // Be capable of seeing Tab / Shift-Tab
336 setFocusTraversalKeysEnabled(false);
338 // Save the text cell width/height
341 // Setup double-buffering
342 if (SwingScreen
.doubleBuffer
) {
343 setIgnoreRepaint(true);
344 createBufferStrategy(2);
345 bufferStrategy
= getBufferStrategy();
350 * Figure out my font dimensions.
352 private void getFontDimensions() {
353 Graphics gr
= getGraphics();
354 FontMetrics fm
= gr
.getFontMetrics();
355 maxDescent
= fm
.getMaxDescent();
356 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
357 int leading
= fm
.getLeading();
358 textWidth
= (int)Math
.round(bounds
.getWidth());
359 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
360 // This also produces the same number, but works better for ugly
362 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
364 if (System
.getProperty("os.name").startsWith("Windows")) {
371 * Resize to font dimensions.
373 public void resizeToScreen() {
374 // Figure out the thickness of borders and use that to set the
376 Insets insets
= getInsets();
380 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
381 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
385 * Update redraws the whole screen.
387 * @param gr the Swing Graphics context
390 public void update(final Graphics gr
) {
391 // The default update clears the area. Don't do that, instead
392 // just paint it directly.
397 * Paint redraws the whole screen.
399 * @param gr the Swing Graphics context
402 public void paint(final Graphics gr
) {
403 // Do nothing until the screen reference has been set.
404 if (screen
== null) {
407 if (screen
.frame
== null) {
411 // See if it is time to flip the blink time.
412 long nowTime
= (new Date()).getTime();
413 if (nowTime
> blinkMillis
+ lastBlinkTime
) {
414 lastBlinkTime
= nowTime
;
415 cursorBlinkVisible
= !cursorBlinkVisible
;
419 int xCellMax
= screen
.width
;
421 int yCellMax
= screen
.height
;
423 Rectangle bounds
= gr
.getClipBounds();
424 if (bounds
!= null) {
425 // Only update what is in the bounds
426 xCellMin
= screen
.textColumn(bounds
.x
);
427 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
);
428 if (xCellMax
> screen
.width
) {
429 xCellMax
= screen
.width
;
431 if (xCellMin
>= xCellMax
) {
432 xCellMin
= xCellMax
- 2;
437 yCellMin
= screen
.textRow(bounds
.y
);
438 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
);
439 if (yCellMax
> screen
.height
) {
440 yCellMax
= screen
.height
;
442 if (yCellMin
>= yCellMax
) {
443 yCellMin
= yCellMax
- 2;
449 // We need a total repaint
450 reallyCleared
= true;
453 // Prevent updates to the screen's data from the TApplication
455 synchronized (screen
) {
457 System.err.printf("bounds %s X %d %d Y %d %d\n",
458 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
461 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
462 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
464 int xPixel
= x
* textWidth
+ left
;
465 int yPixel
= y
* textHeight
+ top
;
467 Cell lCell
= screen
.logical
[x
][y
];
468 Cell pCell
= screen
.physical
[x
][y
];
470 if (!lCell
.equals(pCell
)
474 Cell lCellColor
= new Cell();
475 lCellColor
.setTo(lCell
);
478 if (lCell
.isReverse()) {
479 lCellColor
.setForeColor(lCell
.getBackColor());
480 lCellColor
.setBackColor(lCell
.getForeColor());
483 // Draw the background rectangle, then the
484 // foreground character.
485 gr
.setColor(attrToBackgroundColor(lCellColor
));
486 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
488 // Handle blink and underline
490 || (lCell
.isBlink() && cursorBlinkVisible
)
492 gr
.setColor(attrToForegroundColor(lCellColor
));
493 char [] chars
= new char[1];
494 chars
[0] = lCell
.getChar();
495 gr
.drawChars(chars
, 0, 1, xPixel
+ textAdjustX
,
496 yPixel
+ textHeight
- maxDescent
499 if (lCell
.isUnderline()) {
500 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2,
505 // Physical is always updated
506 physical
[x
][y
].setTo(lCell
);
511 // Draw the cursor if it is visible
513 && (cursorY
<= screen
.height
- 1)
514 && (cursorX
<= screen
.width
- 1)
515 && cursorBlinkVisible
517 int xPixel
= cursorX
* textWidth
+ left
;
518 int yPixel
= cursorY
* textHeight
+ top
;
519 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
520 gr
.setColor(attrToForegroundColor(lCell
));
521 switch (cursorStyle
) {
523 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2,
527 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
530 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1,
537 reallyCleared
= false;
538 } // synchronized (screen)
541 } // class SwingFrame
544 * The raw Swing JFrame. Note package private access.
549 * Restore terminal to normal state.
551 public void shutdown() {
556 * Public constructor.
558 public SwingScreen() {
560 SwingUtilities
.invokeAndWait(new Runnable() {
562 SwingScreen
.this.frame
= new SwingFrame(SwingScreen
.this);
563 SwingScreen
.this.sessionInfo
=
564 new SwingSessionInfo(SwingScreen
.this.frame
,
568 SwingScreen
.this.setDimensions(sessionInfo
.getWindowWidth(),
569 sessionInfo
.getWindowHeight());
571 SwingScreen
.this.frame
.resizeToScreen();
572 SwingScreen
.this.frame
.setVisible(true);
575 } catch (Exception e
) {
583 private SwingSessionInfo sessionInfo
;
586 * Create the SwingSessionInfo. Note package private access.
588 * @return the sessionInfo
590 SwingSessionInfo
getSessionInfo() {
595 * Push the logical screen to the physical device.
598 public void flushPhysical() {
601 // Really refreshed, do it all
602 if (SwingScreen
.doubleBuffer
) {
603 Graphics gr
= frame
.bufferStrategy
.getDrawGraphics();
606 frame
.bufferStrategy
.show();
607 Toolkit
.getDefaultToolkit().sync();
614 // Do nothing if nothing happened.
619 // Request a repaint, let the frame's repaint/update methods do the
622 // Find the minimum-size damaged region.
623 int xMin
= frame
.getWidth();
625 int yMin
= frame
.getHeight();
628 synchronized (this) {
629 for (int y
= 0; y
< height
; y
++) {
630 for (int x
= 0; x
< width
; x
++) {
631 Cell lCell
= logical
[x
][y
];
632 Cell pCell
= physical
[x
][y
];
634 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
635 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
637 if (!lCell
.equals(pCell
)
646 if (xPixel
+ frame
.textWidth
> xMax
) {
647 xMax
= xPixel
+ frame
.textWidth
;
652 if (yPixel
+ frame
.textHeight
> yMax
) {
653 yMax
= yPixel
+ frame
.textHeight
;
659 if (xMin
+ frame
.textWidth
>= xMax
) {
660 xMax
+= frame
.textWidth
;
662 if (yMin
+ frame
.textHeight
>= yMax
) {
663 yMax
+= frame
.textHeight
;
666 // Repaint the desired area
667 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
669 if (SwingScreen
.doubleBuffer
) {
670 Graphics gr
= frame
.bufferStrategy
.getDrawGraphics();
671 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
676 frame
.bufferStrategy
.show();
677 Toolkit
.getDefaultToolkit().sync();
679 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
684 * Put the cursor at (x,y).
686 * @param visible if true, the cursor should be visible
687 * @param x column coordinate to put the cursor on
688 * @param y row coordinate to put the cursor on
691 public void putCursor(final boolean visible
, final int x
, final int y
) {
693 if ((visible
== cursorVisible
) && ((x
== cursorX
) && (y
== cursorY
))) {
694 // See if it is time to flip the blink time.
695 long nowTime
= (new Date()).getTime();
696 if (nowTime
< frame
.blinkMillis
+ frame
.lastBlinkTime
) {
697 // Nothing has changed, so don't do anything.
703 && (cursorY
<= height
- 1)
704 && (cursorX
<= width
- 1)
706 // Make the current cursor position dirty
707 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
708 physical
[cursorX
][cursorY
].setChar('X');
710 physical
[cursorX
][cursorY
].setChar('Q');
714 super.putCursor(visible
, x
, y
);
718 * Convert pixel column position to text cell column position.
720 * @param x pixel column position
721 * @return text cell column position
723 public int textColumn(final int x
) {
724 return ((x
- frame
.left
) / frame
.textWidth
);
728 * Convert pixel row position to text cell row position.
730 * @param y pixel row position
731 * @return text cell row position
733 public int textRow(final int y
) {
734 return ((y
- frame
.top
) / frame
.textHeight
);