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
.event
.ComponentEvent
;
40 import java
.awt
.event
.ComponentListener
;
41 import java
.awt
.event
.KeyEvent
;
42 import java
.awt
.event
.KeyListener
;
43 import java
.awt
.event
.MouseEvent
;
44 import java
.awt
.event
.MouseListener
;
45 import java
.awt
.event
.MouseMotionListener
;
46 import java
.awt
.event
.MouseWheelEvent
;
47 import java
.awt
.event
.MouseWheelListener
;
48 import java
.awt
.event
.WindowEvent
;
49 import java
.awt
.event
.WindowListener
;
50 import java
.awt
.geom
.Rectangle2D
;
51 import java
.awt
.image
.BufferedImage
;
52 import java
.io
.InputStream
;
53 import java
.util
.Date
;
54 import java
.util
.HashMap
;
55 import java
.util
.LinkedList
;
56 import java
.util
.List
;
57 import javax
.swing
.JComponent
;
58 import javax
.swing
.JFrame
;
59 import javax
.swing
.SwingUtilities
;
61 import jexer
.TKeypress
;
62 import jexer
.bits
.Cell
;
63 import jexer
.bits
.CellAttributes
;
64 import jexer
.event
.TCommandEvent
;
65 import jexer
.event
.TInputEvent
;
66 import jexer
.event
.TKeypressEvent
;
67 import jexer
.event
.TMouseEvent
;
68 import jexer
.event
.TResizeEvent
;
69 import static jexer
.TCommand
.*;
70 import static jexer
.TKeypress
.*;
73 * This Screen backend reads keystrokes and mouse events and draws to either
74 * a Java Swing JFrame (potentially triple-buffered) or a JComponent.
76 * This class is a bit of an inversion of typical GUI classes. It performs
77 * all of the drawing logic from SwingTerminal (which is not a Swing class),
78 * and uses a SwingComponent wrapper class to call the JFrame or JComponent
81 public final class SwingTerminal
extends LogicalScreen
82 implements TerminalReader
,
83 ComponentListener
, KeyListener
,
84 MouseListener
, MouseMotionListener
,
85 MouseWheelListener
, WindowListener
{
88 * The Swing component or frame to draw to.
90 private SwingComponent swing
;
92 // ------------------------------------------------------------------------
93 // Screen -----------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Cursor style to draw.
99 public enum CursorStyle
{
101 * Use an underscore for the cursor.
106 * Use a solid block for the cursor.
111 * Use an outlined block for the cursor.
117 * A cache of previously-rendered glyphs for blinking text, when it is
120 private HashMap
<Cell
, BufferedImage
> glyphCacheBlink
;
123 * A cache of previously-rendered glyphs for non-blinking, or
124 * blinking-and-visible, text.
126 private HashMap
<Cell
, BufferedImage
> glyphCache
;
128 // Colors to map DOS colors to AWT colors.
129 private static Color MYBLACK
;
130 private static Color MYRED
;
131 private static Color MYGREEN
;
132 private static Color MYYELLOW
;
133 private static Color MYBLUE
;
134 private static Color MYMAGENTA
;
135 private static Color MYCYAN
;
136 private static Color MYWHITE
;
137 private static Color MYBOLD_BLACK
;
138 private static Color MYBOLD_RED
;
139 private static Color MYBOLD_GREEN
;
140 private static Color MYBOLD_YELLOW
;
141 private static Color MYBOLD_BLUE
;
142 private static Color MYBOLD_MAGENTA
;
143 private static Color MYBOLD_CYAN
;
144 private static Color MYBOLD_WHITE
;
147 * When true, all the MYBLACK, MYRED, etc. colors are set.
149 private static boolean dosColors
= false;
152 * Setup Swing colors to match DOS color palette.
154 private static void setDOSColors() {
158 MYBLACK
= new Color(0x00, 0x00, 0x00);
159 MYRED
= new Color(0xa8, 0x00, 0x00);
160 MYGREEN
= new Color(0x00, 0xa8, 0x00);
161 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
162 MYBLUE
= new Color(0x00, 0x00, 0xa8);
163 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
164 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
165 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
166 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
167 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
168 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
169 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
170 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
171 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
172 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
173 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
179 * The terminus font resource filename.
181 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
184 * If true, we were successful getting Terminus.
186 private boolean gotTerminus
= false;
189 * If true, we were successful at getting the font dimensions.
191 private boolean gotFontDimensions
= false;
194 * The currently selected font.
196 private Font font
= null;
199 * The currently selected font size in points.
201 private int fontSize
= 16;
204 * Width of a character cell in pixels.
206 private int textWidth
= 1;
209 * Height of a character cell in pixels.
211 private int textHeight
= 1;
214 * Descent of a character cell in pixels.
216 private int maxDescent
= 0;
219 * System-dependent Y adjustment for text in the character cell.
221 private int textAdjustY
= 0;
224 * System-dependent X adjustment for text in the character cell.
226 private int textAdjustX
= 0;
229 * Top pixel absolute location.
231 private int top
= 30;
234 * Left pixel absolute location.
236 private int left
= 30;
239 * The cursor style to draw.
241 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
244 * The number of millis to wait before switching the blink from
245 * visible to invisible.
247 private long blinkMillis
= 500;
250 * If true, the cursor should be visible right now based on the blink
253 private boolean cursorBlinkVisible
= true;
256 * The time that the blink last flipped from visible to invisible or
257 * from invisible to visible.
259 private long lastBlinkTime
= 0;
262 * Get the font size in points.
264 * @return font size in points
266 public int getFontSize() {
271 * Set the font size in points.
273 * @param fontSize font size in points
275 public void setFontSize(final int fontSize
) {
276 this.fontSize
= fontSize
;
277 Font newFont
= font
.deriveFont((float) fontSize
);
282 * Set to a new font, and resize the screen to match its dimensions.
284 * @param font the new font
286 public void setFont(final Font font
) {
290 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
291 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
296 * Set the font to Terminus, the best all-around font for both CP437 and
299 public void getDefaultFont() {
301 ClassLoader loader
= Thread
.currentThread().
302 getContextClassLoader();
303 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
304 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
305 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, fontSize
);
308 } catch (Exception e
) {
310 font
= new Font(Font
.MONOSPACED
, Font
.PLAIN
, fontSize
);
317 * Convert a CellAttributes foreground color to an Swing Color.
319 * @param attr the text attributes
320 * @return the Swing Color
322 private Color
attrToForegroundColor(final CellAttributes attr
) {
324 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
326 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
328 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
330 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
332 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
333 return MYBOLD_YELLOW
;
334 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
336 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
337 return MYBOLD_MAGENTA
;
338 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
342 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
344 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
346 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
348 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
350 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
352 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
354 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
356 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
360 throw new IllegalArgumentException("Invalid color: " +
361 attr
.getForeColor().getValue());
365 * Convert a CellAttributes background color to an Swing Color.
367 * @param attr the text attributes
368 * @return the Swing Color
370 private Color
attrToBackgroundColor(final CellAttributes attr
) {
371 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
373 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
375 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
377 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
379 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
381 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
383 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
385 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
388 throw new IllegalArgumentException("Invalid color: " +
389 attr
.getBackColor().getValue());
393 * Figure out what textAdjustX and textAdjustY should be, based on the
394 * location of a vertical bar (to find textAdjustY) and a horizontal bar
395 * (to find textAdjustX).
397 * @return true if textAdjustX and textAdjustY were guessed at correctly
399 private boolean getFontAdjustments() {
400 BufferedImage image
= null;
402 // What SHOULD happen is that the topmost/leftmost white pixel is at
403 // position (gr2x, gr2y). But it might also be off by a pixel in
406 Graphics2D gr2
= null;
409 image
= new BufferedImage(textWidth
* 2, textHeight
* 2,
410 BufferedImage
.TYPE_INT_ARGB
);
412 gr2
= image
.createGraphics();
413 gr2
.setFont(swing
.getFont());
414 gr2
.setColor(java
.awt
.Color
.BLACK
);
415 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
416 gr2
.setColor(java
.awt
.Color
.WHITE
);
417 char [] chars
= new char[1];
418 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
419 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
422 for (int x
= 0; x
< textWidth
; x
++) {
423 for (int y
= 0; y
< textHeight
; y
++) {
426 System.err.println("X: " + x + " Y: " + y + " " +
430 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
431 textAdjustY
= (gr2y
- y
);
433 // System.err.println("textAdjustY: " + textAdjustY);
440 gr2
= image
.createGraphics();
441 gr2
.setFont(swing
.getFont());
442 gr2
.setColor(java
.awt
.Color
.BLACK
);
443 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
444 gr2
.setColor(java
.awt
.Color
.WHITE
);
445 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
446 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
449 for (int x
= 0; x
< textWidth
; x
++) {
450 for (int y
= 0; y
< textHeight
; y
++) {
453 System.err.println("X: " + x + " Y: " + y + " " +
457 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
458 textAdjustX
= (gr2x
- x
);
460 // System.err.println("textAdjustX: " + textAdjustX);
466 // Something weird happened, don't rely on this function.
467 // System.err.println("getFontAdjustments: false");
472 * Figure out my font dimensions. This code path works OK for the JFrame
473 * case, and can be called immediately after JFrame creation.
475 private void getFontDimensions() {
477 Graphics gr
= swing
.getGraphics();
481 getFontDimensions(gr
);
485 * Figure out my font dimensions. This code path is needed to lazy-load
486 * the information inside paint().
488 * @param gr Graphics object to use
490 private void getFontDimensions(final Graphics gr
) {
492 FontMetrics fm
= gr
.getFontMetrics();
493 maxDescent
= fm
.getMaxDescent();
494 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
495 int leading
= fm
.getLeading();
496 textWidth
= (int)Math
.round(bounds
.getWidth());
497 // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
499 // This produces the same number, but works better for ugly
501 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
503 if (gotTerminus
== true) {
507 if (getFontAdjustments() == false) {
508 // We were unable to programmatically determine textAdjustX and
509 // textAdjustY, so try some guesses based on VM vendor.
510 String runtime
= System
.getProperty("java.runtime.name");
511 if ((runtime
!= null) && (runtime
.contains("Java(TM)"))) {
517 if (sessionInfo
!= null) {
518 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
520 gotFontDimensions
= true;
524 * Resize to font dimensions.
526 public void resizeToScreen() {
527 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
531 * Draw one glyph to the screen.
533 * @param gr the Swing Graphics context
534 * @param cell the Cell to draw
535 * @param xPixel the x-coordinate to render to. 0 means the
536 * left-most pixel column.
537 * @param yPixel the y-coordinate to render to. 0 means the top-most
540 private void drawGlyph(final Graphics gr
, final Cell cell
,
541 final int xPixel
, final int yPixel
) {
544 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
548 BufferedImage image
= null;
549 if (cell
.isBlink() && !cursorBlinkVisible
) {
550 image
= glyphCacheBlink
.get(cell
);
552 image
= glyphCache
.get(cell
);
555 if (swing
.getFrame() != null) {
556 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
558 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
563 // Generate glyph and draw it.
564 Graphics2D gr2
= null;
567 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
568 image
= new BufferedImage(textWidth
, textHeight
,
569 BufferedImage
.TYPE_INT_ARGB
);
570 gr2
= image
.createGraphics();
571 gr2
.setFont(swing
.getFont());
575 gr2
= (Graphics2D
) gr
;
578 Cell cellColor
= new Cell();
579 cellColor
.setTo(cell
);
582 if (cell
.isReverse()) {
583 cellColor
.setForeColor(cell
.getBackColor());
584 cellColor
.setBackColor(cell
.getForeColor());
587 // Draw the background rectangle, then the foreground character.
588 gr2
.setColor(attrToBackgroundColor(cellColor
));
589 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
591 // Handle blink and underline
593 || (cell
.isBlink() && cursorBlinkVisible
)
595 gr2
.setColor(attrToForegroundColor(cellColor
));
596 char [] chars
= new char[1];
597 chars
[0] = cell
.getChar();
598 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
599 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
601 if (cell
.isUnderline()) {
602 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
606 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
609 // We need a new key that will not be mutated by
611 Cell key
= new Cell();
613 if (cell
.isBlink() && !cursorBlinkVisible
) {
614 glyphCacheBlink
.put(key
, image
);
616 glyphCache
.put(key
, image
);
619 if (swing
.getFrame() != null) {
620 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
622 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
629 * Check if the cursor is visible, and if so draw it.
631 * @param gr the Swing Graphics context
633 private void drawCursor(final Graphics gr
) {
636 && (cursorY
<= height
- 1)
637 && (cursorX
<= width
- 1)
638 && cursorBlinkVisible
640 int xPixel
= cursorX
* textWidth
+ left
;
641 int yPixel
= cursorY
* textHeight
+ top
;
642 Cell lCell
= logical
[cursorX
][cursorY
];
643 gr
.setColor(attrToForegroundColor(lCell
));
644 switch (cursorStyle
) {
648 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
651 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
654 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
661 * Reset the blink timer.
663 private void resetBlinkTimer() {
664 // See if it is time to flip the blink time.
665 long nowTime
= (new Date()).getTime();
666 lastBlinkTime
= nowTime
;
667 cursorBlinkVisible
= true;
671 * Paint redraws the whole screen.
673 * @param gr the Swing Graphics context
675 public void paint(final Graphics gr
) {
677 if (gotFontDimensions
== false) {
678 // Lazy-load the text width/height
679 // System.err.println("calling getFontDimensions...");
680 getFontDimensions(gr
);
682 System.err.println("textWidth " + textWidth +
683 " textHeight " + textHeight);
684 System.err.println("FONT: " + swing.getFont() + " font " + font);
689 // See if it is time to flip the blink time.
690 long nowTime
= (new Date()).getTime();
691 if (nowTime
> blinkMillis
+ lastBlinkTime
) {
692 lastBlinkTime
= nowTime
;
693 cursorBlinkVisible
= !cursorBlinkVisible
;
697 int xCellMax
= width
;
699 int yCellMax
= height
;
701 Rectangle bounds
= gr
.getClipBounds();
702 if (bounds
!= null) {
703 // Only update what is in the bounds
704 xCellMin
= textColumn(bounds
.x
);
705 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
706 if (xCellMax
> width
) {
709 if (xCellMin
>= xCellMax
) {
710 xCellMin
= xCellMax
- 2;
715 yCellMin
= textRow(bounds
.y
);
716 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
717 if (yCellMax
> height
) {
720 if (yCellMin
>= yCellMax
) {
721 yCellMin
= yCellMax
- 2;
727 // We need a total repaint
728 reallyCleared
= true;
731 // Prevent updates to the screen's data from the TApplication
733 synchronized (this) {
736 System.err.printf("bounds %s X %d %d Y %d %d\n",
737 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
740 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
741 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
743 int xPixel
= x
* textWidth
+ left
;
744 int yPixel
= y
* textHeight
+ top
;
746 Cell lCell
= logical
[x
][y
];
747 Cell pCell
= physical
[x
][y
];
749 if (!lCell
.equals(pCell
)
752 || (swing
.getFrame() == null)) {
754 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
756 // Physical is always updated
757 physical
[x
][y
].setTo(lCell
);
764 reallyCleared
= false;
765 } // synchronized (this)
769 * Restore terminal to normal state.
771 public void shutdown() {
776 * Push the logical screen to the physical device.
779 public void flushPhysical() {
782 System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n",
783 reallyCleared, dirty);
786 // If reallyCleared is set, we have to draw everything.
787 if ((swing
.getFrame() != null)
788 && (swing
.getBufferStrategy() != null)
789 && (reallyCleared
== true)
791 // Triple-buffering: we have to redraw everything on this thread.
792 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
795 swing
.getBufferStrategy().show();
796 // sync() doesn't seem to help the tearing for me.
797 // Toolkit.getDefaultToolkit().sync();
799 } else if (((swing
.getFrame() != null)
800 && (swing
.getBufferStrategy() == null))
801 || (reallyCleared
== true)
803 // Repaint everything on the Swing thread.
804 // System.err.println("REPAINT ALL");
809 // Do nothing if nothing happened.
814 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
815 // See if it is time to flip the blink time.
816 long nowTime
= (new Date()).getTime();
817 if (nowTime
> blinkMillis
+ lastBlinkTime
) {
818 lastBlinkTime
= nowTime
;
819 cursorBlinkVisible
= !cursorBlinkVisible
;
822 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
824 synchronized (this) {
825 for (int y
= 0; y
< height
; y
++) {
826 for (int x
= 0; x
< width
; x
++) {
827 Cell lCell
= logical
[x
][y
];
828 Cell pCell
= physical
[x
][y
];
830 int xPixel
= x
* textWidth
+ left
;
831 int yPixel
= y
* textHeight
+ top
;
833 if (!lCell
.equals(pCell
)
839 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
840 physical
[x
][y
].setTo(lCell
);
845 } // synchronized (this)
848 swing
.getBufferStrategy().show();
849 // sync() doesn't seem to help the tearing for me.
850 // Toolkit.getDefaultToolkit().sync();
854 // Swing thread version: request a repaint, but limit it to the area
857 // Find the minimum-size damaged region.
858 int xMin
= swing
.getWidth();
860 int yMin
= swing
.getHeight();
863 synchronized (this) {
864 for (int y
= 0; y
< height
; y
++) {
865 for (int x
= 0; x
< width
; x
++) {
866 Cell lCell
= logical
[x
][y
];
867 Cell pCell
= physical
[x
][y
];
869 int xPixel
= x
* textWidth
+ left
;
870 int yPixel
= y
* textHeight
+ top
;
872 if (!lCell
.equals(pCell
)
881 if (xPixel
+ textWidth
> xMax
) {
882 xMax
= xPixel
+ textWidth
;
887 if (yPixel
+ textHeight
> yMax
) {
888 yMax
= yPixel
+ textHeight
;
894 if (xMin
+ textWidth
>= xMax
) {
897 if (yMin
+ textHeight
>= yMax
) {
901 // Repaint the desired area
903 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
907 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
908 // This path should never be taken, but is left here for
910 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
911 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
916 swing
.getBufferStrategy().show();
917 // sync() doesn't seem to help the tearing for me.
918 // Toolkit.getDefaultToolkit().sync();
920 // Repaint on the Swing thread.
921 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
926 * Put the cursor at (x,y).
928 * @param visible if true, the cursor should be visible
929 * @param x column coordinate to put the cursor on
930 * @param y row coordinate to put the cursor on
933 public void putCursor(final boolean visible
, final int x
, final int y
) {
935 if ((visible
== cursorVisible
) && ((x
== cursorX
) && (y
== cursorY
))) {
936 // See if it is time to flip the blink time.
937 long nowTime
= (new Date()).getTime();
938 if (nowTime
< blinkMillis
+ lastBlinkTime
) {
939 // Nothing has changed, so don't do anything.
945 && (cursorY
<= height
- 1)
946 && (cursorX
<= width
- 1)
948 // Make the current cursor position dirty
949 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
950 physical
[cursorX
][cursorY
].setChar('X');
952 physical
[cursorX
][cursorY
].setChar('Q');
956 super.putCursor(visible
, x
, y
);
960 * Convert pixel column position to text cell column position.
962 * @param x pixel column position
963 * @return text cell column position
965 public int textColumn(final int x
) {
966 return ((x
- left
) / textWidth
);
970 * Convert pixel row position to text cell row position.
972 * @param y pixel row position
973 * @return text cell row position
975 public int textRow(final int y
) {
976 return ((y
- top
) / textHeight
);
980 * Set the window title.
982 * @param title the new title
984 public void setTitle(final String title
) {
985 swing
.setTitle(title
);
988 // ------------------------------------------------------------------------
989 // TerminalReader ---------------------------------------------------------
990 // ------------------------------------------------------------------------
993 * The session information.
995 private SwingSessionInfo sessionInfo
;
998 * Getter for sessionInfo.
1000 * @return the SessionInfo
1002 public SessionInfo
getSessionInfo() {
1007 * The listening object that run() wakes up on new input.
1009 private Object listener
;
1012 * Set listener to a different Object.
1014 * @param listener the new listening object that run() wakes up on new
1017 public void setListener(final Object listener
) {
1018 this.listener
= listener
;
1022 * The event queue, filled up by a thread reading on input.
1024 private List
<TInputEvent
> eventQueue
;
1027 * The last reported mouse X position.
1029 private int oldMouseX
= -1;
1032 * The last reported mouse Y position.
1034 private int oldMouseY
= -1;
1037 * true if mouse1 was down. Used to report mouse1 on the release event.
1039 private boolean mouse1
= false;
1042 * true if mouse2 was down. Used to report mouse2 on the release event.
1044 private boolean mouse2
= false;
1047 * true if mouse3 was down. Used to report mouse3 on the release event.
1049 private boolean mouse3
= false;
1052 * Public constructor creates a new JFrame to render to.
1054 * @param windowWidth the number of text columns to start with
1055 * @param windowHeight the number of text rows to start with
1056 * @param fontSize the size in points. Good values to pick are: 16, 20,
1058 * @param listener the object this backend needs to wake up when new
1061 public SwingTerminal(final int windowWidth
, final int windowHeight
,
1062 final int fontSize
, final Object listener
) {
1064 this.fontSize
= fontSize
;
1068 // Figure out my cursor style.
1069 String cursorStyleString
= System
.getProperty(
1070 "jexer.Swing.cursorStyle", "underline").toLowerCase();
1071 if (cursorStyleString
.equals("underline")) {
1072 cursorStyle
= CursorStyle
.UNDERLINE
;
1073 } else if (cursorStyleString
.equals("outline")) {
1074 cursorStyle
= CursorStyle
.OUTLINE
;
1075 } else if (cursorStyleString
.equals("block")) {
1076 cursorStyle
= CursorStyle
.BLOCK
;
1079 // Pull the system property for triple buffering.
1080 if (System
.getProperty("jexer.Swing.tripleBuffer") != null) {
1081 if (System
.getProperty("jexer.Swing.tripleBuffer").equals("true")) {
1082 SwingComponent
.tripleBuffer
= true;
1084 SwingComponent
.tripleBuffer
= false;
1089 SwingUtilities
.invokeAndWait(new Runnable() {
1092 JFrame frame
= new JFrame() {
1095 * Serializable version.
1097 private static final long serialVersionUID
= 1;
1100 * The code that performs the actual drawing.
1102 public SwingTerminal screen
= null;
1105 * Anonymous class initializer saves the screen
1106 * reference, so that paint() and the like call out
1110 this.screen
= SwingTerminal
.this;
1114 * Update redraws the whole screen.
1116 * @param gr the Swing Graphics context
1119 public void update(final Graphics gr
) {
1120 // The default update clears the area. Don't do
1121 // that, instead just paint it directly.
1126 * Paint redraws the whole screen.
1128 * @param gr the Swing Graphics context
1131 public void paint(final Graphics gr
) {
1132 if (screen
!= null) {
1138 // Get the Swing component
1139 SwingTerminal
.this.swing
= new SwingComponent(frame
);
1141 // Hang onto top and left for drawing.
1142 Insets insets
= SwingTerminal
.this.swing
.getInsets();
1143 SwingTerminal
.this.left
= insets
.left
;
1144 SwingTerminal
.this.top
= insets
.top
;
1146 // Load the font so that we can set sessionInfo.
1149 // Get the default cols x rows and set component size
1151 SwingTerminal
.this.sessionInfo
=
1152 new SwingSessionInfo(SwingTerminal
.this.swing
,
1153 SwingTerminal
.this.textWidth
,
1154 SwingTerminal
.this.textHeight
,
1155 windowWidth
, windowHeight
);
1157 SwingTerminal
.this.setDimensions(sessionInfo
.getWindowWidth(),
1158 sessionInfo
.getWindowHeight());
1160 SwingTerminal
.this.resizeToScreen();
1161 SwingTerminal
.this.swing
.setVisible(true);
1164 } catch (Exception e
) {
1165 e
.printStackTrace();
1168 this.listener
= listener
;
1172 eventQueue
= new LinkedList
<TInputEvent
>();
1174 // Add listeners to Swing.
1175 swing
.addKeyListener(this);
1176 swing
.addWindowListener(this);
1177 swing
.addComponentListener(this);
1178 swing
.addMouseListener(this);
1179 swing
.addMouseMotionListener(this);
1180 swing
.addMouseWheelListener(this);
1184 * Public constructor renders to an existing JComponent.
1186 * @param component the Swing component to render to
1187 * @param windowWidth the number of text columns to start with
1188 * @param windowHeight the number of text rows to start with
1189 * @param fontSize the size in points. Good values to pick are: 16, 20,
1191 * @param listener the object this backend needs to wake up when new
1194 public SwingTerminal(final JComponent component
, final int windowWidth
,
1195 final int windowHeight
, final int fontSize
, final Object listener
) {
1197 this.fontSize
= fontSize
;
1201 // Figure out my cursor style.
1202 String cursorStyleString
= System
.getProperty(
1203 "jexer.Swing.cursorStyle", "underline").toLowerCase();
1204 if (cursorStyleString
.equals("underline")) {
1205 cursorStyle
= CursorStyle
.UNDERLINE
;
1206 } else if (cursorStyleString
.equals("outline")) {
1207 cursorStyle
= CursorStyle
.OUTLINE
;
1208 } else if (cursorStyleString
.equals("block")) {
1209 cursorStyle
= CursorStyle
.BLOCK
;
1213 SwingUtilities
.invokeAndWait(new Runnable() {
1216 JComponent newComponent
= new JComponent() {
1219 * Serializable version.
1221 private static final long serialVersionUID
= 1;
1224 * The code that performs the actual drawing.
1226 public SwingTerminal screen
= null;
1229 * Anonymous class initializer saves the screen
1230 * reference, so that paint() and the like call out
1234 this.screen
= SwingTerminal
.this;
1238 * Update redraws the whole screen.
1240 * @param gr the Swing Graphics context
1243 public void update(final Graphics gr
) {
1244 // The default update clears the area. Don't do
1245 // that, instead just paint it directly.
1250 * Paint redraws the whole screen.
1252 * @param gr the Swing Graphics context
1255 public void paint(final Graphics gr
) {
1256 if (screen
!= null) {
1261 component
.setLayout(new BorderLayout());
1262 component
.add(newComponent
);
1264 // Get the Swing component
1265 SwingTerminal
.this.swing
= new SwingComponent(component
);
1267 // Hang onto top and left for drawing.
1268 Insets insets
= SwingTerminal
.this.swing
.getInsets();
1269 SwingTerminal
.this.left
= insets
.left
;
1270 SwingTerminal
.this.top
= insets
.top
;
1272 // Load the font so that we can set sessionInfo.
1275 // Get the default cols x rows and set component size
1277 SwingTerminal
.this.sessionInfo
=
1278 new SwingSessionInfo(SwingTerminal
.this.swing
,
1279 SwingTerminal
.this.textWidth
,
1280 SwingTerminal
.this.textHeight
);
1283 } catch (Exception e
) {
1284 e
.printStackTrace();
1287 this.listener
= listener
;
1291 eventQueue
= new LinkedList
<TInputEvent
>();
1293 // Add listeners to Swing.
1294 swing
.addKeyListener(this);
1295 swing
.addWindowListener(this);
1296 swing
.addComponentListener(this);
1297 swing
.addMouseListener(this);
1298 swing
.addMouseMotionListener(this);
1299 swing
.addMouseWheelListener(this);
1303 * Check if there are events in the queue.
1305 * @return if true, getEvents() has something to return to the backend
1307 public boolean hasEvents() {
1308 synchronized (eventQueue
) {
1309 return (eventQueue
.size() > 0);
1314 * Return any events in the IO queue.
1316 * @param queue list to append new events to
1318 public void getEvents(final List
<TInputEvent
> queue
) {
1319 synchronized (eventQueue
) {
1320 if (eventQueue
.size() > 0) {
1321 synchronized (queue
) {
1322 queue
.addAll(eventQueue
);
1330 * Restore terminal to normal state.
1332 public void closeTerminal() {
1337 * Pass Swing keystrokes into the event queue.
1339 * @param key keystroke received
1341 public void keyReleased(final KeyEvent key
) {
1342 // Ignore release events
1346 * Pass Swing keystrokes into the event queue.
1348 * @param key keystroke received
1350 public void keyTyped(final KeyEvent key
) {
1351 // Ignore typed events
1355 * Pass Swing keystrokes into the event queue.
1357 * @param key keystroke received
1359 public void keyPressed(final KeyEvent key
) {
1360 boolean alt
= false;
1361 boolean shift
= false;
1362 boolean ctrl
= false;
1364 boolean isKey
= false;
1365 if (key
.isActionKey()) {
1368 ch
= key
.getKeyChar();
1370 alt
= key
.isAltDown();
1371 ctrl
= key
.isControlDown();
1372 shift
= key
.isShiftDown();
1375 System.err.printf("Swing Key: %s\n", key);
1376 System.err.printf(" isKey: %s\n", isKey);
1377 System.err.printf(" alt: %s\n", alt);
1378 System.err.printf(" ctrl: %s\n", ctrl);
1379 System.err.printf(" shift: %s\n", shift);
1380 System.err.printf(" ch: %s\n", ch);
1383 // Special case: not return the bare modifier presses
1384 switch (key
.getKeyCode()) {
1385 case KeyEvent
.VK_ALT
:
1387 case KeyEvent
.VK_ALT_GRAPH
:
1389 case KeyEvent
.VK_CONTROL
:
1391 case KeyEvent
.VK_SHIFT
:
1393 case KeyEvent
.VK_META
:
1399 TKeypress keypress
= null;
1401 switch (key
.getKeyCode()) {
1402 case KeyEvent
.VK_F1
:
1403 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1406 case KeyEvent
.VK_F2
:
1407 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1410 case KeyEvent
.VK_F3
:
1411 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1414 case KeyEvent
.VK_F4
:
1415 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1418 case KeyEvent
.VK_F5
:
1419 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1422 case KeyEvent
.VK_F6
:
1423 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1426 case KeyEvent
.VK_F7
:
1427 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1430 case KeyEvent
.VK_F8
:
1431 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1434 case KeyEvent
.VK_F9
:
1435 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1438 case KeyEvent
.VK_F10
:
1439 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1442 case KeyEvent
.VK_F11
:
1443 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1446 case KeyEvent
.VK_F12
:
1447 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1450 case KeyEvent
.VK_HOME
:
1451 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1454 case KeyEvent
.VK_END
:
1455 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1458 case KeyEvent
.VK_PAGE_UP
:
1459 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1462 case KeyEvent
.VK_PAGE_DOWN
:
1463 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1466 case KeyEvent
.VK_INSERT
:
1467 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1470 case KeyEvent
.VK_DELETE
:
1471 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1474 case KeyEvent
.VK_RIGHT
:
1475 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1478 case KeyEvent
.VK_LEFT
:
1479 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1482 case KeyEvent
.VK_UP
:
1483 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1486 case KeyEvent
.VK_DOWN
:
1487 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1490 case KeyEvent
.VK_TAB
:
1491 // Special case: distinguish TAB vs BTAB
1493 keypress
= kbShiftTab
;
1498 case KeyEvent
.VK_ENTER
:
1499 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1502 case KeyEvent
.VK_ESCAPE
:
1503 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1506 case KeyEvent
.VK_BACK_SPACE
:
1507 // Special case: return it as kbBackspace (Ctrl-H)
1508 keypress
= new TKeypress(false, 0, 'H', false, true, false);
1511 // Unsupported, ignore
1516 if (keypress
== null) {
1519 keypress
= kbBackspace
;
1532 keypress
= kbShiftTab
;
1541 if (!alt
&& ctrl
&& !shift
) {
1542 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1544 // Not a special key, put it together
1545 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1549 // Save it and we are done.
1550 synchronized (eventQueue
) {
1551 eventQueue
.add(new TKeypressEvent(keypress
));
1554 if (listener
!= null) {
1555 synchronized (listener
) {
1556 listener
.notifyAll();
1562 * Pass window events into the event queue.
1564 * @param event window event received
1566 public void windowActivated(final WindowEvent event
) {
1567 // Force a total repaint
1568 synchronized (this) {
1574 * Pass window events into the event queue.
1576 * @param event window event received
1578 public void windowClosed(final WindowEvent event
) {
1583 * Pass window events into the event queue.
1585 * @param event window event received
1587 public void windowClosing(final WindowEvent event
) {
1588 // Drop a cmAbort and walk away
1589 synchronized (eventQueue
) {
1590 eventQueue
.add(new TCommandEvent(cmAbort
));
1593 if (listener
!= null) {
1594 synchronized (listener
) {
1595 listener
.notifyAll();
1601 * Pass window events into the event queue.
1603 * @param event window event received
1605 public void windowDeactivated(final WindowEvent event
) {
1610 * Pass window events into the event queue.
1612 * @param event window event received
1614 public void windowDeiconified(final WindowEvent event
) {
1619 * Pass window events into the event queue.
1621 * @param event window event received
1623 public void windowIconified(final WindowEvent event
) {
1628 * Pass window events into the event queue.
1630 * @param event window event received
1632 public void windowOpened(final WindowEvent event
) {
1637 * Pass component events into the event queue.
1639 * @param event component event received
1641 public void componentHidden(final ComponentEvent event
) {
1646 * Pass component events into the event queue.
1648 * @param event component event received
1650 public void componentShown(final ComponentEvent event
) {
1655 * Pass component events into the event queue.
1657 * @param event component event received
1659 public void componentMoved(final ComponentEvent event
) {
1664 * Pass component events into the event queue.
1666 * @param event component event received
1668 public void componentResized(final ComponentEvent event
) {
1669 if (gotFontDimensions
== false) {
1670 // We are still waiting to get font information. Don't pass a
1672 // System.err.println("size " + swing.getComponent().getSize());
1676 // Drop a new TResizeEvent into the queue
1677 sessionInfo
.queryWindowSize();
1678 synchronized (eventQueue
) {
1679 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1680 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1681 eventQueue
.add(windowResize
);
1684 if (listener
!= null) {
1685 synchronized (listener
) {
1686 listener
.notifyAll();
1692 * Pass mouse events into the event queue.
1694 * @param mouse mouse event received
1696 public void mouseDragged(final MouseEvent mouse
) {
1697 int modifiers
= mouse
.getModifiersEx();
1698 boolean eventMouse1
= false;
1699 boolean eventMouse2
= false;
1700 boolean eventMouse3
= false;
1701 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1704 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1707 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1710 mouse1
= eventMouse1
;
1711 mouse2
= eventMouse2
;
1712 mouse3
= eventMouse3
;
1713 int x
= textColumn(mouse
.getX());
1714 int y
= textRow(mouse
.getY());
1716 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1717 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1719 synchronized (eventQueue
) {
1720 eventQueue
.add(mouseEvent
);
1723 if (listener
!= null) {
1724 synchronized (listener
) {
1725 listener
.notifyAll();
1731 * Pass mouse events into the event queue.
1733 * @param mouse mouse event received
1735 public void mouseMoved(final MouseEvent mouse
) {
1736 int x
= textColumn(mouse
.getX());
1737 int y
= textRow(mouse
.getY());
1738 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
1739 // Bail out, we've moved some pixels but not a whole text cell.
1745 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1746 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1748 synchronized (eventQueue
) {
1749 eventQueue
.add(mouseEvent
);
1752 if (listener
!= null) {
1753 synchronized (listener
) {
1754 listener
.notifyAll();
1760 * Pass mouse events into the event queue.
1762 * @param mouse mouse event received
1764 public void mouseClicked(final MouseEvent mouse
) {
1769 * Pass mouse events into the event queue.
1771 * @param mouse mouse event received
1773 public void mouseEntered(final MouseEvent mouse
) {
1778 * Pass mouse events into the event queue.
1780 * @param mouse mouse event received
1782 public void mouseExited(final MouseEvent mouse
) {
1787 * Pass mouse events into the event queue.
1789 * @param mouse mouse event received
1791 public void mousePressed(final MouseEvent mouse
) {
1792 int modifiers
= mouse
.getModifiersEx();
1793 boolean eventMouse1
= false;
1794 boolean eventMouse2
= false;
1795 boolean eventMouse3
= false;
1796 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1799 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1802 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1805 mouse1
= eventMouse1
;
1806 mouse2
= eventMouse2
;
1807 mouse3
= eventMouse3
;
1808 int x
= textColumn(mouse
.getX());
1809 int y
= textRow(mouse
.getY());
1811 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
1812 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1814 synchronized (eventQueue
) {
1815 eventQueue
.add(mouseEvent
);
1818 if (listener
!= null) {
1819 synchronized (listener
) {
1820 listener
.notifyAll();
1826 * Pass mouse events into the event queue.
1828 * @param mouse mouse event received
1830 public void mouseReleased(final MouseEvent mouse
) {
1831 int modifiers
= mouse
.getModifiersEx();
1832 boolean eventMouse1
= false;
1833 boolean eventMouse2
= false;
1834 boolean eventMouse3
= false;
1835 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1838 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1841 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1856 int x
= textColumn(mouse
.getX());
1857 int y
= textRow(mouse
.getY());
1859 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
1860 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
1862 synchronized (eventQueue
) {
1863 eventQueue
.add(mouseEvent
);
1866 if (listener
!= null) {
1867 synchronized (listener
) {
1868 listener
.notifyAll();
1874 * Pass mouse events into the event queue.
1876 * @param mouse mouse event received
1878 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
1879 int modifiers
= mouse
.getModifiersEx();
1880 boolean eventMouse1
= false;
1881 boolean eventMouse2
= false;
1882 boolean eventMouse3
= false;
1883 boolean mouseWheelUp
= false;
1884 boolean mouseWheelDown
= false;
1885 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1888 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1891 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1894 mouse1
= eventMouse1
;
1895 mouse2
= eventMouse2
;
1896 mouse3
= eventMouse3
;
1897 int x
= textColumn(mouse
.getX());
1898 int y
= textRow(mouse
.getY());
1899 if (mouse
.getWheelRotation() > 0) {
1900 mouseWheelDown
= true;
1902 if (mouse
.getWheelRotation() < 0) {
1903 mouseWheelUp
= true;
1906 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
1907 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
1909 synchronized (eventQueue
) {
1910 eventQueue
.add(mouseEvent
);
1913 if (listener
!= null) {
1914 synchronized (listener
) {
1915 listener
.notifyAll();