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 * Use a vertical bar for the cursor.
128 // ------------------------------------------------------------------------
129 // Variables --------------------------------------------------------------
130 // ------------------------------------------------------------------------
132 // Colors to map DOS colors to AWT colors.
133 private static Color MYBLACK
;
134 private static Color MYRED
;
135 private static Color MYGREEN
;
136 private static Color MYYELLOW
;
137 private static Color MYBLUE
;
138 private static Color MYMAGENTA
;
139 private static Color MYCYAN
;
140 private static Color MYWHITE
;
141 private static Color MYBOLD_BLACK
;
142 private static Color MYBOLD_RED
;
143 private static Color MYBOLD_GREEN
;
144 private static Color MYBOLD_YELLOW
;
145 private static Color MYBOLD_BLUE
;
146 private static Color MYBOLD_MAGENTA
;
147 private static Color MYBOLD_CYAN
;
148 private static Color MYBOLD_WHITE
;
151 * When true, all the MYBLACK, MYRED, etc. colors are set.
153 private static boolean dosColors
= false;
156 * The Swing component or frame to draw to.
158 private SwingComponent swing
;
161 * A cache of previously-rendered glyphs for blinking text, when it is
164 private Map
<Cell
, BufferedImage
> glyphCacheBlink
;
167 * A cache of previously-rendered glyphs for non-blinking, or
168 * blinking-and-visible, text.
170 private Map
<Cell
, BufferedImage
> glyphCache
;
173 * If true, we were successful at getting the font dimensions.
175 private boolean gotFontDimensions
= false;
178 * The currently selected font.
180 private Font font
= null;
183 * The currently selected font size in points.
185 private int fontSize
= 16;
188 * Width of a character cell in pixels.
190 private int textWidth
= 16;
193 * Height of a character cell in pixels.
195 private int textHeight
= 20;
198 * Width of a character cell in pixels, as reported by font.
200 private int fontTextWidth
= 1;
203 * Height of a character cell in pixels, as reported by font.
205 private int fontTextHeight
= 1;
208 * Descent of a character cell in pixels.
210 private int maxDescent
= 0;
213 * System-dependent Y adjustment for text in the character cell.
215 private int textAdjustY
= 0;
218 * System-dependent X adjustment for text in the character cell.
220 private int textAdjustX
= 0;
223 * System-dependent height adjustment for text in the character cell.
225 private int textAdjustHeight
= 0;
228 * System-dependent width adjustment for text in the character cell.
230 private int textAdjustWidth
= 0;
233 * Top pixel absolute location.
235 private int top
= 30;
238 * Left pixel absolute location.
240 private int left
= 30;
243 * The cursor style to draw.
245 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
248 * The number of millis to wait before switching the blink from visible
249 * to invisible. Set to 0 or negative to disable blinking.
251 private long blinkMillis
= 500;
254 * If true, the cursor should be visible right now based on the blink
257 private boolean cursorBlinkVisible
= true;
260 * The time that the blink last flipped from visible to invisible or
261 * from invisible to visible.
263 private long lastBlinkTime
= 0;
266 * The session information.
268 private SwingSessionInfo sessionInfo
;
271 * The listening object that run() wakes up on new input.
273 private Object listener
;
276 * The event queue, filled up by a thread reading on input.
278 private List
<TInputEvent
> eventQueue
;
281 * The last reported mouse X position.
283 private int oldMouseX
= -1;
286 * The last reported mouse Y position.
288 private int oldMouseY
= -1;
291 * true if mouse1 was down. Used to report mouse1 on the release event.
293 private boolean mouse1
= false;
296 * true if mouse2 was down. Used to report mouse2 on the release event.
298 private boolean mouse2
= false;
301 * true if mouse3 was down. Used to report mouse3 on the release event.
303 private boolean mouse3
= false;
305 // ------------------------------------------------------------------------
306 // Constructors -----------------------------------------------------------
307 // ------------------------------------------------------------------------
310 * Static constructor.
317 * Public constructor creates a new JFrame to render to.
319 * @param windowWidth the number of text columns to start with
320 * @param windowHeight the number of text rows to start with
321 * @param fontSize the size in points. Good values to pick are: 16, 20,
323 * @param listener the object this backend needs to wake up when new
326 public SwingTerminal(final int windowWidth
, final int windowHeight
,
327 final int fontSize
, final Object listener
) {
329 this.fontSize
= fontSize
;
334 SwingUtilities
.invokeAndWait(new Runnable() {
337 JFrame frame
= new JFrame() {
340 * Serializable version.
342 private static final long serialVersionUID
= 1;
345 * The code that performs the actual drawing.
347 public SwingTerminal screen
= null;
350 * Anonymous class initializer saves the screen
351 * reference, so that paint() and the like call out
355 this.screen
= SwingTerminal
.this;
359 * Update redraws the whole screen.
361 * @param gr the Swing Graphics context
364 public void update(final Graphics gr
) {
365 // The default update clears the area. Don't do
366 // that, instead just paint it directly.
371 * Paint redraws the whole screen.
373 * @param gr the Swing Graphics context
376 public void paint(final Graphics gr
) {
377 if (screen
!= null) {
384 ClassLoader loader
= Thread
.currentThread().
385 getContextClassLoader();
386 frame
.setIconImage((new ImageIcon(loader
.
387 getResource(ICONFILE
))).getImage());
389 // Get the Swing component
390 SwingTerminal
.this.swing
= new SwingComponent(frame
);
392 // Hang onto top and left for drawing.
393 Insets insets
= SwingTerminal
.this.swing
.getInsets();
394 SwingTerminal
.this.left
= insets
.left
;
395 SwingTerminal
.this.top
= insets
.top
;
397 // Load the font so that we can set sessionInfo.
400 // Get the default cols x rows and set component size
402 SwingTerminal
.this.sessionInfo
=
403 new SwingSessionInfo(SwingTerminal
.this.swing
,
404 SwingTerminal
.this.textWidth
,
405 SwingTerminal
.this.textHeight
,
406 windowWidth
, windowHeight
);
408 SwingTerminal
.this.setDimensions(sessionInfo
.
409 getWindowWidth(), sessionInfo
.getWindowHeight());
411 SwingTerminal
.this.resizeToScreen(true);
412 SwingTerminal
.this.swing
.setVisible(true);
415 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
417 } catch (InterruptedException e
) {
421 this.listener
= listener
;
425 eventQueue
= new ArrayList
<TInputEvent
>();
427 // Add listeners to Swing.
428 swing
.addKeyListener(this);
429 swing
.addWindowListener(this);
430 swing
.addComponentListener(this);
431 swing
.addMouseListener(this);
432 swing
.addMouseMotionListener(this);
433 swing
.addMouseWheelListener(this);
437 * Public constructor renders to an existing JComponent.
439 * @param component the Swing component to render to
440 * @param windowWidth the number of text columns to start with
441 * @param windowHeight the number of text rows to start with
442 * @param fontSize the size in points. Good values to pick are: 16, 20,
444 * @param listener the object this backend needs to wake up when new
447 public SwingTerminal(final JComponent component
, final int windowWidth
,
448 final int windowHeight
, final int fontSize
, final Object listener
) {
450 this.fontSize
= fontSize
;
455 SwingUtilities
.invokeAndWait(new Runnable() {
458 JComponent newComponent
= new JComponent() {
461 * Serializable version.
463 private static final long serialVersionUID
= 1;
466 * The code that performs the actual drawing.
468 public SwingTerminal screen
= null;
471 * Anonymous class initializer saves the screen
472 * reference, so that paint() and the like call out
476 this.screen
= SwingTerminal
.this;
480 * Update redraws the whole screen.
482 * @param gr the Swing Graphics context
485 public void update(final Graphics gr
) {
486 // The default update clears the area. Don't do
487 // that, instead just paint it directly.
492 * Paint redraws the whole screen.
494 * @param gr the Swing Graphics context
497 public void paint(final Graphics gr
) {
498 if (screen
!= null) {
503 component
.setLayout(new BorderLayout());
504 component
.add(newComponent
);
506 // Allow key events to be received
507 component
.setFocusable(true);
509 // Get the Swing component
510 SwingTerminal
.this.swing
= new SwingComponent(component
);
512 // Hang onto top and left for drawing.
513 Insets insets
= SwingTerminal
.this.swing
.getInsets();
514 SwingTerminal
.this.left
= insets
.left
;
515 SwingTerminal
.this.top
= insets
.top
;
517 // Load the font so that we can set sessionInfo.
520 // Get the default cols x rows and set component size
522 SwingTerminal
.this.sessionInfo
=
523 new SwingSessionInfo(SwingTerminal
.this.swing
,
524 SwingTerminal
.this.textWidth
,
525 SwingTerminal
.this.textHeight
);
528 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
530 } catch (InterruptedException e
) {
534 this.listener
= listener
;
538 eventQueue
= new ArrayList
<TInputEvent
>();
540 // Add listeners to Swing.
541 swing
.addKeyListener(this);
542 swing
.addWindowListener(this);
543 swing
.addComponentListener(this);
544 swing
.addMouseListener(this);
545 swing
.addMouseMotionListener(this);
546 swing
.addMouseWheelListener(this);
549 // ------------------------------------------------------------------------
550 // LogicalScreen ----------------------------------------------------------
551 // ------------------------------------------------------------------------
554 * Set the window title.
556 * @param title the new title
559 public void setTitle(final String title
) {
560 swing
.setTitle(title
);
564 * Push the logical screen to the physical device.
567 public void flushPhysical() {
568 // See if it is time to flip the blink time.
569 long nowTime
= System
.currentTimeMillis();
570 if (nowTime
>= blinkMillis
+ lastBlinkTime
) {
571 lastBlinkTime
= nowTime
;
572 cursorBlinkVisible
= !cursorBlinkVisible
;
573 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
576 if ((swing
.getFrame() != null)
577 && (swing
.getBufferStrategy() != null)
583 } while (swing
.getBufferStrategy().contentsRestored());
585 swing
.getBufferStrategy().show();
586 Toolkit
.getDefaultToolkit().sync();
587 } while (swing
.getBufferStrategy().contentsLost());
590 // Non-triple-buffered, call drawToSwing() once
595 // ------------------------------------------------------------------------
596 // TerminalReader ---------------------------------------------------------
597 // ------------------------------------------------------------------------
600 * Check if there are events in the queue.
602 * @return if true, getEvents() has something to return to the backend
604 public boolean hasEvents() {
605 synchronized (eventQueue
) {
606 return (eventQueue
.size() > 0);
611 * Return any events in the IO queue.
613 * @param queue list to append new events to
615 public void getEvents(final List
<TInputEvent
> queue
) {
616 synchronized (eventQueue
) {
617 if (eventQueue
.size() > 0) {
618 synchronized (queue
) {
619 queue
.addAll(eventQueue
);
627 * Restore terminal to normal state.
629 public void closeTerminal() {
634 * Set listener to a different Object.
636 * @param listener the new listening object that run() wakes up on new
639 public void setListener(final Object listener
) {
640 this.listener
= listener
;
644 * Reload options from System properties.
646 public void reloadOptions() {
647 // Figure out my cursor style.
648 String cursorStyleString
= System
.getProperty(
649 "jexer.Swing.cursorStyle", "underline").toLowerCase();
650 if (cursorStyleString
.equals("underline")) {
651 cursorStyle
= CursorStyle
.UNDERLINE
;
652 } else if (cursorStyleString
.equals("outline")) {
653 cursorStyle
= CursorStyle
.OUTLINE
;
654 } else if (cursorStyleString
.equals("block")) {
655 cursorStyle
= CursorStyle
.BLOCK
;
656 } else if (cursorStyleString
.equals("verticalbar")) {
657 cursorStyle
= CursorStyle
.VERTICAL_BAR
;
660 // Pull the system property for triple buffering.
661 if (System
.getProperty("jexer.Swing.tripleBuffer",
662 "true").equals("true")
664 SwingComponent
.tripleBuffer
= true;
666 SwingComponent
.tripleBuffer
= false;
670 // ------------------------------------------------------------------------
671 // SwingTerminal ----------------------------------------------------------
672 // ------------------------------------------------------------------------
675 * Get the width of a character cell in pixels.
677 * @return the width in pixels of a character cell
679 public int getTextWidth() {
684 * Get the height of a character cell in pixels.
686 * @return the height in pixels of a character cell
688 public int getTextHeight() {
693 * Setup Swing colors to match DOS color palette.
695 private static void setDOSColors() {
699 MYBLACK
= new Color(0x00, 0x00, 0x00);
700 MYRED
= new Color(0xa8, 0x00, 0x00);
701 MYGREEN
= new Color(0x00, 0xa8, 0x00);
702 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
703 MYBLUE
= new Color(0x00, 0x00, 0xa8);
704 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
705 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
706 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
707 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
708 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
709 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
710 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
711 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
712 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
713 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
714 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
720 * Get the number of millis to wait before switching the blink from
721 * visible to invisible.
723 * @return the number of milli to wait before switching the blink from
724 * visible to invisible
726 public long getBlinkMillis() {
731 * Get the current status of the blink flag.
733 * @return true if the cursor and blinking text should be visible
735 public boolean getCursorBlinkVisible() {
736 return cursorBlinkVisible
;
740 * Get the font size in points.
742 * @return font size in points
744 public int getFontSize() {
749 * Set the font size in points.
751 * @param fontSize font size in points
753 public void setFontSize(final int fontSize
) {
754 this.fontSize
= fontSize
;
755 Font newFont
= font
.deriveFont((float) fontSize
);
760 * Set to a new font, and resize the screen to match its dimensions.
762 * @param font the new font
764 public void setFont(final Font font
) {
765 synchronized (this) {
769 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
770 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
771 resizeToScreen(true);
776 * Get the font this screen was last set to.
780 public Font
getFont() {
785 * Set the font to Terminus, the best all-around font for both CP437 and
788 public void setDefaultFont() {
790 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
791 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
792 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
793 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
795 } catch (java
.awt
.FontFormatException e
) {
797 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
798 } catch (java
.io
.IOException e
) {
800 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
807 * Get the X text adjustment.
809 * @return X text adjustment
811 public int getTextAdjustX() {
816 * Set the X text adjustment.
818 * @param textAdjustX the X text adjustment
820 public void setTextAdjustX(final int textAdjustX
) {
821 synchronized (this) {
822 this.textAdjustX
= textAdjustX
;
823 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
824 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
830 * Get the Y text adjustment.
832 * @return Y text adjustment
834 public int getTextAdjustY() {
839 * Set the Y text adjustment.
841 * @param textAdjustY the Y text adjustment
843 public void setTextAdjustY(final int textAdjustY
) {
844 synchronized (this) {
845 this.textAdjustY
= textAdjustY
;
846 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
847 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
853 * Get the height text adjustment.
855 * @return height text adjustment
857 public int getTextAdjustHeight() {
858 return textAdjustHeight
;
862 * Set the height text adjustment.
864 * @param textAdjustHeight the height text adjustment
866 public void setTextAdjustHeight(final int textAdjustHeight
) {
867 synchronized (this) {
868 this.textAdjustHeight
= textAdjustHeight
;
869 textHeight
= fontTextHeight
+ textAdjustHeight
;
870 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
871 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
877 * Get the width text adjustment.
879 * @return width text adjustment
881 public int getTextAdjustWidth() {
882 return textAdjustWidth
;
886 * Set the width text adjustment.
888 * @param textAdjustWidth the width text adjustment
890 public void setTextAdjustWidth(final int textAdjustWidth
) {
891 synchronized (this) {
892 this.textAdjustWidth
= textAdjustWidth
;
893 textWidth
= fontTextWidth
+ textAdjustWidth
;
894 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
895 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
901 * Convert a CellAttributes foreground color to an Swing Color.
903 * @param attr the text attributes
904 * @return the Swing Color
906 public static Color
attrToForegroundColor(final CellAttributes attr
) {
907 int rgb
= attr
.getForeColorRGB();
909 int red
= (rgb
>> 16) & 0xFF;
910 int green
= (rgb
>> 8) & 0xFF;
911 int blue
= rgb
& 0xFF;
913 return new Color(red
, green
, blue
);
917 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
919 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
921 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
923 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
925 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
926 return MYBOLD_YELLOW
;
927 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
929 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
930 return MYBOLD_MAGENTA
;
931 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
935 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
937 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
939 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
941 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
943 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
945 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
947 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
949 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
953 throw new IllegalArgumentException("Invalid color: " +
954 attr
.getForeColor().getValue());
958 * Convert a CellAttributes background color to an Swing Color.
960 * @param attr the text attributes
961 * @return the Swing Color
963 public static Color
attrToBackgroundColor(final CellAttributes attr
) {
964 int rgb
= attr
.getBackColorRGB();
966 int red
= (rgb
>> 16) & 0xFF;
967 int green
= (rgb
>> 8) & 0xFF;
968 int blue
= rgb
& 0xFF;
970 return new Color(red
, green
, blue
);
973 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
975 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
977 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
979 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
981 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
983 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
985 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
987 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
990 throw new IllegalArgumentException("Invalid color: " +
991 attr
.getBackColor().getValue());
995 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
996 * textAdjustWidth should be, based on the location of a vertical bar and
999 private void getFontAdjustments() {
1000 BufferedImage image
= null;
1002 // What SHOULD happen is that the topmost/leftmost white pixel is at
1003 // position (gr2x, gr2y). But it might also be off by a pixel in
1004 // either direction.
1006 Graphics2D gr2
= null;
1009 image
= new BufferedImage(fontTextWidth
* 2, fontTextHeight
* 2,
1010 BufferedImage
.TYPE_INT_ARGB
);
1012 gr2
= image
.createGraphics();
1013 gr2
.setFont(swing
.getFont());
1014 gr2
.setColor(java
.awt
.Color
.BLACK
);
1015 gr2
.fillRect(0, 0, fontTextWidth
* 2, fontTextHeight
* 2);
1016 gr2
.setColor(java
.awt
.Color
.WHITE
);
1017 char [] chars
= new char[1];
1018 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
1019 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1020 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
1021 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1024 int top
= fontTextHeight
* 2;
1026 int left
= fontTextWidth
* 2;
1030 textAdjustHeight
= 0;
1031 textAdjustWidth
= 0;
1033 for (int x
= 0; x
< fontTextWidth
* 2; x
++) {
1034 for (int y
= 0; y
< fontTextHeight
* 2; y
++) {
1037 System.err.println("H X: " + x + " Y: " + y + " " +
1038 image.getRGB(x, y));
1041 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
1042 // Pixel is present.
1059 textAdjustX
= (gr2x
- left
);
1060 textAdjustWidth
= fontTextWidth
- (right
- left
+ 1);
1063 textAdjustY
= (gr2y
- top
);
1064 textAdjustHeight
= fontTextHeight
- (bottom
- top
+ 1);
1066 // System.err.println("top " + top + " bottom " + bottom);
1067 // System.err.println("left " + left + " right " + right);
1069 // Special case: do not believe fonts that claim to be wider than
1071 if (fontTextWidth
>= fontTextHeight
) {
1073 textAdjustWidth
= 0;
1074 fontTextWidth
= fontTextHeight
/ 2;
1079 * Figure out my font dimensions. This code path works OK for the JFrame
1080 * case, and can be called immediately after JFrame creation.
1082 private void getFontDimensions() {
1083 swing
.setFont(font
);
1084 Graphics gr
= swing
.getGraphics();
1088 getFontDimensions(gr
);
1092 * Figure out my font dimensions. This code path is needed to lazy-load
1093 * the information inside paint().
1095 * @param gr Graphics object to use
1097 private void getFontDimensions(final Graphics gr
) {
1098 swing
.setFont(font
);
1099 FontMetrics fm
= gr
.getFontMetrics();
1100 maxDescent
= fm
.getMaxDescent();
1101 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
1102 int leading
= fm
.getLeading();
1103 fontTextWidth
= (int)Math
.round(bounds
.getWidth());
1104 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1106 // This produces the same number, but works better for ugly
1108 fontTextHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
1110 getFontAdjustments();
1111 textHeight
= fontTextHeight
+ textAdjustHeight
;
1112 textWidth
= fontTextWidth
+ textAdjustWidth
;
1114 if (sessionInfo
!= null) {
1115 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
1117 gotFontDimensions
= true;
1121 * Resize the physical screen to match the logical screen dimensions.
1123 * @param resizeComponent if true, resize the Swing component
1125 private void resizeToScreen(final boolean resizeComponent
) {
1126 if (resizeComponent
) {
1127 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
1133 * Resize the physical screen to match the logical screen dimensions.
1136 public void resizeToScreen() {
1137 resizeToScreen(false);
1141 * Draw one cell's image to the screen.
1143 * @param gr the Swing Graphics context
1144 * @param cell the Cell to draw
1145 * @param xPixel the x-coordinate to render to. 0 means the
1146 * left-most pixel column.
1147 * @param yPixel the y-coordinate to render to. 0 means the top-most
1150 private void drawImage(final Graphics gr
, final Cell cell
,
1151 final int xPixel
, final int yPixel
) {
1154 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1158 // Draw the background rectangle, then the foreground character.
1159 assert (cell
.isImage());
1160 gr
.setColor(cell
.getBackground());
1161 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1163 BufferedImage image
= cell
.getImage();
1164 if (image
!= null) {
1165 if (swing
.getFrame() != null) {
1166 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1168 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1175 * Draw one glyph to the screen.
1177 * @param gr the Swing Graphics context
1178 * @param cell the Cell to draw
1179 * @param xPixel the x-coordinate to render to. 0 means the
1180 * left-most pixel column.
1181 * @param yPixel the y-coordinate to render to. 0 means the top-most
1184 private void drawGlyph(final Graphics gr
, final Cell cell
,
1185 final int xPixel
, final int yPixel
) {
1188 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1192 BufferedImage image
= null;
1193 if (cell
.isBlink() && !cursorBlinkVisible
) {
1194 image
= glyphCacheBlink
.get(cell
);
1196 image
= glyphCache
.get(cell
);
1198 if (image
!= null) {
1199 if (swing
.getFrame() != null) {
1200 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1202 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1207 // Generate glyph and draw it.
1208 Graphics2D gr2
= null;
1211 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1212 image
= new BufferedImage(textWidth
, textHeight
,
1213 BufferedImage
.TYPE_INT_ARGB
);
1214 gr2
= image
.createGraphics();
1215 gr2
.setFont(swing
.getFont());
1219 gr2
= (Graphics2D
) gr
;
1222 Cell cellColor
= new Cell(cell
);
1224 // Check for reverse
1225 if (cell
.isReverse()) {
1226 cellColor
.setForeColor(cell
.getBackColor());
1227 cellColor
.setBackColor(cell
.getForeColor());
1230 // Draw the background rectangle, then the foreground character.
1231 gr2
.setColor(attrToBackgroundColor(cellColor
));
1232 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1234 // Handle blink and underline
1236 || (cell
.isBlink() && cursorBlinkVisible
)
1238 gr2
.setColor(attrToForegroundColor(cellColor
));
1239 char [] chars
= Character
.toChars(cell
.getChar());
1240 gr2
.drawChars(chars
, 0, chars
.length
, gr2x
+ textAdjustX
,
1241 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1243 if (cell
.isUnderline()) {
1244 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1248 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1251 // We need a new key that will not be mutated by
1253 Cell key
= new Cell(cell
);
1254 if (cell
.isBlink() && !cursorBlinkVisible
) {
1255 glyphCacheBlink
.put(key
, image
);
1257 glyphCache
.put(key
, image
);
1260 if (swing
.getFrame() != null) {
1261 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1263 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1270 * Check if the cursor is visible, and if so draw it.
1272 * @param gr the Swing Graphics context
1274 private void drawCursor(final Graphics gr
) {
1279 && (cursorY
<= height
- 1)
1280 && (cursorX
<= width
- 1)
1281 && cursorBlinkVisible
1283 int xPixel
= cursorX
* textWidth
+ left
;
1284 int yPixel
= cursorY
* textHeight
+ top
;
1285 Cell lCell
= logical
[cursorX
][cursorY
];
1286 int cursorWidth
= textWidth
;
1287 switch (lCell
.getWidth()) {
1296 xPixel
-= textWidth
;
1299 gr
.setColor(attrToForegroundColor(lCell
));
1300 switch (cursorStyle
) {
1304 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, cursorWidth
, 2);
1307 gr
.fillRect(xPixel
, yPixel
, cursorWidth
, textHeight
);
1310 gr
.drawRect(xPixel
, yPixel
, cursorWidth
- 1, textHeight
- 1);
1313 gr
.fillRect(xPixel
, yPixel
, 2, textHeight
);
1320 * Reset the blink timer.
1322 private void resetBlinkTimer() {
1323 lastBlinkTime
= System
.currentTimeMillis();
1324 cursorBlinkVisible
= true;
1328 * Paint redraws the whole screen.
1330 * @param gr the Swing Graphics context
1332 public void paint(final Graphics gr
) {
1334 if (gotFontDimensions
== false) {
1335 // Lazy-load the text width/height
1336 getFontDimensions(gr
);
1338 System.err.println("textWidth " + textWidth +
1339 " textHeight " + textHeight);
1340 System.err.println("FONT: " + swing.getFont() + " font " + font);
1344 if ((swing
.getFrame() != null)
1345 && (swing
.getBufferStrategy() != null)
1346 && (SwingUtilities
.isEventDispatchThread())
1348 // System.err.println("paint(), skip first paint on swing thread");
1353 int xCellMax
= width
;
1355 int yCellMax
= height
;
1357 Rectangle bounds
= gr
.getClipBounds();
1358 if (bounds
!= null) {
1359 // Only update what is in the bounds
1360 xCellMin
= textColumn(bounds
.x
);
1361 xCellMax
= textColumn(bounds
.x
+ bounds
.width
) + 1;
1362 if (xCellMax
> width
) {
1365 if (xCellMin
>= xCellMax
) {
1366 xCellMin
= xCellMax
- 2;
1371 yCellMin
= textRow(bounds
.y
);
1372 yCellMax
= textRow(bounds
.y
+ bounds
.height
) + 1;
1373 if (yCellMax
> height
) {
1376 if (yCellMin
>= yCellMax
) {
1377 yCellMin
= yCellMax
- 2;
1383 // We need a total repaint
1384 reallyCleared
= true;
1387 // Prevent updates to the screen's data from the TApplication
1389 synchronized (this) {
1392 System.err.printf("bounds %s X %d %d Y %d %d\n",
1393 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1396 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1397 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1399 int xPixel
= x
* textWidth
+ left
;
1400 int yPixel
= y
* textHeight
+ top
;
1402 Cell lCell
= logical
[x
][y
];
1403 Cell pCell
= physical
[x
][y
];
1405 if (!lCell
.equals(pCell
)
1408 || (swing
.getFrame() == null)) {
1410 if (lCell
.isImage()) {
1411 drawImage(gr
, lCell
, xPixel
, yPixel
);
1413 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1416 // Physical is always updated
1417 physical
[x
][y
].setTo(lCell
);
1423 reallyCleared
= false;
1424 } // synchronized (this)
1428 * Restore terminal to normal state.
1430 public void shutdown() {
1435 * Push the logical screen to the physical device.
1437 private void drawToSwing() {
1440 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1441 reallyCleared, dirty);
1444 // If reallyCleared is set, we have to draw everything.
1445 if ((swing
.getFrame() != null)
1446 && (swing
.getBufferStrategy() != null)
1447 && (reallyCleared
== true)
1449 // Triple-buffering: we have to redraw everything on this thread.
1450 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1453 swing
.getBufferStrategy().show();
1454 Toolkit
.getDefaultToolkit().sync();
1456 } else if (((swing
.getFrame() != null)
1457 && (swing
.getBufferStrategy() == null))
1458 || (reallyCleared
== true)
1460 // Repaint everything on the Swing thread.
1461 // System.err.println("REPAINT ALL");
1466 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1467 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1469 synchronized (this) {
1470 for (int y
= 0; y
< height
; y
++) {
1471 for (int x
= 0; x
< width
; x
++) {
1472 Cell lCell
= logical
[x
][y
];
1473 Cell pCell
= physical
[x
][y
];
1475 int xPixel
= x
* textWidth
+ left
;
1476 int yPixel
= y
* textHeight
+ top
;
1478 if (!lCell
.equals(pCell
)
1482 || (lCell
.isBlink())
1484 if (lCell
.isImage()) {
1485 drawImage(gr
, lCell
, xPixel
, yPixel
);
1487 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1489 physical
[x
][y
].setTo(lCell
);
1494 } // synchronized (this)
1497 swing
.getBufferStrategy().show();
1498 Toolkit
.getDefaultToolkit().sync();
1502 // Swing thread version: request a repaint, but limit it to the area
1503 // that has changed.
1505 // Find the minimum-size damaged region.
1506 int xMin
= swing
.getWidth();
1508 int yMin
= swing
.getHeight();
1511 synchronized (this) {
1512 for (int y
= 0; y
< height
; y
++) {
1513 for (int x
= 0; x
< width
; x
++) {
1514 Cell lCell
= logical
[x
][y
];
1515 Cell pCell
= physical
[x
][y
];
1517 int xPixel
= x
* textWidth
+ left
;
1518 int yPixel
= y
* textHeight
+ top
;
1520 if (!lCell
.equals(pCell
)
1526 if (xPixel
< xMin
) {
1529 if (xPixel
+ textWidth
> xMax
) {
1530 xMax
= xPixel
+ textWidth
;
1532 if (yPixel
< yMin
) {
1535 if (yPixel
+ textHeight
> yMax
) {
1536 yMax
= yPixel
+ textHeight
;
1542 if (xMin
+ textWidth
>= xMax
) {
1545 if (yMin
+ textHeight
>= yMax
) {
1549 // Repaint the desired area
1551 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1555 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1556 // This path should never be taken, but is left here for
1558 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1559 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1564 swing
.getBufferStrategy().show();
1565 Toolkit
.getDefaultToolkit().sync();
1567 // Repaint on the Swing thread.
1568 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1573 * Convert pixel column position to text cell column position.
1575 * @param x pixel column position
1576 * @return text cell column position
1578 public int textColumn(final int x
) {
1579 int column
= ((x
- left
) / textWidth
);
1583 if (column
> width
- 1) {
1590 * Convert pixel row position to text cell row position.
1592 * @param y pixel row position
1593 * @return text cell row position
1595 public int textRow(final int y
) {
1596 int row
= ((y
- top
) / textHeight
);
1600 if (row
> height
- 1) {
1607 * Getter for sessionInfo.
1609 * @return the SessionInfo
1611 public SessionInfo
getSessionInfo() {
1616 * Getter for the underlying Swing component.
1618 * @return the SwingComponent
1620 public SwingComponent
getSwingComponent() {
1624 // ------------------------------------------------------------------------
1625 // KeyListener ------------------------------------------------------------
1626 // ------------------------------------------------------------------------
1629 * Pass Swing keystrokes into the event queue.
1631 * @param key keystroke received
1633 public void keyReleased(final KeyEvent key
) {
1634 // Ignore release events
1638 * Pass Swing keystrokes into the event queue.
1640 * @param key keystroke received
1642 public void keyTyped(final KeyEvent key
) {
1643 // Ignore typed events
1647 * Pass Swing keystrokes into the event queue.
1649 * @param key keystroke received
1651 public void keyPressed(final KeyEvent key
) {
1652 boolean alt
= false;
1653 boolean shift
= false;
1654 boolean ctrl
= false;
1656 boolean isKey
= false;
1657 if (key
.isActionKey()) {
1660 ch
= key
.getKeyChar();
1662 alt
= key
.isAltDown();
1663 ctrl
= key
.isControlDown();
1664 shift
= key
.isShiftDown();
1667 System.err.printf("Swing Key: %s\n", key);
1668 System.err.printf(" isKey: %s\n", isKey);
1669 System.err.printf(" alt: %s\n", alt);
1670 System.err.printf(" ctrl: %s\n", ctrl);
1671 System.err.printf(" shift: %s\n", shift);
1672 System.err.printf(" ch: %s\n", ch);
1675 // Special case: not return the bare modifier presses
1676 switch (key
.getKeyCode()) {
1677 case KeyEvent
.VK_ALT
:
1679 case KeyEvent
.VK_ALT_GRAPH
:
1681 case KeyEvent
.VK_CONTROL
:
1683 case KeyEvent
.VK_SHIFT
:
1685 case KeyEvent
.VK_META
:
1691 TKeypress keypress
= null;
1693 switch (key
.getKeyCode()) {
1694 case KeyEvent
.VK_F1
:
1695 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1698 case KeyEvent
.VK_F2
:
1699 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1702 case KeyEvent
.VK_F3
:
1703 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1706 case KeyEvent
.VK_F4
:
1707 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1710 case KeyEvent
.VK_F5
:
1711 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1714 case KeyEvent
.VK_F6
:
1715 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1718 case KeyEvent
.VK_F7
:
1719 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1722 case KeyEvent
.VK_F8
:
1723 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1726 case KeyEvent
.VK_F9
:
1727 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1730 case KeyEvent
.VK_F10
:
1731 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1734 case KeyEvent
.VK_F11
:
1735 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1738 case KeyEvent
.VK_F12
:
1739 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1742 case KeyEvent
.VK_HOME
:
1743 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1746 case KeyEvent
.VK_END
:
1747 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1750 case KeyEvent
.VK_PAGE_UP
:
1751 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1754 case KeyEvent
.VK_PAGE_DOWN
:
1755 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1758 case KeyEvent
.VK_INSERT
:
1759 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1762 case KeyEvent
.VK_DELETE
:
1763 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1766 case KeyEvent
.VK_RIGHT
:
1767 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1770 case KeyEvent
.VK_LEFT
:
1771 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1774 case KeyEvent
.VK_UP
:
1775 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1778 case KeyEvent
.VK_DOWN
:
1779 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1782 case KeyEvent
.VK_TAB
:
1783 // Special case: distinguish TAB vs BTAB
1785 keypress
= kbShiftTab
;
1790 case KeyEvent
.VK_ENTER
:
1791 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1794 case KeyEvent
.VK_ESCAPE
:
1795 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1798 case KeyEvent
.VK_BACK_SPACE
:
1799 keypress
= kbBackspace
;
1802 // Unsupported, ignore
1807 if (keypress
== null) {
1810 // Disambiguate ^H from Backspace.
1811 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1813 keypress
= kbBackspace
;
1815 // We are emulating Xterm here, where the backspace key
1816 // on the keyboard returns ^?.
1817 keypress
= kbBackspaceDel
;
1831 keypress
= kbShiftTab
;
1840 if (!alt
&& ctrl
&& !shift
) {
1841 // Control character, replace ch with 'A', 'B', etc.
1842 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1844 // Not a special key, put it together
1845 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1849 // Save it and we are done.
1850 synchronized (eventQueue
) {
1851 eventQueue
.add(new TKeypressEvent(keypress
));
1854 if (listener
!= null) {
1855 synchronized (listener
) {
1856 listener
.notifyAll();
1861 // ------------------------------------------------------------------------
1862 // WindowListener ---------------------------------------------------------
1863 // ------------------------------------------------------------------------
1866 * Pass window events into the event queue.
1868 * @param event window event received
1870 public void windowActivated(final WindowEvent event
) {
1871 // Force a total repaint
1872 synchronized (this) {
1878 * Pass window events into the event queue.
1880 * @param event window event received
1882 public void windowClosed(final WindowEvent event
) {
1887 * Pass window events into the event queue.
1889 * @param event window event received
1891 public void windowClosing(final WindowEvent event
) {
1892 // Drop a cmBackendDisconnect and walk away
1893 synchronized (eventQueue
) {
1894 eventQueue
.add(new TCommandEvent(cmBackendDisconnect
));
1897 if (listener
!= null) {
1898 synchronized (listener
) {
1899 listener
.notifyAll();
1905 * Pass window events into the event queue.
1907 * @param event window event received
1909 public void windowDeactivated(final WindowEvent event
) {
1914 * Pass window events into the event queue.
1916 * @param event window event received
1918 public void windowDeiconified(final WindowEvent event
) {
1923 * Pass window events into the event queue.
1925 * @param event window event received
1927 public void windowIconified(final WindowEvent event
) {
1932 * Pass window events into the event queue.
1934 * @param event window event received
1936 public void windowOpened(final WindowEvent event
) {
1940 // ------------------------------------------------------------------------
1941 // ComponentListener ------------------------------------------------------
1942 // ------------------------------------------------------------------------
1945 * Pass component events into the event queue.
1947 * @param event component event received
1949 public void componentHidden(final ComponentEvent event
) {
1954 * Pass component events into the event queue.
1956 * @param event component event received
1958 public void componentShown(final ComponentEvent event
) {
1963 * Pass component events into the event queue.
1965 * @param event component event received
1967 public void componentMoved(final ComponentEvent event
) {
1972 * Pass component events into the event queue.
1974 * @param event component event received
1976 public void componentResized(final ComponentEvent event
) {
1977 if (gotFontDimensions
== false) {
1978 // We are still waiting to get font information. Don't pass a
1980 // System.err.println("size " + swing.getComponent().getSize());
1984 if (sessionInfo
== null) {
1985 // This is the initial component resize in construction, bail
1990 // Drop a new TResizeEvent into the queue
1991 sessionInfo
.queryWindowSize();
1992 synchronized (eventQueue
) {
1993 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1994 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1995 eventQueue
.add(windowResize
);
1998 System.err.println("Add resize event: " + windowResize.getWidth() +
1999 " x " + windowResize.getHeight());
2002 if (listener
!= null) {
2003 synchronized (listener
) {
2004 listener
.notifyAll();
2009 // ------------------------------------------------------------------------
2010 // MouseMotionListener ----------------------------------------------------
2011 // ------------------------------------------------------------------------
2014 * Pass mouse events into the event queue.
2016 * @param mouse mouse event received
2018 public void mouseDragged(final MouseEvent mouse
) {
2019 int modifiers
= mouse
.getModifiersEx();
2020 boolean eventMouse1
= false;
2021 boolean eventMouse2
= false;
2022 boolean eventMouse3
= false;
2023 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2026 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2029 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2032 mouse1
= eventMouse1
;
2033 mouse2
= eventMouse2
;
2034 mouse3
= eventMouse3
;
2035 int x
= textColumn(mouse
.getX());
2036 int y
= textRow(mouse
.getY());
2038 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2039 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2041 synchronized (eventQueue
) {
2042 eventQueue
.add(mouseEvent
);
2045 if (listener
!= null) {
2046 synchronized (listener
) {
2047 listener
.notifyAll();
2053 * Pass mouse events into the event queue.
2055 * @param mouse mouse event received
2057 public void mouseMoved(final MouseEvent mouse
) {
2058 int x
= textColumn(mouse
.getX());
2059 int y
= textRow(mouse
.getY());
2060 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2061 // Bail out, we've moved some pixels but not a whole text cell.
2067 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2068 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2070 synchronized (eventQueue
) {
2071 eventQueue
.add(mouseEvent
);
2074 if (listener
!= null) {
2075 synchronized (listener
) {
2076 listener
.notifyAll();
2081 // ------------------------------------------------------------------------
2082 // MouseListener ----------------------------------------------------------
2083 // ------------------------------------------------------------------------
2086 * Pass mouse events into the event queue.
2088 * @param mouse mouse event received
2090 public void mouseClicked(final MouseEvent mouse
) {
2095 * Pass mouse events into the event queue.
2097 * @param mouse mouse event received
2099 public void mouseEntered(final MouseEvent mouse
) {
2104 * Pass mouse events into the event queue.
2106 * @param mouse mouse event received
2108 public void mouseExited(final MouseEvent mouse
) {
2113 * Pass mouse events into the event queue.
2115 * @param mouse mouse event received
2117 public void mousePressed(final MouseEvent mouse
) {
2118 int modifiers
= mouse
.getModifiersEx();
2119 boolean eventMouse1
= false;
2120 boolean eventMouse2
= false;
2121 boolean eventMouse3
= false;
2122 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2125 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2128 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2131 mouse1
= eventMouse1
;
2132 mouse2
= eventMouse2
;
2133 mouse3
= eventMouse3
;
2134 int x
= textColumn(mouse
.getX());
2135 int y
= textRow(mouse
.getY());
2137 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2138 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2140 synchronized (eventQueue
) {
2141 eventQueue
.add(mouseEvent
);
2144 if (listener
!= null) {
2145 synchronized (listener
) {
2146 listener
.notifyAll();
2152 * Pass mouse events into the event queue.
2154 * @param mouse mouse event received
2156 public void mouseReleased(final MouseEvent mouse
) {
2157 int modifiers
= mouse
.getModifiersEx();
2158 boolean eventMouse1
= false;
2159 boolean eventMouse2
= false;
2160 boolean eventMouse3
= false;
2161 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2164 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2167 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2182 int x
= textColumn(mouse
.getX());
2183 int y
= textRow(mouse
.getY());
2185 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2186 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
2188 synchronized (eventQueue
) {
2189 eventQueue
.add(mouseEvent
);
2192 if (listener
!= null) {
2193 synchronized (listener
) {
2194 listener
.notifyAll();
2199 // ------------------------------------------------------------------------
2200 // MouseWheelListener -----------------------------------------------------
2201 // ------------------------------------------------------------------------
2204 * Pass mouse events into the event queue.
2206 * @param mouse mouse event received
2208 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2209 int modifiers
= mouse
.getModifiersEx();
2210 boolean eventMouse1
= false;
2211 boolean eventMouse2
= false;
2212 boolean eventMouse3
= false;
2213 boolean mouseWheelUp
= false;
2214 boolean mouseWheelDown
= false;
2215 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2218 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2221 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2224 mouse1
= eventMouse1
;
2225 mouse2
= eventMouse2
;
2226 mouse3
= eventMouse3
;
2227 int x
= textColumn(mouse
.getX());
2228 int y
= textRow(mouse
.getY());
2229 if (mouse
.getWheelRotation() > 0) {
2230 mouseWheelDown
= true;
2232 if (mouse
.getWheelRotation() < 0) {
2233 mouseWheelUp
= true;
2236 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2237 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2239 synchronized (eventQueue
) {
2240 eventQueue
.add(mouseEvent
);
2243 if (listener
!= null) {
2244 synchronized (listener
) {
2245 listener
.notifyAll();