7457f57a0d73b185f1f618e4a035faade6e7c88b
[fanfix.git] / 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 final class SwingSessionInfo implements SessionInfo {
39
40 /**
41 * The Swing JFrame or JComponent.
42 */
43 private SwingComponent swing;
44
45 /**
46 * The width of a text cell in pixels.
47 */
48 private int textWidth = 10;
49
50 /**
51 * The height of a text cell in pixels.
52 */
53 private int textHeight = 10;
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 */
68 private int windowWidth = 80;
69
70 /**
71 * Text window height.
72 */
73 private int windowHeight = 25;
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
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
142 /**
143 * Public constructor.
144 *
145 * @param swing the Swing JFrame or JComponent
146 * @param textWidth the width of a cell in pixels
147 * @param textHeight the height of a cell in pixels
148 */
149 public SwingSessionInfo(final SwingComponent swing, final int textWidth,
150 final int textHeight) {
151
152 this.swing = swing;
153 this.textWidth = textWidth;
154 this.textHeight = textHeight;
155 }
156
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
176 /**
177 * Re-query the text window size.
178 */
179 public void queryWindowSize() {
180 Insets insets = swing.getInsets();
181 int width = swing.getWidth() - insets.left - insets.right;
182 int height = swing.getHeight() - insets.top - insets.bottom;
183 windowWidth = width / textWidth;
184 windowHeight = height / textHeight;
185
186 /*
187 System.err.printf("queryWindowSize(): frame %d %d window %d %d\n",
188 swing.getWidth(), swing.getHeight(),
189 windowWidth, windowHeight);
190 */
191
192 }
193
194 }