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)
572 } while (swing
.getBufferStrategy().contentsRestored());
574 swing
.getBufferStrategy().show();
575 Toolkit
.getDefaultToolkit().sync();
576 } while (swing
.getBufferStrategy().contentsLost());
579 // Non-triple-buffered, call drawToSwing() once
584 // ------------------------------------------------------------------------
585 // TerminalReader ---------------------------------------------------------
586 // ------------------------------------------------------------------------
589 * Check if there are events in the queue.
591 * @return if true, getEvents() has something to return to the backend
593 public boolean hasEvents() {
594 synchronized (eventQueue
) {
595 return (eventQueue
.size() > 0);
600 * Return any events in the IO queue.
602 * @param queue list to append new events to
604 public void getEvents(final List
<TInputEvent
> queue
) {
605 synchronized (eventQueue
) {
606 if (eventQueue
.size() > 0) {
607 synchronized (queue
) {
608 queue
.addAll(eventQueue
);
616 * Restore terminal to normal state.
618 public void closeTerminal() {
623 * Set listener to a different Object.
625 * @param listener the new listening object that run() wakes up on new
628 public void setListener(final Object listener
) {
629 this.listener
= listener
;
633 * Reload options from System properties.
635 public void reloadOptions() {
636 // Figure out my cursor style.
637 String cursorStyleString
= System
.getProperty(
638 "jexer.Swing.cursorStyle", "underline").toLowerCase();
639 if (cursorStyleString
.equals("underline")) {
640 cursorStyle
= CursorStyle
.UNDERLINE
;
641 } else if (cursorStyleString
.equals("outline")) {
642 cursorStyle
= CursorStyle
.OUTLINE
;
643 } else if (cursorStyleString
.equals("block")) {
644 cursorStyle
= CursorStyle
.BLOCK
;
647 // Pull the system property for triple buffering.
648 if (System
.getProperty("jexer.Swing.tripleBuffer",
649 "true").equals("true")
651 SwingComponent
.tripleBuffer
= true;
653 SwingComponent
.tripleBuffer
= false;
657 // ------------------------------------------------------------------------
658 // SwingTerminal ----------------------------------------------------------
659 // ------------------------------------------------------------------------
662 * Get the width of a character cell in pixels.
664 * @return the width in pixels of a character cell
666 public int getTextWidth() {
671 * Get the height of a character cell in pixels.
673 * @return the height in pixels of a character cell
675 public int getTextHeight() {
680 * Setup Swing colors to match DOS color palette.
682 private static void setDOSColors() {
686 MYBLACK
= new Color(0x00, 0x00, 0x00);
687 MYRED
= new Color(0xa8, 0x00, 0x00);
688 MYGREEN
= new Color(0x00, 0xa8, 0x00);
689 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
690 MYBLUE
= new Color(0x00, 0x00, 0xa8);
691 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
692 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
693 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
694 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
695 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
696 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
697 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
698 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
699 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
700 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
701 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
707 * Get the number of millis to wait before switching the blink from
708 * visible to invisible.
710 * @return the number of milli to wait before switching the blink from
711 * visible to invisible
713 public long getBlinkMillis() {
718 * Get the font size in points.
720 * @return font size in points
722 public int getFontSize() {
727 * Set the font size in points.
729 * @param fontSize font size in points
731 public void setFontSize(final int fontSize
) {
732 this.fontSize
= fontSize
;
733 Font newFont
= font
.deriveFont((float) fontSize
);
738 * Set to a new font, and resize the screen to match its dimensions.
740 * @param font the new font
742 public void setFont(final Font font
) {
743 synchronized (this) {
747 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
748 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
754 * Get the font this screen was last set to.
758 public Font
getFont() {
763 * Set the font to Terminus, the best all-around font for both CP437 and
766 public void setDefaultFont() {
768 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
769 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
770 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
771 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
773 } catch (java
.awt
.FontFormatException e
) {
775 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
776 } catch (java
.io
.IOException e
) {
778 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
785 * Get the X text adjustment.
787 * @return X text adjustment
789 public int getTextAdjustX() {
794 * Set the X text adjustment.
796 * @param textAdjustX the X text adjustment
798 public void setTextAdjustX(final int textAdjustX
) {
799 synchronized (this) {
800 this.textAdjustX
= textAdjustX
;
801 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
802 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
808 * Get the Y text adjustment.
810 * @return Y text adjustment
812 public int getTextAdjustY() {
817 * Set the Y text adjustment.
819 * @param textAdjustY the Y text adjustment
821 public void setTextAdjustY(final int textAdjustY
) {
822 synchronized (this) {
823 this.textAdjustY
= textAdjustY
;
824 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
825 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
831 * Get the height text adjustment.
833 * @return height text adjustment
835 public int getTextAdjustHeight() {
836 return textAdjustHeight
;
840 * Set the height text adjustment.
842 * @param textAdjustHeight the height text adjustment
844 public void setTextAdjustHeight(final int textAdjustHeight
) {
845 synchronized (this) {
846 this.textAdjustHeight
= textAdjustHeight
;
847 textHeight
= fontTextHeight
+ textAdjustHeight
;
848 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
849 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
855 * Get the width text adjustment.
857 * @return width text adjustment
859 public int getTextAdjustWidth() {
860 return textAdjustWidth
;
864 * Set the width text adjustment.
866 * @param textAdjustWidth the width text adjustment
868 public void setTextAdjustWidth(final int textAdjustWidth
) {
869 synchronized (this) {
870 this.textAdjustWidth
= textAdjustWidth
;
871 textWidth
= fontTextWidth
+ textAdjustWidth
;
872 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
873 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
879 * Convert a CellAttributes foreground color to an Swing Color.
881 * @param attr the text attributes
882 * @return the Swing Color
884 private Color
attrToForegroundColor(final CellAttributes attr
) {
885 int rgb
= attr
.getForeColorRGB();
887 int red
= (rgb
>> 16) & 0xFF;
888 int green
= (rgb
>> 8) & 0xFF;
889 int blue
= rgb
& 0xFF;
891 return new Color(red
, green
, blue
);
895 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
897 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
899 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
901 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
903 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
904 return MYBOLD_YELLOW
;
905 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
907 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
908 return MYBOLD_MAGENTA
;
909 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
913 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
915 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
917 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
919 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
921 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
923 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
925 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
927 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
931 throw new IllegalArgumentException("Invalid color: " +
932 attr
.getForeColor().getValue());
936 * Convert a CellAttributes background color to an Swing Color.
938 * @param attr the text attributes
939 * @return the Swing Color
941 private Color
attrToBackgroundColor(final CellAttributes attr
) {
942 int rgb
= attr
.getBackColorRGB();
944 int red
= (rgb
>> 16) & 0xFF;
945 int green
= (rgb
>> 8) & 0xFF;
946 int blue
= rgb
& 0xFF;
948 return new Color(red
, green
, blue
);
951 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
953 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
955 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
957 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
959 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
961 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
963 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
965 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
968 throw new IllegalArgumentException("Invalid color: " +
969 attr
.getBackColor().getValue());
973 * Figure out what textAdjustX, textAdjustY, textAdjustHeight, and
974 * textAdjustWidth should be, based on the location of a vertical bar and
977 private void getFontAdjustments() {
978 BufferedImage image
= null;
980 // What SHOULD happen is that the topmost/leftmost white pixel is at
981 // position (gr2x, gr2y). But it might also be off by a pixel in
984 Graphics2D gr2
= null;
987 image
= new BufferedImage(fontTextWidth
* 2, fontTextHeight
* 2,
988 BufferedImage
.TYPE_INT_ARGB
);
990 gr2
= image
.createGraphics();
991 gr2
.setFont(swing
.getFont());
992 gr2
.setColor(java
.awt
.Color
.BLACK
);
993 gr2
.fillRect(0, 0, fontTextWidth
* 2, fontTextHeight
* 2);
994 gr2
.setColor(java
.awt
.Color
.WHITE
);
995 char [] chars
= new char[1];
996 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
997 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
998 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
999 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ fontTextHeight
- maxDescent
);
1002 int top
= fontTextHeight
* 2;
1004 int left
= fontTextWidth
* 2;
1008 textAdjustHeight
= 0;
1009 textAdjustWidth
= 0;
1011 for (int x
= 0; x
< fontTextWidth
* 2; x
++) {
1012 for (int y
= 0; y
< fontTextHeight
* 2; y
++) {
1015 System.err.println("H X: " + x + " Y: " + y + " " +
1016 image.getRGB(x, y));
1019 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
1020 // Pixel is present.
1037 textAdjustX
= (gr2x
- left
);
1038 textAdjustWidth
= fontTextWidth
- (right
- left
+ 1);
1041 textAdjustY
= (gr2y
- top
);
1042 textAdjustHeight
= fontTextHeight
- (bottom
- top
+ 1);
1044 // System.err.println("top " + top + " bottom " + bottom);
1045 // System.err.println("left " + left + " right " + right);
1047 // Special case: do not believe fonts that claim to be wider than
1049 if (fontTextWidth
>= fontTextHeight
) {
1051 textAdjustWidth
= 0;
1052 fontTextWidth
= fontTextHeight
/ 2;
1057 * Figure out my font dimensions. This code path works OK for the JFrame
1058 * case, and can be called immediately after JFrame creation.
1060 private void getFontDimensions() {
1061 swing
.setFont(font
);
1062 Graphics gr
= swing
.getGraphics();
1066 getFontDimensions(gr
);
1070 * Figure out my font dimensions. This code path is needed to lazy-load
1071 * the information inside paint().
1073 * @param gr Graphics object to use
1075 private void getFontDimensions(final Graphics gr
) {
1076 swing
.setFont(font
);
1077 FontMetrics fm
= gr
.getFontMetrics();
1078 maxDescent
= fm
.getMaxDescent();
1079 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
1080 int leading
= fm
.getLeading();
1081 fontTextWidth
= (int)Math
.round(bounds
.getWidth());
1082 // fontTextHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
1084 // This produces the same number, but works better for ugly
1086 fontTextHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
1088 getFontAdjustments();
1089 textHeight
= fontTextHeight
+ textAdjustHeight
;
1090 textWidth
= fontTextWidth
+ textAdjustWidth
;
1092 if (sessionInfo
!= null) {
1093 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
1095 gotFontDimensions
= true;
1099 * Resize to font dimensions.
1101 public void resizeToScreen() {
1102 swing
.setDimensions(textWidth
* (width
+ 1), textHeight
* (height
+ 1));
1106 * Draw one cell's image to the screen.
1108 * @param gr the Swing Graphics context
1109 * @param cell the Cell to draw
1110 * @param xPixel the x-coordinate to render to. 0 means the
1111 * left-most pixel column.
1112 * @param yPixel the y-coordinate to render to. 0 means the top-most
1115 private void drawImage(final Graphics gr
, final Cell cell
,
1116 final int xPixel
, final int yPixel
) {
1119 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1123 // Draw the background rectangle, then the foreground character.
1124 assert (cell
.isImage());
1125 gr
.setColor(cell
.getBackground());
1126 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1128 BufferedImage image
= cell
.getImage();
1129 if (image
!= null) {
1130 if (swing
.getFrame() != null) {
1131 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1133 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1140 * Draw one glyph to the screen.
1142 * @param gr the Swing Graphics context
1143 * @param cell the Cell to draw
1144 * @param xPixel the x-coordinate to render to. 0 means the
1145 * left-most pixel column.
1146 * @param yPixel the y-coordinate to render to. 0 means the top-most
1149 private void drawGlyph(final Graphics gr
, final Cell cell
,
1150 final int xPixel
, final int yPixel
) {
1153 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1157 BufferedImage image
= null;
1158 if (cell
.isBlink() && !cursorBlinkVisible
) {
1159 image
= glyphCacheBlink
.get(cell
);
1161 image
= glyphCache
.get(cell
);
1163 if (image
!= null) {
1164 if (swing
.getFrame() != null) {
1165 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1167 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1172 // Generate glyph and draw it.
1173 Graphics2D gr2
= null;
1176 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1177 image
= new BufferedImage(textWidth
, textHeight
,
1178 BufferedImage
.TYPE_INT_ARGB
);
1179 gr2
= image
.createGraphics();
1180 gr2
.setFont(swing
.getFont());
1184 gr2
= (Graphics2D
) gr
;
1187 Cell cellColor
= new Cell();
1188 cellColor
.setTo(cell
);
1190 // Check for reverse
1191 if (cell
.isReverse()) {
1192 cellColor
.setForeColor(cell
.getBackColor());
1193 cellColor
.setBackColor(cell
.getForeColor());
1196 // Draw the background rectangle, then the foreground character.
1197 gr2
.setColor(attrToBackgroundColor(cellColor
));
1198 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1200 // Handle blink and underline
1202 || (cell
.isBlink() && cursorBlinkVisible
)
1204 gr2
.setColor(attrToForegroundColor(cellColor
));
1205 char [] chars
= new char[1];
1206 chars
[0] = cell
.getChar();
1207 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1208 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1210 if (cell
.isUnderline()) {
1211 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1215 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1218 // We need a new key that will not be mutated by
1220 Cell key
= new Cell();
1222 if (cell
.isBlink() && !cursorBlinkVisible
) {
1223 glyphCacheBlink
.put(key
, image
);
1225 glyphCache
.put(key
, image
);
1228 if (swing
.getFrame() != null) {
1229 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1231 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1238 * Check if the cursor is visible, and if so draw it.
1240 * @param gr the Swing Graphics context
1242 private void drawCursor(final Graphics gr
) {
1247 && (cursorY
<= height
- 1)
1248 && (cursorX
<= width
- 1)
1249 && cursorBlinkVisible
1251 int xPixel
= cursorX
* textWidth
+ left
;
1252 int yPixel
= cursorY
* textHeight
+ top
;
1253 Cell lCell
= logical
[cursorX
][cursorY
];
1254 gr
.setColor(attrToForegroundColor(lCell
));
1255 switch (cursorStyle
) {
1259 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1262 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1265 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1272 * Reset the blink timer.
1274 private void resetBlinkTimer() {
1275 lastBlinkTime
= System
.currentTimeMillis();
1276 cursorBlinkVisible
= true;
1280 * Paint redraws the whole screen.
1282 * @param gr the Swing Graphics context
1284 public void paint(final Graphics gr
) {
1286 if (gotFontDimensions
== false) {
1287 // Lazy-load the text width/height
1288 getFontDimensions(gr
);
1290 System.err.println("textWidth " + textWidth +
1291 " textHeight " + textHeight);
1292 System.err.println("FONT: " + swing.getFont() + " font " + font);
1296 if ((swing
.getFrame() != null)
1297 && (swing
.getBufferStrategy() != null)
1298 && (SwingUtilities
.isEventDispatchThread())
1300 // System.err.println("paint(), skip first paint on swing thread");
1305 int xCellMax
= width
;
1307 int yCellMax
= height
;
1309 Rectangle bounds
= gr
.getClipBounds();
1310 if (bounds
!= null) {
1311 // Only update what is in the bounds
1312 xCellMin
= textColumn(bounds
.x
);
1313 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1314 if (xCellMax
> width
) {
1317 if (xCellMin
>= xCellMax
) {
1318 xCellMin
= xCellMax
- 2;
1323 yCellMin
= textRow(bounds
.y
);
1324 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1325 if (yCellMax
> height
) {
1328 if (yCellMin
>= yCellMax
) {
1329 yCellMin
= yCellMax
- 2;
1335 // We need a total repaint
1336 reallyCleared
= true;
1339 // Prevent updates to the screen's data from the TApplication
1341 synchronized (this) {
1344 System.err.printf("bounds %s X %d %d Y %d %d\n",
1345 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1348 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1349 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1351 int xPixel
= x
* textWidth
+ left
;
1352 int yPixel
= y
* textHeight
+ top
;
1354 Cell lCell
= logical
[x
][y
];
1355 Cell pCell
= physical
[x
][y
];
1357 if (!lCell
.equals(pCell
)
1360 || (swing
.getFrame() == null)) {
1362 if (lCell
.isImage()) {
1363 drawImage(gr
, lCell
, xPixel
, yPixel
);
1365 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1368 // Physical is always updated
1369 physical
[x
][y
].setTo(lCell
);
1375 reallyCleared
= false;
1376 } // synchronized (this)
1380 * Restore terminal to normal state.
1382 public void shutdown() {
1387 * Push the logical screen to the physical device.
1389 private void drawToSwing() {
1392 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1393 reallyCleared, dirty);
1396 // If reallyCleared is set, we have to draw everything.
1397 if ((swing
.getFrame() != null)
1398 && (swing
.getBufferStrategy() != null)
1399 && (reallyCleared
== true)
1401 // Triple-buffering: we have to redraw everything on this thread.
1402 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1405 swing
.getBufferStrategy().show();
1406 Toolkit
.getDefaultToolkit().sync();
1408 } else if (((swing
.getFrame() != null)
1409 && (swing
.getBufferStrategy() == null))
1410 || (reallyCleared
== true)
1412 // Repaint everything on the Swing thread.
1413 // System.err.println("REPAINT ALL");
1418 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1419 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1421 synchronized (this) {
1422 for (int y
= 0; y
< height
; y
++) {
1423 for (int x
= 0; x
< width
; x
++) {
1424 Cell lCell
= logical
[x
][y
];
1425 Cell pCell
= physical
[x
][y
];
1427 int xPixel
= x
* textWidth
+ left
;
1428 int yPixel
= y
* textHeight
+ top
;
1430 if (!lCell
.equals(pCell
)
1434 || (lCell
.isBlink())
1436 if (lCell
.isImage()) {
1437 drawImage(gr
, lCell
, xPixel
, yPixel
);
1439 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1441 physical
[x
][y
].setTo(lCell
);
1446 } // synchronized (this)
1449 swing
.getBufferStrategy().show();
1450 Toolkit
.getDefaultToolkit().sync();
1454 // Swing thread version: request a repaint, but limit it to the area
1455 // that has changed.
1457 // Find the minimum-size damaged region.
1458 int xMin
= swing
.getWidth();
1460 int yMin
= swing
.getHeight();
1463 synchronized (this) {
1464 for (int y
= 0; y
< height
; y
++) {
1465 for (int x
= 0; x
< width
; x
++) {
1466 Cell lCell
= logical
[x
][y
];
1467 Cell pCell
= physical
[x
][y
];
1469 int xPixel
= x
* textWidth
+ left
;
1470 int yPixel
= y
* textHeight
+ top
;
1472 if (!lCell
.equals(pCell
)
1478 if (xPixel
< xMin
) {
1481 if (xPixel
+ textWidth
> xMax
) {
1482 xMax
= xPixel
+ textWidth
;
1484 if (yPixel
< yMin
) {
1487 if (yPixel
+ textHeight
> yMax
) {
1488 yMax
= yPixel
+ textHeight
;
1494 if (xMin
+ textWidth
>= xMax
) {
1497 if (yMin
+ textHeight
>= yMax
) {
1501 // Repaint the desired area
1503 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1507 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1508 // This path should never be taken, but is left here for
1510 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1511 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1516 swing
.getBufferStrategy().show();
1517 Toolkit
.getDefaultToolkit().sync();
1519 // Repaint on the Swing thread.
1520 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1525 * Convert pixel column position to text cell column position.
1527 * @param x pixel column position
1528 * @return text cell column position
1530 public int textColumn(final int x
) {
1531 int column
= ((x
- left
) / textWidth
);
1535 if (column
> width
- 1) {
1542 * Convert pixel row position to text cell row position.
1544 * @param y pixel row position
1545 * @return text cell row position
1547 public int textRow(final int y
) {
1548 int row
= ((y
- top
) / textHeight
);
1552 if (row
> height
- 1) {
1559 * Getter for sessionInfo.
1561 * @return the SessionInfo
1563 public SessionInfo
getSessionInfo() {
1568 * Getter for the underlying Swing component.
1570 * @return the SwingComponent
1572 public SwingComponent
getSwingComponent() {
1576 // ------------------------------------------------------------------------
1577 // KeyListener ------------------------------------------------------------
1578 // ------------------------------------------------------------------------
1581 * Pass Swing keystrokes into the event queue.
1583 * @param key keystroke received
1585 public void keyReleased(final KeyEvent key
) {
1586 // Ignore release events
1590 * Pass Swing keystrokes into the event queue.
1592 * @param key keystroke received
1594 public void keyTyped(final KeyEvent key
) {
1595 // Ignore typed events
1599 * Pass Swing keystrokes into the event queue.
1601 * @param key keystroke received
1603 public void keyPressed(final KeyEvent key
) {
1604 boolean alt
= false;
1605 boolean shift
= false;
1606 boolean ctrl
= false;
1608 boolean isKey
= false;
1609 if (key
.isActionKey()) {
1612 ch
= key
.getKeyChar();
1614 alt
= key
.isAltDown();
1615 ctrl
= key
.isControlDown();
1616 shift
= key
.isShiftDown();
1619 System.err.printf("Swing Key: %s\n", key);
1620 System.err.printf(" isKey: %s\n", isKey);
1621 System.err.printf(" alt: %s\n", alt);
1622 System.err.printf(" ctrl: %s\n", ctrl);
1623 System.err.printf(" shift: %s\n", shift);
1624 System.err.printf(" ch: %s\n", ch);
1627 // Special case: not return the bare modifier presses
1628 switch (key
.getKeyCode()) {
1629 case KeyEvent
.VK_ALT
:
1631 case KeyEvent
.VK_ALT_GRAPH
:
1633 case KeyEvent
.VK_CONTROL
:
1635 case KeyEvent
.VK_SHIFT
:
1637 case KeyEvent
.VK_META
:
1643 TKeypress keypress
= null;
1645 switch (key
.getKeyCode()) {
1646 case KeyEvent
.VK_F1
:
1647 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1650 case KeyEvent
.VK_F2
:
1651 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1654 case KeyEvent
.VK_F3
:
1655 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1658 case KeyEvent
.VK_F4
:
1659 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1662 case KeyEvent
.VK_F5
:
1663 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1666 case KeyEvent
.VK_F6
:
1667 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1670 case KeyEvent
.VK_F7
:
1671 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1674 case KeyEvent
.VK_F8
:
1675 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1678 case KeyEvent
.VK_F9
:
1679 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1682 case KeyEvent
.VK_F10
:
1683 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1686 case KeyEvent
.VK_F11
:
1687 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1690 case KeyEvent
.VK_F12
:
1691 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1694 case KeyEvent
.VK_HOME
:
1695 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1698 case KeyEvent
.VK_END
:
1699 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1702 case KeyEvent
.VK_PAGE_UP
:
1703 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1706 case KeyEvent
.VK_PAGE_DOWN
:
1707 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1710 case KeyEvent
.VK_INSERT
:
1711 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1714 case KeyEvent
.VK_DELETE
:
1715 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1718 case KeyEvent
.VK_RIGHT
:
1719 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1722 case KeyEvent
.VK_LEFT
:
1723 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1726 case KeyEvent
.VK_UP
:
1727 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1730 case KeyEvent
.VK_DOWN
:
1731 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1734 case KeyEvent
.VK_TAB
:
1735 // Special case: distinguish TAB vs BTAB
1737 keypress
= kbShiftTab
;
1742 case KeyEvent
.VK_ENTER
:
1743 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1746 case KeyEvent
.VK_ESCAPE
:
1747 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1750 case KeyEvent
.VK_BACK_SPACE
:
1751 keypress
= kbBackspace
;
1754 // Unsupported, ignore
1759 if (keypress
== null) {
1762 // Disambiguate ^H from Backspace.
1763 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1765 keypress
= kbBackspace
;
1767 // We are emulating Xterm here, where the backspace key
1768 // on the keyboard returns ^?.
1769 keypress
= kbBackspaceDel
;
1783 keypress
= kbShiftTab
;
1792 if (!alt
&& ctrl
&& !shift
) {
1793 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1795 // Not a special key, put it together
1796 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1800 // Save it and we are done.
1801 synchronized (eventQueue
) {
1802 eventQueue
.add(new TKeypressEvent(keypress
));
1805 if (listener
!= null) {
1806 synchronized (listener
) {
1807 listener
.notifyAll();
1812 // ------------------------------------------------------------------------
1813 // WindowListener ---------------------------------------------------------
1814 // ------------------------------------------------------------------------
1817 * Pass window events into the event queue.
1819 * @param event window event received
1821 public void windowActivated(final WindowEvent event
) {
1822 // Force a total repaint
1823 synchronized (this) {
1829 * Pass window events into the event queue.
1831 * @param event window event received
1833 public void windowClosed(final WindowEvent event
) {
1838 * Pass window events into the event queue.
1840 * @param event window event received
1842 public void windowClosing(final WindowEvent event
) {
1843 // Drop a cmAbort and walk away
1844 synchronized (eventQueue
) {
1845 eventQueue
.add(new TCommandEvent(cmAbort
));
1848 if (listener
!= null) {
1849 synchronized (listener
) {
1850 listener
.notifyAll();
1856 * Pass window events into the event queue.
1858 * @param event window event received
1860 public void windowDeactivated(final WindowEvent event
) {
1865 * Pass window events into the event queue.
1867 * @param event window event received
1869 public void windowDeiconified(final WindowEvent event
) {
1874 * Pass window events into the event queue.
1876 * @param event window event received
1878 public void windowIconified(final WindowEvent event
) {
1883 * Pass window events into the event queue.
1885 * @param event window event received
1887 public void windowOpened(final WindowEvent event
) {
1891 // ------------------------------------------------------------------------
1892 // ComponentListener ------------------------------------------------------
1893 // ------------------------------------------------------------------------
1896 * Pass component events into the event queue.
1898 * @param event component event received
1900 public void componentHidden(final ComponentEvent event
) {
1905 * Pass component events into the event queue.
1907 * @param event component event received
1909 public void componentShown(final ComponentEvent event
) {
1914 * Pass component events into the event queue.
1916 * @param event component event received
1918 public void componentMoved(final ComponentEvent event
) {
1923 * Pass component events into the event queue.
1925 * @param event component event received
1927 public void componentResized(final ComponentEvent event
) {
1928 if (gotFontDimensions
== false) {
1929 // We are still waiting to get font information. Don't pass a
1931 // System.err.println("size " + swing.getComponent().getSize());
1935 // Drop a new TResizeEvent into the queue
1936 sessionInfo
.queryWindowSize();
1937 synchronized (eventQueue
) {
1938 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1939 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1940 eventQueue
.add(windowResize
);
1943 System.err.println("Add resize event: " + windowResize.getWidth() +
1944 " x " + windowResize.getHeight());
1947 if (listener
!= null) {
1948 synchronized (listener
) {
1949 listener
.notifyAll();
1954 // ------------------------------------------------------------------------
1955 // MouseMotionListener ----------------------------------------------------
1956 // ------------------------------------------------------------------------
1959 * Pass mouse events into the event queue.
1961 * @param mouse mouse event received
1963 public void mouseDragged(final MouseEvent mouse
) {
1964 int modifiers
= mouse
.getModifiersEx();
1965 boolean eventMouse1
= false;
1966 boolean eventMouse2
= false;
1967 boolean eventMouse3
= false;
1968 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1971 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1974 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1977 mouse1
= eventMouse1
;
1978 mouse2
= eventMouse2
;
1979 mouse3
= eventMouse3
;
1980 int x
= textColumn(mouse
.getX());
1981 int y
= textRow(mouse
.getY());
1983 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1984 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1986 synchronized (eventQueue
) {
1987 eventQueue
.add(mouseEvent
);
1990 if (listener
!= null) {
1991 synchronized (listener
) {
1992 listener
.notifyAll();
1998 * Pass mouse events into the event queue.
2000 * @param mouse mouse event received
2002 public void mouseMoved(final MouseEvent mouse
) {
2003 int x
= textColumn(mouse
.getX());
2004 int y
= textRow(mouse
.getY());
2005 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2006 // Bail out, we've moved some pixels but not a whole text cell.
2012 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2013 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2015 synchronized (eventQueue
) {
2016 eventQueue
.add(mouseEvent
);
2019 if (listener
!= null) {
2020 synchronized (listener
) {
2021 listener
.notifyAll();
2026 // ------------------------------------------------------------------------
2027 // MouseListener ----------------------------------------------------------
2028 // ------------------------------------------------------------------------
2031 * Pass mouse events into the event queue.
2033 * @param mouse mouse event received
2035 public void mouseClicked(final MouseEvent mouse
) {
2040 * Pass mouse events into the event queue.
2042 * @param mouse mouse event received
2044 public void mouseEntered(final MouseEvent mouse
) {
2049 * Pass mouse events into the event queue.
2051 * @param mouse mouse event received
2053 public void mouseExited(final MouseEvent mouse
) {
2058 * Pass mouse events into the event queue.
2060 * @param mouse mouse event received
2062 public void mousePressed(final MouseEvent mouse
) {
2063 int modifiers
= mouse
.getModifiersEx();
2064 boolean eventMouse1
= false;
2065 boolean eventMouse2
= false;
2066 boolean eventMouse3
= false;
2067 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2070 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2073 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2076 mouse1
= eventMouse1
;
2077 mouse2
= eventMouse2
;
2078 mouse3
= eventMouse3
;
2079 int x
= textColumn(mouse
.getX());
2080 int y
= textRow(mouse
.getY());
2082 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2083 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2085 synchronized (eventQueue
) {
2086 eventQueue
.add(mouseEvent
);
2089 if (listener
!= null) {
2090 synchronized (listener
) {
2091 listener
.notifyAll();
2097 * Pass mouse events into the event queue.
2099 * @param mouse mouse event received
2101 public void mouseReleased(final MouseEvent mouse
) {
2102 int modifiers
= mouse
.getModifiersEx();
2103 boolean eventMouse1
= false;
2104 boolean eventMouse2
= false;
2105 boolean eventMouse3
= false;
2106 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2109 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2112 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2127 int x
= textColumn(mouse
.getX());
2128 int y
= textRow(mouse
.getY());
2130 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2131 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
2133 synchronized (eventQueue
) {
2134 eventQueue
.add(mouseEvent
);
2137 if (listener
!= null) {
2138 synchronized (listener
) {
2139 listener
.notifyAll();
2144 // ------------------------------------------------------------------------
2145 // MouseWheelListener -----------------------------------------------------
2146 // ------------------------------------------------------------------------
2149 * Pass mouse events into the event queue.
2151 * @param mouse mouse event received
2153 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2154 int modifiers
= mouse
.getModifiersEx();
2155 boolean eventMouse1
= false;
2156 boolean eventMouse2
= false;
2157 boolean eventMouse3
= false;
2158 boolean mouseWheelUp
= false;
2159 boolean mouseWheelDown
= false;
2160 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2163 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2166 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2169 mouse1
= eventMouse1
;
2170 mouse2
= eventMouse2
;
2171 mouse3
= eventMouse3
;
2172 int x
= textColumn(mouse
.getX());
2173 int y
= textRow(mouse
.getY());
2174 if (mouse
.getWheelRotation() > 0) {
2175 mouseWheelDown
= true;
2177 if (mouse
.getWheelRotation() < 0) {
2178 mouseWheelUp
= true;
2181 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2182 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2184 synchronized (eventQueue
) {
2185 eventQueue
.add(mouseEvent
);
2188 if (listener
!= null) {
2189 synchronized (listener
) {
2190 listener
.notifyAll();