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
.io
.InputStream
;
45 import java
.util
.Date
;
46 import javax
.swing
.JFrame
;
47 import javax
.swing
.SwingUtilities
;
49 import jexer
.bits
.Cell
;
50 import jexer
.bits
.CellAttributes
;
51 import jexer
.session
.SwingSessionInfo
;
54 * This Screen implementation draws to a Java Swing JFrame.
56 public final class SwingScreen
extends Screen
{
59 * Cursor style to draw.
61 public enum CursorStyle
{
63 * Use an underscore for the cursor.
68 * Use a solid block for the cursor.
73 * Use an outlined block for the cursor.
78 private static Color MYBLACK
;
79 private static Color MYRED
;
80 private static Color MYGREEN
;
81 private static Color MYYELLOW
;
82 private static Color MYBLUE
;
83 private static Color MYMAGENTA
;
84 private static Color MYCYAN
;
85 private static Color MYWHITE
;
87 private static Color MYBOLD_BLACK
;
88 private static Color MYBOLD_RED
;
89 private static Color MYBOLD_GREEN
;
90 private static Color MYBOLD_YELLOW
;
91 private static Color MYBOLD_BLUE
;
92 private static Color MYBOLD_MAGENTA
;
93 private static Color MYBOLD_CYAN
;
94 private static Color MYBOLD_WHITE
;
96 private static boolean dosColors
= false;
99 * Setup Swing colors to match DOS color palette.
101 private static void setDOSColors() {
105 MYBLACK
= new Color(0x00, 0x00, 0x00);
106 MYRED
= new Color(0xa8, 0x00, 0x00);
107 MYGREEN
= new Color(0x00, 0xa8, 0x00);
108 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
109 MYBLUE
= new Color(0x00, 0x00, 0xa8);
110 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
111 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
112 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
113 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
114 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
115 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
116 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
117 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
118 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
119 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
120 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
126 * SwingFrame is our top-level hook into the Swing system.
128 class SwingFrame
extends JFrame
{
131 * Serializable version.
133 private static final long serialVersionUID
= 1;
136 * The terminus font resource filename.
138 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
141 * The TUI Screen data.
146 * Width of a character cell.
148 private int textWidth
= 1;
151 * Height of a character cell.
153 private int textHeight
= 1;
156 * Descent of a character cell.
158 private int maxDescent
= 0;
161 * Top pixel absolute location.
163 private int top
= 30;
166 * Left pixel absolute location.
168 private int left
= 30;
171 * The cursor style to draw.
173 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
176 * The number of millis to wait before switching the blink from
177 * visible to invisible.
179 private long blinkMillis
= 500;
182 * If true, the cursor should be visible right now based on the blink
185 private boolean cursorBlinkVisible
= true;
188 * The time that the blink last flipped from visible to invisible or
189 * from invisible to visible.
191 private long lastBlinkTime
= 0;
194 * Convert a CellAttributes foreground color to an Swing Color.
196 * @param attr the text attributes
197 * @return the Swing Color
199 private Color
attrToForegroundColor(final CellAttributes attr
) {
207 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
209 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
211 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
213 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
215 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
216 return MYBOLD_YELLOW
;
217 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
219 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
220 return MYBOLD_MAGENTA
;
221 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
225 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
227 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
229 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
231 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
233 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
235 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
237 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
239 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
243 throw new IllegalArgumentException("Invalid color: " + attr
.getForeColor().getValue());
247 * Convert a CellAttributes background color to an Swing Color.
249 * @param attr the text attributes
250 * @return the Swing Color
252 private Color
attrToBackgroundColor(final CellAttributes attr
) {
259 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
261 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
263 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
265 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
267 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
269 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
271 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
273 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
276 throw new IllegalArgumentException("Invalid color: " + attr
.getBackColor().getValue());
280 * Public constructor.
282 * @param screen the Screen that Backend talks to
284 public SwingFrame(final SwingScreen screen
) {
285 this.screen
= screen
;
288 // Figure out my cursor style
289 String cursorStyleString
= System
.getProperty("jexer.Swing.cursorStyle",
290 "underline").toLowerCase();
292 if (cursorStyleString
.equals("underline")) {
293 cursorStyle
= CursorStyle
.UNDERLINE
;
294 } else if (cursorStyleString
.equals("outline")) {
295 cursorStyle
= CursorStyle
.OUTLINE
;
296 } else if (cursorStyleString
.equals("block")) {
297 cursorStyle
= CursorStyle
.BLOCK
;
300 setTitle("Jexer Application");
301 setBackground(Color
.black
);
304 // Always try to use Terminus, the one decent font.
305 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
306 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
307 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
308 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 22);
310 } catch (Exception e
) {
312 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
313 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
317 // Kill the X11 cursor
318 // Transparent 16 x 16 pixel cursor image.
319 BufferedImage cursorImg
= new BufferedImage(16, 16,
320 BufferedImage
.TYPE_INT_ARGB
);
321 // Create a new blank cursor.
322 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
323 cursorImg
, new Point(0, 0), "blank cursor");
324 setCursor(blankCursor
);
326 // Be capable of seeing Tab / Shift-Tab
327 setFocusTraversalKeysEnabled(false);
329 // Save the text cell width/height
334 * Figure out my font dimensions.
336 private void getFontDimensions() {
337 Graphics gr
= getGraphics();
338 FontMetrics fm
= gr
.getFontMetrics();
339 maxDescent
= fm
.getMaxDescent();
340 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
341 int leading
= fm
.getLeading();
342 textWidth
= (int)Math
.round(bounds
.getWidth());
343 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
344 // This also produces the same number, but works better for ugly
346 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
350 * Resize to font dimensions.
352 public void resizeToScreen() {
353 // Figure out the thickness of borders and use that to set the
355 Insets insets
= getInsets();
359 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
360 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
364 * Update redraws the whole screen.
366 * @param gr the Swing Graphics context
369 public void update(final Graphics gr
) {
370 // The default update clears the area. Don't do that, instead
371 // just paint it directly.
376 * Paint redraws the whole screen.
378 * @param gr the Swing Graphics context
381 public void paint(final Graphics gr
) {
382 // Do nothing until the screen reference has been set.
383 if (screen
== null) {
386 if (screen
.frame
== null) {
390 // See if it is time to flip the blink time.
391 long nowTime
= (new Date()).getTime();
392 if (nowTime
> blinkMillis
+ lastBlinkTime
) {
393 lastBlinkTime
= nowTime
;
394 cursorBlinkVisible
= !cursorBlinkVisible
;
398 int xCellMax
= screen
.width
;
400 int yCellMax
= screen
.height
;
402 Rectangle bounds
= gr
.getClipBounds();
403 if (bounds
!= null) {
404 // Only update what is in the bounds
405 xCellMin
= screen
.textColumn(bounds
.x
);
406 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
);
407 if (xCellMax
> screen
.width
) {
408 xCellMax
= screen
.width
;
410 if (xCellMin
>= xCellMax
) {
411 xCellMin
= xCellMax
- 2;
416 yCellMin
= screen
.textRow(bounds
.y
);
417 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
);
418 if (yCellMax
> screen
.height
) {
419 yCellMax
= screen
.height
;
421 if (yCellMin
>= yCellMax
) {
422 yCellMin
= yCellMax
- 2;
428 // We need a total repaint
429 reallyCleared
= true;
432 // Prevent updates to the screen's data from the TApplication
434 synchronized (screen
) {
436 System.err.printf("bounds %s X %d %d Y %d %d\n",
437 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
440 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
441 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
443 int xPixel
= x
* textWidth
+ left
;
444 int yPixel
= y
* textHeight
+ top
;
446 Cell lCell
= screen
.logical
[x
][y
];
447 Cell pCell
= screen
.physical
[x
][y
];
449 if (!lCell
.equals(pCell
) || reallyCleared
) {
450 // Draw the background rectangle, then the
451 // foreground character.
452 gr
.setColor(attrToBackgroundColor(lCell
));
453 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
454 gr
.setColor(attrToForegroundColor(lCell
));
455 char [] chars
= new char[1];
456 chars
[0] = lCell
.getChar();
457 gr
.drawChars(chars
, 0, 1, xPixel
,
458 yPixel
+ textHeight
- maxDescent
);
460 // Physical is always updated
461 physical
[x
][y
].setTo(lCell
);
466 // Draw the cursor if it is visible
468 && (cursorY
<= screen
.height
- 1)
469 && (cursorX
<= screen
.width
- 1)
470 && cursorBlinkVisible
472 int xPixel
= cursorX
* textWidth
+ left
;
473 int yPixel
= cursorY
* textHeight
+ top
;
474 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
475 gr
.setColor(attrToForegroundColor(lCell
));
476 switch (cursorStyle
) {
478 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2,
482 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
485 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1,
492 reallyCleared
= false;
493 } // synchronized (screen)
496 } // class SwingFrame
499 * The raw Swing JFrame. Note package private access.
504 * Public constructor.
506 public SwingScreen() {
508 SwingUtilities
.invokeAndWait(new Runnable() {
510 SwingScreen
.this.frame
= new SwingFrame(SwingScreen
.this);
511 SwingScreen
.this.sessionInfo
=
512 new SwingSessionInfo(SwingScreen
.this.frame
,
516 SwingScreen
.this.setDimensions(sessionInfo
.getWindowWidth(),
517 sessionInfo
.getWindowHeight());
519 SwingScreen
.this.frame
.resizeToScreen();
520 SwingScreen
.this.frame
.setVisible(true);
523 } catch (Exception e
) {
531 private SwingSessionInfo sessionInfo
;
534 * Create the SwingSessionInfo. Note package private access.
536 * @return the sessionInfo
538 SwingSessionInfo
getSessionInfo() {
543 * Push the logical screen to the physical device.
546 public void flushPhysical() {
549 // Really refreshed, do it all
554 // Do nothing if nothing happened.
559 // Request a repaint, let the frame's repaint/update methods do the
562 // Find the minimum-size damaged region.
563 int xMin
= frame
.getWidth();
565 int yMin
= frame
.getHeight();
568 synchronized (this) {
569 for (int y
= 0; y
< height
; y
++) {
570 for (int x
= 0; x
< width
; x
++) {
571 Cell lCell
= logical
[x
][y
];
572 Cell pCell
= physical
[x
][y
];
574 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
575 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
577 if (!lCell
.equals(pCell
)
585 if (xPixel
+ frame
.textWidth
> xMax
) {
586 xMax
= xPixel
+ frame
.textWidth
;
591 if (yPixel
+ frame
.textHeight
> yMax
) {
592 yMax
= yPixel
+ frame
.textHeight
;
598 if (xMin
+ frame
.textWidth
>= xMax
) {
599 xMax
+= frame
.textWidth
;
601 if (yMin
+ frame
.textHeight
>= yMax
) {
602 yMax
+= frame
.textHeight
;
605 // Repaint the desired area
606 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
607 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
611 * Put the cursor at (x,y).
613 * @param visible if true, the cursor should be visible
614 * @param x column coordinate to put the cursor on
615 * @param y row coordinate to put the cursor on
618 public void putCursor(final boolean visible
, final int x
, final int y
) {
620 if ((visible
== cursorVisible
) && ((x
== cursorX
) && (y
== cursorY
))) {
621 // See if it is time to flip the blink time.
622 long nowTime
= (new Date()).getTime();
623 if (nowTime
< frame
.blinkMillis
+ frame
.lastBlinkTime
) {
624 // Nothing has changed, so don't do anything.
630 && (cursorY
<= height
- 1)
631 && (cursorX
<= width
- 1)
633 // Make the current cursor position dirty
634 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
635 physical
[cursorX
][cursorY
].setChar('X');
637 physical
[cursorX
][cursorY
].setChar('Q');
641 super.putCursor(visible
, x
, y
);
645 * Convert pixel column position to text cell column position.
647 * @param x pixel column position
648 * @return text cell column position
650 public int textColumn(final int x
) {
651 return ((x
- frame
.left
) / frame
.textWidth
);
655 * Convert pixel row position to text cell row position.
657 * @param y pixel row position
658 * @return text cell row position
660 public int textRow(final int y
) {
661 return ((y
- frame
.top
) / frame
.textHeight
);