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 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
) {
745 int rgb
= attr
.getForeColorRGB();
747 int red
= (rgb
>> 16) & 0xFF;
748 int green
= (rgb
>> 8) & 0xFF;
749 int blue
= rgb
& 0xFF;
751 return new Color(red
, green
, blue
);
755 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
757 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
759 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
761 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
763 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
764 return MYBOLD_YELLOW
;
765 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
767 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
768 return MYBOLD_MAGENTA
;
769 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
773 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
775 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
777 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
779 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
781 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
783 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
785 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
787 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
791 throw new IllegalArgumentException("Invalid color: " +
792 attr
.getForeColor().getValue());
796 * Convert a CellAttributes background color to an Swing Color.
798 * @param attr the text attributes
799 * @return the Swing Color
801 private Color
attrToBackgroundColor(final CellAttributes attr
) {
802 int rgb
= attr
.getBackColorRGB();
804 int red
= (rgb
>> 16) & 0xFF;
805 int green
= (rgb
>> 8) & 0xFF;
806 int blue
= rgb
& 0xFF;
808 return new Color(red
, green
, blue
);
811 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
813 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
815 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
817 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
819 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
821 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
823 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
825 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
828 throw new IllegalArgumentException("Invalid color: " +
829 attr
.getBackColor().getValue());
833 * Figure out what textAdjustX and textAdjustY should be, based on the
834 * location of a vertical bar (to find textAdjustY) and a horizontal bar
835 * (to find textAdjustX).
837 * @return true if textAdjustX and textAdjustY were guessed at correctly
839 private boolean getFontAdjustments() {
840 BufferedImage image
= null;
842 // What SHOULD happen is that the topmost/leftmost white pixel is at
843 // position (gr2x, gr2y). But it might also be off by a pixel in
846 Graphics2D gr2
= null;
849 image
= new BufferedImage(textWidth
* 2, textHeight
* 2,
850 BufferedImage
.TYPE_INT_ARGB
);
852 gr2
= image
.createGraphics();
853 gr2
.setFont(swing
.getFont());
854 gr2
.setColor(java
.awt
.Color
.BLACK
);
855 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
856 gr2
.setColor(java
.awt
.Color
.WHITE
);
857 char [] chars
= new char[1];
858 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
859 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
862 for (int x
= 0; x
< textWidth
; x
++) {
863 for (int y
= 0; y
< textHeight
; y
++) {
866 System.err.println("X: " + x + " Y: " + y + " " +
870 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
871 textAdjustY
= (gr2y
- y
);
873 // System.err.println("textAdjustY: " + textAdjustY);
880 gr2
= image
.createGraphics();
881 gr2
.setFont(swing
.getFont());
882 gr2
.setColor(java
.awt
.Color
.BLACK
);
883 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
884 gr2
.setColor(java
.awt
.Color
.WHITE
);
885 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
886 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
889 for (int x
= 0; x
< textWidth
; x
++) {
890 for (int y
= 0; y
< textHeight
; y
++) {
893 System.err.println("X: " + x + " Y: " + y + " " +
897 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
898 textAdjustX
= (gr2x
- x
);
900 // System.err.println("textAdjustX: " + textAdjustX);
906 // Something weird happened, don't rely on this function.
907 // System.err.println("getFontAdjustments: false");
912 * Figure out my font dimensions. This code path works OK for the JFrame
913 * case, and can be called immediately after JFrame creation.
915 private void getFontDimensions() {
917 Graphics gr
= swing
.getGraphics();
921 getFontDimensions(gr
);
925 * Figure out my font dimensions. This code path is needed to lazy-load
926 * the information inside paint().
928 * @param gr Graphics object to use
930 private void getFontDimensions(final Graphics gr
) {
932 FontMetrics fm
= gr
.getFontMetrics();
933 maxDescent
= fm
.getMaxDescent();
934 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
935 int leading
= fm
.getLeading();
936 textWidth
= (int)Math
.round(bounds
.getWidth());
937 // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
939 // This produces the same number, but works better for ugly
941 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
943 if (gotTerminus
== true) {
947 if (getFontAdjustments() == false) {
948 // We were unable to programmatically determine textAdjustX and
949 // textAdjustY, so try some guesses based on VM vendor.
950 String runtime
= System
.getProperty("java.runtime.name");
951 if ((runtime
!= null) && (runtime
.contains("Java(TM)"))) {
957 if (sessionInfo
!= null) {
958 sessionInfo
.setTextCellDimensions(textWidth
, textHeight
);
960 gotFontDimensions
= true;
964 * Resize to font dimensions.
966 public void resizeToScreen() {
967 swing
.setDimensions(textWidth
* width
, textHeight
* height
);
971 * Draw one glyph to the screen.
973 * @param gr the Swing Graphics context
974 * @param cell the Cell to draw
975 * @param xPixel the x-coordinate to render to. 0 means the
976 * left-most pixel column.
977 * @param yPixel the y-coordinate to render to. 0 means the top-most
980 private void drawGlyph(final Graphics gr
, final Cell cell
,
981 final int xPixel
, final int yPixel
) {
984 System.err.println("drawGlyph(): " + xPixel + " " + yPixel +
988 BufferedImage image
= null;
989 if (cell
.isBlink() && !cursorBlinkVisible
) {
990 image
= glyphCacheBlink
.get(cell
);
992 image
= glyphCache
.get(cell
);
995 if (swing
.getFrame() != null) {
996 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
998 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1003 // Generate glyph and draw it.
1004 Graphics2D gr2
= null;
1007 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1008 image
= new BufferedImage(textWidth
, textHeight
,
1009 BufferedImage
.TYPE_INT_ARGB
);
1010 gr2
= image
.createGraphics();
1011 gr2
.setFont(swing
.getFont());
1015 gr2
= (Graphics2D
) gr
;
1018 Cell cellColor
= new Cell();
1019 cellColor
.setTo(cell
);
1021 // Check for reverse
1022 if (cell
.isReverse()) {
1023 cellColor
.setForeColor(cell
.getBackColor());
1024 cellColor
.setBackColor(cell
.getForeColor());
1027 // Draw the background rectangle, then the foreground character.
1028 gr2
.setColor(attrToBackgroundColor(cellColor
));
1029 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
1031 // Handle blink and underline
1033 || (cell
.isBlink() && cursorBlinkVisible
)
1035 gr2
.setColor(attrToForegroundColor(cellColor
));
1036 char [] chars
= new char[1];
1037 chars
[0] = cell
.getChar();
1038 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
1039 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
1041 if (cell
.isUnderline()) {
1042 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
1046 if ((SwingComponent
.tripleBuffer
) && (swing
.getFrame() != null)) {
1049 // We need a new key that will not be mutated by
1051 Cell key
= new Cell();
1053 if (cell
.isBlink() && !cursorBlinkVisible
) {
1054 glyphCacheBlink
.put(key
, image
);
1056 glyphCache
.put(key
, image
);
1059 if (swing
.getFrame() != null) {
1060 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getFrame());
1062 gr
.drawImage(image
, xPixel
, yPixel
, swing
.getComponent());
1069 * Check if the cursor is visible, and if so draw it.
1071 * @param gr the Swing Graphics context
1073 private void drawCursor(final Graphics gr
) {
1078 && (cursorY
<= height
- 1)
1079 && (cursorX
<= width
- 1)
1080 && cursorBlinkVisible
1082 int xPixel
= cursorX
* textWidth
+ left
;
1083 int yPixel
= cursorY
* textHeight
+ top
;
1084 Cell lCell
= logical
[cursorX
][cursorY
];
1085 gr
.setColor(attrToForegroundColor(lCell
));
1086 switch (cursorStyle
) {
1090 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
1093 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
1096 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
1103 * Reset the blink timer.
1105 private void resetBlinkTimer() {
1106 lastBlinkTime
= System
.currentTimeMillis();
1107 cursorBlinkVisible
= true;
1111 * Paint redraws the whole screen.
1113 * @param gr the Swing Graphics context
1115 public void paint(final Graphics gr
) {
1117 if (gotFontDimensions
== false) {
1118 // Lazy-load the text width/height
1119 getFontDimensions(gr
);
1121 System.err.println("textWidth " + textWidth +
1122 " textHeight " + textHeight);
1123 System.err.println("FONT: " + swing.getFont() + " font " + font);
1127 if ((swing
.getFrame() != null)
1128 && (swing
.getBufferStrategy() != null)
1129 && (SwingUtilities
.isEventDispatchThread())
1131 // System.err.println("paint(), skip first paint on swing thread");
1136 int xCellMax
= width
;
1138 int yCellMax
= height
;
1140 Rectangle bounds
= gr
.getClipBounds();
1141 if (bounds
!= null) {
1142 // Only update what is in the bounds
1143 xCellMin
= textColumn(bounds
.x
);
1144 xCellMax
= textColumn(bounds
.x
+ bounds
.width
);
1145 if (xCellMax
> width
) {
1148 if (xCellMin
>= xCellMax
) {
1149 xCellMin
= xCellMax
- 2;
1154 yCellMin
= textRow(bounds
.y
);
1155 yCellMax
= textRow(bounds
.y
+ bounds
.height
);
1156 if (yCellMax
> height
) {
1159 if (yCellMin
>= yCellMax
) {
1160 yCellMin
= yCellMax
- 2;
1166 // We need a total repaint
1167 reallyCleared
= true;
1170 // Prevent updates to the screen's data from the TApplication
1172 synchronized (this) {
1175 System.err.printf("bounds %s X %d %d Y %d %d\n",
1176 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
1179 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
1180 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
1182 int xPixel
= x
* textWidth
+ left
;
1183 int yPixel
= y
* textHeight
+ top
;
1185 Cell lCell
= logical
[x
][y
];
1186 Cell pCell
= physical
[x
][y
];
1188 if (!lCell
.equals(pCell
)
1191 || (swing
.getFrame() == null)) {
1193 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1195 // Physical is always updated
1196 physical
[x
][y
].setTo(lCell
);
1202 reallyCleared
= false;
1203 } // synchronized (this)
1207 * Restore terminal to normal state.
1209 public void shutdown() {
1214 * Push the logical screen to the physical device.
1216 private void drawToSwing() {
1219 System.err.printf("drawToSwing(): reallyCleared %s dirty %s\n",
1220 reallyCleared, dirty);
1223 // If reallyCleared is set, we have to draw everything.
1224 if ((swing
.getFrame() != null)
1225 && (swing
.getBufferStrategy() != null)
1226 && (reallyCleared
== true)
1228 // Triple-buffering: we have to redraw everything on this thread.
1229 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1232 swing
.getBufferStrategy().show();
1233 Toolkit
.getDefaultToolkit().sync();
1235 } else if (((swing
.getFrame() != null)
1236 && (swing
.getBufferStrategy() == null))
1237 || (reallyCleared
== true)
1239 // Repaint everything on the Swing thread.
1240 // System.err.println("REPAINT ALL");
1245 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1246 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1248 synchronized (this) {
1249 for (int y
= 0; y
< height
; y
++) {
1250 for (int x
= 0; x
< width
; x
++) {
1251 Cell lCell
= logical
[x
][y
];
1252 Cell pCell
= physical
[x
][y
];
1254 int xPixel
= x
* textWidth
+ left
;
1255 int yPixel
= y
* textHeight
+ top
;
1257 if (!lCell
.equals(pCell
)
1261 || (lCell
.isBlink())
1263 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
1264 physical
[x
][y
].setTo(lCell
);
1269 } // synchronized (this)
1272 swing
.getBufferStrategy().show();
1273 Toolkit
.getDefaultToolkit().sync();
1277 // Swing thread version: request a repaint, but limit it to the area
1278 // that has changed.
1280 // Find the minimum-size damaged region.
1281 int xMin
= swing
.getWidth();
1283 int yMin
= swing
.getHeight();
1286 synchronized (this) {
1287 for (int y
= 0; y
< height
; y
++) {
1288 for (int x
= 0; x
< width
; x
++) {
1289 Cell lCell
= logical
[x
][y
];
1290 Cell pCell
= physical
[x
][y
];
1292 int xPixel
= x
* textWidth
+ left
;
1293 int yPixel
= y
* textHeight
+ top
;
1295 if (!lCell
.equals(pCell
)
1301 if (xPixel
< xMin
) {
1304 if (xPixel
+ textWidth
> xMax
) {
1305 xMax
= xPixel
+ textWidth
;
1307 if (yPixel
< yMin
) {
1310 if (yPixel
+ textHeight
> yMax
) {
1311 yMax
= yPixel
+ textHeight
;
1317 if (xMin
+ textWidth
>= xMax
) {
1320 if (yMin
+ textHeight
>= yMax
) {
1324 // Repaint the desired area
1326 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
1330 if ((swing
.getFrame() != null) && (swing
.getBufferStrategy() != null)) {
1331 // This path should never be taken, but is left here for
1333 Graphics gr
= swing
.getBufferStrategy().getDrawGraphics();
1334 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
1339 swing
.getBufferStrategy().show();
1340 Toolkit
.getDefaultToolkit().sync();
1342 // Repaint on the Swing thread.
1343 swing
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
1348 * Convert pixel column position to text cell column position.
1350 * @param x pixel column position
1351 * @return text cell column position
1353 public int textColumn(final int x
) {
1354 int column
= ((x
- left
) / textWidth
);
1358 if (column
> width
- 1) {
1365 * Convert pixel row position to text cell row position.
1367 * @param y pixel row position
1368 * @return text cell row position
1370 public int textRow(final int y
) {
1371 int row
= ((y
- top
) / textHeight
);
1375 if (row
> height
- 1) {
1382 * Getter for sessionInfo.
1384 * @return the SessionInfo
1386 public SessionInfo
getSessionInfo() {
1391 * Getter for the underlying Swing component.
1393 * @return the SwingComponent
1395 public SwingComponent
getSwingComponent() {
1399 // ------------------------------------------------------------------------
1400 // KeyListener ------------------------------------------------------------
1401 // ------------------------------------------------------------------------
1404 * Pass Swing keystrokes into the event queue.
1406 * @param key keystroke received
1408 public void keyReleased(final KeyEvent key
) {
1409 // Ignore release events
1413 * Pass Swing keystrokes into the event queue.
1415 * @param key keystroke received
1417 public void keyTyped(final KeyEvent key
) {
1418 // Ignore typed events
1422 * Pass Swing keystrokes into the event queue.
1424 * @param key keystroke received
1426 public void keyPressed(final KeyEvent key
) {
1427 boolean alt
= false;
1428 boolean shift
= false;
1429 boolean ctrl
= false;
1431 boolean isKey
= false;
1432 if (key
.isActionKey()) {
1435 ch
= key
.getKeyChar();
1437 alt
= key
.isAltDown();
1438 ctrl
= key
.isControlDown();
1439 shift
= key
.isShiftDown();
1442 System.err.printf("Swing Key: %s\n", key);
1443 System.err.printf(" isKey: %s\n", isKey);
1444 System.err.printf(" alt: %s\n", alt);
1445 System.err.printf(" ctrl: %s\n", ctrl);
1446 System.err.printf(" shift: %s\n", shift);
1447 System.err.printf(" ch: %s\n", ch);
1450 // Special case: not return the bare modifier presses
1451 switch (key
.getKeyCode()) {
1452 case KeyEvent
.VK_ALT
:
1454 case KeyEvent
.VK_ALT_GRAPH
:
1456 case KeyEvent
.VK_CONTROL
:
1458 case KeyEvent
.VK_SHIFT
:
1460 case KeyEvent
.VK_META
:
1466 TKeypress keypress
= null;
1468 switch (key
.getKeyCode()) {
1469 case KeyEvent
.VK_F1
:
1470 keypress
= new TKeypress(true, TKeypress
.F1
, ' ',
1473 case KeyEvent
.VK_F2
:
1474 keypress
= new TKeypress(true, TKeypress
.F2
, ' ',
1477 case KeyEvent
.VK_F3
:
1478 keypress
= new TKeypress(true, TKeypress
.F3
, ' ',
1481 case KeyEvent
.VK_F4
:
1482 keypress
= new TKeypress(true, TKeypress
.F4
, ' ',
1485 case KeyEvent
.VK_F5
:
1486 keypress
= new TKeypress(true, TKeypress
.F5
, ' ',
1489 case KeyEvent
.VK_F6
:
1490 keypress
= new TKeypress(true, TKeypress
.F6
, ' ',
1493 case KeyEvent
.VK_F7
:
1494 keypress
= new TKeypress(true, TKeypress
.F7
, ' ',
1497 case KeyEvent
.VK_F8
:
1498 keypress
= new TKeypress(true, TKeypress
.F8
, ' ',
1501 case KeyEvent
.VK_F9
:
1502 keypress
= new TKeypress(true, TKeypress
.F9
, ' ',
1505 case KeyEvent
.VK_F10
:
1506 keypress
= new TKeypress(true, TKeypress
.F10
, ' ',
1509 case KeyEvent
.VK_F11
:
1510 keypress
= new TKeypress(true, TKeypress
.F11
, ' ',
1513 case KeyEvent
.VK_F12
:
1514 keypress
= new TKeypress(true, TKeypress
.F12
, ' ',
1517 case KeyEvent
.VK_HOME
:
1518 keypress
= new TKeypress(true, TKeypress
.HOME
, ' ',
1521 case KeyEvent
.VK_END
:
1522 keypress
= new TKeypress(true, TKeypress
.END
, ' ',
1525 case KeyEvent
.VK_PAGE_UP
:
1526 keypress
= new TKeypress(true, TKeypress
.PGUP
, ' ',
1529 case KeyEvent
.VK_PAGE_DOWN
:
1530 keypress
= new TKeypress(true, TKeypress
.PGDN
, ' ',
1533 case KeyEvent
.VK_INSERT
:
1534 keypress
= new TKeypress(true, TKeypress
.INS
, ' ',
1537 case KeyEvent
.VK_DELETE
:
1538 keypress
= new TKeypress(true, TKeypress
.DEL
, ' ',
1541 case KeyEvent
.VK_RIGHT
:
1542 keypress
= new TKeypress(true, TKeypress
.RIGHT
, ' ',
1545 case KeyEvent
.VK_LEFT
:
1546 keypress
= new TKeypress(true, TKeypress
.LEFT
, ' ',
1549 case KeyEvent
.VK_UP
:
1550 keypress
= new TKeypress(true, TKeypress
.UP
, ' ',
1553 case KeyEvent
.VK_DOWN
:
1554 keypress
= new TKeypress(true, TKeypress
.DOWN
, ' ',
1557 case KeyEvent
.VK_TAB
:
1558 // Special case: distinguish TAB vs BTAB
1560 keypress
= kbShiftTab
;
1565 case KeyEvent
.VK_ENTER
:
1566 keypress
= new TKeypress(true, TKeypress
.ENTER
, ' ',
1569 case KeyEvent
.VK_ESCAPE
:
1570 keypress
= new TKeypress(true, TKeypress
.ESC
, ' ',
1573 case KeyEvent
.VK_BACK_SPACE
:
1574 keypress
= kbBackspace
;
1577 // Unsupported, ignore
1582 if (keypress
== null) {
1585 // Disambiguate ^H from Backspace.
1586 if (KeyEvent
.getKeyText(key
.getKeyCode()).equals("H")) {
1588 keypress
= kbBackspace
;
1590 // We are emulating Xterm here, where the backspace key
1591 // on the keyboard returns ^?.
1592 keypress
= kbBackspaceDel
;
1606 keypress
= kbShiftTab
;
1615 if (!alt
&& ctrl
&& !shift
) {
1616 ch
= KeyEvent
.getKeyText(key
.getKeyCode()).charAt(0);
1618 // Not a special key, put it together
1619 keypress
= new TKeypress(false, 0, ch
, alt
, ctrl
, shift
);
1623 // Save it and we are done.
1624 synchronized (eventQueue
) {
1625 eventQueue
.add(new TKeypressEvent(keypress
));
1628 if (listener
!= null) {
1629 synchronized (listener
) {
1630 listener
.notifyAll();
1635 // ------------------------------------------------------------------------
1636 // WindowListener ---------------------------------------------------------
1637 // ------------------------------------------------------------------------
1640 * Pass window events into the event queue.
1642 * @param event window event received
1644 public void windowActivated(final WindowEvent event
) {
1645 // Force a total repaint
1646 synchronized (this) {
1652 * Pass window events into the event queue.
1654 * @param event window event received
1656 public void windowClosed(final WindowEvent event
) {
1661 * Pass window events into the event queue.
1663 * @param event window event received
1665 public void windowClosing(final WindowEvent event
) {
1666 // Drop a cmAbort and walk away
1667 synchronized (eventQueue
) {
1668 eventQueue
.add(new TCommandEvent(cmAbort
));
1671 if (listener
!= null) {
1672 synchronized (listener
) {
1673 listener
.notifyAll();
1679 * Pass window events into the event queue.
1681 * @param event window event received
1683 public void windowDeactivated(final WindowEvent event
) {
1688 * Pass window events into the event queue.
1690 * @param event window event received
1692 public void windowDeiconified(final WindowEvent event
) {
1697 * Pass window events into the event queue.
1699 * @param event window event received
1701 public void windowIconified(final WindowEvent event
) {
1706 * Pass window events into the event queue.
1708 * @param event window event received
1710 public void windowOpened(final WindowEvent event
) {
1714 // ------------------------------------------------------------------------
1715 // ComponentListener ------------------------------------------------------
1716 // ------------------------------------------------------------------------
1719 * Pass component events into the event queue.
1721 * @param event component event received
1723 public void componentHidden(final ComponentEvent event
) {
1728 * Pass component events into the event queue.
1730 * @param event component event received
1732 public void componentShown(final ComponentEvent event
) {
1737 * Pass component events into the event queue.
1739 * @param event component event received
1741 public void componentMoved(final ComponentEvent event
) {
1746 * Pass component events into the event queue.
1748 * @param event component event received
1750 public void componentResized(final ComponentEvent event
) {
1751 if (gotFontDimensions
== false) {
1752 // We are still waiting to get font information. Don't pass a
1754 // System.err.println("size " + swing.getComponent().getSize());
1758 // Drop a new TResizeEvent into the queue
1759 sessionInfo
.queryWindowSize();
1760 synchronized (eventQueue
) {
1761 TResizeEvent windowResize
= new TResizeEvent(TResizeEvent
.Type
.SCREEN
,
1762 sessionInfo
.getWindowWidth(), sessionInfo
.getWindowHeight());
1763 eventQueue
.add(windowResize
);
1766 System.err.println("Add resize event: " + windowResize.getWidth() +
1767 " x " + windowResize.getHeight());
1770 if (listener
!= null) {
1771 synchronized (listener
) {
1772 listener
.notifyAll();
1777 // ------------------------------------------------------------------------
1778 // MouseMotionListener ----------------------------------------------------
1779 // ------------------------------------------------------------------------
1782 * Pass mouse events into the event queue.
1784 * @param mouse mouse event received
1786 public void mouseDragged(final MouseEvent mouse
) {
1787 int modifiers
= mouse
.getModifiersEx();
1788 boolean eventMouse1
= false;
1789 boolean eventMouse2
= false;
1790 boolean eventMouse3
= false;
1791 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1794 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1797 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1800 mouse1
= eventMouse1
;
1801 mouse2
= eventMouse2
;
1802 mouse3
= eventMouse3
;
1803 int x
= textColumn(mouse
.getX());
1804 int y
= textRow(mouse
.getY());
1806 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1807 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1809 synchronized (eventQueue
) {
1810 eventQueue
.add(mouseEvent
);
1813 if (listener
!= null) {
1814 synchronized (listener
) {
1815 listener
.notifyAll();
1821 * Pass mouse events into the event queue.
1823 * @param mouse mouse event received
1825 public void mouseMoved(final MouseEvent mouse
) {
1826 int x
= textColumn(mouse
.getX());
1827 int y
= textRow(mouse
.getY());
1828 if ((x
== oldMouseX
) && (y
== oldMouseY
)) {
1829 // Bail out, we've moved some pixels but not a whole text cell.
1835 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_MOTION
,
1836 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1838 synchronized (eventQueue
) {
1839 eventQueue
.add(mouseEvent
);
1842 if (listener
!= null) {
1843 synchronized (listener
) {
1844 listener
.notifyAll();
1849 // ------------------------------------------------------------------------
1850 // MouseListener ----------------------------------------------------------
1851 // ------------------------------------------------------------------------
1854 * Pass mouse events into the event queue.
1856 * @param mouse mouse event received
1858 public void mouseClicked(final MouseEvent mouse
) {
1863 * Pass mouse events into the event queue.
1865 * @param mouse mouse event received
1867 public void mouseEntered(final MouseEvent mouse
) {
1872 * Pass mouse events into the event queue.
1874 * @param mouse mouse event received
1876 public void mouseExited(final MouseEvent mouse
) {
1881 * Pass mouse events into the event queue.
1883 * @param mouse mouse event received
1885 public void mousePressed(final MouseEvent mouse
) {
1886 int modifiers
= mouse
.getModifiersEx();
1887 boolean eventMouse1
= false;
1888 boolean eventMouse2
= false;
1889 boolean eventMouse3
= false;
1890 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1893 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1896 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1899 mouse1
= eventMouse1
;
1900 mouse2
= eventMouse2
;
1901 mouse3
= eventMouse3
;
1902 int x
= textColumn(mouse
.getX());
1903 int y
= textRow(mouse
.getY());
1905 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
1906 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, false, false);
1908 synchronized (eventQueue
) {
1909 eventQueue
.add(mouseEvent
);
1912 if (listener
!= null) {
1913 synchronized (listener
) {
1914 listener
.notifyAll();
1920 * Pass mouse events into the event queue.
1922 * @param mouse mouse event received
1924 public void mouseReleased(final MouseEvent mouse
) {
1925 int modifiers
= mouse
.getModifiersEx();
1926 boolean eventMouse1
= false;
1927 boolean eventMouse2
= false;
1928 boolean eventMouse3
= false;
1929 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1932 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1935 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1950 int x
= textColumn(mouse
.getX());
1951 int y
= textRow(mouse
.getY());
1953 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_UP
,
1954 x
, y
, x
, y
, eventMouse1
, eventMouse2
, eventMouse3
, false, false);
1956 synchronized (eventQueue
) {
1957 eventQueue
.add(mouseEvent
);
1960 if (listener
!= null) {
1961 synchronized (listener
) {
1962 listener
.notifyAll();
1967 // ------------------------------------------------------------------------
1968 // MouseWheelListener -----------------------------------------------------
1969 // ------------------------------------------------------------------------
1972 * Pass mouse events into the event queue.
1974 * @param mouse mouse event received
1976 public void mouseWheelMoved(final MouseWheelEvent mouse
) {
1977 int modifiers
= mouse
.getModifiersEx();
1978 boolean eventMouse1
= false;
1979 boolean eventMouse2
= false;
1980 boolean eventMouse3
= false;
1981 boolean mouseWheelUp
= false;
1982 boolean mouseWheelDown
= false;
1983 if ((modifiers
& MouseEvent
.BUTTON1_DOWN_MASK
) != 0) {
1986 if ((modifiers
& MouseEvent
.BUTTON2_DOWN_MASK
) != 0) {
1989 if ((modifiers
& MouseEvent
.BUTTON3_DOWN_MASK
) != 0) {
1992 mouse1
= eventMouse1
;
1993 mouse2
= eventMouse2
;
1994 mouse3
= eventMouse3
;
1995 int x
= textColumn(mouse
.getX());
1996 int y
= textRow(mouse
.getY());
1997 if (mouse
.getWheelRotation() > 0) {
1998 mouseWheelDown
= true;
2000 if (mouse
.getWheelRotation() < 0) {
2001 mouseWheelUp
= true;
2004 TMouseEvent mouseEvent
= new TMouseEvent(TMouseEvent
.Type
.MOUSE_DOWN
,
2005 x
, y
, x
, y
, mouse1
, mouse2
, mouse3
, mouseWheelUp
, mouseWheelDown
);
2007 synchronized (eventQueue
) {
2008 eventQueue
.add(mouseEvent
);
2011 if (listener
!= null) {
2012 synchronized (listener
) {
2013 listener
.notifyAll();