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 the physical screen to match the logical screen dimensions.
1102 public void resizeToScreen() {
1103 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
1107 * Draw one cell's image to the screen.
1109 * @param gr the Swing Graphics context
1110 * @param cell the Cell to draw
1111 * @param xPixel the x-coordinate to render to. 0 means the
1112 * left-most pixel column.
1113 * @param yPixel the y-coordinate to render to. 0 means the top-most
1116 private void drawImage(final Graphics gr
, final Cell cell
,
1117 final int xPixel
, final int yPixel
) {
1120 System.err.println("drawImage(): " + xPixel + " " + yPixel +
1124 // Draw the background rectangle, then the foreground character.
1125 assert (cell
.isImage());
1126 gr
.setColor(cell
.getBackground());
1127 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1129 BufferedImage image
= cell
.getImage();
1130 if (image
!= null) {
1131 if (swing
.getFrame() != null) {
1132 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1134 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1141 * Draw one glyph to the screen.
1143 * @param gr the Swing Graphics context
1144 * @param cell the Cell to draw
1145 * @param xPixel the x-coordinate to render to. 0 means the
1146 * left-most pixel column.
1147 * @param yPixel the y-coordinate to render to. 0 means the top-most
1150 private void drawGlyph(final Graphics gr
, final Cell cell
,
1151 final int xPixel
, final int yPixel
) {
1154 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
1158 BufferedImage image
= null;
1159 if (cell
.isBlink() && !cursorBlinkVisible
) {
1160 image
= glyphCacheBlink
.get(cell
);
1162 image
= glyphCache
.get(cell
);
1164 if (image
!= null) {
1165 if (swing
.getFrame() != null) {
1166 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1168 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1173 // Generate glyph and draw it.
1174 Graphics2D gr2
= null;
1177 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1178 image
= new BufferedImage(textWidth
, textHeight
,
1179 BufferedImage
.TYPE_INT_ARGB
);
1180 gr2
= image
.createGraphics();
1181 gr2
.setFont(swing
.getFont());
1185 gr2
= (Graphics2D
) gr
;
1188 Cell cellColor
= new Cell();
1189 cellColor
.setTo(cell
);
1191 // Check for reverse
1192 if (cell
.isReverse()) {
1193 cellColor
.setForeColor(cell
.getBackColor());
1194 cellColor
.setBackColor(cell
.getForeColor());
1197 // Draw the background rectangle, then the foreground character.
1198 gr2
.setColor(attrToBackgroundColor(cellColor
));
1199 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1201 // Handle blink and underline
1203 || (cell
.isBlink() && cursorBlinkVisible
)
1205 gr2
.setColor(attrToForegroundColor(cellColor
));
1206 char [] chars
= new char[1];
1207 chars
[0] = cell
.getChar();
1208 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1209 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1211 if (cell
.isUnderline()) {
1212 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1216 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1219 // We need a new key that will not be mutated by
1221 Cell key
= new Cell();
1223 if (cell
.isBlink() && !cursorBlinkVisible
) {
1224 glyphCacheBlink
.put(key
, image
);
1226 glyphCache
.put(key
, image
);
1229 if (swing
.getFrame() != null) {
1230 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1232 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1239 * Check if the cursor is visible, and if so draw it.
1241 * @param gr the Swing Graphics context
1243 private void drawCursor(final Graphics gr
) {
1248 && (cursorY
<= height
- 1)
1249 && (cursorX
<= width
- 1)
1250 && cursorBlinkVisible
1252 int xPixel
= cursorX
* textWidth
+ left
;
1253 int yPixel
= cursorY
* textHeight
+ top
;
1254 Cell lCell
= logical
[cursorX
][cursorY
];
1255 gr
.setColor(attrToForegroundColor(lCell
));
1256 switch (cursorStyle
) {
1260 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1263 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1266 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1273 * Reset the blink timer.
1275 private void resetBlinkTimer() {
1276 lastBlinkTime
= System
.currentTimeMillis();
1277 cursorBlinkVisible
= true;
1281 * Paint redraws the whole screen.
1283 * @param gr the Swing Graphics context
1285 public void paint(final Graphics gr
) {
1287 if (gotFontDimensions
== false) {
1288 // Lazy-load the text width/height
1289 getFontDimensions(gr
);
1291 System.err.println("textWidth " + textWidth +
1292 " textHeight " + textHeight);
1293 System.err.println("FONT: " + swing.getFont() + " font " + font);
1297 if ((swing
.getFrame() != null)
1298 && (swing
.getBufferStrategy() != null)
1299 && (SwingUtilities
.isEventDispatchThread())
1301 // System.err.println("paint(), skip first paint on swing thread");
1306 int xCellMax
= width
;
1308 int yCellMax
= height
;
1310 Rectangle bounds
= gr
.getClipBounds();
1311 if (bounds
!= null) {
1312 // Only update what is in the bounds
1313 xCellMin
= textColumn(bounds
.x
);
1314 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1315 if (xCellMax
> width
) {
1318 if (xCellMin
>= xCellMax
) {
1319 xCellMin
= xCellMax
- 2;
1324 yCellMin
= textRow(bounds
.y
);
1325 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1326 if (yCellMax
> height
) {
1329 if (yCellMin
>= yCellMax
) {
1330 yCellMin
= yCellMax
- 2;
1336 // We need a total repaint
1337 reallyCleared
= true;
1340 // Prevent updates to the screen's data from the TApplication
1342 synchronized (this) {
1345 System.err.printf("bounds %s X %d %d Y %d %d\n",
1346 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1349 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1350 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1352 int xPixel
= x
* textWidth
+ left
;
1353 int yPixel
= y
* textHeight
+ top
;
1355 Cell lCell
= logical
[x
][y
];
1356 Cell pCell
= physical
[x
][y
];
1358 if (!lCell
.equals(pCell
)
1361 || (swing
.getFrame() == null)) {
1363 if (lCell
.isImage()) {
1364 drawImage(gr
, lCell
, xPixel
, yPixel
);
1366 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1369 // Physical is always updated
1370 physical
[x
][y
].setTo(lCell
);
1376 reallyCleared
= false;
1377 } // synchronized (this)
1381 * Restore terminal to normal state.
1383 public void shutdown() {
1388 * Push the logical screen to the physical device.
1390 private void drawToSwing() {
1393 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1394 reallyCleared, dirty);
1397 // If reallyCleared is set, we have to draw everything.
1398 if ((swing
.getFrame() != null)
1399 && (swing
.getBufferStrategy() != null)
1400 && (reallyCleared
== true)
1402 // Triple-buffering: we have to redraw everything on this thread.
1403 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1406 swing
.getBufferStrategy().show();
1407 Toolkit
.getDefaultToolkit().sync();
1409 } else if (((swing
.getFrame() != null)
1410 && (swing
.getBufferStrategy() == null))
1411 || (reallyCleared
== true)
1413 // Repaint everything on the Swing thread.
1414 // System.err.println("REPAINT ALL");
1419 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1420 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1422 synchronized (this) {
1423 for (int y
= 0; y
< height
; y
++) {
1424 for (int x
= 0; x
< width
; x
++) {
1425 Cell lCell
= logical
[x
][y
];
1426 Cell pCell
= physical
[x
][y
];
1428 int xPixel
= x
* textWidth
+ left
;
1429 int yPixel
= y
* textHeight
+ top
;
1431 if (!lCell
.equals(pCell
)
1435 || (lCell
.isBlink())
1437 if (lCell
.isImage()) {
1438 drawImage(gr
, lCell
, xPixel
, yPixel
);
1440 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1442 physical
[x
][y
].setTo(lCell
);
1447 } // synchronized (this)
1450 swing
.getBufferStrategy().show();
1451 Toolkit
.getDefaultToolkit().sync();
1455 // Swing thread version: request a repaint, but limit it to the area
1456 // that has changed.
1458 // Find the minimum-size damaged region.
1459 int xMin
= swing
.getWidth();
1461 int yMin
= swing
.getHeight();
1464 synchronized (this) {
1465 for (int y
= 0; y
< height
; y
++) {
1466 for (int x
= 0; x
< width
; x
++) {
1467 Cell lCell
= logical
[x
][y
];
1468 Cell pCell
= physical
[x
][y
];
1470 int xPixel
= x
* textWidth
+ left
;
1471 int yPixel
= y
* textHeight
+ top
;
1473 if (!lCell
.equals(pCell
)
1479 if (xPixel
< xMin
) {
1482 if (xPixel
+ textWidth
> xMax
) {
1483 xMax
= xPixel
+ textWidth
;
1485 if (yPixel
< yMin
) {
1488 if (yPixel
+ textHeight
> yMax
) {
1489 yMax
= yPixel
+ textHeight
;
1495 if (xMin
+ textWidth
>= xMax
) {
1498 if (yMin
+ textHeight
>= yMax
) {
1502 // Repaint the desired area
1504 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1508 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1509 // This path should never be taken, but is left here for
1511 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1512 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1517 swing
.getBufferStrategy().show();
1518 Toolkit
.getDefaultToolkit().sync();
1520 // Repaint on the Swing thread.
1521 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1526 * Convert pixel column position to text cell column position.
1528 * @param x pixel column position
1529 * @return text cell column position
1531 public int textColumn(final int x
) {
1532 int column
= ((x
- left
) / textWidth
);
1536 if (column
> width
- 1) {
1543 * Convert pixel row position to text cell row position.
1545 * @param y pixel row position
1546 * @return text cell row position
1548 public int textRow(final int y
) {
1549 int row
= ((y
- top
) / textHeight
);
1553 if (row
> height
- 1) {
1560 * Getter for sessionInfo.
1562 * @return the SessionInfo
1564 public SessionInfo
getSessionInfo() {
1569 * Getter for the underlying Swing component.
1571 * @return the SwingComponent
1573 public SwingComponent
getSwingComponent() {
1577 // ------------------------------------------------------------------------
1578 // KeyListener ------------------------------------------------------------
1579 // ------------------------------------------------------------------------
1582 * Pass Swing keystrokes into the event queue.
1584 * @param key keystroke received
1586 public void keyReleased(final KeyEvent key
) {
1587 // Ignore release events
1591 * Pass Swing keystrokes into the event queue.
1593 * @param key keystroke received
1595 public void keyTyped(final KeyEvent key
) {
1596 // Ignore typed events
1600 * Pass Swing keystrokes into the event queue.
1602 * @param key keystroke received
1604 public void keyPressed(final KeyEvent key
) {
1605 boolean alt
= false;
1606 boolean shift
= false;
1607 boolean ctrl
= false;
1609 boolean isKey
= false;
1610 if (key
.isActionKey()) {
1613 ch
= key
.getKeyChar();
1615 alt
= key
.isAltDown();
1616 ctrl
= key
.isControlDown();
1617 shift
= key
.isShiftDown();
1620 System.err.printf("Swing Key: %s\n", key);
1621 System.err.printf(" isKey: %s\n", isKey);
1622 System.err.printf(" alt: %s\n", alt);
1623 System.err.printf(" ctrl: %s\n", ctrl);
1624 System.err.printf(" shift: %s\n", shift);
1625 System.err.printf(" ch: %s\n", ch);
1628 // Special case: not return the bare modifier presses
1629 switch (key
.getKeyCode()) {
1630 case KeyEvent
.VK_ALT
:
1632 case KeyEvent
.VK_ALT_GRAPH
:
1634 case KeyEvent
.VK_CONTROL
:
1636 case KeyEvent
.VK_SHIFT
:
1638 case KeyEvent
.VK_META
:
1644 TKeypress keypress
= null;
1646 switch (key
.getKeyCode()) {
1647 case KeyEvent
.VK_F1
:
1648 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1651 case KeyEvent
.VK_F2
:
1652 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1655 case KeyEvent
.VK_F3
:
1656 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1659 case KeyEvent
.VK_F4
:
1660 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1663 case KeyEvent
.VK_F5
:
1664 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1667 case KeyEvent
.VK_F6
:
1668 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1671 case KeyEvent
.VK_F7
:
1672 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1675 case KeyEvent
.VK_F8
:
1676 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1679 case KeyEvent
.VK_F9
:
1680 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1683 case KeyEvent
.VK_F10
:
1684 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1687 case KeyEvent
.VK_F11
:
1688 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1691 case KeyEvent
.VK_F12
:
1692 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1695 case KeyEvent
.VK_HOME
:
1696 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1699 case KeyEvent
.VK_END
:
1700 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1703 case KeyEvent
.VK_PAGE_UP
:
1704 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1707 case KeyEvent
.VK_PAGE_DOWN
:
1708 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1711 case KeyEvent
.VK_INSERT
:
1712 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1715 case KeyEvent
.VK_DELETE
:
1716 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1719 case KeyEvent
.VK_RIGHT
:
1720 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1723 case KeyEvent
.VK_LEFT
:
1724 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1727 case KeyEvent
.VK_UP
:
1728 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1731 case KeyEvent
.VK_DOWN
:
1732 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1735 case KeyEvent
.VK_TAB
:
1736 // Special case: distinguish TAB vs BTAB
1738 keypress
= kbShiftTab
;
1743 case KeyEvent
.VK_ENTER
:
1744 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1747 case KeyEvent
.VK_ESCAPE
:
1748 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1751 case KeyEvent
.VK_BACK_SPACE
:
1752 keypress
= kbBackspace
;
1755 // Unsupported, ignore
1760 if (keypress
== null) {
1763 // Disambiguate ^H from Backspace.
1764 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1766 keypress
= kbBackspace
;
1768 // We are emulating Xterm here, where the backspace key
1769 // on the keyboard returns ^?.
1770 keypress
= kbBackspaceDel
;
1784 keypress
= kbShiftTab
;
1793 if (!alt
&& ctrl
&& !shift
) {
1794 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1796 // Not a special key, put it together
1797 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1801 // Save it and we are done.
1802 synchronized (eventQueue
) {
1803 eventQueue
.add(new TKeypressEvent(keypress
));
1806 if (listener
!= null) {
1807 synchronized (listener
) {
1808 listener
.notifyAll();
1813 // ------------------------------------------------------------------------
1814 // WindowListener ---------------------------------------------------------
1815 // ------------------------------------------------------------------------
1818 * Pass window events into the event queue.
1820 * @param event window event received
1822 public void windowActivated(final WindowEvent event
) {
1823 // Force a total repaint
1824 synchronized (this) {
1830 * Pass window events into the event queue.
1832 * @param event window event received
1834 public void windowClosed(final WindowEvent event
) {
1839 * Pass window events into the event queue.
1841 * @param event window event received
1843 public void windowClosing(final WindowEvent event
) {
1844 // Drop a cmAbort and walk away
1845 synchronized (eventQueue
) {
1846 eventQueue
.add(new TCommandEvent(cmAbort
));
1849 if (listener
!= null) {
1850 synchronized (listener
) {
1851 listener
.notifyAll();
1857 * Pass window events into the event queue.
1859 * @param event window event received
1861 public void windowDeactivated(final WindowEvent event
) {
1866 * Pass window events into the event queue.
1868 * @param event window event received
1870 public void windowDeiconified(final WindowEvent event
) {
1875 * Pass window events into the event queue.
1877 * @param event window event received
1879 public void windowIconified(final WindowEvent event
) {
1884 * Pass window events into the event queue.
1886 * @param event window event received
1888 public void windowOpened(final WindowEvent event
) {
1892 // ------------------------------------------------------------------------
1893 // ComponentListener ------------------------------------------------------
1894 // ------------------------------------------------------------------------
1897 * Pass component events into the event queue.
1899 * @param event component event received
1901 public void componentHidden(final ComponentEvent event
) {
1906 * Pass component events into the event queue.
1908 * @param event component event received
1910 public void componentShown(final ComponentEvent event
) {
1915 * Pass component events into the event queue.
1917 * @param event component event received
1919 public void componentMoved(final ComponentEvent event
) {
1924 * Pass component events into the event queue.
1926 * @param event component event received
1928 public void componentResized(final ComponentEvent event
) {
1929 if (gotFontDimensions
== false) {
1930 // We are still waiting to get font information. Don't pass a
1932 // System.err.println("size " + swing.getComponent().getSize());
1936 // Drop a new TResizeEvent into the queue
1937 sessionInfo
.queryWindowSize();
1938 synchronized (eventQueue
) {
1939 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1940 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1941 eventQueue
.add(windowResize
);
1944 System.err.println("Add resize event: " + windowResize.getWidth() +
1945 " x " + windowResize.getHeight());
1948 if (listener
!= null) {
1949 synchronized (listener
) {
1950 listener
.notifyAll();
1955 // ------------------------------------------------------------------------
1956 // MouseMotionListener ----------------------------------------------------
1957 // ------------------------------------------------------------------------
1960 * Pass mouse events into the event queue.
1962 * @param mouse mouse event received
1964 public void mouseDragged(final MouseEvent mouse
) {
1965 int modifiers
= mouse
.getModifiersEx();
1966 boolean eventMouse1
= false;
1967 boolean eventMouse2
= false;
1968 boolean eventMouse3
= false;
1969 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1972 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1975 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1978 mouse1
= eventMouse1
;
1979 mouse2
= eventMouse2
;
1980 mouse3
= eventMouse3
;
1981 int x
= textColumn(mouse
.getX());
1982 int y
= textRow(mouse
.getY());
1984 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1985 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1987 synchronized (eventQueue
) {
1988 eventQueue
.add(mouseEvent
);
1991 if (listener
!= null) {
1992 synchronized (listener
) {
1993 listener
.notifyAll();
1999 * Pass mouse events into the event queue.
2001 * @param mouse mouse event received
2003 public void mouseMoved(final MouseEvent mouse
) {
2004 int x
= textColumn(mouse
.getX());
2005 int y
= textRow(mouse
.getY());
2006 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
2007 // Bail out, we've moved some pixels but not a whole text cell.
2013 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
2014 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2016 synchronized (eventQueue
) {
2017 eventQueue
.add(mouseEvent
);
2020 if (listener
!= null) {
2021 synchronized (listener
) {
2022 listener
.notifyAll();
2027 // ------------------------------------------------------------------------
2028 // MouseListener ----------------------------------------------------------
2029 // ------------------------------------------------------------------------
2032 * Pass mouse events into the event queue.
2034 * @param mouse mouse event received
2036 public void mouseClicked(final MouseEvent mouse
) {
2041 * Pass mouse events into the event queue.
2043 * @param mouse mouse event received
2045 public void mouseEntered(final MouseEvent mouse
) {
2050 * Pass mouse events into the event queue.
2052 * @param mouse mouse event received
2054 public void mouseExited(final MouseEvent mouse
) {
2059 * Pass mouse events into the event queue.
2061 * @param mouse mouse event received
2063 public void mousePressed(final MouseEvent mouse
) {
2064 int modifiers
= mouse
.getModifiersEx();
2065 boolean eventMouse1
= false;
2066 boolean eventMouse2
= false;
2067 boolean eventMouse3
= false;
2068 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2071 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2074 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2077 mouse1
= eventMouse1
;
2078 mouse2
= eventMouse2
;
2079 mouse3
= eventMouse3
;
2080 int x
= textColumn(mouse
.getX());
2081 int y
= textRow(mouse
.getY());
2083 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2084 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
2086 synchronized (eventQueue
) {
2087 eventQueue
.add(mouseEvent
);
2090 if (listener
!= null) {
2091 synchronized (listener
) {
2092 listener
.notifyAll();
2098 * Pass mouse events into the event queue.
2100 * @param mouse mouse event received
2102 public void mouseReleased(final MouseEvent mouse
) {
2103 int modifiers
= mouse
.getModifiersEx();
2104 boolean eventMouse1
= false;
2105 boolean eventMouse2
= false;
2106 boolean eventMouse3
= false;
2107 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2110 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2113 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2128 int x
= textColumn(mouse
.getX());
2129 int y
= textRow(mouse
.getY());
2131 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
2132 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
2134 synchronized (eventQueue
) {
2135 eventQueue
.add(mouseEvent
);
2138 if (listener
!= null) {
2139 synchronized (listener
) {
2140 listener
.notifyAll();
2145 // ------------------------------------------------------------------------
2146 // MouseWheelListener -----------------------------------------------------
2147 // ------------------------------------------------------------------------
2150 * Pass mouse events into the event queue.
2152 * @param mouse mouse event received
2154 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
2155 int modifiers
= mouse
.getModifiersEx();
2156 boolean eventMouse1
= false;
2157 boolean eventMouse2
= false;
2158 boolean eventMouse3
= false;
2159 boolean mouseWheelUp
= false;
2160 boolean mouseWheelDown
= false;
2161 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
2164 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
2167 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
2170 mouse1
= eventMouse1
;
2171 mouse2
= eventMouse2
;
2172 mouse3
= eventMouse3
;
2173 int x
= textColumn(mouse
.getX());
2174 int y
= textRow(mouse
.getY());
2175 if (mouse
.getWheelRotation() > 0) {
2176 mouseWheelDown
= true;
2178 if (mouse
.getWheelRotation() < 0) {
2179 mouseWheelUp
= true;
2182 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2183 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2185 synchronized (eventQueue
) {
2186 eventQueue
.add(mouseEvent
);
2189 if (listener
!= null) {
2190 synchronized (listener
) {
2191 listener
.notifyAll();