2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.awt
.Insets
;
34 * SwingSessionInfo provides a session implementation with a callback into
35 * Swing to support queryWindowSize(). The username is blank, language is
36 * "en_US", with a 80x25 text window.
38 public class SwingSessionInfo
implements SessionInfo
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * The Swing JFrame or JComponent.
47 private SwingComponent swing
;
50 * The width of a text cell in pixels.
52 private int textWidth
= 10;
55 * The height of a text cell in pixels.
57 private int textHeight
= 10;
62 private String username
= "";
67 private String language
= "en_US";
72 private int windowWidth
= 80;
77 private int windowHeight
= 25;
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
86 * @param swing the Swing JFrame or JComponent
87 * @param textWidth the width of a cell in pixels
88 * @param textHeight the height of a cell in pixels
90 public SwingSessionInfo(final SwingComponent swing
, final int textWidth
,
91 final int textHeight
) {
94 this.textWidth
= textWidth
;
95 this.textHeight
= textHeight
;
101 * @param swing the Swing JFrame or JComponent
102 * @param textWidth the width of a cell in pixels
103 * @param textHeight the height of a cell in pixels
104 * @param width the number of columns
105 * @param height the number of rows
107 public SwingSessionInfo(final SwingComponent swing
, final int textWidth
,
108 final int textHeight
, final int width
, final int height
) {
111 this.textWidth
= textWidth
;
112 this.textHeight
= textHeight
;
113 this.windowWidth
= width
;
114 this.windowHeight
= height
;
117 // ------------------------------------------------------------------------
118 // SessionInfo ------------------------------------------------------------
119 // ------------------------------------------------------------------------
124 * @return the username
126 public String
getUsername() {
127 return this.username
;
133 * @param username the value
135 public void setUsername(final String username
) {
136 this.username
= username
;
142 * @return the language
144 public String
getLanguage() {
145 return this.language
;
151 * @param language the value
153 public void setLanguage(final String language
) {
154 this.language
= language
;
158 * Text window width getter.
160 * @return the window width
162 public int getWindowWidth() {
167 * Text window height getter.
169 * @return the window height
171 public int getWindowHeight() {
176 * Re-query the text window size.
178 public void queryWindowSize() {
179 Insets insets
= swing
.getInsets();
180 int width
= swing
.getWidth() - insets
.left
- insets
.right
;
181 int height
= swing
.getHeight() - insets
.top
- insets
.bottom
;
182 // In theory, if Java reported pixel-perfect dimensions, the
183 // expressions above would precisely line up with the requested
184 // window size from SwingComponent.setDimensions(). In practice,
185 // there appears to be a small difference. Add half a text cell in
186 // both directions before the division to hopefully reach the same
187 // result as setDimensions() was supposed to give us.
188 width
+= (textWidth
/ 2);
189 height
+= (textHeight
/ 2);
190 windowWidth
= width
/ textWidth
;
191 windowHeight
= height
/ textHeight
;
194 System.err.printf("queryWindowSize(): frame %d %d window %d %d\n",
195 swing.getWidth(), swing.getHeight(),
196 windowWidth, windowHeight);
200 // ------------------------------------------------------------------------
201 // SwingSessionInfo -------------------------------------------------------
202 // ------------------------------------------------------------------------
205 * Set the dimensions of a single text cell.
207 * @param textWidth the width of a cell in pixels
208 * @param textHeight the height of a cell in pixels
210 public void setTextCellDimensions(final int textWidth
,
211 final int textHeight
) {
213 this.textWidth
= textWidth
;
214 this.textHeight
= textHeight
;
218 * Getter for the underlying Swing component.
220 * @return the SwingComponent
222 public SwingComponent
getSwingComponent() {