Fix bounds check
[nikiroo-utils.git] / src / jexer / backend / SwingSessionInfo.java
CommitLineData
daa4106c 1/*
30bd4abd
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
30bd4abd 5 *
a2018e99 6 * Copyright (C) 2017 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 29package jexer.backend;
30bd4abd 30
30bd4abd
KL
31import 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 */
a4406f4e 38public final class SwingSessionInfo implements SessionInfo {
30bd4abd
KL
39
40 /**
42873e30 41 * The Swing JFrame or JComponent.
30bd4abd 42 */
42873e30 43 private SwingComponent swing;
30bd4abd
KL
44
45 /**
46 * The width of a text cell in pixels.
47 */
42873e30 48 private int textWidth = 10;
30bd4abd
KL
49
50 /**
51 * The height of a text cell in pixels.
52 */
42873e30 53 private int textHeight = 10;
30bd4abd
KL
54
55 /**
56 * User name.
57 */
58 private String username = "";
59
60 /**
61 * Language.
62 */
63 private String language = "en_US";
64
65 /**
66 * Text window width.
67 */
55d2b2c2 68 private int windowWidth = 80;
30bd4abd
KL
69
70 /**
71 * Text window height.
72 */
55d2b2c2 73 private int windowHeight = 25;
30bd4abd
KL
74
75 /**
76 * Username getter.
77 *
78 * @return the username
79 */
80 public String getUsername() {
81 return this.username;
82 }
83
84 /**
85 * Username setter.
86 *
87 * @param username the value
88 */
89 public void setUsername(final String username) {
90 this.username = username;
91 }
92
93 /**
94 * Language getter.
95 *
96 * @return the language
97 */
98 public String getLanguage() {
99 return this.language;
100 }
101
102 /**
103 * Language setter.
104 *
105 * @param language the value
106 */
107 public void setLanguage(final String language) {
108 this.language = language;
109 }
110
111 /**
112 * Text window width getter.
113 *
114 * @return the window width
115 */
116 public int getWindowWidth() {
117 return windowWidth;
118 }
119
120 /**
121 * Text window height getter.
122 *
123 * @return the window height
124 */
125 public int getWindowHeight() {
126 return windowHeight;
127 }
128
42873e30
KL
129 /**
130 * Set the dimensions of a single text cell.
131 *
132 * @param textWidth the width of a cell in pixels
133 * @param textHeight the height of a cell in pixels
134 */
135 public void setTextCellDimensions(final int textWidth,
136 final int textHeight) {
137
138 this.textWidth = textWidth;
139 this.textHeight = textHeight;
140 }
141
30bd4abd
KL
142 /**
143 * Public constructor.
144 *
42873e30 145 * @param swing the Swing JFrame or JComponent
30bd4abd
KL
146 * @param textWidth the width of a cell in pixels
147 * @param textHeight the height of a cell in pixels
148 */
42873e30
KL
149 public SwingSessionInfo(final SwingComponent swing, final int textWidth,
150 final int textHeight) {
30bd4abd 151
42873e30
KL
152 this.swing = swing;
153 this.textWidth = textWidth;
154 this.textHeight = textHeight;
30bd4abd
KL
155 }
156
3e074355
KL
157 /**
158 * Public constructor.
159 *
160 * @param swing the Swing JFrame or JComponent
161 * @param textWidth the width of a cell in pixels
162 * @param textHeight the height of a cell in pixels
163 * @param width the number of columns
164 * @param height the number of rows
165 */
166 public SwingSessionInfo(final SwingComponent swing, final int textWidth,
167 final int textHeight, final int width, final int height) {
168
169 this.swing = swing;
170 this.textWidth = textWidth;
171 this.textHeight = textHeight;
172 this.windowWidth = width;
173 this.windowHeight = height;
174 }
175
30bd4abd
KL
176 /**
177 * Re-query the text window size.
178 */
179 public void queryWindowSize() {
42873e30
KL
180 Insets insets = swing.getInsets();
181 int width = swing.getWidth() - insets.left - insets.right;
182 int height = swing.getHeight() - insets.top - insets.bottom;
30bd4abd
KL
183 windowWidth = width / textWidth;
184 windowHeight = height / textHeight;
185
186 /*
187 System.err.printf("queryWindowSize(): frame %d %d window %d %d\n",
42873e30 188 swing.getWidth(), swing.getHeight(),
30bd4abd 189 windowWidth, windowHeight);
42873e30 190 */
30bd4abd
KL
191
192 }
193
30bd4abd 194}