Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
30bd4abd KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
30bd4abd | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
30bd4abd | 7 | * |
e16dda65 KL |
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: | |
30bd4abd | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
30bd4abd | 17 | * |
e16dda65 KL |
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. | |
30bd4abd KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
42873e30 | 29 | package jexer.backend; |
30bd4abd | 30 | |
30bd4abd KL |
31 | import java.awt.Insets; |
32 | ||
33 | /** | |
42873e30 KL |
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. | |
30bd4abd | 37 | */ |
051e2913 | 38 | public class SwingSessionInfo implements SessionInfo { |
30bd4abd | 39 | |
d36057df KL |
40 | // ------------------------------------------------------------------------ |
41 | // Variables -------------------------------------------------------------- | |
42 | // ------------------------------------------------------------------------ | |
43 | ||
30bd4abd | 44 | /** |
42873e30 | 45 | * The Swing JFrame or JComponent. |
30bd4abd | 46 | */ |
42873e30 | 47 | private SwingComponent swing; |
30bd4abd KL |
48 | |
49 | /** | |
50 | * The width of a text cell in pixels. | |
51 | */ | |
42873e30 | 52 | private int textWidth = 10; |
30bd4abd KL |
53 | |
54 | /** | |
55 | * The height of a text cell in pixels. | |
56 | */ | |
42873e30 | 57 | private int textHeight = 10; |
30bd4abd KL |
58 | |
59 | /** | |
60 | * User name. | |
61 | */ | |
62 | private String username = ""; | |
63 | ||
64 | /** | |
65 | * Language. | |
66 | */ | |
67 | private String language = "en_US"; | |
68 | ||
69 | /** | |
70 | * Text window width. | |
71 | */ | |
55d2b2c2 | 72 | private int windowWidth = 80; |
30bd4abd KL |
73 | |
74 | /** | |
75 | * Text window height. | |
76 | */ | |
55d2b2c2 | 77 | private int windowHeight = 25; |
30bd4abd | 78 | |
d36057df KL |
79 | // ------------------------------------------------------------------------ |
80 | // Constructors ----------------------------------------------------------- | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
83 | /** | |
84 | * Public constructor. | |
85 | * | |
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 | |
89 | */ | |
90 | public SwingSessionInfo(final SwingComponent swing, final int textWidth, | |
91 | final int textHeight) { | |
92 | ||
93 | this.swing = swing; | |
94 | this.textWidth = textWidth; | |
95 | this.textHeight = textHeight; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Public constructor. | |
100 | * | |
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 | |
106 | */ | |
107 | public SwingSessionInfo(final SwingComponent swing, final int textWidth, | |
108 | final int textHeight, final int width, final int height) { | |
109 | ||
110 | this.swing = swing; | |
111 | this.textWidth = textWidth; | |
112 | this.textHeight = textHeight; | |
113 | this.windowWidth = width; | |
114 | this.windowHeight = height; | |
115 | } | |
116 | ||
117 | // ------------------------------------------------------------------------ | |
118 | // SessionInfo ------------------------------------------------------------ | |
119 | // ------------------------------------------------------------------------ | |
120 | ||
30bd4abd KL |
121 | /** |
122 | * Username getter. | |
123 | * | |
124 | * @return the username | |
125 | */ | |
126 | public String getUsername() { | |
127 | return this.username; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Username setter. | |
132 | * | |
133 | * @param username the value | |
134 | */ | |
135 | public void setUsername(final String username) { | |
136 | this.username = username; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Language getter. | |
141 | * | |
142 | * @return the language | |
143 | */ | |
144 | public String getLanguage() { | |
145 | return this.language; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Language setter. | |
150 | * | |
151 | * @param language the value | |
152 | */ | |
153 | public void setLanguage(final String language) { | |
154 | this.language = language; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Text window width getter. | |
159 | * | |
160 | * @return the window width | |
161 | */ | |
162 | public int getWindowWidth() { | |
163 | return windowWidth; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Text window height getter. | |
168 | * | |
169 | * @return the window height | |
170 | */ | |
171 | public int getWindowHeight() { | |
172 | return windowHeight; | |
173 | } | |
174 | ||
30bd4abd KL |
175 | /** |
176 | * Re-query the text window size. | |
177 | */ | |
178 | public void queryWindowSize() { | |
42873e30 KL |
179 | Insets insets = swing.getInsets(); |
180 | int width = swing.getWidth() - insets.left - insets.right; | |
181 | int height = swing.getHeight() - insets.top - insets.bottom; | |
9696a8f6 KL |
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); | |
30bd4abd KL |
190 | windowWidth = width / textWidth; |
191 | windowHeight = height / textHeight; | |
192 | ||
193 | /* | |
194 | System.err.printf("queryWindowSize(): frame %d %d window %d %d\n", | |
42873e30 | 195 | swing.getWidth(), swing.getHeight(), |
30bd4abd | 196 | windowWidth, windowHeight); |
42873e30 | 197 | */ |
30bd4abd KL |
198 | } |
199 | ||
d36057df KL |
200 | // ------------------------------------------------------------------------ |
201 | // SwingSessionInfo ------------------------------------------------------- | |
202 | // ------------------------------------------------------------------------ | |
203 | ||
204 | /** | |
205 | * Set the dimensions of a single text cell. | |
206 | * | |
207 | * @param textWidth the width of a cell in pixels | |
208 | * @param textHeight the height of a cell in pixels | |
209 | */ | |
210 | public void setTextCellDimensions(final int textWidth, | |
211 | final int textHeight) { | |
212 | ||
213 | this.textWidth = textWidth; | |
214 | this.textHeight = textHeight; | |
215 | } | |
216 | ||
a2855f1d KL |
217 | /** |
218 | * Getter for the underlying Swing component. | |
219 | * | |
220 | * @return the SwingComponent | |
221 | */ | |
222 | public SwingComponent getSwingComponent() { | |
223 | return swing; | |
224 | } | |
225 | ||
30bd4abd | 226 | } |