#34 call onResize() when window size is changed
[nikiroo-utils.git] / src / jexer / backend / SwingSessionInfo.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.backend;
30
31 import java.awt.Insets;
32
33 /**
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.
37 */
38 public class SwingSessionInfo implements SessionInfo {
39
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
43
44 /**
45 * The Swing JFrame or JComponent.
46 */
47 private SwingComponent swing;
48
49 /**
50 * The width of a text cell in pixels.
51 */
52 private int textWidth = 10;
53
54 /**
55 * The height of a text cell in pixels.
56 */
57 private int textHeight = 10;
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 */
72 private int windowWidth = 80;
73
74 /**
75 * Text window height.
76 */
77 private int windowHeight = 25;
78
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
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
175 /**
176 * Re-query the text window size.
177 */
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 windowWidth = width / textWidth;
183 windowHeight = height / textHeight;
184
185 /*
186 System.err.printf("queryWindowSize(): frame %d %d window %d %d\n",
187 swing.getWidth(), swing.getHeight(),
188 windowWidth, windowHeight);
189 */
190
191 }
192
193 // ------------------------------------------------------------------------
194 // SwingSessionInfo -------------------------------------------------------
195 // ------------------------------------------------------------------------
196
197 /**
198 * Set the dimensions of a single text cell.
199 *
200 * @param textWidth the width of a cell in pixels
201 * @param textHeight the height of a cell in pixels
202 */
203 public void setTextCellDimensions(final int textWidth,
204 final int textHeight) {
205
206 this.textWidth = textWidth;
207 this.textHeight = textHeight;
208 }
209
210 /**
211 * Getter for the underlying Swing component.
212 *
213 * @return the SwingComponent
214 */
215 public SwingComponent getSwingComponent() {
216 return swing;
217 }
218
219 }