2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.awt
.BorderLayout
;
32 import java
.awt
.Color
;
34 import java
.awt
.FontMetrics
;
35 import java
.awt
.Graphics2D
;
36 import java
.awt
.Graphics
;
37 import java
.awt
.Insets
;
38 import java
.awt
.Rectangle
;
39 import java
.awt
.RenderingHints
;
40 import java
.awt
.Toolkit
;
41 import java
.awt
.event
.ComponentEvent
;
42 import java
.awt
.event
.ComponentListener
;
43 import java
.awt
.event
.KeyEvent
;
44 import java
.awt
.event
.KeyListener
;
45 import java
.awt
.event
.MouseEvent
;
46 import java
.awt
.event
.MouseListener
;
47 import java
.awt
.event
.MouseMotionListener
;
48 import java
.awt
.event
.MouseWheelEvent
;
49 import java
.awt
.event
.MouseWheelListener
;
50 import java
.awt
.event
.WindowEvent
;
51 import java
.awt
.event
.WindowListener
;
52 import java
.awt
.geom
.Rectangle2D
;
53 import java
.awt
.image
.BufferedImage
;
54 import java
.io
.InputStream
;
55 import java
.util
.ArrayList
;
56 import java
.util
.HashMap
;
57 import java
.util
.List
;
59 import javax
.swing
.JComponent
;
60 import javax
.swing
.JFrame
;
61 import javax
.swing
.ImageIcon
;
62 import javax
.swing
.SwingUtilities
;
64 import jexer
.TKeypress
;
65 import jexer
.bits
.Cell
;
66 import jexer
.bits
.CellAttributes
;
67 import jexer
.event
.TCommandEvent
;
68 import jexer
.event
.TInputEvent
;
69 import jexer
.event
.TKeypressEvent
;
70 import jexer
.event
.TMouseEvent
;
71 import jexer
.event
.TResizeEvent
;
72 import static jexer
.TCommand
.*;
73 import static jexer
.TKeypress
.*;
76 * This Screen backend reads keystrokes and mouse events and draws to either
77 * a Java Swing JFrame (potentially triple-buffered) or a JComponent.
79 * This class is a bit of an inversion of typical GUI classes. It performs
80 * all of the drawing logic from SwingTerminal (which is not a Swing class),
81 * and uses a SwingComponent wrapper class to call the JFrame or JComponent
84 public class SwingTerminal
extends LogicalScreen
85 implements TerminalReader
,
86 ComponentListener
, KeyListener
,
87 MouseListener
, MouseMotionListener
,
88 MouseWheelListener
, WindowListener
{
90 // ------------------------------------------------------------------------
91 // Constants --------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 * The icon image location.
97 private static final String ICONFILE
= "jexer_logo_128.png";
100 * The terminus font resource filename.
102 public static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
105 * Cursor style to draw.
107 public enum CursorStyle
{
109 * Use an underscore for the cursor.
114 * Use a solid block for the cursor.
119 * Use an outlined block for the cursor.
124 * Use a vertical bar for the cursor.
129 // ------------------------------------------------------------------------
130 // Variables --------------------------------------------------------------
131 // ------------------------------------------------------------------------
133 // Colors to map DOS colors to AWT colors.
134 private static Color MYBLACK
;
135 private static Color MYRED
;
136 private static Color MYGREEN
;
137 private static Color MYYELLOW
;
138 private static Color MYBLUE
;
139 private static Color MYMAGENTA
;
140 private static Color MYCYAN
;
141 private static Color MYWHITE
;
142 private static Color MYBOLD_BLACK
;
143 private static Color MYBOLD_RED
;
144 private static Color MYBOLD_GREEN
;
145 private static Color MYBOLD_YELLOW
;
146 private static Color MYBOLD_BLUE
;
147 private static Color MYBOLD_MAGENTA
;
148 private static Color MYBOLD_CYAN
;
149 private static Color MYBOLD_WHITE
;
152 * When true, all the MYBLACK, MYRED, etc. colors are set.
154 private static boolean dosColors
= false;
157 * The Swing component or frame to draw to.
159 private SwingComponent swing
;
162 * A cache of previously-rendered glyphs for blinking text, when it is
165 private Map
<Cell
, BufferedImage
> glyphCacheBlink
;
168 * A cache of previously-rendered glyphs for non-blinking, or
169 * blinking-and-visible, text.
171 private Map
<Cell
, BufferedImage
> glyphCache
;
174 * If true, we were successful at getting the font dimensions.
176 private boolean gotFontDimensions
= false;
179 * The currently selected font.
181 private Font font
= null;
184 * The currently selected font size in points.
186 private int fontSize
= 16;
189 * Width of a character cell in pixels.
191 private int textWidth
= 16;
194 * Height of a character cell in pixels.
196 private int textHeight
= 20;
199 * Width of a character cell in pixels, as reported by font.
201 private int fontTextWidth
= 1;
204 * Height of a character cell in pixels, as reported by font.
206 private int fontTextHeight
= 1;
209 * Descent of a character cell in pixels.
211 private int maxDescent
= 0;
214 * System-dependent Y adjustment for text in the character cell.
216 private int textAdjustY
= 0;
219 * System-dependent X adjustment for text in the character cell.
221 private int textAdjustX
= 0;
224 * System-dependent height adjustment for text in the character cell.
226 private int textAdjustHeight
= 0;
229 * System-dependent width adjustment for text in the character cell.
231 private int textAdjustWidth
= 0;
234 * Top pixel absolute location.
236 private int top
= 30;
239 * Left pixel absolute location.
241 private int left
= 30;
244 * The cursor style to draw.
246 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
249 * The number of millis to wait before switching the blink from visible
250 * to invisible. Set to 0 or negative to disable blinking.
252 private long blinkMillis
= 500;
255 * If true, the cursor should be visible right now based on the blink
258 private boolean cursorBlinkVisible
= true;
261 * The time that the blink last flipped from visible to invisible or
262 * from invisible to visible.
264 private long lastBlinkTime
= 0;
267 * The session information.
269 private SwingSessionInfo sessionInfo
;
272 * The listening object that run() wakes up on new input.
274 private Object listener
;
277 * The event queue, filled up by a thread reading on input.
279 private List
<TInputEvent
> eventQueue
;
282 * The last reported mouse X position.
284 private int oldMouseX
= -1;
287 * The last reported mouse Y position.
289 private int oldMouseY
= -1;
292 * true if mouse1 was down. Used to report mouse1 on the release event.
294 private boolean mouse1
= false;
297 * true if mouse2 was down. Used to report mouse2 on the release event.
299 private boolean mouse2
= false;
302 * true if mouse3 was down. Used to report mouse3 on the release event.
304 private boolean mouse3
= false;
306 // ------------------------------------------------------------------------
307 // Constructors -----------------------------------------------------------
308 // ------------------------------------------------------------------------
311 * Static constructor.
318 * Public constructor creates a new JFrame to render to.
320 * @param windowWidth the number of text columns to start with
321 * @param windowHeight the number of text rows to start with
322 * @param fontSize the size in points. Good values to pick are: 16, 20,
324 * @param listener the object this backend needs to wake up when new
327 public SwingTerminal(final int windowWidth
, final int windowHeight
,
328 final int fontSize
, final Object listener
) {
330 this.fontSize
= fontSize
;
335 SwingUtilities
.invokeAndWait(new Runnable() {
338 JFrame frame
= new JFrame() {
341 * Serializable version.
343 private static final long serialVersionUID
= 1;
346 * The code that performs the actual drawing.
348 public SwingTerminal screen
= null;
351 * Anonymous class initializer saves the screen
352 * reference, so that paint() and the like call out
356 this.screen
= SwingTerminal
.this;
360 * Update redraws the whole screen.
362 * @param gr the Swing Graphics context
365 public void update(final Graphics gr
) {
366 // The default update clears the area. Don't do
367 // that, instead just paint it directly.
372 * Paint redraws the whole screen.
374 * @param gr the Swing Graphics context
377 public void paint(final Graphics gr
) {
378 if (screen
!= null) {
385 ClassLoader loader
= Thread
.currentThread().
386 getContextClassLoader();
387 frame
.setIconImage((new ImageIcon(loader
.
388 getResource(ICONFILE
))).getImage());
390 // Get the Swing component
391 SwingTerminal
.this.swing
= new SwingComponent(frame
);
393 // Hang onto top and left for drawing.
394 Insets insets
= SwingTerminal
.this.swing
.getInsets();
395 SwingTerminal
.this.left
= insets
.left
;
396 SwingTerminal
.this.top
= insets
.top
;
398 // Load the font so that we can set sessionInfo.
401 // Get the default cols x rows and set component size
403 SwingTerminal
.this.sessionInfo
=
404 new SwingSessionInfo(SwingTerminal
.this.swing
,
405 SwingTerminal
.this.textWidth
,
406 SwingTerminal
.this.textHeight
,
407 windowWidth
, windowHeight
);
409 SwingTerminal
.this.setDimensions(sessionInfo
.
410 getWindowWidth(), sessionInfo
.getWindowHeight());
412 SwingTerminal
.this.resizeToScreen(true);
413 SwingTerminal
.this.swing
.setVisible(true);
416 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
418 } catch (InterruptedException e
) {
422 this.listener
= listener
;
426 eventQueue
= new ArrayList
<TInputEvent
>();
428 // Add listeners to Swing.
429 swing
.addKeyListener(this);
430 swing
.addWindowListener(this);
431 swing
.addComponentListener(this);
432 swing
.addMouseListener(this);
433 swing
.addMouseMotionListener(this);
434 swing
.addMouseWheelListener(this);
438 * Public constructor renders to an existing JComponent.
440 * @param component the Swing component to render to
441 * @param windowWidth the number of text columns to start with
442 * @param windowHeight the number of text rows to start with
443 * @param fontSize the size in points. Good values to pick are: 16, 20,
445 * @param listener the object this backend needs to wake up when new
448 public SwingTerminal(final JComponent component
, final int windowWidth
,
449 final int windowHeight
, final int fontSize
, final Object listener
) {
451 this.fontSize
= fontSize
;
456 SwingUtilities
.invokeAndWait(new Runnable() {
459 JComponent newComponent
= new JComponent() {
462 * Serializable version.
464 private static final long serialVersionUID
= 1;
467 * The code that performs the actual drawing.
469 public SwingTerminal screen
= null;
472 * Anonymous class initializer saves the screen
473 * reference, so that paint() and the like call out
477 this.screen
= SwingTerminal
.this;
481 * Update redraws the whole screen.
483 * @param gr the Swing Graphics context
486 public void update(final Graphics gr
) {
487 // The default update clears the area. Don't do
488 // that, instead just paint it directly.
493 * Paint redraws the whole screen.
495 * @param gr the Swing Graphics context
498 public void paint(final Graphics gr
) {
499 if (screen
!= null) {
504 component
.setLayout(new BorderLayout());
505 component
.add(newComponent
);
507 // Allow key events to be received
508 component
.setFocusable(true);
510 // Get the Swing component
511 SwingTerminal
.this.swing
= new SwingComponent(component
);
513 // Hang onto top and left for drawing.
514 Insets insets
= SwingTerminal
.this.swing
.getInsets();
515 SwingTerminal
.this.left
= insets
.left
;
516 SwingTerminal
.this.top
= insets
.top
;
518 // Load the font so that we can set sessionInfo.
521 // Get the default cols x rows and set component size
523 SwingTerminal
.this.sessionInfo
=
524 new SwingSessionInfo(SwingTerminal
.this.swing
,
525 SwingTerminal
.this.textWidth
,
526 SwingTerminal
.this.textHeight
);
529 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
531 } catch (InterruptedException e
) {
535 this.listener
= listener
;
539 eventQueue
= new ArrayList
<TInputEvent
>();
541 // Add listeners to Swing.
542 swing
.addKeyListener(this);
543 swing
.addWindowListener(this);
544 swing
.addComponentListener(this);
545 swing
.addMouseListener(this);
546 swing
.addMouseMotionListener(this);
547 swing
.addMouseWheelListener(this);
550 // ------------------------------------------------------------------------
551 // LogicalScreen ----------------------------------------------------------
552 // ------------------------------------------------------------------------
555 * Set the window title.
557 * @param title the new title
560 public void setTitle(final String title
) {
561 swing
.setTitle(title
);
565 * Push the logical screen to the physical device.
568 public void flushPhysical() {
569 // See if it is time to flip the blink time.
570 long nowTime
= System
.currentTimeMillis();
571 if (nowTime
>= blinkMillis
+ lastBlinkTime
) {
572 lastBlinkTime
= nowTime
;
573 cursorBlinkVisible
= !cursorBlinkVisible
;
574 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
577 if ((swing
.getFrame() != null)
578 && (swing
.getBufferStrategy() != null)
584 } while (swing
.getBufferStrategy().contentsRestored());
586 swing
.getBufferStrategy().show();
587 Toolkit
.getDefaultToolkit().sync();
588 } while (swing
.getBufferStrategy().contentsLost());
591 // Non-triple-buffered, call drawToSwing() once
596 // ------------------------------------------------------------------------
597 // TerminalReader ---------------------------------------------------------
598 // ------------------------------------------------------------------------
601 * Check if there are events in the queue.
603 * @return if true, getEvents() has something to return to the backend
605 public boolean hasEvents() {
606 synchronized (eventQueue
) {
607 return (eventQueue
.size() > 0);
612 * Return any events in the IO queue.
614 * @param queue list to append new events to
616 public void getEvents(final List
<TInputEvent
> queue
) {
617 synchronized (eventQueue
) {
618 if (eventQueue
.size() > 0) {
619 synchronized (queue
) {
620 queue
.addAll(eventQueue
);
628 * Restore terminal to normal state.
630 public void closeTerminal() {
635 * Set listener to a different Object.
637 * @param listener the new listening object that run() wakes up on new
640 public void setListener(final Object listener
) {
641 this.listener
= listener
;
645 * Reload options from System properties.
647 public void reloadOptions() {
648 // Figure out my cursor style.
649 String cursorStyleString
= System
.getProperty(
650 "jexer.Swing.cursorStyle", "underline").toLowerCase();
651 if (cursorStyleString
.equals("underline")) {
652 cursorStyle
= CursorStyle
.UNDERLINE
;
653 } else if (cursorStyleString
.equals("outline")) {
654 cursorStyle
= CursorStyle
.OUTLINE
;
655 } else if (cursorStyleString
.equals("block")) {
656 cursorStyle
= CursorStyle
.BLOCK
;
657 } else if (cursorStyleString
.equals("verticalbar")) {
658 cursorStyle
= CursorStyle
.VERTICAL_BAR
;
661 // Pull the system property for triple buffering.
662 if (System
.getProperty("jexer.Swing.tripleBuffer",
663 "true").equals("true")
665 SwingComponent
.tripleBuffer
= true;
667 SwingComponent
.tripleBuffer
= false;
671 setCustomSystemColors();
674 // ------------------------------------------------------------------------
675 // SwingTerminal ----------------------------------------------------------
676 // ------------------------------------------------------------------------
679 * Get the width of a character cell in pixels.
681 * @return the width in pixels of a character cell
683 public int getTextWidth() {
688 * Get the height of a character cell in pixels.
690 * @return the height in pixels of a character cell
692 public int getTextHeight() {
697 * Setup Swing colors to match DOS color palette.
699 private static void setDOSColors() {
703 MYBLACK
= new Color(0x00, 0x00, 0x00);
704 MYRED
= new Color(0xa8, 0x00, 0x00);
705 MYGREEN
= new Color(0x00, 0xa8, 0x00);
706 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
707 MYBLUE
= new Color(0x00, 0x00, 0xa8);
708 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
709 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
710 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
711 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
712 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
713 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
714 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
715 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
716 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
717 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
718 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
724 * Setup Swing colors to match those provided in system properties.
726 private static void setCustomSystemColors() {
727 synchronized (SwingTerminal
.class) {
728 MYBLACK
= getCustomColor("jexer.Swing.color0", MYBLACK
);
729 MYRED
= getCustomColor("jexer.Swing.color1", MYRED
);
730 MYGREEN
= getCustomColor("jexer.Swing.color2", MYGREEN
);
731 MYYELLOW
= getCustomColor("jexer.Swing.color3", MYYELLOW
);
732 MYBLUE
= getCustomColor("jexer.Swing.color4", MYBLUE
);
733 MYMAGENTA
= getCustomColor("jexer.Swing.color5", MYMAGENTA
);
734 MYCYAN
= getCustomColor("jexer.Swing.color6", MYCYAN
);
735 MYWHITE
= getCustomColor("jexer.Swing.color7", MYWHITE
);
736 MYBOLD_BLACK
= getCustomColor("jexer.Swing.color8", MYBOLD_BLACK
);
737 MYBOLD_RED
= getCustomColor("jexer.Swing.color9", MYBOLD_RED
);
738 MYBOLD_GREEN
= getCustomColor("jexer.Swing.color10", MYBOLD_GREEN
);
739 MYBOLD_YELLOW
= getCustomColor("jexer.Swing.color11", MYBOLD_YELLOW
);
740 MYBOLD_BLUE
= getCustomColor("jexer.Swing.color12", MYBOLD_BLUE
);
741 MYBOLD_MAGENTA
= getCustomColor("jexer.Swing.color13", MYBOLD_MAGENTA
);
742 MYBOLD_CYAN
= getCustomColor("jexer.Swing.color14", MYBOLD_CYAN
);
743 MYBOLD_WHITE
= getCustomColor("jexer.Swing.color15", MYBOLD_WHITE
);
748 * Setup one Swing color to match the RGB value provided in system
751 * @param key the system property key
752 * @param defaultColor the default color to return if key is not set, or
754 * @return a color from the RGB string, or defaultColor
756 private static Color
getCustomColor(final String key
,
757 final Color defaultColor
) {
759 String rgb
= System
.getProperty(key
);
763 if (rgb
.startsWith("#")) {
764 rgb
= rgb
.substring(1);
768 rgbInt
= Integer
.parseInt(rgb
, 16);
769 } catch (NumberFormatException e
) {
772 Color color
= new Color((rgbInt
& 0xFF0000) >>> 16,
773 (rgbInt
& 0x00FF00) >>> 8,
774 (rgbInt
& 0x0000FF));
780 * Get the number of millis to wait before switching the blink from
781 * visible to invisible.
783 * @return the number of milli to wait before switching the blink from
784 * visible to invisible
786 public long getBlinkMillis() {
791 * Get the current status of the blink flag.
793 * @return true if the cursor and blinking text should be visible
795 public boolean getCursorBlinkVisible() {
796 return cursorBlinkVisible
;
800 * Get the font size in points.
802 * @return font size in points
804 public int getFontSize() {
809 * Set the font size in points.
811 * @param fontSize font size in points
813 public void setFontSize(final int fontSize
) {
814 this.fontSize
= fontSize
;
815 Font newFont
= font
.deriveFont((float) fontSize
);
820 * Set to a new font, and resize the screen to match its dimensions.
822 * @param font the new font
824 public void setFont(final Font font
) {
825 if (!SwingUtilities
.isEventDispatchThread()) {
826 // Not in the Swing thread: force this inside the Swing thread.
828 SwingUtilities
.invokeAndWait(new Runnable() {
830 synchronized (this) {
831 SwingTerminal
.this.font
= font
;
834 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
835 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
836 resizeToScreen(true);
840 } catch (InterruptedException e
) {
842 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
846 synchronized (this) {
847 SwingTerminal
.this.font
= font
;
850 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
851 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
852 resizeToScreen(true);
858 * Get the font this screen was last set to.
862 public Font
getFont() {
867 * Set the font to Terminus, the best all-around font for both CP437 and
870 public void setDefaultFont() {
872 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
873 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
874 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
875 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
877 } catch (java
.awt
.FontFormatException e
) {
879 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
880 } catch (java
.io
.IOException e
) {
882 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
889 * Get the X text adjustment.
891 * @return X text adjustment
893 public int getTextAdjustX() {
898 * Set the X text adjustment.
900 * @param textAdjustX the X text adjustment
902 public void setTextAdjustX(final int textAdjustX
) {
903 synchronized (this) {
904 this.textAdjustX
= textAdjustX
;
905 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
906 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
912 * Get the Y text adjustment.
914 * @return Y text adjustment
916 public int getTextAdjustY() {
921 * Set the Y text adjustment.
923 * @param textAdjustY the Y text adjustment
925 public void setTextAdjustY(final int textAdjustY
) {
926 synchronized (this) {
927 this.textAdjustY
= textAdjustY
;
928 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
929 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
935 * Get the height text adjustment.
937 * @return height text adjustment
939 public int getTextAdjustHeight() {
940 return textAdjustHeight
;
944 * Set the height text adjustment.
946 * @param textAdjustHeight the height text adjustment
948 public void setTextAdjustHeight(final int textAdjustHeight
) {
949 synchronized (this) {
950 this.textAdjustHeight
= textAdjustHeight
;
951 textHeight
= fontTextHeight
+ textAdjustHeight
;
952 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
953 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
959 * Get the width text adjustment.
961 * @return width text adjustment
963 public int getTextAdjustWidth() {
964 return textAdjustWidth
;
968 * Set the width text adjustment.
970 * @param textAdjustWidth the width text adjustment
972 public void setTextAdjustWidth(final int textAdjustWidth
) {
973 synchronized (this) {
974 this.textAdjustWidth
= textAdjustWidth
;
975 textWidth
= fontTextWidth
+ textAdjustWidth
;
976 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
977 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
983 * Convert a CellAttributes foreground color to an Swing Color.
985 * @param attr the text attributes
986 * @return the Swing Color
988 public static Color
attrToForegroundColor(final CellAttributes attr
) {
989 int rgb
= attr
.getForeColorRGB();
991 int red
= (rgb
>> 16) & 0xFF;
992 int green
= (rgb
>> 8) & 0xFF;
993 int blue
= rgb
& 0xFF;
995 return new Color(red
, green
, blue
);
999 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
1000 return MYBOLD_BLACK
;
1001 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
1003 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
1005 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
1006 return MYBOLD_GREEN
;
1007 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
1008 return MYBOLD_YELLOW
;
1009 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
1011 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
1012 return MYBOLD_MAGENTA
;
1013 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
1014 return MYBOLD_WHITE
;
1017 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
1019 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
1021 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
1023 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
1025 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
1027 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
1029 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
1031 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
1035 throw new IllegalArgumentException("Invalid color: " +
1036 attr
.getForeColor().getValue());
1040 * Convert a CellAttributes background color to an Swing Color.
1042 * @param attr the text attributes
1043 * @return the Swing Color
1045 public static Color
attrToBackgroundColor(final CellAttributes attr
) {
1046 int rgb
= attr
.getBackColorRGB();
1048 int red
= (rgb
>> 16) & 0xFF;
1049 int green
= (rgb
>> 8) & 0xFF;
1050 int blue
= rgb
& 0xFF;
1052 return new Color(red
, green
, blue
);
1055 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
1057 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
1059 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
1061 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
1063 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
1065 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
1067 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
1069 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
1072 throw new IllegalArgumentException("Invalid color: " +
1073 attr
.getBackColor().getValue());
1077 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
1078 * textAdjustWidth should be, based on the location of a vertical bar and
1081 private void getFontAdjustments() {
1082 BufferedImage image
= null;
1084 // What SHOULD happen is that the topmost/leftmost white pixel is at
1085 // position (gr2x, gr2y). But it might also be off by a pixel in
1086 // either direction.
1088 Graphics2D gr2
= null;
1091 image
= new BufferedImage(fontTextWidth
* 2, fontTextHeight
* 2,
1092 BufferedImage
.TYPE_INT_ARGB
);
1094 gr2
= image
.createGraphics();
1095 gr2
.setFont(swing
.getFont());
1096 gr2
.setColor(java
.awt
.Color
.BLACK
);
1097 gr2
.fillRect(0, 0, fontTextWidth
* 2, fontTextHeight
* 2);
1098 gr2
.setColor(java
.awt
.Color
.WHITE
);
1099 char [] chars
= new char[1];
1100 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
1101 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1102 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
1103 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1106 int top
= fontTextHeight
* 2;
1108 int left
= fontTextWidth
* 2;
1112 textAdjustHeight
= 0;
1113 textAdjustWidth
= 0;
1115 for (int x
= 0; x
< fontTextWidth
* 2; x
++) {
1116 for (int y
= 0; y
< fontTextHeight
* 2; y
++) {
1119 System.err.println("H X: " + x + " Y: " + y + " " +
1120 image.getRGB(x, y));
1123 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
1124 // Pixel is present.
1141 textAdjustX
= (gr2x
- left
);
1142 textAdjustWidth
= fontTextWidth
- (right
- left
+ 1);
1145 textAdjustY
= (gr2y
- top
);
1146 textAdjustHeight
= fontTextHeight
- (bottom
- top
+ 1);
1148 // System.err.println("top " + top + " bottom " + bottom);
1149 // System.err.println("left " + left + " right " + right);
1151 // Special case: do not believe fonts that claim to be wider than
1153 if (fontTextWidth
>= fontTextHeight
) {
1155 textAdjustWidth
= 0;
1156 fontTextWidth
= fontTextHeight
/ 2;
1161 * Figure out my font dimensions. This code path works OK for the JFrame
1162 * case, and can be called immediately after JFrame creation.
1164 private void getFontDimensions() {
1165 swing
.setFont(font
);
1166 Graphics gr
= swing
.getGraphics();
1170 getFontDimensions(gr
);
1174 * Figure out my font dimensions. This code path is needed to lazy-load
1175 * the information inside paint().
1177 * @param gr Graphics object to use
1179 private void getFontDimensions(final Graphics gr
) {
1180 swing
.setFont(font
);
1181 FontMetrics fm
= gr
.getFontMetrics();
1182 maxDescent
= fm
.getMaxDescent();
1183 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
1184 int leading
= fm
.getLeading();
1185 fontTextWidth
= (int)Math
.round(bounds
.getWidth());
1186 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1188 // This produces the same number, but works better for ugly
1190 fontTextHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
1192 getFontAdjustments();
1193 textHeight
= fontTextHeight
+ textAdjustHeight
;
1194 textWidth
= fontTextWidth
+ textAdjustWidth
;
1196 if (sessionInfo
!= null) {
1197 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
1199 gotFontDimensions
= true;
1203 * Resize the physical screen to match the logical screen dimensions.
1205 * @param resizeComponent if true, resize the Swing component
1207 private void resizeToScreen(final boolean resizeComponent
) {
1208 if (resizeComponent
) {
1209 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
1215 * Resize the physical screen to match the logical screen dimensions.
1218 public void resizeToScreen() {
1219 resizeToScreen(false);
1223 * Draw one cell's image to the screen.
1225 * @param gr the Swing Graphics context
1226 * @param cell the Cell to draw
1227 * @param xPixel the x-coordinate to render to. 0 means the
1228 * left-most pixel column.
1229 * @param yPixel the y-coordinate to render to. 0 means the top-most
1232 private void drawImage(final Graphics gr
, final Cell cell
,
1233 final int xPixel
, final int yPixel
) {
1236 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1240 // Draw the background rectangle, then the foreground character.
1241 assert (cell
.isImage());
1243 // Enable anti-aliasing
1244 if (gr
instanceof Graphics2D
) {
1245 ((Graphics2D
) gr
).setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
1246 RenderingHints
.VALUE_ANTIALIAS_ON
);
1247 ((Graphics2D
) gr
).setRenderingHint(RenderingHints
.KEY_RENDERING
,
1248 RenderingHints
.VALUE_RENDER_QUALITY
);
1251 gr
.setColor(cell
.getBackground());
1252 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1254 BufferedImage image
= cell
.getImage();
1255 if (image
!= null) {
1256 if (swing
.getFrame() != null) {
1257 gr
.drawImage(image
, xPixel
, yPixel
, getTextWidth(),
1258 getTextHeight(), swing
.getFrame());
1260 gr
.drawImage(image
, xPixel
, yPixel
, getTextWidth(),
1261 getTextHeight(),swing
.getComponent());
1268 * Draw one glyph to the screen.
1270 * @param gr the Swing Graphics context
1271 * @param cell the Cell to draw
1272 * @param xPixel the x-coordinate to render to. 0 means the
1273 * left-most pixel column.
1274 * @param yPixel the y-coordinate to render to. 0 means the top-most
1277 private void drawGlyph(final Graphics gr
, final Cell cell
,
1278 final int xPixel
, final int yPixel
) {
1281 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1285 BufferedImage image
= null;
1286 if (cell
.isBlink() && !cursorBlinkVisible
) {
1287 image
= glyphCacheBlink
.get(cell
);
1289 image
= glyphCache
.get(cell
);
1291 if (image
!= null) {
1292 if (swing
.getFrame() != null) {
1293 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1295 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1300 // Generate glyph and draw it.
1301 Graphics2D gr2
= null;
1304 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1305 image
= new BufferedImage(textWidth
, textHeight
,
1306 BufferedImage
.TYPE_INT_ARGB
);
1307 gr2
= image
.createGraphics();
1308 gr2
.setFont(swing
.getFont());
1312 gr2
= (Graphics2D
) gr
;
1315 Cell cellColor
= new Cell(cell
);
1317 // Check for reverse
1318 if (cell
.isReverse()) {
1319 cellColor
.setForeColor(cell
.getBackColor());
1320 cellColor
.setBackColor(cell
.getForeColor());
1323 // Enable anti-aliasing
1324 if (gr
instanceof Graphics2D
) {
1325 ((Graphics2D
) gr
).setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
1326 RenderingHints
.VALUE_ANTIALIAS_ON
);
1327 ((Graphics2D
) gr
).setRenderingHint(RenderingHints
.KEY_RENDERING
,
1328 RenderingHints
.VALUE_RENDER_QUALITY
);
1331 // Draw the background rectangle, then the foreground character.
1332 gr2
.setColor(attrToBackgroundColor(cellColor
));
1333 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1336 // Handle blink and underline
1338 || (cell
.isBlink() && cursorBlinkVisible
)
1340 gr2
.setColor(attrToForegroundColor(cellColor
));
1341 char [] chars
= Character
.toChars(cell
.getChar());
1342 gr2
.drawChars(chars
, 0, chars
.length
, gr2x
+ textAdjustX
,
1343 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1345 if (cell
.isUnderline()) {
1346 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1350 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1353 // We need a new key that will not be mutated by
1355 Cell key
= new Cell(cell
);
1356 if (cell
.isBlink() && !cursorBlinkVisible
) {
1357 glyphCacheBlink
.put(key
, image
);
1359 glyphCache
.put(key
, image
);
1362 if (swing
.getFrame() != null) {
1363 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1365 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1372 * Check if the cursor is visible, and if so draw it.
1374 * @param gr the Swing Graphics context
1376 private void drawCursor(final Graphics gr
) {
1381 && (cursorY
<= height
- 1)
1382 && (cursorX
<= width
- 1)
1383 && cursorBlinkVisible
1385 int xPixel
= cursorX
* textWidth
+ left
;
1386 int yPixel
= cursorY
* textHeight
+ top
;
1387 Cell lCell
= logical
[cursorX
][cursorY
];
1388 int cursorWidth
= textWidth
;
1389 switch (lCell
.getWidth()) {
1398 xPixel
-= textWidth
;
1401 gr
.setColor(attrToForegroundColor(lCell
));
1402 switch (cursorStyle
) {
1406 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, cursorWidth
, 2);
1409 gr
.fillRect(xPixel
, yPixel
, cursorWidth
, textHeight
);
1412 gr
.drawRect(xPixel
, yPixel
, cursorWidth
- 1, textHeight
- 1);
1415 gr
.fillRect(xPixel
, yPixel
, 2, textHeight
);
1422 * Reset the blink timer.
1424 private void resetBlinkTimer() {
1425 lastBlinkTime
= System
.currentTimeMillis();
1426 cursorBlinkVisible
= true;
1430 * Paint redraws the whole screen.
1432 * @param gr the Swing Graphics context
1434 public void paint(final Graphics gr
) {
1436 if (gotFontDimensions
== false) {
1437 // Lazy-load the text width/height
1438 getFontDimensions(gr
);
1440 System.err.println("textWidth " + textWidth +
1441 " textHeight " + textHeight);
1442 System.err.println("FONT: " + swing.getFont() + " font " + font);
1446 if ((swing
.getFrame() != null)
1447 && (swing
.getBufferStrategy() != null)
1448 && (SwingUtilities
.isEventDispatchThread())
1450 // System.err.println("paint(), skip first paint on swing thread");
1455 int xCellMax
= width
;
1457 int yCellMax
= height
;
1459 Rectangle bounds
= gr
.getClipBounds();
1460 if (bounds
!= null) {
1461 // Only update what is in the bounds
1462 xCellMin
= textColumn(bounds
.x
);
1463 xCellMax
= textColumn(bounds
.x
+ bounds
.width
) + 1;
1464 if (xCellMax
> width
) {
1467 if (xCellMin
>= xCellMax
) {
1468 xCellMin
= xCellMax
- 2;
1473 yCellMin
= textRow(bounds
.y
);
1474 yCellMax
= textRow(bounds
.y
+ bounds
.height
) + 1;
1475 if (yCellMax
> height
) {
1478 if (yCellMin
>= yCellMax
) {
1479 yCellMin
= yCellMax
- 2;
1485 // We need a total repaint
1486 reallyCleared
= true;
1489 // Prevent updates to the screen's data from the TApplication
1491 synchronized (this) {
1494 System.err.printf("bounds %s X %d %d Y %d %d\n",
1495 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1498 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1499 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1501 int xPixel
= x
* textWidth
+ left
;
1502 int yPixel
= y
* textHeight
+ top
;
1504 Cell lCell
= logical
[x
][y
];
1505 Cell pCell
= physical
[x
][y
];
1507 if (!lCell
.equals(pCell
)
1510 || (swing
.getFrame() == null)) {
1512 if (lCell
.isImage()) {
1513 drawImage(gr
, lCell
, xPixel
, yPixel
);
1515 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1518 // Physical is always updated
1519 physical
[x
][y
].setTo(lCell
);
1525 reallyCleared
= false;
1526 } // synchronized (this)
1530 * Restore terminal to normal state.
1532 public void shutdown() {
1537 * Push the logical screen to the physical device.
1539 private void drawToSwing() {
1542 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1543 reallyCleared, dirty);
1546 // If reallyCleared is set, we have to draw everything.
1547 if ((swing
.getFrame() != null)
1548 && (swing
.getBufferStrategy() != null)
1549 && (reallyCleared
== true)
1551 // Triple-buffering: we have to redraw everything on this thread.
1552 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1555 swing
.getBufferStrategy().show();
1556 Toolkit
.getDefaultToolkit().sync();
1558 } else if (((swing
.getFrame() != null)
1559 && (swing
.getBufferStrategy() == null))
1560 || (reallyCleared
== true)
1562 // Repaint everything on the Swing thread.
1563 // System.err.println("REPAINT ALL");
1568 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1569 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1571 synchronized (this) {
1572 for (int y
= 0; y
< height
; y
++) {
1573 for (int x
= 0; x
< width
; x
++) {
1574 Cell lCell
= logical
[x
][y
];
1575 Cell pCell
= physical
[x
][y
];
1577 int xPixel
= x
* textWidth
+ left
;
1578 int yPixel
= y
* textHeight
+ top
;
1580 if (!lCell
.equals(pCell
)
1584 || (lCell
.isBlink())
1586 if (lCell
.isImage()) {
1587 drawImage(gr
, lCell
, xPixel
, yPixel
);
1589 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1591 physical
[x
][y
].setTo(lCell
);
1596 } // synchronized (this)
1599 swing
.getBufferStrategy().show();
1600 Toolkit
.getDefaultToolkit().sync();
1604 // Swing thread version: request a repaint, but limit it to the area
1605 // that has changed.
1607 // Find the minimum-size damaged region.
1608 int xMin
= swing
.getWidth();
1610 int yMin
= swing
.getHeight();
1613 synchronized (this) {
1614 for (int y
= 0; y
< height
; y
++) {
1615 for (int x
= 0; x
< width
; x
++) {
1616 Cell lCell
= logical
[x
][y
];
1617 Cell pCell
= physical
[x
][y
];
1619 int xPixel
= x
* textWidth
+ left
;
1620 int yPixel
= y
* textHeight
+ top
;
1622 if (!lCell
.equals(pCell
)
1628 if (xPixel
< xMin
) {
1631 if (xPixel
+ textWidth
> xMax
) {
1632 xMax
= xPixel
+ textWidth
;
1634 if (yPixel
< yMin
) {
1637 if (yPixel
+ textHeight
> yMax
) {
1638 yMax
= yPixel
+ textHeight
;
1644 if (xMin
+ textWidth
>= xMax
) {
1647 if (yMin
+ textHeight
>= yMax
) {
1651 // Repaint the desired area
1653 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1657 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1658 // This path should never be taken, but is left here for
1660 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1661 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1666 swing
.getBufferStrategy().show();
1667 Toolkit
.getDefaultToolkit().sync();
1669 // Repaint on the Swing thread.
1670 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1675 * Convert pixel column position to text cell column position.
1677 * @param x pixel column position
1678 * @return text cell column position
1680 public int textColumn(final int x
) {
1681 int column
= ((x
- left
) / textWidth
);
1685 if (column
> width
- 1) {
1692 * Convert pixel row position to text cell row position.
1694 * @param y pixel row position
1695 * @return text cell row position
1697 public int textRow(final int y
) {
1698 int row
= ((y
- top
) / textHeight
);
1702 if (row
> height
- 1) {
1709 * Getter for sessionInfo.
1711 * @return the SessionInfo
1713 public SessionInfo
getSessionInfo() {
1718 * Getter for the underlying Swing component.
1720 * @return the SwingComponent
1722 public SwingComponent
getSwingComponent() {
1726 // ------------------------------------------------------------------------
1727 // KeyListener ------------------------------------------------------------
1728 // ------------------------------------------------------------------------
1731 * Pass Swing keystrokes into the event queue.
1733 * @param key keystroke received
1735 public void keyReleased(final KeyEvent key
) {
1736 // Ignore release events
1740 * Pass Swing keystrokes into the event queue.
1742 * @param key keystroke received
1744 public void keyTyped(final KeyEvent key
) {
1745 // Ignore typed events
1749 * Pass Swing keystrokes into the event queue.
1751 * @param key keystroke received
1753 public void keyPressed(final KeyEvent key
) {
1754 boolean alt
= false;
1755 boolean shift
= false;
1756 boolean ctrl
= false;
1758 boolean isKey
= false;
1759 if (key
.isActionKey()) {
1762 ch
= key
.getKeyChar();
1764 alt
= key
.isAltDown();
1765 ctrl
= key
.isControlDown();
1766 shift
= key
.isShiftDown();
1769 System.err.printf("Swing Key: %s\n", key);
1770 System.err.printf(" isKey: %s\n", isKey);
1771 System.err.printf(" alt: %s\n", alt);
1772 System.err.printf(" ctrl: %s\n", ctrl);
1773 System.err.printf(" shift: %s\n", shift);
1774 System.err.printf(" ch: %s\n", ch);
1777 // Special case: not return the bare modifier presses
1778 switch (key
.getKeyCode()) {
1779 case KeyEvent
.VK_ALT
:
1781 case KeyEvent
.VK_ALT_GRAPH
:
1783 case KeyEvent
.VK_CONTROL
:
1785 case KeyEvent
.VK_SHIFT
:
1787 case KeyEvent
.VK_META
:
1793 TKeypress keypress
= null;
1795 switch (key
.getKeyCode()) {
1796 case KeyEvent
.VK_F1
:
1797 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1800 case KeyEvent
.VK_F2
:
1801 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1804 case KeyEvent
.VK_F3
:
1805 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1808 case KeyEvent
.VK_F4
:
1809 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1812 case KeyEvent
.VK_F5
:
1813 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1816 case KeyEvent
.VK_F6
:
1817 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1820 case KeyEvent
.VK_F7
:
1821 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1824 case KeyEvent
.VK_F8
:
1825 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1828 case KeyEvent
.VK_F9
:
1829 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1832 case KeyEvent
.VK_F10
:
1833 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1836 case KeyEvent
.VK_F11
:
1837 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1840 case KeyEvent
.VK_F12
:
1841 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1844 case KeyEvent
.VK_HOME
:
1845 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1848 case KeyEvent
.VK_END
:
1849 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1852 case KeyEvent
.VK_PAGE_UP
:
1853 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1856 case KeyEvent
.VK_PAGE_DOWN
:
1857 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1860 case KeyEvent
.VK_INSERT
:
1861 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1864 case KeyEvent
.VK_DELETE
:
1865 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1868 case KeyEvent
.VK_RIGHT
:
1869 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1872 case KeyEvent
.VK_LEFT
:
1873 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1876 case KeyEvent
.VK_UP
:
1877 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1880 case KeyEvent
.VK_DOWN
:
1881 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1884 case KeyEvent
.VK_TAB
:
1885 // Special case: distinguish TAB vs BTAB
1887 keypress
= kbShiftTab
;
1892 case KeyEvent
.VK_ENTER
:
1893 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1896 case KeyEvent
.VK_ESCAPE
:
1897 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1900 case KeyEvent
.VK_BACK_SPACE
:
1901 keypress
= kbBackspace
;
1904 // Unsupported, ignore
1909 if (keypress
== null) {
1912 // Disambiguate ^H from Backspace.
1913 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1915 keypress
= kbBackspace
;
1917 // We are emulating Xterm here, where the backspace key
1918 // on the keyboard returns ^?.
1919 keypress
= kbBackspaceDel
;
1933 keypress
= kbShiftTab
;
1942 if (!alt
&& ctrl
&& !shift
) {
1943 // Control character, replace ch with 'A', 'B', etc.
1944 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1946 // Not a special key, put it together
1947 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1951 // Save it and we are done.
1952 synchronized (eventQueue
) {
1953 eventQueue
.add(new TKeypressEvent(keypress
));
1956 if (listener
!= null) {
1957 synchronized (listener
) {
1958 listener
.notifyAll();
1963 // ------------------------------------------------------------------------
1964 // WindowListener ---------------------------------------------------------
1965 // ------------------------------------------------------------------------
1968 * Pass window events into the event queue.
1970 * @param event window event received
1972 public void windowActivated(final WindowEvent event
) {
1973 // Force a total repaint
1974 synchronized (this) {
1980 * Pass window events into the event queue.
1982 * @param event window event received
1984 public void windowClosed(final WindowEvent event
) {
1989 * Pass window events into the event queue.
1991 * @param event window event received
1993 public void windowClosing(final WindowEvent event
) {
1994 // Drop a cmBackendDisconnect and walk away
1995 synchronized (eventQueue
) {
1996 eventQueue
.add(new TCommandEvent(cmBackendDisconnect
));
1999 if (listener
!= null) {
2000 synchronized (listener
) {
2001 listener
.notifyAll();
2007 * Pass window events into the event queue.
2009 * @param event window event received
2011 public void windowDeactivated(final WindowEvent event
) {
2016 * Pass window events into the event queue.
2018 * @param event window event received
2020 public void windowDeiconified(final WindowEvent event
) {
2025 * Pass window events into the event queue.
2027 * @param event window event received
2029 public void windowIconified(final WindowEvent event
) {
2034 * Pass window events into the event queue.
2036 * @param event window event received
2038 public void windowOpened(final WindowEvent event
) {
2042 // ------------------------------------------------------------------------
2043 // ComponentListener ------------------------------------------------------
2044 // ------------------------------------------------------------------------
2047 * Pass component events into the event queue.
2049 * @param event component event received
2051 public void componentHidden(final ComponentEvent event
) {
2056 * Pass component events into the event queue.
2058 * @param event component event received
2060 public void componentShown(final ComponentEvent event
) {
2065 * Pass component events into the event queue.
2067 * @param event component event received
2069 public void componentMoved(final ComponentEvent event
) {
2074 * Pass component events into the event queue.
2076 * @param event component event received
2078 public void componentResized(final ComponentEvent event
) {
2079 if (gotFontDimensions
== false) {
2080 // We are still waiting to get font information. Don't pass a
2082 // System.err.println("size " + swing.getComponent().getSize());
2086 if (sessionInfo
== null) {
2087 // This is the initial component resize in construction, bail
2092 // Drop a new TResizeEvent into the queue
2093 sessionInfo
.queryWindowSize();
2094 synchronized (eventQueue
) {
2095 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
2096 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
2097 eventQueue
.add(windowResize
);
2100 System.err.println("Add resize event: " + windowResize.getWidth() +
2101 " x " + windowResize.getHeight());
2104 if (listener
!= null) {
2105 synchronized (listener
) {
2106 listener
.notifyAll();
2111 // ------------------------------------------------------------------------
2112 // MouseMotionListener ----------------------------------------------------
2113 // ------------------------------------------------------------------------
2116 * Pass mouse events into the event queue.
2118 * @param mouse mouse event received
2120 public void mouseDragged(final MouseEvent mouse
) {
2121 int modifiers
= mouse
.getModifiersEx();
2122 boolean eventMouse1
= false;
2123 boolean eventMouse2
= false;
2124 boolean eventMouse3
= false;
2125 boolean eventAlt
= false;
2126 boolean eventCtrl
= false;
2127 boolean eventShift
= false;
2129 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2132 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2135 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2138 if ((modifiers
& MouseEvent
.ALT_DOWN_MASK
) != 0) {
2141 if ((modifiers
& MouseEvent
.CTRL_DOWN_MASK
) != 0) {
2144 if ((modifiers
& MouseEvent
.SHIFT_DOWN_MASK
) != 0) {
2148 mouse1
= eventMouse1
;
2149 mouse2
= eventMouse2
;
2150 mouse3
= eventMouse3
;
2151 int x
= textColumn(mouse
.getX());
2152 int y
= textRow(mouse
.getY());
2154 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2155 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false,
2156 eventAlt
, eventCtrl
, eventShift
);
2158 synchronized (eventQueue
) {
2159 eventQueue
.add(mouseEvent
);
2162 if (listener
!= null) {
2163 synchronized (listener
) {
2164 listener
.notifyAll();
2170 * Pass mouse events into the event queue.
2172 * @param mouse mouse event received
2174 public void mouseMoved(final MouseEvent mouse
) {
2175 int x
= textColumn(mouse
.getX());
2176 int y
= textRow(mouse
.getY());
2177 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2178 // Bail out, we've moved some pixels but not a whole text cell.
2184 boolean eventAlt
= false;
2185 boolean eventCtrl
= false;
2186 boolean eventShift
= false;
2188 int modifiers
= mouse
.getModifiersEx();
2189 if ((modifiers
& MouseEvent
.ALT_DOWN_MASK
) != 0) {
2192 if ((modifiers
& MouseEvent
.CTRL_DOWN_MASK
) != 0) {
2195 if ((modifiers
& MouseEvent
.SHIFT_DOWN_MASK
) != 0) {
2199 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2200 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false,
2201 eventAlt
, eventCtrl
, eventShift
);
2203 synchronized (eventQueue
) {
2204 eventQueue
.add(mouseEvent
);
2207 if (listener
!= null) {
2208 synchronized (listener
) {
2209 listener
.notifyAll();
2214 // ------------------------------------------------------------------------
2215 // MouseListener ----------------------------------------------------------
2216 // ------------------------------------------------------------------------
2219 * Pass mouse events into the event queue.
2221 * @param mouse mouse event received
2223 public void mouseClicked(final MouseEvent mouse
) {
2228 * Pass mouse events into the event queue.
2230 * @param mouse mouse event received
2232 public void mouseEntered(final MouseEvent mouse
) {
2233 swing
.requestFocusInWindow();
2237 * Pass mouse events into the event queue.
2239 * @param mouse mouse event received
2241 public void mouseExited(final MouseEvent mouse
) {
2246 * Pass mouse events into the event queue.
2248 * @param mouse mouse event received
2250 public void mousePressed(final MouseEvent mouse
) {
2251 int modifiers
= mouse
.getModifiersEx();
2252 boolean eventMouse1
= false;
2253 boolean eventMouse2
= false;
2254 boolean eventMouse3
= false;
2255 boolean eventAlt
= false;
2256 boolean eventCtrl
= false;
2257 boolean eventShift
= false;
2259 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2262 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2265 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2268 if ((modifiers
& MouseEvent
.ALT_DOWN_MASK
) != 0) {
2271 if ((modifiers
& MouseEvent
.CTRL_DOWN_MASK
) != 0) {
2274 if ((modifiers
& MouseEvent
.SHIFT_DOWN_MASK
) != 0) {
2278 mouse1
= eventMouse1
;
2279 mouse2
= eventMouse2
;
2280 mouse3
= eventMouse3
;
2281 int x
= textColumn(mouse
.getX());
2282 int y
= textRow(mouse
.getY());
2284 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2285 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false,
2286 eventAlt
, eventCtrl
, eventShift
);
2288 synchronized (eventQueue
) {
2289 eventQueue
.add(mouseEvent
);
2292 if (listener
!= null) {
2293 synchronized (listener
) {
2294 listener
.notifyAll();
2300 * Pass mouse events into the event queue.
2302 * @param mouse mouse event received
2304 public void mouseReleased(final MouseEvent mouse
) {
2305 int modifiers
= mouse
.getModifiersEx();
2306 boolean eventMouse1
= false;
2307 boolean eventMouse2
= false;
2308 boolean eventMouse3
= false;
2309 boolean eventAlt
= false;
2310 boolean eventCtrl
= false;
2311 boolean eventShift
= false;
2313 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2316 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2319 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2322 if ((modifiers
& MouseEvent
.ALT_DOWN_MASK
) != 0) {
2325 if ((modifiers
& MouseEvent
.CTRL_DOWN_MASK
) != 0) {
2328 if ((modifiers
& MouseEvent
.SHIFT_DOWN_MASK
) != 0) {
2344 int x
= textColumn(mouse
.getX());
2345 int y
= textRow(mouse
.getY());
2347 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2348 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false,
2349 eventAlt
, eventCtrl
, eventShift
);
2351 synchronized (eventQueue
) {
2352 eventQueue
.add(mouseEvent
);
2355 if (listener
!= null) {
2356 synchronized (listener
) {
2357 listener
.notifyAll();
2362 // ------------------------------------------------------------------------
2363 // MouseWheelListener -----------------------------------------------------
2364 // ------------------------------------------------------------------------
2367 * Pass mouse events into the event queue.
2369 * @param mouse mouse event received
2371 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2372 int modifiers
= mouse
.getModifiersEx();
2373 boolean eventMouse1
= false;
2374 boolean eventMouse2
= false;
2375 boolean eventMouse3
= false;
2376 boolean mouseWheelUp
= false;
2377 boolean mouseWheelDown
= false;
2378 boolean eventAlt
= false;
2379 boolean eventCtrl
= false;
2380 boolean eventShift
= false;
2382 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2385 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2388 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2391 if ((modifiers
& MouseEvent
.ALT_DOWN_MASK
) != 0) {
2394 if ((modifiers
& MouseEvent
.CTRL_DOWN_MASK
) != 0) {
2397 if ((modifiers
& MouseEvent
.SHIFT_DOWN_MASK
) != 0) {
2401 mouse1
= eventMouse1
;
2402 mouse2
= eventMouse2
;
2403 mouse3
= eventMouse3
;
2404 int x
= textColumn(mouse
.getX());
2405 int y
= textRow(mouse
.getY());
2406 if (mouse
.getWheelRotation() > 0) {
2407 mouseWheelDown
= true;
2409 if (mouse
.getWheelRotation() < 0) {
2410 mouseWheelUp
= true;
2413 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2414 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
,
2415 eventAlt
, eventCtrl
, eventShift
);
2417 synchronized (eventQueue
) {
2418 eventQueue
.add(mouseEvent
);
2421 if (listener
!= null) {
2422 synchronized (listener
) {
2423 listener
.notifyAll();