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
.Toolkit
;
40 import java
.awt
.event
.ComponentEvent
;
41 import java
.awt
.event
.ComponentListener
;
42 import java
.awt
.event
.KeyEvent
;
43 import java
.awt
.event
.KeyListener
;
44 import java
.awt
.event
.MouseEvent
;
45 import java
.awt
.event
.MouseListener
;
46 import java
.awt
.event
.MouseMotionListener
;
47 import java
.awt
.event
.MouseWheelEvent
;
48 import java
.awt
.event
.MouseWheelListener
;
49 import java
.awt
.event
.WindowEvent
;
50 import java
.awt
.event
.WindowListener
;
51 import java
.awt
.geom
.Rectangle2D
;
52 import java
.awt
.image
.BufferedImage
;
53 import java
.io
.InputStream
;
54 import java
.util
.ArrayList
;
55 import java
.util
.HashMap
;
56 import java
.util
.List
;
58 import javax
.swing
.JComponent
;
59 import javax
.swing
.JFrame
;
60 import javax
.swing
.ImageIcon
;
61 import javax
.swing
.SwingUtilities
;
63 import jexer
.TKeypress
;
64 import jexer
.bits
.Cell
;
65 import jexer
.bits
.CellAttributes
;
66 import jexer
.event
.TCommandEvent
;
67 import jexer
.event
.TInputEvent
;
68 import jexer
.event
.TKeypressEvent
;
69 import jexer
.event
.TMouseEvent
;
70 import jexer
.event
.TResizeEvent
;
71 import static jexer
.TCommand
.*;
72 import static jexer
.TKeypress
.*;
75 * This Screen backend reads keystrokes and mouse events and draws to either
76 * a Java Swing JFrame (potentially triple-buffered) or a JComponent.
78 * This class is a bit of an inversion of typical GUI classes. It performs
79 * all of the drawing logic from SwingTerminal (which is not a Swing class),
80 * and uses a SwingComponent wrapper class to call the JFrame or JComponent
83 public class SwingTerminal
extends LogicalScreen
84 implements TerminalReader
,
85 ComponentListener
, KeyListener
,
86 MouseListener
, MouseMotionListener
,
87 MouseWheelListener
, WindowListener
{
89 // ------------------------------------------------------------------------
90 // Constants --------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 * The icon image location.
96 private static final String ICONFILE
= "jexer_logo_128.png";
99 * The terminus font resource filename.
101 public static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
104 * Cursor style to draw.
106 public enum CursorStyle
{
108 * Use an underscore for the cursor.
113 * Use a solid block for the cursor.
118 * Use an outlined block for the cursor.
123 // ------------------------------------------------------------------------
124 // Variables --------------------------------------------------------------
125 // ------------------------------------------------------------------------
127 // Colors to map DOS colors to AWT colors.
128 private static Color MYBLACK
;
129 private static Color MYRED
;
130 private static Color MYGREEN
;
131 private static Color MYYELLOW
;
132 private static Color MYBLUE
;
133 private static Color MYMAGENTA
;
134 private static Color MYCYAN
;
135 private static Color MYWHITE
;
136 private static Color MYBOLD_BLACK
;
137 private static Color MYBOLD_RED
;
138 private static Color MYBOLD_GREEN
;
139 private static Color MYBOLD_YELLOW
;
140 private static Color MYBOLD_BLUE
;
141 private static Color MYBOLD_MAGENTA
;
142 private static Color MYBOLD_CYAN
;
143 private static Color MYBOLD_WHITE
;
146 * When true, all the MYBLACK, MYRED, etc. colors are set.
148 private static boolean dosColors
= false;
151 * The Swing component or frame to draw to.
153 private SwingComponent swing
;
156 * A cache of previously-rendered glyphs for blinking text, when it is
159 private Map
<Cell
, BufferedImage
> glyphCacheBlink
;
162 * A cache of previously-rendered glyphs for non-blinking, or
163 * blinking-and-visible, text.
165 private Map
<Cell
, BufferedImage
> glyphCache
;
168 * If true, we were successful at getting the font dimensions.
170 private boolean gotFontDimensions
= false;
173 * The currently selected font.
175 private Font font
= null;
178 * The currently selected font size in points.
180 private int fontSize
= 16;
183 * Width of a character cell in pixels.
185 private int textWidth
= 1;
188 * Height of a character cell in pixels.
190 private int textHeight
= 1;
193 * Width of a character cell in pixels, as reported by font.
195 private int fontTextWidth
= 1;
198 * Height of a character cell in pixels, as reported by font.
200 private int fontTextHeight
= 1;
203 * Descent of a character cell in pixels.
205 private int maxDescent
= 0;
208 * System-dependent Y adjustment for text in the character cell.
210 private int textAdjustY
= 0;
213 * System-dependent X adjustment for text in the character cell.
215 private int textAdjustX
= 0;
218 * System-dependent height adjustment for text in the character cell.
220 private int textAdjustHeight
= 0;
223 * System-dependent width adjustment for text in the character cell.
225 private int textAdjustWidth
= 0;
228 * Top pixel absolute location.
230 private int top
= 30;
233 * Left pixel absolute location.
235 private int left
= 30;
238 * The cursor style to draw.
240 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
243 * The number of millis to wait before switching the blink from visible
244 * to invisible. Set to 0 or negative to disable blinking.
246 private long blinkMillis
= 500;
249 * If true, the cursor should be visible right now based on the blink
252 private boolean cursorBlinkVisible
= true;
255 * The time that the blink last flipped from visible to invisible or
256 * from invisible to visible.
258 private long lastBlinkTime
= 0;
261 * The session information.
263 private SwingSessionInfo sessionInfo
;
266 * The listening object that run() wakes up on new input.
268 private Object listener
;
271 * The event queue, filled up by a thread reading on input.
273 private List
<TInputEvent
> eventQueue
;
276 * The last reported mouse X position.
278 private int oldMouseX
= -1;
281 * The last reported mouse Y position.
283 private int oldMouseY
= -1;
286 * true if mouse1 was down. Used to report mouse1 on the release event.
288 private boolean mouse1
= false;
291 * true if mouse2 was down. Used to report mouse2 on the release event.
293 private boolean mouse2
= false;
296 * true if mouse3 was down. Used to report mouse3 on the release event.
298 private boolean mouse3
= false;
300 // ------------------------------------------------------------------------
301 // Constructors -----------------------------------------------------------
302 // ------------------------------------------------------------------------
305 * Static constructor.
312 * Public constructor creates a new JFrame to render to.
314 * @param windowWidth the number of text columns to start with
315 * @param windowHeight the number of text rows to start with
316 * @param fontSize the size in points. Good values to pick are: 16, 20,
318 * @param listener the object this backend needs to wake up when new
321 public SwingTerminal(final int windowWidth
, final int windowHeight
,
322 final int fontSize
, final Object listener
) {
324 this.fontSize
= fontSize
;
329 SwingUtilities
.invokeAndWait(new Runnable() {
332 JFrame frame
= new JFrame() {
335 * Serializable version.
337 private static final long serialVersionUID
= 1;
340 * The code that performs the actual drawing.
342 public SwingTerminal screen
= null;
345 * Anonymous class initializer saves the screen
346 * reference, so that paint() and the like call out
350 this.screen
= SwingTerminal
.this;
354 * Update redraws the whole screen.
356 * @param gr the Swing Graphics context
359 public void update(final Graphics gr
) {
360 // The default update clears the area. Don't do
361 // that, instead just paint it directly.
366 * Paint redraws the whole screen.
368 * @param gr the Swing Graphics context
371 public void paint(final Graphics gr
) {
372 if (screen
!= null) {
379 ClassLoader loader
= Thread
.currentThread().
380 getContextClassLoader();
381 frame
.setIconImage((new ImageIcon(loader
.
382 getResource(ICONFILE
))).getImage());
384 // Get the Swing component
385 SwingTerminal
.this.swing
= new SwingComponent(frame
);
387 // Hang onto top and left for drawing.
388 Insets insets
= SwingTerminal
.this.swing
.getInsets();
389 SwingTerminal
.this.left
= insets
.left
;
390 SwingTerminal
.this.top
= insets
.top
;
392 // Load the font so that we can set sessionInfo.
395 // Get the default cols x rows and set component size
397 SwingTerminal
.this.sessionInfo
=
398 new SwingSessionInfo(SwingTerminal
.this.swing
,
399 SwingTerminal
.this.textWidth
,
400 SwingTerminal
.this.textHeight
,
401 windowWidth
, windowHeight
);
403 SwingTerminal
.this.setDimensions(sessionInfo
.
404 getWindowWidth(), sessionInfo
.getWindowHeight());
406 SwingTerminal
.this.resizeToScreen();
407 SwingTerminal
.this.swing
.setVisible(true);
410 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
412 } catch (InterruptedException e
) {
416 this.listener
= listener
;
420 eventQueue
= new ArrayList
<TInputEvent
>();
422 // Add listeners to Swing.
423 swing
.addKeyListener(this);
424 swing
.addWindowListener(this);
425 swing
.addComponentListener(this);
426 swing
.addMouseListener(this);
427 swing
.addMouseMotionListener(this);
428 swing
.addMouseWheelListener(this);
432 * Public constructor renders to an existing JComponent.
434 * @param component the Swing component to render to
435 * @param windowWidth the number of text columns to start with
436 * @param windowHeight the number of text rows to start with
437 * @param fontSize the size in points. Good values to pick are: 16, 20,
439 * @param listener the object this backend needs to wake up when new
442 public SwingTerminal(final JComponent component
, final int windowWidth
,
443 final int windowHeight
, final int fontSize
, final Object listener
) {
445 this.fontSize
= fontSize
;
450 SwingUtilities
.invokeAndWait(new Runnable() {
453 JComponent newComponent
= new JComponent() {
456 * Serializable version.
458 private static final long serialVersionUID
= 1;
461 * The code that performs the actual drawing.
463 public SwingTerminal screen
= null;
466 * Anonymous class initializer saves the screen
467 * reference, so that paint() and the like call out
471 this.screen
= SwingTerminal
.this;
475 * Update redraws the whole screen.
477 * @param gr the Swing Graphics context
480 public void update(final Graphics gr
) {
481 // The default update clears the area. Don't do
482 // that, instead just paint it directly.
487 * Paint redraws the whole screen.
489 * @param gr the Swing Graphics context
492 public void paint(final Graphics gr
) {
493 if (screen
!= null) {
498 component
.setLayout(new BorderLayout());
499 component
.add(newComponent
);
501 // Allow key events to be received
502 component
.setFocusable(true);
504 // Get the Swing component
505 SwingTerminal
.this.swing
= new SwingComponent(component
);
507 // Hang onto top and left for drawing.
508 Insets insets
= SwingTerminal
.this.swing
.getInsets();
509 SwingTerminal
.this.left
= insets
.left
;
510 SwingTerminal
.this.top
= insets
.top
;
512 // Load the font so that we can set sessionInfo.
515 // Get the default cols x rows and set component size
517 SwingTerminal
.this.sessionInfo
=
518 new SwingSessionInfo(SwingTerminal
.this.swing
,
519 SwingTerminal
.this.textWidth
,
520 SwingTerminal
.this.textHeight
);
523 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
525 } catch (InterruptedException e
) {
529 this.listener
= listener
;
533 eventQueue
= new ArrayList
<TInputEvent
>();
535 // Add listeners to Swing.
536 swing
.addKeyListener(this);
537 swing
.addWindowListener(this);
538 swing
.addComponentListener(this);
539 swing
.addMouseListener(this);
540 swing
.addMouseMotionListener(this);
541 swing
.addMouseWheelListener(this);
544 // ------------------------------------------------------------------------
545 // LogicalScreen ----------------------------------------------------------
546 // ------------------------------------------------------------------------
549 * Set the window title.
551 * @param title the new title
554 public void setTitle(final String title
) {
555 swing
.setTitle(title
);
559 * Push the logical screen to the physical device.
562 public void flushPhysical() {
563 // See if it is time to flip the blink time.
564 long nowTime
= System
.currentTimeMillis();
565 if (nowTime
>= blinkMillis
+ lastBlinkTime
) {
566 lastBlinkTime
= nowTime
;
567 cursorBlinkVisible
= !cursorBlinkVisible
;
568 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
571 if ((swing
.getFrame() != null)
572 && (swing
.getBufferStrategy() != null)
577 } while (swing
.getBufferStrategy().contentsRestored());
579 swing
.getBufferStrategy().show();
580 Toolkit
.getDefaultToolkit().sync();
581 } while (swing
.getBufferStrategy().contentsLost());
584 // Non-triple-buffered, call drawToSwing() once
589 // ------------------------------------------------------------------------
590 // TerminalReader ---------------------------------------------------------
591 // ------------------------------------------------------------------------
594 * Check if there are events in the queue.
596 * @return if true, getEvents() has something to return to the backend
598 public boolean hasEvents() {
599 synchronized (eventQueue
) {
600 return (eventQueue
.size() > 0);
605 * Return any events in the IO queue.
607 * @param queue list to append new events to
609 public void getEvents(final List
<TInputEvent
> queue
) {
610 synchronized (eventQueue
) {
611 if (eventQueue
.size() > 0) {
612 synchronized (queue
) {
613 queue
.addAll(eventQueue
);
621 * Restore terminal to normal state.
623 public void closeTerminal() {
628 * Set listener to a different Object.
630 * @param listener the new listening object that run() wakes up on new
633 public void setListener(final Object listener
) {
634 this.listener
= listener
;
638 * Reload options from System properties.
640 public void reloadOptions() {
641 // Figure out my cursor style.
642 String cursorStyleString
= System
.getProperty(
643 "jexer.Swing.cursorStyle", "underline").toLowerCase();
644 if (cursorStyleString
.equals("underline")) {
645 cursorStyle
= CursorStyle
.UNDERLINE
;
646 } else if (cursorStyleString
.equals("outline")) {
647 cursorStyle
= CursorStyle
.OUTLINE
;
648 } else if (cursorStyleString
.equals("block")) {
649 cursorStyle
= CursorStyle
.BLOCK
;
652 // Pull the system property for triple buffering.
653 if (System
.getProperty("jexer.Swing.tripleBuffer",
654 "true").equals("true")
656 SwingComponent
.tripleBuffer
= true;
658 SwingComponent
.tripleBuffer
= false;
662 // ------------------------------------------------------------------------
663 // SwingTerminal ----------------------------------------------------------
664 // ------------------------------------------------------------------------
667 * Get the width of a character cell in pixels.
669 * @return the width in pixels of a character cell
671 public int getTextWidth() {
676 * Get the height of a character cell in pixels.
678 * @return the height in pixels of a character cell
680 public int getTextHeight() {
685 * Setup Swing colors to match DOS color palette.
687 private static void setDOSColors() {
691 MYBLACK
= new Color(0x00, 0x00, 0x00);
692 MYRED
= new Color(0xa8, 0x00, 0x00);
693 MYGREEN
= new Color(0x00, 0xa8, 0x00);
694 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
695 MYBLUE
= new Color(0x00, 0x00, 0xa8);
696 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
697 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
698 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
699 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
700 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
701 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
702 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
703 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
704 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
705 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
706 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
712 * Get the number of millis to wait before switching the blink from
713 * visible to invisible.
715 * @return the number of milli to wait before switching the blink from
716 * visible to invisible
718 public long getBlinkMillis() {
723 * Get the current status of the blink flag.
725 * @return true if the cursor and blinking text should be visible
727 public boolean getCursorBlinkVisible() {
728 return cursorBlinkVisible
;
732 * Get the font size in points.
734 * @return font size in points
736 public int getFontSize() {
741 * Set the font size in points.
743 * @param fontSize font size in points
745 public void setFontSize(final int fontSize
) {
746 this.fontSize
= fontSize
;
747 Font newFont
= font
.deriveFont((float) fontSize
);
752 * Set to a new font, and resize the screen to match its dimensions.
754 * @param font the new font
756 public void setFont(final Font font
) {
757 synchronized (this) {
761 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
762 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
768 * Get the font this screen was last set to.
772 public Font
getFont() {
777 * Set the font to Terminus, the best all-around font for both CP437 and
780 public void setDefaultFont() {
782 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
783 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
784 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
785 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
787 } catch (java
.awt
.FontFormatException e
) {
789 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
790 } catch (java
.io
.IOException e
) {
792 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
799 * Get the X text adjustment.
801 * @return X text adjustment
803 public int getTextAdjustX() {
808 * Set the X text adjustment.
810 * @param textAdjustX the X text adjustment
812 public void setTextAdjustX(final int textAdjustX
) {
813 synchronized (this) {
814 this.textAdjustX
= textAdjustX
;
815 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
816 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
822 * Get the Y text adjustment.
824 * @return Y text adjustment
826 public int getTextAdjustY() {
831 * Set the Y text adjustment.
833 * @param textAdjustY the Y text adjustment
835 public void setTextAdjustY(final int textAdjustY
) {
836 synchronized (this) {
837 this.textAdjustY
= textAdjustY
;
838 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
839 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
845 * Get the height text adjustment.
847 * @return height text adjustment
849 public int getTextAdjustHeight() {
850 return textAdjustHeight
;
854 * Set the height text adjustment.
856 * @param textAdjustHeight the height text adjustment
858 public void setTextAdjustHeight(final int textAdjustHeight
) {
859 synchronized (this) {
860 this.textAdjustHeight
= textAdjustHeight
;
861 textHeight
= fontTextHeight
+ textAdjustHeight
;
862 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
863 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
869 * Get the width text adjustment.
871 * @return width text adjustment
873 public int getTextAdjustWidth() {
874 return textAdjustWidth
;
878 * Set the width text adjustment.
880 * @param textAdjustWidth the width text adjustment
882 public void setTextAdjustWidth(final int textAdjustWidth
) {
883 synchronized (this) {
884 this.textAdjustWidth
= textAdjustWidth
;
885 textWidth
= fontTextWidth
+ textAdjustWidth
;
886 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
887 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
893 * Convert a CellAttributes foreground color to an Swing Color.
895 * @param attr the text attributes
896 * @return the Swing Color
898 public static Color
attrToForegroundColor(final CellAttributes attr
) {
899 int rgb
= attr
.getForeColorRGB();
901 int red
= (rgb
>> 16) & 0xFF;
902 int green
= (rgb
>> 8) & 0xFF;
903 int blue
= rgb
& 0xFF;
905 return new Color(red
, green
, blue
);
909 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
911 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
913 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
915 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
917 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
918 return MYBOLD_YELLOW
;
919 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
921 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
922 return MYBOLD_MAGENTA
;
923 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
927 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
929 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
931 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
933 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
935 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
937 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
939 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
941 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
945 throw new IllegalArgumentException("Invalid color: " +
946 attr
.getForeColor().getValue());
950 * Convert a CellAttributes background color to an Swing Color.
952 * @param attr the text attributes
953 * @return the Swing Color
955 public static Color
attrToBackgroundColor(final CellAttributes attr
) {
956 int rgb
= attr
.getBackColorRGB();
958 int red
= (rgb
>> 16) & 0xFF;
959 int green
= (rgb
>> 8) & 0xFF;
960 int blue
= rgb
& 0xFF;
962 return new Color(red
, green
, blue
);
965 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
967 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
969 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
971 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
973 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
975 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
977 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
979 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
982 throw new IllegalArgumentException("Invalid color: " +
983 attr
.getBackColor().getValue());
987 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
988 * textAdjustWidth should be, based on the location of a vertical bar and
991 private void getFontAdjustments() {
992 BufferedImage image
= null;
994 // What SHOULD happen is that the topmost/leftmost white pixel is at
995 // position (gr2x, gr2y). But it might also be off by a pixel in
998 Graphics2D gr2
= null;
1001 image
= new BufferedImage(fontTextWidth
* 2, fontTextHeight
* 2,
1002 BufferedImage
.TYPE_INT_ARGB
);
1004 gr2
= image
.createGraphics();
1005 gr2
.setFont(swing
.getFont());
1006 gr2
.setColor(java
.awt
.Color
.BLACK
);
1007 gr2
.fillRect(0, 0, fontTextWidth
* 2, fontTextHeight
* 2);
1008 gr2
.setColor(java
.awt
.Color
.WHITE
);
1009 char [] chars
= new char[1];
1010 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
1011 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1012 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
1013 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1016 int top
= fontTextHeight
* 2;
1018 int left
= fontTextWidth
* 2;
1022 textAdjustHeight
= 0;
1023 textAdjustWidth
= 0;
1025 for (int x
= 0; x
< fontTextWidth
* 2; x
++) {
1026 for (int y
= 0; y
< fontTextHeight
* 2; y
++) {
1029 System.err.println("H X: " + x + " Y: " + y + " " +
1030 image.getRGB(x, y));
1033 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
1034 // Pixel is present.
1051 textAdjustX
= (gr2x
- left
);
1052 textAdjustWidth
= fontTextWidth
- (right
- left
+ 1);
1055 textAdjustY
= (gr2y
- top
);
1056 textAdjustHeight
= fontTextHeight
- (bottom
- top
+ 1);
1058 // System.err.println("top " + top + " bottom " + bottom);
1059 // System.err.println("left " + left + " right " + right);
1061 // Special case: do not believe fonts that claim to be wider than
1063 if (fontTextWidth
>= fontTextHeight
) {
1065 textAdjustWidth
= 0;
1066 fontTextWidth
= fontTextHeight
/ 2;
1071 * Figure out my font dimensions. This code path works OK for the JFrame
1072 * case, and can be called immediately after JFrame creation.
1074 private void getFontDimensions() {
1075 swing
.setFont(font
);
1076 Graphics gr
= swing
.getGraphics();
1080 getFontDimensions(gr
);
1084 * Figure out my font dimensions. This code path is needed to lazy-load
1085 * the information inside paint().
1087 * @param gr Graphics object to use
1089 private void getFontDimensions(final Graphics gr
) {
1090 swing
.setFont(font
);
1091 FontMetrics fm
= gr
.getFontMetrics();
1092 maxDescent
= fm
.getMaxDescent();
1093 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
1094 int leading
= fm
.getLeading();
1095 fontTextWidth
= (int)Math
.round(bounds
.getWidth());
1096 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1098 // This produces the same number, but works better for ugly
1100 fontTextHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
1102 getFontAdjustments();
1103 textHeight
= fontTextHeight
+ textAdjustHeight
;
1104 textWidth
= fontTextWidth
+ textAdjustWidth
;
1106 if (sessionInfo
!= null) {
1107 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
1109 gotFontDimensions
= true;
1113 * Resize the physical screen to match the logical screen dimensions.
1116 public void resizeToScreen() {
1117 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
1122 * Draw one cell's image to the screen.
1124 * @param gr the Swing Graphics context
1125 * @param cell the Cell to draw
1126 * @param xPixel the x-coordinate to render to. 0 means the
1127 * left-most pixel column.
1128 * @param yPixel the y-coordinate to render to. 0 means the top-most
1131 private void drawImage(final Graphics gr
, final Cell cell
,
1132 final int xPixel
, final int yPixel
) {
1135 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1139 // Draw the background rectangle, then the foreground character.
1140 assert (cell
.isImage());
1141 gr
.setColor(cell
.getBackground());
1142 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1144 BufferedImage image
= cell
.getImage();
1145 if (image
!= null) {
1146 if (swing
.getFrame() != null) {
1147 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1149 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1156 * Draw one glyph to the screen.
1158 * @param gr the Swing Graphics context
1159 * @param cell the Cell to draw
1160 * @param xPixel the x-coordinate to render to. 0 means the
1161 * left-most pixel column.
1162 * @param yPixel the y-coordinate to render to. 0 means the top-most
1165 private void drawGlyph(final Graphics gr
, final Cell cell
,
1166 final int xPixel
, final int yPixel
) {
1169 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1173 BufferedImage image
= null;
1174 if (cell
.isBlink() && !cursorBlinkVisible
) {
1175 image
= glyphCacheBlink
.get(cell
);
1177 image
= glyphCache
.get(cell
);
1179 if (image
!= null) {
1180 if (swing
.getFrame() != null) {
1181 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1183 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1188 // Generate glyph and draw it.
1189 Graphics2D gr2
= null;
1192 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1193 image
= new BufferedImage(textWidth
, textHeight
,
1194 BufferedImage
.TYPE_INT_ARGB
);
1195 gr2
= image
.createGraphics();
1196 gr2
.setFont(swing
.getFont());
1200 gr2
= (Graphics2D
) gr
;
1203 Cell cellColor
= new Cell(cell
);
1205 // Check for reverse
1206 if (cell
.isReverse()) {
1207 cellColor
.setForeColor(cell
.getBackColor());
1208 cellColor
.setBackColor(cell
.getForeColor());
1211 // Draw the background rectangle, then the foreground character.
1212 gr2
.setColor(attrToBackgroundColor(cellColor
));
1213 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1215 // Handle blink and underline
1217 || (cell
.isBlink() && cursorBlinkVisible
)
1219 gr2
.setColor(attrToForegroundColor(cellColor
));
1220 char [] chars
= new char[1];
1221 chars
[0] = cell
.getChar();
1222 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1223 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1225 if (cell
.isUnderline()) {
1226 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1230 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1233 // We need a new key that will not be mutated by
1235 Cell key
= new Cell(cell
);
1236 if (cell
.isBlink() && !cursorBlinkVisible
) {
1237 glyphCacheBlink
.put(key
, image
);
1239 glyphCache
.put(key
, image
);
1242 if (swing
.getFrame() != null) {
1243 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1245 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1252 * Check if the cursor is visible, and if so draw it.
1254 * @param gr the Swing Graphics context
1256 private void drawCursor(final Graphics gr
) {
1261 && (cursorY
<= height
- 1)
1262 && (cursorX
<= width
- 1)
1263 && cursorBlinkVisible
1265 int xPixel
= cursorX
* textWidth
+ left
;
1266 int yPixel
= cursorY
* textHeight
+ top
;
1267 Cell lCell
= logical
[cursorX
][cursorY
];
1268 gr
.setColor(attrToForegroundColor(lCell
));
1269 switch (cursorStyle
) {
1273 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1276 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1279 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1286 * Reset the blink timer.
1288 private void resetBlinkTimer() {
1289 lastBlinkTime
= System
.currentTimeMillis();
1290 cursorBlinkVisible
= true;
1294 * Paint redraws the whole screen.
1296 * @param gr the Swing Graphics context
1298 public void paint(final Graphics gr
) {
1300 if (gotFontDimensions
== false) {
1301 // Lazy-load the text width/height
1302 getFontDimensions(gr
);
1304 System.err.println("textWidth " + textWidth +
1305 " textHeight " + textHeight);
1306 System.err.println("FONT: " + swing.getFont() + " font " + font);
1310 if ((swing
.getFrame() != null)
1311 && (swing
.getBufferStrategy() != null)
1312 && (SwingUtilities
.isEventDispatchThread())
1314 // System.err.println("paint(), skip first paint on swing thread");
1319 int xCellMax
= width
;
1321 int yCellMax
= height
;
1323 Rectangle bounds
= gr
.getClipBounds();
1324 if (bounds
!= null) {
1325 // Only update what is in the bounds
1326 xCellMin
= textColumn(bounds
.x
);
1327 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1328 if (xCellMax
> width
) {
1331 if (xCellMin
>= xCellMax
) {
1332 xCellMin
= xCellMax
- 2;
1337 yCellMin
= textRow(bounds
.y
);
1338 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1339 if (yCellMax
> height
) {
1342 if (yCellMin
>= yCellMax
) {
1343 yCellMin
= yCellMax
- 2;
1349 // We need a total repaint
1350 reallyCleared
= true;
1353 // Prevent updates to the screen's data from the TApplication
1355 synchronized (this) {
1358 System.err.printf("bounds %s X %d %d Y %d %d\n",
1359 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1362 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1363 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1365 int xPixel
= x
* textWidth
+ left
;
1366 int yPixel
= y
* textHeight
+ top
;
1368 Cell lCell
= logical
[x
][y
];
1369 Cell pCell
= physical
[x
][y
];
1371 if (!lCell
.equals(pCell
)
1374 || (swing
.getFrame() == null)) {
1376 if (lCell
.isImage()) {
1377 drawImage(gr
, lCell
, xPixel
, yPixel
);
1379 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1382 // Physical is always updated
1383 physical
[x
][y
].setTo(lCell
);
1389 reallyCleared
= false;
1390 } // synchronized (this)
1394 * Restore terminal to normal state.
1396 public void shutdown() {
1401 * Push the logical screen to the physical device.
1403 private void drawToSwing() {
1406 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1407 reallyCleared, dirty);
1410 // If reallyCleared is set, we have to draw everything.
1411 if ((swing
.getFrame() != null)
1412 && (swing
.getBufferStrategy() != null)
1413 && (reallyCleared
== true)
1415 // Triple-buffering: we have to redraw everything on this thread.
1416 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1419 swing
.getBufferStrategy().show();
1420 Toolkit
.getDefaultToolkit().sync();
1422 } else if (((swing
.getFrame() != null)
1423 && (swing
.getBufferStrategy() == null))
1424 || (reallyCleared
== true)
1426 // Repaint everything on the Swing thread.
1427 // System.err.println("REPAINT ALL");
1432 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1433 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1435 synchronized (this) {
1436 for (int y
= 0; y
< height
; y
++) {
1437 for (int x
= 0; x
< width
; x
++) {
1438 Cell lCell
= logical
[x
][y
];
1439 Cell pCell
= physical
[x
][y
];
1441 int xPixel
= x
* textWidth
+ left
;
1442 int yPixel
= y
* textHeight
+ top
;
1444 if (!lCell
.equals(pCell
)
1448 || (lCell
.isBlink())
1450 if (lCell
.isImage()) {
1451 drawImage(gr
, lCell
, xPixel
, yPixel
);
1453 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1455 physical
[x
][y
].setTo(lCell
);
1460 } // synchronized (this)
1463 swing
.getBufferStrategy().show();
1464 Toolkit
.getDefaultToolkit().sync();
1468 // Swing thread version: request a repaint, but limit it to the area
1469 // that has changed.
1471 // Find the minimum-size damaged region.
1472 int xMin
= swing
.getWidth();
1474 int yMin
= swing
.getHeight();
1477 synchronized (this) {
1478 for (int y
= 0; y
< height
; y
++) {
1479 for (int x
= 0; x
< width
; x
++) {
1480 Cell lCell
= logical
[x
][y
];
1481 Cell pCell
= physical
[x
][y
];
1483 int xPixel
= x
* textWidth
+ left
;
1484 int yPixel
= y
* textHeight
+ top
;
1486 if (!lCell
.equals(pCell
)
1492 if (xPixel
< xMin
) {
1495 if (xPixel
+ textWidth
> xMax
) {
1496 xMax
= xPixel
+ textWidth
;
1498 if (yPixel
< yMin
) {
1501 if (yPixel
+ textHeight
> yMax
) {
1502 yMax
= yPixel
+ textHeight
;
1508 if (xMin
+ textWidth
>= xMax
) {
1511 if (yMin
+ textHeight
>= yMax
) {
1515 // Repaint the desired area
1517 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1521 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1522 // This path should never be taken, but is left here for
1524 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1525 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1530 swing
.getBufferStrategy().show();
1531 Toolkit
.getDefaultToolkit().sync();
1533 // Repaint on the Swing thread.
1534 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1539 * Convert pixel column position to text cell column position.
1541 * @param x pixel column position
1542 * @return text cell column position
1544 public int textColumn(final int x
) {
1545 int column
= ((x
- left
) / textWidth
);
1549 if (column
> width
- 1) {
1556 * Convert pixel row position to text cell row position.
1558 * @param y pixel row position
1559 * @return text cell row position
1561 public int textRow(final int y
) {
1562 int row
= ((y
- top
) / textHeight
);
1566 if (row
> height
- 1) {
1573 * Getter for sessionInfo.
1575 * @return the SessionInfo
1577 public SessionInfo
getSessionInfo() {
1582 * Getter for the underlying Swing component.
1584 * @return the SwingComponent
1586 public SwingComponent
getSwingComponent() {
1590 // ------------------------------------------------------------------------
1591 // KeyListener ------------------------------------------------------------
1592 // ------------------------------------------------------------------------
1595 * Pass Swing keystrokes into the event queue.
1597 * @param key keystroke received
1599 public void keyReleased(final KeyEvent key
) {
1600 // Ignore release events
1604 * Pass Swing keystrokes into the event queue.
1606 * @param key keystroke received
1608 public void keyTyped(final KeyEvent key
) {
1609 // Ignore typed events
1613 * Pass Swing keystrokes into the event queue.
1615 * @param key keystroke received
1617 public void keyPressed(final KeyEvent key
) {
1618 boolean alt
= false;
1619 boolean shift
= false;
1620 boolean ctrl
= false;
1622 boolean isKey
= false;
1623 if (key
.isActionKey()) {
1626 ch
= key
.getKeyChar();
1628 alt
= key
.isAltDown();
1629 ctrl
= key
.isControlDown();
1630 shift
= key
.isShiftDown();
1633 System.err.printf("Swing Key: %s\n", key);
1634 System.err.printf(" isKey: %s\n", isKey);
1635 System.err.printf(" alt: %s\n", alt);
1636 System.err.printf(" ctrl: %s\n", ctrl);
1637 System.err.printf(" shift: %s\n", shift);
1638 System.err.printf(" ch: %s\n", ch);
1641 // Special case: not return the bare modifier presses
1642 switch (key
.getKeyCode()) {
1643 case KeyEvent
.VK_ALT
:
1645 case KeyEvent
.VK_ALT_GRAPH
:
1647 case KeyEvent
.VK_CONTROL
:
1649 case KeyEvent
.VK_SHIFT
:
1651 case KeyEvent
.VK_META
:
1657 TKeypress keypress
= null;
1659 switch (key
.getKeyCode()) {
1660 case KeyEvent
.VK_F1
:
1661 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1664 case KeyEvent
.VK_F2
:
1665 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1668 case KeyEvent
.VK_F3
:
1669 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1672 case KeyEvent
.VK_F4
:
1673 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1676 case KeyEvent
.VK_F5
:
1677 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1680 case KeyEvent
.VK_F6
:
1681 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1684 case KeyEvent
.VK_F7
:
1685 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1688 case KeyEvent
.VK_F8
:
1689 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1692 case KeyEvent
.VK_F9
:
1693 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1696 case KeyEvent
.VK_F10
:
1697 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1700 case KeyEvent
.VK_F11
:
1701 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1704 case KeyEvent
.VK_F12
:
1705 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1708 case KeyEvent
.VK_HOME
:
1709 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1712 case KeyEvent
.VK_END
:
1713 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1716 case KeyEvent
.VK_PAGE_UP
:
1717 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1720 case KeyEvent
.VK_PAGE_DOWN
:
1721 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1724 case KeyEvent
.VK_INSERT
:
1725 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1728 case KeyEvent
.VK_DELETE
:
1729 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1732 case KeyEvent
.VK_RIGHT
:
1733 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1736 case KeyEvent
.VK_LEFT
:
1737 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1740 case KeyEvent
.VK_UP
:
1741 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1744 case KeyEvent
.VK_DOWN
:
1745 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1748 case KeyEvent
.VK_TAB
:
1749 // Special case: distinguish TAB vs BTAB
1751 keypress
= kbShiftTab
;
1756 case KeyEvent
.VK_ENTER
:
1757 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1760 case KeyEvent
.VK_ESCAPE
:
1761 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1764 case KeyEvent
.VK_BACK_SPACE
:
1765 keypress
= kbBackspace
;
1768 // Unsupported, ignore
1773 if (keypress
== null) {
1776 // Disambiguate ^H from Backspace.
1777 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1779 keypress
= kbBackspace
;
1781 // We are emulating Xterm here, where the backspace key
1782 // on the keyboard returns ^?.
1783 keypress
= kbBackspaceDel
;
1797 keypress
= kbShiftTab
;
1806 if (!alt
&& ctrl
&& !shift
) {
1807 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1809 // Not a special key, put it together
1810 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1814 // Save it and we are done.
1815 synchronized (eventQueue
) {
1816 eventQueue
.add(new TKeypressEvent(keypress
));
1819 if (listener
!= null) {
1820 synchronized (listener
) {
1821 listener
.notifyAll();
1826 // ------------------------------------------------------------------------
1827 // WindowListener ---------------------------------------------------------
1828 // ------------------------------------------------------------------------
1831 * Pass window events into the event queue.
1833 * @param event window event received
1835 public void windowActivated(final WindowEvent event
) {
1836 // Force a total repaint
1837 synchronized (this) {
1843 * Pass window events into the event queue.
1845 * @param event window event received
1847 public void windowClosed(final WindowEvent event
) {
1852 * Pass window events into the event queue.
1854 * @param event window event received
1856 public void windowClosing(final WindowEvent event
) {
1857 // Drop a cmBackendDisconnect and walk away
1858 synchronized (eventQueue
) {
1859 eventQueue
.add(new TCommandEvent(cmBackendDisconnect
));
1862 if (listener
!= null) {
1863 synchronized (listener
) {
1864 listener
.notifyAll();
1870 * Pass window events into the event queue.
1872 * @param event window event received
1874 public void windowDeactivated(final WindowEvent event
) {
1879 * Pass window events into the event queue.
1881 * @param event window event received
1883 public void windowDeiconified(final WindowEvent event
) {
1888 * Pass window events into the event queue.
1890 * @param event window event received
1892 public void windowIconified(final WindowEvent event
) {
1897 * Pass window events into the event queue.
1899 * @param event window event received
1901 public void windowOpened(final WindowEvent event
) {
1905 // ------------------------------------------------------------------------
1906 // ComponentListener ------------------------------------------------------
1907 // ------------------------------------------------------------------------
1910 * Pass component events into the event queue.
1912 * @param event component event received
1914 public void componentHidden(final ComponentEvent event
) {
1919 * Pass component events into the event queue.
1921 * @param event component event received
1923 public void componentShown(final ComponentEvent event
) {
1928 * Pass component events into the event queue.
1930 * @param event component event received
1932 public void componentMoved(final ComponentEvent event
) {
1937 * Pass component events into the event queue.
1939 * @param event component event received
1941 public void componentResized(final ComponentEvent event
) {
1942 if (gotFontDimensions
== false) {
1943 // We are still waiting to get font information. Don't pass a
1945 // System.err.println("size " + swing.getComponent().getSize());
1949 if (sessionInfo
== null) {
1950 // This is the initial component resize in construction, bail
1955 // Drop a new TResizeEvent into the queue
1956 sessionInfo
.queryWindowSize();
1957 synchronized (eventQueue
) {
1958 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1959 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1960 eventQueue
.add(windowResize
);
1963 System.err.println("Add resize event: " + windowResize.getWidth() +
1964 " x " + windowResize.getHeight());
1967 if (listener
!= null) {
1968 synchronized (listener
) {
1969 listener
.notifyAll();
1974 // ------------------------------------------------------------------------
1975 // MouseMotionListener ----------------------------------------------------
1976 // ------------------------------------------------------------------------
1979 * Pass mouse events into the event queue.
1981 * @param mouse mouse event received
1983 public void mouseDragged(final MouseEvent mouse
) {
1984 int modifiers
= mouse
.getModifiersEx();
1985 boolean eventMouse1
= false;
1986 boolean eventMouse2
= false;
1987 boolean eventMouse3
= false;
1988 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1991 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1994 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1997 mouse1
= eventMouse1
;
1998 mouse2
= eventMouse2
;
1999 mouse3
= eventMouse3
;
2000 int x
= textColumn(mouse
.getX());
2001 int y
= textRow(mouse
.getY());
2003 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2004 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2006 synchronized (eventQueue
) {
2007 eventQueue
.add(mouseEvent
);
2010 if (listener
!= null) {
2011 synchronized (listener
) {
2012 listener
.notifyAll();
2018 * Pass mouse events into the event queue.
2020 * @param mouse mouse event received
2022 public void mouseMoved(final MouseEvent mouse
) {
2023 int x
= textColumn(mouse
.getX());
2024 int y
= textRow(mouse
.getY());
2025 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2026 // Bail out, we've moved some pixels but not a whole text cell.
2032 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2033 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2035 synchronized (eventQueue
) {
2036 eventQueue
.add(mouseEvent
);
2039 if (listener
!= null) {
2040 synchronized (listener
) {
2041 listener
.notifyAll();
2046 // ------------------------------------------------------------------------
2047 // MouseListener ----------------------------------------------------------
2048 // ------------------------------------------------------------------------
2051 * Pass mouse events into the event queue.
2053 * @param mouse mouse event received
2055 public void mouseClicked(final MouseEvent mouse
) {
2060 * Pass mouse events into the event queue.
2062 * @param mouse mouse event received
2064 public void mouseEntered(final MouseEvent mouse
) {
2069 * Pass mouse events into the event queue.
2071 * @param mouse mouse event received
2073 public void mouseExited(final MouseEvent mouse
) {
2078 * Pass mouse events into the event queue.
2080 * @param mouse mouse event received
2082 public void mousePressed(final MouseEvent mouse
) {
2083 int modifiers
= mouse
.getModifiersEx();
2084 boolean eventMouse1
= false;
2085 boolean eventMouse2
= false;
2086 boolean eventMouse3
= false;
2087 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2090 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2093 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2096 mouse1
= eventMouse1
;
2097 mouse2
= eventMouse2
;
2098 mouse3
= eventMouse3
;
2099 int x
= textColumn(mouse
.getX());
2100 int y
= textRow(mouse
.getY());
2102 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2103 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2105 synchronized (eventQueue
) {
2106 eventQueue
.add(mouseEvent
);
2109 if (listener
!= null) {
2110 synchronized (listener
) {
2111 listener
.notifyAll();
2117 * Pass mouse events into the event queue.
2119 * @param mouse mouse event received
2121 public void mouseReleased(final MouseEvent mouse
) {
2122 int modifiers
= mouse
.getModifiersEx();
2123 boolean eventMouse1
= false;
2124 boolean eventMouse2
= false;
2125 boolean eventMouse3
= false;
2126 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2129 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2132 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2147 int x
= textColumn(mouse
.getX());
2148 int y
= textRow(mouse
.getY());
2150 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2151 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
2153 synchronized (eventQueue
) {
2154 eventQueue
.add(mouseEvent
);
2157 if (listener
!= null) {
2158 synchronized (listener
) {
2159 listener
.notifyAll();
2164 // ------------------------------------------------------------------------
2165 // MouseWheelListener -----------------------------------------------------
2166 // ------------------------------------------------------------------------
2169 * Pass mouse events into the event queue.
2171 * @param mouse mouse event received
2173 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2174 int modifiers
= mouse
.getModifiersEx();
2175 boolean eventMouse1
= false;
2176 boolean eventMouse2
= false;
2177 boolean eventMouse3
= false;
2178 boolean mouseWheelUp
= false;
2179 boolean mouseWheelDown
= false;
2180 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2183 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2186 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2189 mouse1
= eventMouse1
;
2190 mouse2
= eventMouse2
;
2191 mouse3
= eventMouse3
;
2192 int x
= textColumn(mouse
.getX());
2193 int y
= textRow(mouse
.getY());
2194 if (mouse
.getWheelRotation() > 0) {
2195 mouseWheelDown
= true;
2197 if (mouse
.getWheelRotation() < 0) {
2198 mouseWheelUp
= true;
2201 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2202 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2204 synchronized (eventQueue
) {
2205 eventQueue
.add(mouseEvent
);
2208 if (listener
!= null) {
2209 synchronized (listener
) {
2210 listener
.notifyAll();