2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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 final 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 getting Terminus.
170 private boolean gotTerminus
= false;
173 * If true, we were successful at getting the font dimensions.
175 private boolean gotFontDimensions
= false;
178 * The currently selected font.
180 private Font font
= null;
183 * The currently selected font size in points.
185 private int fontSize
= 16;
188 * Width of a character cell in pixels.
190 private int textWidth
= 1;
193 * Height of a character cell in pixels.
195 private int textHeight
= 1;
198 * Descent of a character cell in pixels.
200 private int maxDescent
= 0;
203 * System-dependent Y adjustment for text in the character cell.
205 private int textAdjustY
= 0;
208 * System-dependent X adjustment for text in the character cell.
210 private int textAdjustX
= 0;
213 * Top pixel absolute location.
215 private int top
= 30;
218 * Left pixel absolute location.
220 private int left
= 30;
223 * The cursor style to draw.
225 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
228 * The number of millis to wait before switching the blink from visible
229 * to invisible. Set to 0 or negative to disable blinking.
231 private long blinkMillis
= 500;
234 * If true, the cursor should be visible right now based on the blink
237 private boolean cursorBlinkVisible
= true;
240 * The time that the blink last flipped from visible to invisible or
241 * from invisible to visible.
243 private long lastBlinkTime
= 0;
246 * The session information.
248 private SwingSessionInfo sessionInfo
;
251 * The listening object that run() wakes up on new input.
253 private Object listener
;
256 * The event queue, filled up by a thread reading on input.
258 private List
<TInputEvent
> eventQueue
;
261 * The last reported mouse X position.
263 private int oldMouseX
= -1;
266 * The last reported mouse Y position.
268 private int oldMouseY
= -1;
271 * true if mouse1 was down. Used to report mouse1 on the release event.
273 private boolean mouse1
= false;
276 * true if mouse2 was down. Used to report mouse2 on the release event.
278 private boolean mouse2
= false;
281 * true if mouse3 was down. Used to report mouse3 on the release event.
283 private boolean mouse3
= false;
285 // ------------------------------------------------------------------------
286 // Constructors -----------------------------------------------------------
287 // ------------------------------------------------------------------------
290 * Public constructor creates a new JFrame to render to.
292 * @param windowWidth the number of text columns to start with
293 * @param windowHeight the number of text rows to start with
294 * @param fontSize the size in points. Good values to pick are: 16, 20,
296 * @param listener the object this backend needs to wake up when new
299 public SwingTerminal(final int windowWidth
, final int windowHeight
,
300 final int fontSize
, final Object listener
) {
302 this.fontSize
= fontSize
;
306 // Figure out my cursor style.
307 String cursorStyleString
= System
.getProperty(
308 "jexer.Swing.cursorStyle", "underline").toLowerCase();
309 if (cursorStyleString
.equals("underline")) {
310 cursorStyle
= CursorStyle
.UNDERLINE
;
311 } else if (cursorStyleString
.equals("outline")) {
312 cursorStyle
= CursorStyle
.OUTLINE
;
313 } else if (cursorStyleString
.equals("block")) {
314 cursorStyle
= CursorStyle
.BLOCK
;
317 // Pull the system property for triple buffering.
318 if (System
.getProperty("jexer.Swing.tripleBuffer") != null) {
319 if (System
.getProperty("jexer.Swing.tripleBuffer").equals("true")) {
320 SwingComponent
.tripleBuffer
= true;
322 SwingComponent
.tripleBuffer
= false;
327 SwingUtilities
.invokeAndWait(new Runnable() {
330 JFrame frame
= new JFrame() {
333 * Serializable version.
335 private static final long serialVersionUID
= 1;
338 * The code that performs the actual drawing.
340 public SwingTerminal screen
= null;
343 * Anonymous class initializer saves the screen
344 * reference, so that paint() and the like call out
348 this.screen
= SwingTerminal
.this;
352 * Update redraws the whole screen.
354 * @param gr the Swing Graphics context
357 public void update(final Graphics gr
) {
358 // The default update clears the area. Don't do
359 // that, instead just paint it directly.
364 * Paint redraws the whole screen.
366 * @param gr the Swing Graphics context
369 public void paint(final Graphics gr
) {
370 if (screen
!= null) {
377 ClassLoader loader
= Thread
.currentThread().
378 getContextClassLoader();
379 frame
.setIconImage((new ImageIcon(loader
.
380 getResource(ICONFILE
))).getImage());
382 // Get the Swing component
383 SwingTerminal
.this.swing
= new SwingComponent(frame
);
385 // Hang onto top and left for drawing.
386 Insets insets
= SwingTerminal
.this.swing
.getInsets();
387 SwingTerminal
.this.left
= insets
.left
;
388 SwingTerminal
.this.top
= insets
.top
;
390 // Load the font so that we can set sessionInfo.
393 // Get the default cols x rows and set component size
395 SwingTerminal
.this.sessionInfo
=
396 new SwingSessionInfo(SwingTerminal
.this.swing
,
397 SwingTerminal
.this.textWidth
,
398 SwingTerminal
.this.textHeight
,
399 windowWidth
, windowHeight
);
401 SwingTerminal
.this.setDimensions(sessionInfo
.getWindowWidth(),
402 sessionInfo
.getWindowHeight());
404 SwingTerminal
.this.resizeToScreen();
405 SwingTerminal
.this.swing
.setVisible(true);
408 } catch (Exception e
) {
412 this.listener
= listener
;
416 eventQueue
= new LinkedList
<TInputEvent
>();
418 // Add listeners to Swing.
419 swing
.addKeyListener(this);
420 swing
.addWindowListener(this);
421 swing
.addComponentListener(this);
422 swing
.addMouseListener(this);
423 swing
.addMouseMotionListener(this);
424 swing
.addMouseWheelListener(this);
428 * Public constructor renders to an existing JComponent.
430 * @param component the Swing component to render to
431 * @param windowWidth the number of text columns to start with
432 * @param windowHeight the number of text rows to start with
433 * @param fontSize the size in points. Good values to pick are: 16, 20,
435 * @param listener the object this backend needs to wake up when new
438 public SwingTerminal(final JComponent component
, final int windowWidth
,
439 final int windowHeight
, final int fontSize
, final Object listener
) {
441 this.fontSize
= fontSize
;
445 // Figure out my cursor style.
446 String cursorStyleString
= System
.getProperty(
447 "jexer.Swing.cursorStyle", "underline").toLowerCase();
448 if (cursorStyleString
.equals("underline")) {
449 cursorStyle
= CursorStyle
.UNDERLINE
;
450 } else if (cursorStyleString
.equals("outline")) {
451 cursorStyle
= CursorStyle
.OUTLINE
;
452 } else if (cursorStyleString
.equals("block")) {
453 cursorStyle
= CursorStyle
.BLOCK
;
457 SwingUtilities
.invokeAndWait(new Runnable() {
460 JComponent newComponent
= new JComponent() {
463 * Serializable version.
465 private static final long serialVersionUID
= 1;
468 * The code that performs the actual drawing.
470 public SwingTerminal screen
= null;
473 * Anonymous class initializer saves the screen
474 * reference, so that paint() and the like call out
478 this.screen
= SwingTerminal
.this;
482 * Update redraws the whole screen.
484 * @param gr the Swing Graphics context
487 public void update(final Graphics gr
) {
488 // The default update clears the area. Don't do
489 // that, instead just paint it directly.
494 * Paint redraws the whole screen.
496 * @param gr the Swing Graphics context
499 public void paint(final Graphics gr
) {
500 if (screen
!= null) {
505 component
.setLayout(new BorderLayout());
506 component
.add(newComponent
);
508 // Allow key events to be received
509 component
.setFocusable(true);
511 // Get the Swing component
512 SwingTerminal
.this.swing
= new SwingComponent(component
);
514 // Hang onto top and left for drawing.
515 Insets insets
= SwingTerminal
.this.swing
.getInsets();
516 SwingTerminal
.this.left
= insets
.left
;
517 SwingTerminal
.this.top
= insets
.top
;
519 // Load the font so that we can set sessionInfo.
522 // Get the default cols x rows and set component size
524 SwingTerminal
.this.sessionInfo
=
525 new SwingSessionInfo(SwingTerminal
.this.swing
,
526 SwingTerminal
.this.textWidth
,
527 SwingTerminal
.this.textHeight
);
530 } catch (Exception e
) {
534 this.listener
= listener
;
538 eventQueue
= new LinkedList
<TInputEvent
>();
540 // Add listeners to Swing.
541 swing
.addKeyListener(this);
542 swing
.addWindowListener(this);
543 swing
.addComponentListener(this);
544 swing
.addMouseListener(this);
545 swing
.addMouseMotionListener(this);
546 swing
.addMouseWheelListener(this);
549 // ------------------------------------------------------------------------
550 // LogicalScreen ----------------------------------------------------------
551 // ------------------------------------------------------------------------
554 * Set the window title.
556 * @param title the new title
559 public void setTitle(final String title
) {
560 swing
.setTitle(title
);
564 * Push the logical screen to the physical device.
567 public void flushPhysical() {
568 // See if it is time to flip the blink time.
569 long nowTime
= System
.currentTimeMillis();
570 if (nowTime
>= blinkMillis
+ lastBlinkTime
) {
571 lastBlinkTime
= nowTime
;
572 cursorBlinkVisible
= !cursorBlinkVisible
;
573 // System.err.println("New lastBlinkTime: " + lastBlinkTime);
576 if ((swing
.getFrame() != null)
577 && (swing
.getBufferStrategy() != null)
582 } while (swing
.getBufferStrategy().contentsRestored());
584 swing
.getBufferStrategy().show();
585 Toolkit
.getDefaultToolkit().sync();
586 } while (swing
.getBufferStrategy().contentsLost());
589 // Non-triple-buffered, call drawToSwing() once
594 // ------------------------------------------------------------------------
595 // TerminalReader ---------------------------------------------------------
596 // ------------------------------------------------------------------------
599 * Check if there are events in the queue.
601 * @return if true, getEvents() has something to return to the backend
603 public boolean hasEvents() {
604 synchronized (eventQueue
) {
605 return (eventQueue
.size() > 0);
610 * Return any events in the IO queue.
612 * @param queue list to append new events to
614 public void getEvents(final List
<TInputEvent
> queue
) {
615 synchronized (eventQueue
) {
616 if (eventQueue
.size() > 0) {
617 synchronized (queue
) {
618 queue
.addAll(eventQueue
);
626 * Restore terminal to normal state.
628 public void closeTerminal() {
633 * Set listener to a different Object.
635 * @param listener the new listening object that run() wakes up on new
638 public void setListener(final Object listener
) {
639 this.listener
= listener
;
642 // ------------------------------------------------------------------------
643 // SwingTerminal ----------------------------------------------------------
644 // ------------------------------------------------------------------------
647 * Setup Swing colors to match DOS color palette.
649 private static void setDOSColors() {
653 MYBLACK
= new Color(0x00, 0x00, 0x00);
654 MYRED
= new Color(0xa8, 0x00, 0x00);
655 MYGREEN
= new Color(0x00, 0xa8, 0x00);
656 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
657 MYBLUE
= new Color(0x00, 0x00, 0xa8);
658 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
659 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
660 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
661 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
662 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
663 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
664 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
665 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
666 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
667 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
668 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
674 * Get the number of millis to wait before switching the blink from
675 * visible to invisible.
677 * @return the number of milli to wait before switching the blink from
678 * visible to invisible
680 public long getBlinkMillis() {
685 * Get the font size in points.
687 * @return font size in points
689 public int getFontSize() {
694 * Set the font size in points.
696 * @param fontSize font size in points
698 public void setFontSize(final int fontSize
) {
699 this.fontSize
= fontSize
;
700 Font newFont
= font
.deriveFont((float) fontSize
);
705 * Set to a new font, and resize the screen to match its dimensions.
707 * @param font the new font
709 public void setFont(final Font font
) {
713 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
714 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
719 * Set the font to Terminus, the best all-around font for both CP437 and
722 public void getDefaultFont() {
724 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
725 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
726 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
727 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
730 } catch (Exception e
) {
732 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
739 * Convert a CellAttributes foreground color to an Swing Color.
741 * @param attr the text attributes
742 * @return the Swing Color
744 private Color
attrToForegroundColor(final CellAttributes attr
) {
746 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
748 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
750 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
752 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
754 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
755 return MYBOLD_YELLOW
;
756 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
758 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
759 return MYBOLD_MAGENTA
;
760 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
764 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
766 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
768 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
770 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
772 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
774 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
776 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
778 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
782 throw new IllegalArgumentException("Invalid color: " +
783 attr
.getForeColor().getValue());
787 * Convert a CellAttributes background color to an Swing Color.
789 * @param attr the text attributes
790 * @return the Swing Color
792 private Color
attrToBackgroundColor(final CellAttributes attr
) {
793 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
795 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
797 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
799 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
801 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
803 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
805 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
807 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
810 throw new IllegalArgumentException("Invalid color: " +
811 attr
.getBackColor().getValue());
815 * Figure out what textAdjustX and textAdjustY should be, based on the
816 * location of a vertical bar (to find textAdjustY) and a horizontal bar
817 * (to find textAdjustX).
819 * @return true if textAdjustX and textAdjustY were guessed at correctly
821 private boolean getFontAdjustments() {
822 BufferedImage image
= null;
824 // What SHOULD happen is that the topmost/leftmost white pixel is at
825 // position (gr2x, gr2y). But it might also be off by a pixel in
828 Graphics2D gr2
= null;
831 image
= new BufferedImage(textWidth
* 2, textHeight
* 2,
832 BufferedImage
.TYPE_INT_ARGB
);
834 gr2
= image
.createGraphics();
835 gr2
.setFont(swing
.getFont());
836 gr2
.setColor(java
.awt
.Color
.BLACK
);
837 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
838 gr2
.setColor(java
.awt
.Color
.WHITE
);
839 char [] chars
= new char[1];
840 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
841 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
844 for (int x
= 0; x
< textWidth
; x
++) {
845 for (int y
= 0; y
< textHeight
; y
++) {
848 System.err.println("X: " + x + " Y: " + y + " " +
852 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
853 textAdjustY
= (gr2y
- y
);
855 // System.err.println("textAdjustY: " + textAdjustY);
862 gr2
= image
.createGraphics();
863 gr2
.setFont(swing
.getFont());
864 gr2
.setColor(java
.awt
.Color
.BLACK
);
865 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
866 gr2
.setColor(java
.awt
.Color
.WHITE
);
867 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
868 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
871 for (int x
= 0; x
< textWidth
; x
++) {
872 for (int y
= 0; y
< textHeight
; y
++) {
875 System.err.println("X: " + x + " Y: " + y + " " +
879 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
880 textAdjustX
= (gr2x
- x
);
882 // System.err.println("textAdjustX: " + textAdjustX);
888 // Something weird happened, don't rely on this function.
889 // System.err.println("getFontAdjustments: false");
894 * Figure out my font dimensions. This code path works OK for the JFrame
895 * case, and can be called immediately after JFrame creation.
897 private void getFontDimensions() {
899 Graphics gr
= swing
.getGraphics();
903 getFontDimensions(gr
);
907 * Figure out my font dimensions. This code path is needed to lazy-load
908 * the information inside paint().
910 * @param gr Graphics object to use
912 private void getFontDimensions(final Graphics gr
) {
914 FontMetrics fm
= gr
.getFontMetrics();
915 maxDescent
= fm
.getMaxDescent();
916 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
917 int leading
= fm
.getLeading();
918 textWidth
= (int)Math
.round(bounds
.getWidth());
919 // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
921 // This produces the same number, but works better for ugly
923 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
925 if (gotTerminus
== true) {
929 if (getFontAdjustments() == false) {
930 // We were unable to programmatically determine textAdjustX and
931 // textAdjustY, so try some guesses based on VM vendor.
932 String runtime
= System
.getProperty("java.runtime.name");
933 if ((runtime
!= null) && (runtime
.contains("Java(TM)"))) {
939 if (sessionInfo
!= null) {
940 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
942 gotFontDimensions
= true;
946 * Resize to font dimensions.
948 public void resizeToScreen() {
949 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
953 * Draw one glyph to the screen.
955 * @param gr the Swing Graphics context
956 * @param cell the Cell to draw
957 * @param xPixel the x-coordinate to render to. 0 means the
958 * left-most pixel column.
959 * @param yPixel the y-coordinate to render to. 0 means the top-most
962 private void drawGlyph(final Graphics gr
, final Cell cell
,
963 final int xPixel
, final int yPixel
) {
966 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
970 BufferedImage image
= null;
971 if (cell
.isBlink() && !cursorBlinkVisible
) {
972 image
= glyphCacheBlink
.get(cell
);
974 image
= glyphCache
.get(cell
);
977 if (swing
.getFrame() != null) {
978 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
980 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
985 // Generate glyph and draw it.
986 Graphics2D gr2
= null;
989 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
990 image
= new BufferedImage(textWidth
, textHeight
,
991 BufferedImage
.TYPE_INT_ARGB
);
992 gr2
= image
.createGraphics();
993 gr2
.setFont(swing
.getFont());
997 gr2
= (Graphics2D
) gr
;
1000 Cell cellColor
= new Cell();
1001 cellColor
.setTo(cell
);
1003 // Check for reverse
1004 if (cell
.isReverse()) {
1005 cellColor
.setForeColor(cell
.getBackColor());
1006 cellColor
.setBackColor(cell
.getForeColor());
1009 // Draw the background rectangle, then the foreground character.
1010 gr2
.setColor(attrToBackgroundColor(cellColor
));
1011 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1013 // Handle blink and underline
1015 || (cell
.isBlink() && cursorBlinkVisible
)
1017 gr2
.setColor(attrToForegroundColor(cellColor
));
1018 char [] chars
= new char[1];
1019 chars
[0] = cell
.getChar();
1020 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1021 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1023 if (cell
.isUnderline()) {
1024 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1028 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1031 // We need a new key that will not be mutated by
1033 Cell key
= new Cell();
1035 if (cell
.isBlink() && !cursorBlinkVisible
) {
1036 glyphCacheBlink
.put(key
, image
);
1038 glyphCache
.put(key
, image
);
1041 if (swing
.getFrame() != null) {
1042 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1044 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1051 * Check if the cursor is visible, and if so draw it.
1053 * @param gr the Swing Graphics context
1055 private void drawCursor(final Graphics gr
) {
1060 && (cursorY
<= height
- 1)
1061 && (cursorX
<= width
- 1)
1062 && cursorBlinkVisible
1064 int xPixel
= cursorX
* textWidth
+ left
;
1065 int yPixel
= cursorY
* textHeight
+ top
;
1066 Cell lCell
= logical
[cursorX
][cursorY
];
1067 gr
.setColor(attrToForegroundColor(lCell
));
1068 switch (cursorStyle
) {
1072 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1075 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1078 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1085 * Reset the blink timer.
1087 private void resetBlinkTimer() {
1088 lastBlinkTime
= System
.currentTimeMillis();
1089 cursorBlinkVisible
= true;
1093 * Paint redraws the whole screen.
1095 * @param gr the Swing Graphics context
1097 public void paint(final Graphics gr
) {
1099 if (gotFontDimensions
== false) {
1100 // Lazy-load the text width/height
1101 getFontDimensions(gr
);
1103 System.err.println("textWidth " + textWidth +
1104 " textHeight " + textHeight);
1105 System.err.println("FONT: " + swing.getFont() + " font " + font);
1109 if ((swing
.getBufferStrategy() != null)
1110 && (SwingUtilities
.isEventDispatchThread())
1112 // System.err.println("paint(), skip first paint on swing thread");
1117 int xCellMax
= width
;
1119 int yCellMax
= height
;
1121 Rectangle bounds
= gr
.getClipBounds();
1122 if (bounds
!= null) {
1123 // Only update what is in the bounds
1124 xCellMin
= textColumn(bounds
.x
);
1125 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1126 if (xCellMax
> width
) {
1129 if (xCellMin
>= xCellMax
) {
1130 xCellMin
= xCellMax
- 2;
1135 yCellMin
= textRow(bounds
.y
);
1136 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1137 if (yCellMax
> height
) {
1140 if (yCellMin
>= yCellMax
) {
1141 yCellMin
= yCellMax
- 2;
1147 // We need a total repaint
1148 reallyCleared
= true;
1151 // Prevent updates to the screen's data from the TApplication
1153 synchronized (this) {
1156 System.err.printf("bounds %s X %d %d Y %d %d\n",
1157 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1160 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1161 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1163 int xPixel
= x
* textWidth
+ left
;
1164 int yPixel
= y
* textHeight
+ top
;
1166 Cell lCell
= logical
[x
][y
];
1167 Cell pCell
= physical
[x
][y
];
1169 if (!lCell
.equals(pCell
)
1172 || (swing
.getFrame() == null)) {
1174 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1176 // Physical is always updated
1177 physical
[x
][y
].setTo(lCell
);
1183 reallyCleared
= false;
1184 } // synchronized (this)
1188 * Restore terminal to normal state.
1190 public void shutdown() {
1195 * Push the logical screen to the physical device.
1197 private void drawToSwing() {
1200 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1201 reallyCleared, dirty);
1204 // If reallyCleared is set, we have to draw everything.
1205 if ((swing
.getFrame() != null)
1206 && (swing
.getBufferStrategy() != null)
1207 && (reallyCleared
== true)
1209 // Triple-buffering: we have to redraw everything on this thread.
1210 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1213 swing
.getBufferStrategy().show();
1214 Toolkit
.getDefaultToolkit().sync();
1216 } else if (((swing
.getFrame() != null)
1217 && (swing
.getBufferStrategy() == null))
1218 || (reallyCleared
== true)
1220 // Repaint everything on the Swing thread.
1221 // System.err.println("REPAINT ALL");
1226 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1227 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1229 synchronized (this) {
1230 for (int y
= 0; y
< height
; y
++) {
1231 for (int x
= 0; x
< width
; x
++) {
1232 Cell lCell
= logical
[x
][y
];
1233 Cell pCell
= physical
[x
][y
];
1235 int xPixel
= x
* textWidth
+ left
;
1236 int yPixel
= y
* textHeight
+ top
;
1238 if (!lCell
.equals(pCell
)
1242 || (lCell
.isBlink())
1244 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1245 physical
[x
][y
].setTo(lCell
);
1250 } // synchronized (this)
1253 swing
.getBufferStrategy().show();
1254 Toolkit
.getDefaultToolkit().sync();
1258 // Swing thread version: request a repaint, but limit it to the area
1259 // that has changed.
1261 // Find the minimum-size damaged region.
1262 int xMin
= swing
.getWidth();
1264 int yMin
= swing
.getHeight();
1267 synchronized (this) {
1268 for (int y
= 0; y
< height
; y
++) {
1269 for (int x
= 0; x
< width
; x
++) {
1270 Cell lCell
= logical
[x
][y
];
1271 Cell pCell
= physical
[x
][y
];
1273 int xPixel
= x
* textWidth
+ left
;
1274 int yPixel
= y
* textHeight
+ top
;
1276 if (!lCell
.equals(pCell
)
1282 if (xPixel
< xMin
) {
1285 if (xPixel
+ textWidth
> xMax
) {
1286 xMax
= xPixel
+ textWidth
;
1288 if (yPixel
< yMin
) {
1291 if (yPixel
+ textHeight
> yMax
) {
1292 yMax
= yPixel
+ textHeight
;
1298 if (xMin
+ textWidth
>= xMax
) {
1301 if (yMin
+ textHeight
>= yMax
) {
1305 // Repaint the desired area
1307 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1311 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1312 // This path should never be taken, but is left here for
1314 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1315 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1320 swing
.getBufferStrategy().show();
1321 Toolkit
.getDefaultToolkit().sync();
1323 // Repaint on the Swing thread.
1324 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1329 * Convert pixel column position to text cell column position.
1331 * @param x pixel column position
1332 * @return text cell column position
1334 public int textColumn(final int x
) {
1335 int column
= ((x
- left
) / textWidth
);
1339 if (column
> width
- 1) {
1346 * Convert pixel row position to text cell row position.
1348 * @param y pixel row position
1349 * @return text cell row position
1351 public int textRow(final int y
) {
1352 int row
= ((y
- top
) / textHeight
);
1356 if (row
> height
- 1) {
1363 * Getter for sessionInfo.
1365 * @return the SessionInfo
1367 public SessionInfo
getSessionInfo() {
1371 // ------------------------------------------------------------------------
1372 // KeyListener ------------------------------------------------------------
1373 // ------------------------------------------------------------------------
1376 * Pass Swing keystrokes into the event queue.
1378 * @param key keystroke received
1380 public void keyReleased(final KeyEvent key
) {
1381 // Ignore release events
1385 * Pass Swing keystrokes into the event queue.
1387 * @param key keystroke received
1389 public void keyTyped(final KeyEvent key
) {
1390 // Ignore typed events
1394 * Pass Swing keystrokes into the event queue.
1396 * @param key keystroke received
1398 public void keyPressed(final KeyEvent key
) {
1399 boolean alt
= false;
1400 boolean shift
= false;
1401 boolean ctrl
= false;
1403 boolean isKey
= false;
1404 if (key
.isActionKey()) {
1407 ch
= key
.getKeyChar();
1409 alt
= key
.isAltDown();
1410 ctrl
= key
.isControlDown();
1411 shift
= key
.isShiftDown();
1414 System.err.printf("Swing Key: %s\n", key);
1415 System.err.printf(" isKey: %s\n", isKey);
1416 System.err.printf(" alt: %s\n", alt);
1417 System.err.printf(" ctrl: %s\n", ctrl);
1418 System.err.printf(" shift: %s\n", shift);
1419 System.err.printf(" ch: %s\n", ch);
1422 // Special case: not return the bare modifier presses
1423 switch (key
.getKeyCode()) {
1424 case KeyEvent
.VK_ALT
:
1426 case KeyEvent
.VK_ALT_GRAPH
:
1428 case KeyEvent
.VK_CONTROL
:
1430 case KeyEvent
.VK_SHIFT
:
1432 case KeyEvent
.VK_META
:
1438 TKeypress keypress
= null;
1440 switch (key
.getKeyCode()) {
1441 case KeyEvent
.VK_F1
:
1442 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1445 case KeyEvent
.VK_F2
:
1446 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1449 case KeyEvent
.VK_F3
:
1450 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1453 case KeyEvent
.VK_F4
:
1454 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1457 case KeyEvent
.VK_F5
:
1458 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1461 case KeyEvent
.VK_F6
:
1462 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1465 case KeyEvent
.VK_F7
:
1466 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1469 case KeyEvent
.VK_F8
:
1470 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1473 case KeyEvent
.VK_F9
:
1474 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1477 case KeyEvent
.VK_F10
:
1478 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1481 case KeyEvent
.VK_F11
:
1482 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1485 case KeyEvent
.VK_F12
:
1486 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1489 case KeyEvent
.VK_HOME
:
1490 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1493 case KeyEvent
.VK_END
:
1494 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1497 case KeyEvent
.VK_PAGE_UP
:
1498 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1501 case KeyEvent
.VK_PAGE_DOWN
:
1502 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1505 case KeyEvent
.VK_INSERT
:
1506 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1509 case KeyEvent
.VK_DELETE
:
1510 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1513 case KeyEvent
.VK_RIGHT
:
1514 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1517 case KeyEvent
.VK_LEFT
:
1518 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1521 case KeyEvent
.VK_UP
:
1522 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1525 case KeyEvent
.VK_DOWN
:
1526 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1529 case KeyEvent
.VK_TAB
:
1530 // Special case: distinguish TAB vs BTAB
1532 keypress
= kbShiftTab
;
1537 case KeyEvent
.VK_ENTER
:
1538 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1541 case KeyEvent
.VK_ESCAPE
:
1542 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1545 case KeyEvent
.VK_BACK_SPACE
:
1546 keypress
= kbBackspace
;
1549 // Unsupported, ignore
1554 if (keypress
== null) {
1557 // Disambiguate ^H from Backspace.
1558 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1560 keypress
= kbBackspace
;
1562 // We are emulating Xterm here, where the backspace key
1563 // on the keyboard returns ^?.
1564 keypress
= kbBackspaceDel
;
1578 keypress
= kbShiftTab
;
1587 if (!alt
&& ctrl
&& !shift
) {
1588 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1590 // Not a special key, put it together
1591 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1595 // Save it and we are done.
1596 synchronized (eventQueue
) {
1597 eventQueue
.add(new TKeypressEvent(keypress
));
1600 if (listener
!= null) {
1601 synchronized (listener
) {
1602 listener
.notifyAll();
1607 // ------------------------------------------------------------------------
1608 // WindowListener ---------------------------------------------------------
1609 // ------------------------------------------------------------------------
1612 * Pass window events into the event queue.
1614 * @param event window event received
1616 public void windowActivated(final WindowEvent event
) {
1617 // Force a total repaint
1618 synchronized (this) {
1624 * Pass window events into the event queue.
1626 * @param event window event received
1628 public void windowClosed(final WindowEvent event
) {
1633 * Pass window events into the event queue.
1635 * @param event window event received
1637 public void windowClosing(final WindowEvent event
) {
1638 // Drop a cmAbort and walk away
1639 synchronized (eventQueue
) {
1640 eventQueue
.add(new TCommandEvent(cmAbort
));
1643 if (listener
!= null) {
1644 synchronized (listener
) {
1645 listener
.notifyAll();
1651 * Pass window events into the event queue.
1653 * @param event window event received
1655 public void windowDeactivated(final WindowEvent event
) {
1660 * Pass window events into the event queue.
1662 * @param event window event received
1664 public void windowDeiconified(final WindowEvent event
) {
1669 * Pass window events into the event queue.
1671 * @param event window event received
1673 public void windowIconified(final WindowEvent event
) {
1678 * Pass window events into the event queue.
1680 * @param event window event received
1682 public void windowOpened(final WindowEvent event
) {
1686 // ------------------------------------------------------------------------
1687 // ComponentListener ------------------------------------------------------
1688 // ------------------------------------------------------------------------
1691 * Pass component events into the event queue.
1693 * @param event component event received
1695 public void componentHidden(final ComponentEvent event
) {
1700 * Pass component events into the event queue.
1702 * @param event component event received
1704 public void componentShown(final ComponentEvent event
) {
1709 * Pass component events into the event queue.
1711 * @param event component event received
1713 public void componentMoved(final ComponentEvent event
) {
1718 * Pass component events into the event queue.
1720 * @param event component event received
1722 public void componentResized(final ComponentEvent event
) {
1723 if (gotFontDimensions
== false) {
1724 // We are still waiting to get font information. Don't pass a
1726 // System.err.println("size " + swing.getComponent().getSize());
1730 // Drop a new TResizeEvent into the queue
1731 sessionInfo
.queryWindowSize();
1732 synchronized (eventQueue
) {
1733 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1734 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1735 eventQueue
.add(windowResize
);
1738 System.err.println("Add resize event: " + windowResize.getWidth() +
1739 " x " + windowResize.getHeight());
1742 if (listener
!= null) {
1743 synchronized (listener
) {
1744 listener
.notifyAll();
1749 // ------------------------------------------------------------------------
1750 // MouseMotionListener ----------------------------------------------------
1751 // ------------------------------------------------------------------------
1754 * Pass mouse events into the event queue.
1756 * @param mouse mouse event received
1758 public void mouseDragged(final MouseEvent mouse
) {
1759 int modifiers
= mouse
.getModifiersEx();
1760 boolean eventMouse1
= false;
1761 boolean eventMouse2
= false;
1762 boolean eventMouse3
= false;
1763 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1766 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1769 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1772 mouse1
= eventMouse1
;
1773 mouse2
= eventMouse2
;
1774 mouse3
= eventMouse3
;
1775 int x
= textColumn(mouse
.getX());
1776 int y
= textRow(mouse
.getY());
1778 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1779 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1781 synchronized (eventQueue
) {
1782 eventQueue
.add(mouseEvent
);
1785 if (listener
!= null) {
1786 synchronized (listener
) {
1787 listener
.notifyAll();
1793 * Pass mouse events into the event queue.
1795 * @param mouse mouse event received
1797 public void mouseMoved(final MouseEvent mouse
) {
1798 int x
= textColumn(mouse
.getX());
1799 int y
= textRow(mouse
.getY());
1800 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
1801 // Bail out, we've moved some pixels but not a whole text cell.
1807 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1808 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1810 synchronized (eventQueue
) {
1811 eventQueue
.add(mouseEvent
);
1814 if (listener
!= null) {
1815 synchronized (listener
) {
1816 listener
.notifyAll();
1821 // ------------------------------------------------------------------------
1822 // MouseListener ----------------------------------------------------------
1823 // ------------------------------------------------------------------------
1826 * Pass mouse events into the event queue.
1828 * @param mouse mouse event received
1830 public void mouseClicked(final MouseEvent mouse
) {
1835 * Pass mouse events into the event queue.
1837 * @param mouse mouse event received
1839 public void mouseEntered(final MouseEvent mouse
) {
1844 * Pass mouse events into the event queue.
1846 * @param mouse mouse event received
1848 public void mouseExited(final MouseEvent mouse
) {
1853 * Pass mouse events into the event queue.
1855 * @param mouse mouse event received
1857 public void mousePressed(final MouseEvent mouse
) {
1858 int modifiers
= mouse
.getModifiersEx();
1859 boolean eventMouse1
= false;
1860 boolean eventMouse2
= false;
1861 boolean eventMouse3
= false;
1862 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1865 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1868 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1871 mouse1
= eventMouse1
;
1872 mouse2
= eventMouse2
;
1873 mouse3
= eventMouse3
;
1874 int x
= textColumn(mouse
.getX());
1875 int y
= textRow(mouse
.getY());
1877 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
1878 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1880 synchronized (eventQueue
) {
1881 eventQueue
.add(mouseEvent
);
1884 if (listener
!= null) {
1885 synchronized (listener
) {
1886 listener
.notifyAll();
1892 * Pass mouse events into the event queue.
1894 * @param mouse mouse event received
1896 public void mouseReleased(final MouseEvent mouse
) {
1897 int modifiers
= mouse
.getModifiersEx();
1898 boolean eventMouse1
= false;
1899 boolean eventMouse2
= false;
1900 boolean eventMouse3
= false;
1901 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1904 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1907 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1922 int x
= textColumn(mouse
.getX());
1923 int y
= textRow(mouse
.getY());
1925 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
1926 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
1928 synchronized (eventQueue
) {
1929 eventQueue
.add(mouseEvent
);
1932 if (listener
!= null) {
1933 synchronized (listener
) {
1934 listener
.notifyAll();
1939 // ------------------------------------------------------------------------
1940 // MouseWheelListener -----------------------------------------------------
1941 // ------------------------------------------------------------------------
1944 * Pass mouse events into the event queue.
1946 * @param mouse mouse event received
1948 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
1949 int modifiers
= mouse
.getModifiersEx();
1950 boolean eventMouse1
= false;
1951 boolean eventMouse2
= false;
1952 boolean eventMouse3
= false;
1953 boolean mouseWheelUp
= false;
1954 boolean mouseWheelDown
= false;
1955 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1958 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1961 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1964 mouse1
= eventMouse1
;
1965 mouse2
= eventMouse2
;
1966 mouse3
= eventMouse3
;
1967 int x
= textColumn(mouse
.getX());
1968 int y
= textRow(mouse
.getY());
1969 if (mouse
.getWheelRotation() > 0) {
1970 mouseWheelDown
= true;
1972 if (mouse
.getWheelRotation() < 0) {
1973 mouseWheelUp
= true;
1976 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
1977 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
1979 synchronized (eventQueue
) {
1980 eventQueue
.add(mouseEvent
);
1983 if (listener
!= null) {
1984 synchronized (listener
) {
1985 listener
.notifyAll();