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
.HashMap
;
55 import java
.util
.LinkedList
;
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 private 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 * Public constructor creates a new JFrame to render to.
307 * @param windowWidth the number of text columns to start with
308 * @param windowHeight the number of text rows to start with
309 * @param fontSize the size in points. Good values to pick are: 16, 20,
311 * @param listener the object this backend needs to wake up when new
314 public SwingTerminal(final int windowWidth
, final int windowHeight
,
315 final int fontSize
, final Object listener
) {
317 this.fontSize
= fontSize
;
323 SwingUtilities
.invokeAndWait(new Runnable() {
326 JFrame frame
= new JFrame() {
329 * Serializable version.
331 private static final long serialVersionUID
= 1;
334 * The code that performs the actual drawing.
336 public SwingTerminal screen
= null;
339 * Anonymous class initializer saves the screen
340 * reference, so that paint() and the like call out
344 this.screen
= SwingTerminal
.this;
348 * Update redraws the whole screen.
350 * @param gr the Swing Graphics context
353 public void update(final Graphics gr
) {
354 // The default update clears the area. Don't do
355 // that, instead just paint it directly.
360 * Paint redraws the whole screen.
362 * @param gr the Swing Graphics context
365 public void paint(final Graphics gr
) {
366 if (screen
!= null) {
373 ClassLoader loader
= Thread
.currentThread().
374 getContextClassLoader();
375 frame
.setIconImage((new ImageIcon(loader
.
376 getResource(ICONFILE
))).getImage());
378 // Get the Swing component
379 SwingTerminal
.this.swing
= new SwingComponent(frame
);
381 // Hang onto top and left for drawing.
382 Insets insets
= SwingTerminal
.this.swing
.getInsets();
383 SwingTerminal
.this.left
= insets
.left
;
384 SwingTerminal
.this.top
= insets
.top
;
386 // Load the font so that we can set sessionInfo.
389 // Get the default cols x rows and set component size
391 SwingTerminal
.this.sessionInfo
=
392 new SwingSessionInfo(SwingTerminal
.this.swing
,
393 SwingTerminal
.this.textWidth
,
394 SwingTerminal
.this.textHeight
,
395 windowWidth
, windowHeight
);
397 SwingTerminal
.this.setDimensions(sessionInfo
.
398 getWindowWidth(), sessionInfo
.getWindowHeight());
400 SwingTerminal
.this.resizeToScreen();
401 SwingTerminal
.this.swing
.setVisible(true);
404 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
406 } catch (InterruptedException e
) {
410 this.listener
= listener
;
414 eventQueue
= new LinkedList
<TInputEvent
>();
416 // Add listeners to Swing.
417 swing
.addKeyListener(this);
418 swing
.addWindowListener(this);
419 swing
.addComponentListener(this);
420 swing
.addMouseListener(this);
421 swing
.addMouseMotionListener(this);
422 swing
.addMouseWheelListener(this);
426 * Public constructor renders to an existing JComponent.
428 * @param component the Swing component to render to
429 * @param windowWidth the number of text columns to start with
430 * @param windowHeight the number of text rows to start with
431 * @param fontSize the size in points. Good values to pick are: 16, 20,
433 * @param listener the object this backend needs to wake up when new
436 public SwingTerminal(final JComponent component
, final int windowWidth
,
437 final int windowHeight
, final int fontSize
, final Object listener
) {
439 this.fontSize
= fontSize
;
445 SwingUtilities
.invokeAndWait(new Runnable() {
448 JComponent newComponent
= new JComponent() {
451 * Serializable version.
453 private static final long serialVersionUID
= 1;
456 * The code that performs the actual drawing.
458 public SwingTerminal screen
= null;
461 * Anonymous class initializer saves the screen
462 * reference, so that paint() and the like call out
466 this.screen
= SwingTerminal
.this;
470 * Update redraws the whole screen.
472 * @param gr the Swing Graphics context
475 public void update(final Graphics gr
) {
476 // The default update clears the area. Don't do
477 // that, instead just paint it directly.
482 * Paint redraws the whole screen.
484 * @param gr the Swing Graphics context
487 public void paint(final Graphics gr
) {
488 if (screen
!= null) {
493 component
.setLayout(new BorderLayout());
494 component
.add(newComponent
);
496 // Allow key events to be received
497 component
.setFocusable(true);
499 // Get the Swing component
500 SwingTerminal
.this.swing
= new SwingComponent(component
);
502 // Hang onto top and left for drawing.
503 Insets insets
= SwingTerminal
.this.swing
.getInsets();
504 SwingTerminal
.this.left
= insets
.left
;
505 SwingTerminal
.this.top
= insets
.top
;
507 // Load the font so that we can set sessionInfo.
510 // Get the default cols x rows and set component size
512 SwingTerminal
.this.sessionInfo
=
513 new SwingSessionInfo(SwingTerminal
.this.swing
,
514 SwingTerminal
.this.textWidth
,
515 SwingTerminal
.this.textHeight
);
518 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
520 } catch (InterruptedException e
) {
524 this.listener
= listener
;
528 eventQueue
= new LinkedList
<TInputEvent
>();
530 // Add listeners to Swing.
531 swing
.addKeyListener(this);
532 swing
.addWindowListener(this);
533 swing
.addComponentListener(this);
534 swing
.addMouseListener(this);
535 swing
.addMouseMotionListener(this);
536 swing
.addMouseWheelListener(this);
539 // ------------------------------------------------------------------------
540 // LogicalScreen ----------------------------------------------------------
541 // ------------------------------------------------------------------------
544 * Set the window title.
546 * @param title the new title
549 public void setTitle(final String title
) {
550 swing
.setTitle(title
);
554 * Push the logical screen to the physical device.
557 public void flushPhysical() {
558 // See if it is time to flip the blink time.
559 long nowTime
= System
.currentTimeMillis();
560 if (nowTime
>= blinkMillis
+ lastBlinkTime
) {
561 lastBlinkTime
= nowTime
;
562 cursorBlinkVisible
= !cursorBlinkVisible
;
563 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
566 if ((swing
.getFrame() != null)
567 && (swing
.getBufferStrategy() != null)
573 } while (swing
.getBufferStrategy().contentsRestored());
575 swing
.getBufferStrategy().show();
576 Toolkit
.getDefaultToolkit().sync();
577 } while (swing
.getBufferStrategy().contentsLost());
580 // Non-triple-buffered, call drawToSwing() once
585 // ------------------------------------------------------------------------
586 // TerminalReader ---------------------------------------------------------
587 // ------------------------------------------------------------------------
590 * Check if there are events in the queue.
592 * @return if true, getEvents() has something to return to the backend
594 public boolean hasEvents() {
595 synchronized (eventQueue
) {
596 return (eventQueue
.size() > 0);
601 * Return any events in the IO queue.
603 * @param queue list to append new events to
605 public void getEvents(final List
<TInputEvent
> queue
) {
606 synchronized (eventQueue
) {
607 if (eventQueue
.size() > 0) {
608 synchronized (queue
) {
609 queue
.addAll(eventQueue
);
617 * Restore terminal to normal state.
619 public void closeTerminal() {
624 * Set listener to a different Object.
626 * @param listener the new listening object that run() wakes up on new
629 public void setListener(final Object listener
) {
630 this.listener
= listener
;
634 * Reload options from System properties.
636 public void reloadOptions() {
637 // Figure out my cursor style.
638 String cursorStyleString
= System
.getProperty(
639 "jexer.Swing.cursorStyle", "underline").toLowerCase();
640 if (cursorStyleString
.equals("underline")) {
641 cursorStyle
= CursorStyle
.UNDERLINE
;
642 } else if (cursorStyleString
.equals("outline")) {
643 cursorStyle
= CursorStyle
.OUTLINE
;
644 } else if (cursorStyleString
.equals("block")) {
645 cursorStyle
= CursorStyle
.BLOCK
;
648 // Pull the system property for triple buffering.
649 if (System
.getProperty("jexer.Swing.tripleBuffer",
650 "true").equals("true")
652 SwingComponent
.tripleBuffer
= true;
654 SwingComponent
.tripleBuffer
= false;
658 // ------------------------------------------------------------------------
659 // SwingTerminal ----------------------------------------------------------
660 // ------------------------------------------------------------------------
663 * Get the width of a character cell in pixels.
665 * @return the width in pixels of a character cell
667 public int getTextWidth() {
672 * Get the height of a character cell in pixels.
674 * @return the height in pixels of a character cell
676 public int getTextHeight() {
681 * Setup Swing colors to match DOS color palette.
683 private static void setDOSColors() {
687 MYBLACK
= new Color(0x00, 0x00, 0x00);
688 MYRED
= new Color(0xa8, 0x00, 0x00);
689 MYGREEN
= new Color(0x00, 0xa8, 0x00);
690 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
691 MYBLUE
= new Color(0x00, 0x00, 0xa8);
692 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
693 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
694 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
695 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
696 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
697 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
698 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
699 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
700 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
701 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
702 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
708 * Get the number of millis to wait before switching the blink from
709 * visible to invisible.
711 * @return the number of milli to wait before switching the blink from
712 * visible to invisible
714 public long getBlinkMillis() {
719 * Get the font size in points.
721 * @return font size in points
723 public int getFontSize() {
728 * Set the font size in points.
730 * @param fontSize font size in points
732 public void setFontSize(final int fontSize
) {
733 this.fontSize
= fontSize
;
734 Font newFont
= font
.deriveFont((float) fontSize
);
739 * Set to a new font, and resize the screen to match its dimensions.
741 * @param font the new font
743 public void setFont(final Font font
) {
744 synchronized (this) {
748 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
749 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
755 * Get the font this screen was last set to.
759 public Font
getFont() {
764 * Set the font to Terminus, the best all-around font for both CP437 and
767 public void setDefaultFont() {
769 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
770 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
771 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
772 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
774 } catch (java
.awt
.FontFormatException e
) {
776 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
777 } catch (java
.io
.IOException e
) {
779 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
786 * Get the X text adjustment.
788 * @return X text adjustment
790 public int getTextAdjustX() {
795 * Set the X text adjustment.
797 * @param textAdjustX the X text adjustment
799 public void setTextAdjustX(final int textAdjustX
) {
800 synchronized (this) {
801 this.textAdjustX
= textAdjustX
;
802 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
803 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
809 * Get the Y text adjustment.
811 * @return Y text adjustment
813 public int getTextAdjustY() {
818 * Set the Y text adjustment.
820 * @param textAdjustY the Y text adjustment
822 public void setTextAdjustY(final int textAdjustY
) {
823 synchronized (this) {
824 this.textAdjustY
= textAdjustY
;
825 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
826 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
832 * Get the height text adjustment.
834 * @return height text adjustment
836 public int getTextAdjustHeight() {
837 return textAdjustHeight
;
841 * Set the height text adjustment.
843 * @param textAdjustHeight the height text adjustment
845 public void setTextAdjustHeight(final int textAdjustHeight
) {
846 synchronized (this) {
847 this.textAdjustHeight
= textAdjustHeight
;
848 textHeight
= fontTextHeight
+ textAdjustHeight
;
849 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
850 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
856 * Get the width text adjustment.
858 * @return width text adjustment
860 public int getTextAdjustWidth() {
861 return textAdjustWidth
;
865 * Set the width text adjustment.
867 * @param textAdjustWidth the width text adjustment
869 public void setTextAdjustWidth(final int textAdjustWidth
) {
870 synchronized (this) {
871 this.textAdjustWidth
= textAdjustWidth
;
872 textWidth
= fontTextWidth
+ textAdjustWidth
;
873 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
874 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
880 * Convert a CellAttributes foreground color to an Swing Color.
882 * @param attr the text attributes
883 * @return the Swing Color
885 private Color
attrToForegroundColor(final CellAttributes attr
) {
886 int rgb
= attr
.getForeColorRGB();
888 int red
= (rgb
>> 16) & 0xFF;
889 int green
= (rgb
>> 8) & 0xFF;
890 int blue
= rgb
& 0xFF;
892 return new Color(red
, green
, blue
);
896 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
898 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
900 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
902 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
904 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
905 return MYBOLD_YELLOW
;
906 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
908 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
909 return MYBOLD_MAGENTA
;
910 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
914 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
916 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
918 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
920 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
922 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
924 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
926 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
928 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
932 throw new IllegalArgumentException("Invalid color: " +
933 attr
.getForeColor().getValue());
937 * Convert a CellAttributes background color to an Swing Color.
939 * @param attr the text attributes
940 * @return the Swing Color
942 private Color
attrToBackgroundColor(final CellAttributes attr
) {
943 int rgb
= attr
.getBackColorRGB();
945 int red
= (rgb
>> 16) & 0xFF;
946 int green
= (rgb
>> 8) & 0xFF;
947 int blue
= rgb
& 0xFF;
949 return new Color(red
, green
, blue
);
952 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
954 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
956 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
958 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
960 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
962 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
964 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
966 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
969 throw new IllegalArgumentException("Invalid color: " +
970 attr
.getBackColor().getValue());
974 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
975 * textAdjustWidth should be, based on the location of a vertical bar and
978 private void getFontAdjustments() {
979 BufferedImage image
= null;
981 // What SHOULD happen is that the topmost/leftmost white pixel is at
982 // position (gr2x, gr2y). But it might also be off by a pixel in
985 Graphics2D gr2
= null;
988 image
= new BufferedImage(fontTextWidth
* 2, fontTextHeight
* 2,
989 BufferedImage
.TYPE_INT_ARGB
);
991 gr2
= image
.createGraphics();
992 gr2
.setFont(swing
.getFont());
993 gr2
.setColor(java
.awt
.Color
.BLACK
);
994 gr2
.fillRect(0, 0, fontTextWidth
* 2, fontTextHeight
* 2);
995 gr2
.setColor(java
.awt
.Color
.WHITE
);
996 char [] chars
= new char[1];
997 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
998 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
999 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
1000 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1003 int top
= fontTextHeight
* 2;
1005 int left
= fontTextWidth
* 2;
1009 textAdjustHeight
= 0;
1010 textAdjustWidth
= 0;
1012 for (int x
= 0; x
< fontTextWidth
* 2; x
++) {
1013 for (int y
= 0; y
< fontTextHeight
* 2; y
++) {
1016 System.err.println("H X: " + x + " Y: " + y + " " +
1017 image.getRGB(x, y));
1020 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
1021 // Pixel is present.
1038 textAdjustX
= (gr2x
- left
);
1039 textAdjustWidth
= fontTextWidth
- (right
- left
+ 1);
1042 textAdjustY
= (gr2y
- top
);
1043 textAdjustHeight
= fontTextHeight
- (bottom
- top
+ 1);
1045 // System.err.println("top " + top + " bottom " + bottom);
1046 // System.err.println("left " + left + " right " + right);
1048 // Special case: do not believe fonts that claim to be wider than
1050 if (fontTextWidth
>= fontTextHeight
) {
1052 textAdjustWidth
= 0;
1053 fontTextWidth
= fontTextHeight
/ 2;
1058 * Figure out my font dimensions. This code path works OK for the JFrame
1059 * case, and can be called immediately after JFrame creation.
1061 private void getFontDimensions() {
1062 swing
.setFont(font
);
1063 Graphics gr
= swing
.getGraphics();
1067 getFontDimensions(gr
);
1071 * Figure out my font dimensions. This code path is needed to lazy-load
1072 * the information inside paint().
1074 * @param gr Graphics object to use
1076 private void getFontDimensions(final Graphics gr
) {
1077 swing
.setFont(font
);
1078 FontMetrics fm
= gr
.getFontMetrics();
1079 maxDescent
= fm
.getMaxDescent();
1080 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
1081 int leading
= fm
.getLeading();
1082 fontTextWidth
= (int)Math
.round(bounds
.getWidth());
1083 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1085 // This produces the same number, but works better for ugly
1087 fontTextHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
1089 getFontAdjustments();
1090 textHeight
= fontTextHeight
+ textAdjustHeight
;
1091 textWidth
= fontTextWidth
+ textAdjustWidth
;
1093 if (sessionInfo
!= null) {
1094 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
1096 gotFontDimensions
= true;
1100 * Resize the physical screen to match the logical screen dimensions.
1103 public void resizeToScreen() {
1104 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
1109 * Draw one cell's image to the screen.
1111 * @param gr the Swing Graphics context
1112 * @param cell the Cell to draw
1113 * @param xPixel the x-coordinate to render to. 0 means the
1114 * left-most pixel column.
1115 * @param yPixel the y-coordinate to render to. 0 means the top-most
1118 private void drawImage(final Graphics gr
, final Cell cell
,
1119 final int xPixel
, final int yPixel
) {
1122 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1126 // Draw the background rectangle, then the foreground character.
1127 assert (cell
.isImage());
1128 gr
.setColor(cell
.getBackground());
1129 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1131 BufferedImage image
= cell
.getImage();
1132 if (image
!= null) {
1133 if (swing
.getFrame() != null) {
1134 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1136 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1143 * Draw one glyph to the screen.
1145 * @param gr the Swing Graphics context
1146 * @param cell the Cell to draw
1147 * @param xPixel the x-coordinate to render to. 0 means the
1148 * left-most pixel column.
1149 * @param yPixel the y-coordinate to render to. 0 means the top-most
1152 private void drawGlyph(final Graphics gr
, final Cell cell
,
1153 final int xPixel
, final int yPixel
) {
1156 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1160 BufferedImage image
= null;
1161 if (cell
.isBlink() && !cursorBlinkVisible
) {
1162 image
= glyphCacheBlink
.get(cell
);
1164 image
= glyphCache
.get(cell
);
1166 if (image
!= null) {
1167 if (swing
.getFrame() != null) {
1168 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1170 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1175 // Generate glyph and draw it.
1176 Graphics2D gr2
= null;
1179 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1180 image
= new BufferedImage(textWidth
, textHeight
,
1181 BufferedImage
.TYPE_INT_ARGB
);
1182 gr2
= image
.createGraphics();
1183 gr2
.setFont(swing
.getFont());
1187 gr2
= (Graphics2D
) gr
;
1190 Cell cellColor
= new Cell();
1191 cellColor
.setTo(cell
);
1193 // Check for reverse
1194 if (cell
.isReverse()) {
1195 cellColor
.setForeColor(cell
.getBackColor());
1196 cellColor
.setBackColor(cell
.getForeColor());
1199 // Draw the background rectangle, then the foreground character.
1200 gr2
.setColor(attrToBackgroundColor(cellColor
));
1201 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1203 // Handle blink and underline
1205 || (cell
.isBlink() && cursorBlinkVisible
)
1207 gr2
.setColor(attrToForegroundColor(cellColor
));
1208 char [] chars
= new char[1];
1209 chars
[0] = cell
.getChar();
1210 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1211 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1213 if (cell
.isUnderline()) {
1214 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1218 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1221 // We need a new key that will not be mutated by
1223 Cell key
= new Cell();
1225 if (cell
.isBlink() && !cursorBlinkVisible
) {
1226 glyphCacheBlink
.put(key
, image
);
1228 glyphCache
.put(key
, image
);
1231 if (swing
.getFrame() != null) {
1232 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1234 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1241 * Check if the cursor is visible, and if so draw it.
1243 * @param gr the Swing Graphics context
1245 private void drawCursor(final Graphics gr
) {
1250 && (cursorY
<= height
- 1)
1251 && (cursorX
<= width
- 1)
1252 && cursorBlinkVisible
1254 int xPixel
= cursorX
* textWidth
+ left
;
1255 int yPixel
= cursorY
* textHeight
+ top
;
1256 Cell lCell
= logical
[cursorX
][cursorY
];
1257 gr
.setColor(attrToForegroundColor(lCell
));
1258 switch (cursorStyle
) {
1262 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1265 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1268 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1275 * Reset the blink timer.
1277 private void resetBlinkTimer() {
1278 lastBlinkTime
= System
.currentTimeMillis();
1279 cursorBlinkVisible
= true;
1283 * Paint redraws the whole screen.
1285 * @param gr the Swing Graphics context
1287 public void paint(final Graphics gr
) {
1289 if (gotFontDimensions
== false) {
1290 // Lazy-load the text width/height
1291 getFontDimensions(gr
);
1293 System.err.println("textWidth " + textWidth +
1294 " textHeight " + textHeight);
1295 System.err.println("FONT: " + swing.getFont() + " font " + font);
1299 if ((swing
.getFrame() != null)
1300 && (swing
.getBufferStrategy() != null)
1301 && (SwingUtilities
.isEventDispatchThread())
1303 // System.err.println("paint(), skip first paint on swing thread");
1308 int xCellMax
= width
;
1310 int yCellMax
= height
;
1312 Rectangle bounds
= gr
.getClipBounds();
1313 if (bounds
!= null) {
1314 // Only update what is in the bounds
1315 xCellMin
= textColumn(bounds
.x
);
1316 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1317 if (xCellMax
> width
) {
1320 if (xCellMin
>= xCellMax
) {
1321 xCellMin
= xCellMax
- 2;
1326 yCellMin
= textRow(bounds
.y
);
1327 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1328 if (yCellMax
> height
) {
1331 if (yCellMin
>= yCellMax
) {
1332 yCellMin
= yCellMax
- 2;
1338 // We need a total repaint
1339 reallyCleared
= true;
1342 // Prevent updates to the screen's data from the TApplication
1344 synchronized (this) {
1347 System.err.printf("bounds %s X %d %d Y %d %d\n",
1348 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1351 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1352 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1354 int xPixel
= x
* textWidth
+ left
;
1355 int yPixel
= y
* textHeight
+ top
;
1357 Cell lCell
= logical
[x
][y
];
1358 Cell pCell
= physical
[x
][y
];
1360 if (!lCell
.equals(pCell
)
1363 || (swing
.getFrame() == null)) {
1365 if (lCell
.isImage()) {
1366 drawImage(gr
, lCell
, xPixel
, yPixel
);
1368 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1371 // Physical is always updated
1372 physical
[x
][y
].setTo(lCell
);
1378 reallyCleared
= false;
1379 } // synchronized (this)
1383 * Restore terminal to normal state.
1385 public void shutdown() {
1390 * Push the logical screen to the physical device.
1392 private void drawToSwing() {
1395 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1396 reallyCleared, dirty);
1399 // If reallyCleared is set, we have to draw everything.
1400 if ((swing
.getFrame() != null)
1401 && (swing
.getBufferStrategy() != null)
1402 && (reallyCleared
== true)
1404 // Triple-buffering: we have to redraw everything on this thread.
1405 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1408 swing
.getBufferStrategy().show();
1409 Toolkit
.getDefaultToolkit().sync();
1411 } else if (((swing
.getFrame() != null)
1412 && (swing
.getBufferStrategy() == null))
1413 || (reallyCleared
== true)
1415 // Repaint everything on the Swing thread.
1416 // System.err.println("REPAINT ALL");
1421 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1422 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1424 synchronized (this) {
1425 for (int y
= 0; y
< height
; y
++) {
1426 for (int x
= 0; x
< width
; x
++) {
1427 Cell lCell
= logical
[x
][y
];
1428 Cell pCell
= physical
[x
][y
];
1430 int xPixel
= x
* textWidth
+ left
;
1431 int yPixel
= y
* textHeight
+ top
;
1433 if (!lCell
.equals(pCell
)
1437 || (lCell
.isBlink())
1439 if (lCell
.isImage()) {
1440 drawImage(gr
, lCell
, xPixel
, yPixel
);
1442 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1444 physical
[x
][y
].setTo(lCell
);
1449 } // synchronized (this)
1452 swing
.getBufferStrategy().show();
1453 Toolkit
.getDefaultToolkit().sync();
1457 // Swing thread version: request a repaint, but limit it to the area
1458 // that has changed.
1460 // Find the minimum-size damaged region.
1461 int xMin
= swing
.getWidth();
1463 int yMin
= swing
.getHeight();
1466 synchronized (this) {
1467 for (int y
= 0; y
< height
; y
++) {
1468 for (int x
= 0; x
< width
; x
++) {
1469 Cell lCell
= logical
[x
][y
];
1470 Cell pCell
= physical
[x
][y
];
1472 int xPixel
= x
* textWidth
+ left
;
1473 int yPixel
= y
* textHeight
+ top
;
1475 if (!lCell
.equals(pCell
)
1481 if (xPixel
< xMin
) {
1484 if (xPixel
+ textWidth
> xMax
) {
1485 xMax
= xPixel
+ textWidth
;
1487 if (yPixel
< yMin
) {
1490 if (yPixel
+ textHeight
> yMax
) {
1491 yMax
= yPixel
+ textHeight
;
1497 if (xMin
+ textWidth
>= xMax
) {
1500 if (yMin
+ textHeight
>= yMax
) {
1504 // Repaint the desired area
1506 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1510 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1511 // This path should never be taken, but is left here for
1513 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1514 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1519 swing
.getBufferStrategy().show();
1520 Toolkit
.getDefaultToolkit().sync();
1522 // Repaint on the Swing thread.
1523 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1528 * Convert pixel column position to text cell column position.
1530 * @param x pixel column position
1531 * @return text cell column position
1533 public int textColumn(final int x
) {
1534 int column
= ((x
- left
) / textWidth
);
1538 if (column
> width
- 1) {
1545 * Convert pixel row position to text cell row position.
1547 * @param y pixel row position
1548 * @return text cell row position
1550 public int textRow(final int y
) {
1551 int row
= ((y
- top
) / textHeight
);
1555 if (row
> height
- 1) {
1562 * Getter for sessionInfo.
1564 * @return the SessionInfo
1566 public SessionInfo
getSessionInfo() {
1571 * Getter for the underlying Swing component.
1573 * @return the SwingComponent
1575 public SwingComponent
getSwingComponent() {
1579 // ------------------------------------------------------------------------
1580 // KeyListener ------------------------------------------------------------
1581 // ------------------------------------------------------------------------
1584 * Pass Swing keystrokes into the event queue.
1586 * @param key keystroke received
1588 public void keyReleased(final KeyEvent key
) {
1589 // Ignore release events
1593 * Pass Swing keystrokes into the event queue.
1595 * @param key keystroke received
1597 public void keyTyped(final KeyEvent key
) {
1598 // Ignore typed events
1602 * Pass Swing keystrokes into the event queue.
1604 * @param key keystroke received
1606 public void keyPressed(final KeyEvent key
) {
1607 boolean alt
= false;
1608 boolean shift
= false;
1609 boolean ctrl
= false;
1611 boolean isKey
= false;
1612 if (key
.isActionKey()) {
1615 ch
= key
.getKeyChar();
1617 alt
= key
.isAltDown();
1618 ctrl
= key
.isControlDown();
1619 shift
= key
.isShiftDown();
1622 System.err.printf("Swing Key: %s\n", key);
1623 System.err.printf(" isKey: %s\n", isKey);
1624 System.err.printf(" alt: %s\n", alt);
1625 System.err.printf(" ctrl: %s\n", ctrl);
1626 System.err.printf(" shift: %s\n", shift);
1627 System.err.printf(" ch: %s\n", ch);
1630 // Special case: not return the bare modifier presses
1631 switch (key
.getKeyCode()) {
1632 case KeyEvent
.VK_ALT
:
1634 case KeyEvent
.VK_ALT_GRAPH
:
1636 case KeyEvent
.VK_CONTROL
:
1638 case KeyEvent
.VK_SHIFT
:
1640 case KeyEvent
.VK_META
:
1646 TKeypress keypress
= null;
1648 switch (key
.getKeyCode()) {
1649 case KeyEvent
.VK_F1
:
1650 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1653 case KeyEvent
.VK_F2
:
1654 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1657 case KeyEvent
.VK_F3
:
1658 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1661 case KeyEvent
.VK_F4
:
1662 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1665 case KeyEvent
.VK_F5
:
1666 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1669 case KeyEvent
.VK_F6
:
1670 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1673 case KeyEvent
.VK_F7
:
1674 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1677 case KeyEvent
.VK_F8
:
1678 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1681 case KeyEvent
.VK_F9
:
1682 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1685 case KeyEvent
.VK_F10
:
1686 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1689 case KeyEvent
.VK_F11
:
1690 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1693 case KeyEvent
.VK_F12
:
1694 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1697 case KeyEvent
.VK_HOME
:
1698 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1701 case KeyEvent
.VK_END
:
1702 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1705 case KeyEvent
.VK_PAGE_UP
:
1706 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1709 case KeyEvent
.VK_PAGE_DOWN
:
1710 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1713 case KeyEvent
.VK_INSERT
:
1714 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1717 case KeyEvent
.VK_DELETE
:
1718 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1721 case KeyEvent
.VK_RIGHT
:
1722 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1725 case KeyEvent
.VK_LEFT
:
1726 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1729 case KeyEvent
.VK_UP
:
1730 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1733 case KeyEvent
.VK_DOWN
:
1734 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1737 case KeyEvent
.VK_TAB
:
1738 // Special case: distinguish TAB vs BTAB
1740 keypress
= kbShiftTab
;
1745 case KeyEvent
.VK_ENTER
:
1746 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1749 case KeyEvent
.VK_ESCAPE
:
1750 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1753 case KeyEvent
.VK_BACK_SPACE
:
1754 keypress
= kbBackspace
;
1757 // Unsupported, ignore
1762 if (keypress
== null) {
1765 // Disambiguate ^H from Backspace.
1766 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1768 keypress
= kbBackspace
;
1770 // We are emulating Xterm here, where the backspace key
1771 // on the keyboard returns ^?.
1772 keypress
= kbBackspaceDel
;
1786 keypress
= kbShiftTab
;
1795 if (!alt
&& ctrl
&& !shift
) {
1796 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1798 // Not a special key, put it together
1799 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1803 // Save it and we are done.
1804 synchronized (eventQueue
) {
1805 eventQueue
.add(new TKeypressEvent(keypress
));
1808 if (listener
!= null) {
1809 synchronized (listener
) {
1810 listener
.notifyAll();
1815 // ------------------------------------------------------------------------
1816 // WindowListener ---------------------------------------------------------
1817 // ------------------------------------------------------------------------
1820 * Pass window events into the event queue.
1822 * @param event window event received
1824 public void windowActivated(final WindowEvent event
) {
1825 // Force a total repaint
1826 synchronized (this) {
1832 * Pass window events into the event queue.
1834 * @param event window event received
1836 public void windowClosed(final WindowEvent event
) {
1841 * Pass window events into the event queue.
1843 * @param event window event received
1845 public void windowClosing(final WindowEvent event
) {
1846 // Drop a cmAbort and walk away
1847 synchronized (eventQueue
) {
1848 eventQueue
.add(new TCommandEvent(cmAbort
));
1851 if (listener
!= null) {
1852 synchronized (listener
) {
1853 listener
.notifyAll();
1859 * Pass window events into the event queue.
1861 * @param event window event received
1863 public void windowDeactivated(final WindowEvent event
) {
1868 * Pass window events into the event queue.
1870 * @param event window event received
1872 public void windowDeiconified(final WindowEvent event
) {
1877 * Pass window events into the event queue.
1879 * @param event window event received
1881 public void windowIconified(final WindowEvent event
) {
1886 * Pass window events into the event queue.
1888 * @param event window event received
1890 public void windowOpened(final WindowEvent event
) {
1894 // ------------------------------------------------------------------------
1895 // ComponentListener ------------------------------------------------------
1896 // ------------------------------------------------------------------------
1899 * Pass component events into the event queue.
1901 * @param event component event received
1903 public void componentHidden(final ComponentEvent event
) {
1908 * Pass component events into the event queue.
1910 * @param event component event received
1912 public void componentShown(final ComponentEvent event
) {
1917 * Pass component events into the event queue.
1919 * @param event component event received
1921 public void componentMoved(final ComponentEvent event
) {
1926 * Pass component events into the event queue.
1928 * @param event component event received
1930 public void componentResized(final ComponentEvent event
) {
1931 if (gotFontDimensions
== false) {
1932 // We are still waiting to get font information. Don't pass a
1934 // System.err.println("size " + swing.getComponent().getSize());
1938 // Drop a new TResizeEvent into the queue
1939 sessionInfo
.queryWindowSize();
1940 synchronized (eventQueue
) {
1941 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1942 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1943 eventQueue
.add(windowResize
);
1946 System.err.println("Add resize event: " + windowResize.getWidth() +
1947 " x " + windowResize.getHeight());
1950 if (listener
!= null) {
1951 synchronized (listener
) {
1952 listener
.notifyAll();
1957 // ------------------------------------------------------------------------
1958 // MouseMotionListener ----------------------------------------------------
1959 // ------------------------------------------------------------------------
1962 * Pass mouse events into the event queue.
1964 * @param mouse mouse event received
1966 public void mouseDragged(final MouseEvent mouse
) {
1967 int modifiers
= mouse
.getModifiersEx();
1968 boolean eventMouse1
= false;
1969 boolean eventMouse2
= false;
1970 boolean eventMouse3
= false;
1971 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1974 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1977 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1980 mouse1
= eventMouse1
;
1981 mouse2
= eventMouse2
;
1982 mouse3
= eventMouse3
;
1983 int x
= textColumn(mouse
.getX());
1984 int y
= textRow(mouse
.getY());
1986 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1987 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1989 synchronized (eventQueue
) {
1990 eventQueue
.add(mouseEvent
);
1993 if (listener
!= null) {
1994 synchronized (listener
) {
1995 listener
.notifyAll();
2001 * Pass mouse events into the event queue.
2003 * @param mouse mouse event received
2005 public void mouseMoved(final MouseEvent mouse
) {
2006 int x
= textColumn(mouse
.getX());
2007 int y
= textRow(mouse
.getY());
2008 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2009 // Bail out, we've moved some pixels but not a whole text cell.
2015 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2016 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2018 synchronized (eventQueue
) {
2019 eventQueue
.add(mouseEvent
);
2022 if (listener
!= null) {
2023 synchronized (listener
) {
2024 listener
.notifyAll();
2029 // ------------------------------------------------------------------------
2030 // MouseListener ----------------------------------------------------------
2031 // ------------------------------------------------------------------------
2034 * Pass mouse events into the event queue.
2036 * @param mouse mouse event received
2038 public void mouseClicked(final MouseEvent mouse
) {
2043 * Pass mouse events into the event queue.
2045 * @param mouse mouse event received
2047 public void mouseEntered(final MouseEvent mouse
) {
2052 * Pass mouse events into the event queue.
2054 * @param mouse mouse event received
2056 public void mouseExited(final MouseEvent mouse
) {
2061 * Pass mouse events into the event queue.
2063 * @param mouse mouse event received
2065 public void mousePressed(final MouseEvent mouse
) {
2066 int modifiers
= mouse
.getModifiersEx();
2067 boolean eventMouse1
= false;
2068 boolean eventMouse2
= false;
2069 boolean eventMouse3
= false;
2070 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2073 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2076 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2079 mouse1
= eventMouse1
;
2080 mouse2
= eventMouse2
;
2081 mouse3
= eventMouse3
;
2082 int x
= textColumn(mouse
.getX());
2083 int y
= textRow(mouse
.getY());
2085 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2086 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2088 synchronized (eventQueue
) {
2089 eventQueue
.add(mouseEvent
);
2092 if (listener
!= null) {
2093 synchronized (listener
) {
2094 listener
.notifyAll();
2100 * Pass mouse events into the event queue.
2102 * @param mouse mouse event received
2104 public void mouseReleased(final MouseEvent mouse
) {
2105 int modifiers
= mouse
.getModifiersEx();
2106 boolean eventMouse1
= false;
2107 boolean eventMouse2
= false;
2108 boolean eventMouse3
= false;
2109 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2112 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2115 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2130 int x
= textColumn(mouse
.getX());
2131 int y
= textRow(mouse
.getY());
2133 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2134 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
2136 synchronized (eventQueue
) {
2137 eventQueue
.add(mouseEvent
);
2140 if (listener
!= null) {
2141 synchronized (listener
) {
2142 listener
.notifyAll();
2147 // ------------------------------------------------------------------------
2148 // MouseWheelListener -----------------------------------------------------
2149 // ------------------------------------------------------------------------
2152 * Pass mouse events into the event queue.
2154 * @param mouse mouse event received
2156 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2157 int modifiers
= mouse
.getModifiersEx();
2158 boolean eventMouse1
= false;
2159 boolean eventMouse2
= false;
2160 boolean eventMouse3
= false;
2161 boolean mouseWheelUp
= false;
2162 boolean mouseWheelDown
= false;
2163 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2166 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2169 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2172 mouse1
= eventMouse1
;
2173 mouse2
= eventMouse2
;
2174 mouse3
= eventMouse3
;
2175 int x
= textColumn(mouse
.getX());
2176 int y
= textRow(mouse
.getY());
2177 if (mouse
.getWheelRotation() > 0) {
2178 mouseWheelDown
= true;
2180 if (mouse
.getWheelRotation() < 0) {
2181 mouseWheelUp
= true;
2184 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2185 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2187 synchronized (eventQueue
) {
2188 eventQueue
.add(mouseEvent
);
2191 if (listener
!= null) {
2192 synchronized (listener
) {
2193 listener
.notifyAll();