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]
32 import java
.awt
.GraphicsEnvironment
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Arrays
;
35 import java
.util
.List
;
36 import java
.util
.ResourceBundle
;
38 import jexer
.backend
.SwingTerminal
;
39 import jexer
.bits
.CellAttributes
;
40 import jexer
.bits
.GraphicsChars
;
41 import jexer
.event
.TKeypressEvent
;
42 import static jexer
.TKeypress
.*;
45 * TFontChooserWindow provides an easy UI for users to alter the running
49 public class TFontChooserWindow
extends TWindow
{
54 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TFontChooserWindow
.class.getName());
56 // ------------------------------------------------------------------------
57 // Variables --------------------------------------------------------------
58 // ------------------------------------------------------------------------
63 private SwingTerminal terminal
= null;
68 private TComboBox fontName
;
73 private TField fontSize
;
76 * The X text adjustment.
78 private TField textAdjustX
;
81 * The Y text adjustment.
83 private TField textAdjustY
;
86 * The height text adjustment.
88 private TField textAdjustHeight
;
91 * The width text adjustment.
93 private TField textAdjustWidth
;
96 * The original font size.
98 private int oldFontSize
= 20;
103 private Font oldFont
= null;
106 * The original text adjust X value.
108 private int oldTextAdjustX
= 0;
111 * The original text adjust Y value.
113 private int oldTextAdjustY
= 0;
116 * The original text adjust height value.
118 private int oldTextAdjustHeight
= 0;
121 * The original text adjust width value.
123 private int oldTextAdjustWidth
= 0;
125 // ------------------------------------------------------------------------
126 // Constructors -----------------------------------------------------------
127 // ------------------------------------------------------------------------
130 * Public constructor. The window will be centered on screen.
132 * @param application the TApplication that manages this window
134 public TFontChooserWindow(final TApplication application
) {
136 // Register with the TApplication
137 super(application
, i18n
.getString("windowTitle"), 0, 0, 60, 18, MODAL
);
140 newStatusBar(i18n
.getString("statusBar"));
142 if (getScreen() instanceof SwingTerminal
) {
143 terminal
= (SwingTerminal
) getScreen();
146 addLabel(i18n
.getString("fontName"), 1, 1, "ttext", false);
147 addLabel(i18n
.getString("fontSize"), 1, 2, "ttext", false);
148 addLabel(i18n
.getString("textAdjustX"), 1, 4, "ttext", false);
149 addLabel(i18n
.getString("textAdjustY"), 1, 5, "ttext", false);
150 addLabel(i18n
.getString("textAdjustHeight"), 1, 6, "ttext", false);
151 addLabel(i18n
.getString("textAdjustWidth"), 1, 7, "ttext", false);
154 if (terminal
== null) {
155 // Non-Swing case: we can't change anything
156 addLabel(i18n
.getString("unavailable"), col
, 1);
157 addLabel(i18n
.getString("unavailable"), col
, 2);
158 addLabel(i18n
.getString("unavailable"), col
, 4);
159 addLabel(i18n
.getString("unavailable"), col
, 5);
160 addLabel(i18n
.getString("unavailable"), col
, 6);
161 addLabel(i18n
.getString("unavailable"), col
, 7);
163 oldFont
= terminal
.getFont();
164 oldFontSize
= terminal
.getFontSize();
165 oldTextAdjustX
= terminal
.getTextAdjustX();
166 oldTextAdjustY
= terminal
.getTextAdjustY();
167 oldTextAdjustHeight
= terminal
.getTextAdjustHeight();
168 oldTextAdjustWidth
= terminal
.getTextAdjustWidth();
170 String
[] fontNames
= GraphicsEnvironment
.
171 getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
172 List
<String
> fonts
= new ArrayList
<String
>();
173 fonts
.add(0, i18n
.getString("builtInTerminus"));
174 fonts
.addAll(Arrays
.asList(fontNames
));
175 fontName
= addComboBox(col
, 1, 25, fonts
, 0, 10,
178 if (fontName
.getText().equals(i18n
.
179 getString("builtInTerminus"))) {
181 terminal
.setDefaultFont();
183 terminal
.setFont(new Font(fontName
.getText(),
184 Font
.PLAIN
, terminal
.getFontSize()));
185 fontSize
.setText(Integer
.toString(
186 terminal
.getFontSize()));
187 textAdjustX
.setText(Integer
.toString(
188 terminal
.getTextAdjustX()));
189 textAdjustY
.setText(Integer
.toString(
190 terminal
.getTextAdjustY()));
191 textAdjustHeight
.setText(Integer
.toString(
192 terminal
.getTextAdjustHeight()));
193 textAdjustWidth
.setText(Integer
.toString(
194 terminal
.getTextAdjustWidth()));
201 fontSize
= addField(col
, 2, 3, true,
202 Integer
.toString(terminal
.getFontSize()),
205 int currentSize
= terminal
.getFontSize();
206 int newSize
= currentSize
;
208 newSize
= Integer
.parseInt(fontSize
.getText());
209 } catch (NumberFormatException e
) {
210 fontSize
.setText(Integer
.toString(currentSize
));
212 if (newSize
!= currentSize
) {
213 terminal
.setFontSize(newSize
);
214 textAdjustX
.setText(Integer
.toString(
215 terminal
.getTextAdjustX()));
216 textAdjustY
.setText(Integer
.toString(
217 terminal
.getTextAdjustY()));
218 textAdjustHeight
.setText(Integer
.toString(
219 terminal
.getTextAdjustHeight()));
220 textAdjustWidth
.setText(Integer
.toString(
221 terminal
.getTextAdjustWidth()));
227 addSpinner(col
+ 3, 2,
230 int currentSize
= terminal
.getFontSize();
231 int newSize
= currentSize
;
233 newSize
= Integer
.parseInt(fontSize
.getText());
235 } catch (NumberFormatException e
) {
236 fontSize
.setText(Integer
.toString(currentSize
));
238 fontSize
.setText(Integer
.toString(newSize
));
239 if (newSize
!= currentSize
) {
240 terminal
.setFontSize(newSize
);
241 textAdjustX
.setText(Integer
.toString(
242 terminal
.getTextAdjustX()));
243 textAdjustY
.setText(Integer
.toString(
244 terminal
.getTextAdjustY()));
245 textAdjustHeight
.setText(Integer
.toString(
246 terminal
.getTextAdjustHeight()));
247 textAdjustWidth
.setText(Integer
.toString(
248 terminal
.getTextAdjustWidth()));
254 int currentSize
= terminal
.getFontSize();
255 int newSize
= currentSize
;
257 newSize
= Integer
.parseInt(fontSize
.getText());
259 } catch (NumberFormatException e
) {
260 fontSize
.setText(Integer
.toString(currentSize
));
262 fontSize
.setText(Integer
.toString(newSize
));
263 if (newSize
!= currentSize
) {
264 terminal
.setFontSize(newSize
);
265 textAdjustX
.setText(Integer
.toString(
266 terminal
.getTextAdjustX()));
267 textAdjustY
.setText(Integer
.toString(
268 terminal
.getTextAdjustY()));
269 textAdjustHeight
.setText(Integer
.toString(
270 terminal
.getTextAdjustHeight()));
271 textAdjustWidth
.setText(Integer
.toString(
272 terminal
.getTextAdjustWidth()));
279 textAdjustX
= addField(col
, 4, 3, true,
280 Integer
.toString(terminal
.getTextAdjustX()),
283 int currentAdjust
= terminal
.getTextAdjustX();
284 int newAdjust
= currentAdjust
;
286 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
287 } catch (NumberFormatException e
) {
288 textAdjustX
.setText(Integer
.toString(currentAdjust
));
290 if (newAdjust
!= currentAdjust
) {
291 terminal
.setTextAdjustX(newAdjust
);
297 addSpinner(col
+ 3, 4,
300 int currentAdjust
= terminal
.getTextAdjustX();
301 int newAdjust
= currentAdjust
;
303 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
305 } catch (NumberFormatException e
) {
306 textAdjustX
.setText(Integer
.toString(currentAdjust
));
308 textAdjustX
.setText(Integer
.toString(newAdjust
));
309 if (newAdjust
!= currentAdjust
) {
310 terminal
.setTextAdjustX(newAdjust
);
316 int currentAdjust
= terminal
.getTextAdjustX();
317 int newAdjust
= currentAdjust
;
319 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
321 } catch (NumberFormatException e
) {
322 textAdjustX
.setText(Integer
.toString(currentAdjust
));
324 textAdjustX
.setText(Integer
.toString(newAdjust
));
325 if (newAdjust
!= currentAdjust
) {
326 terminal
.setTextAdjustX(newAdjust
);
333 textAdjustY
= addField(col
, 5, 3, true,
334 Integer
.toString(terminal
.getTextAdjustY()),
337 int currentAdjust
= terminal
.getTextAdjustY();
338 int newAdjust
= currentAdjust
;
340 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
341 } catch (NumberFormatException e
) {
342 textAdjustY
.setText(Integer
.toString(currentAdjust
));
344 if (newAdjust
!= currentAdjust
) {
345 terminal
.setTextAdjustY(newAdjust
);
351 addSpinner(col
+ 3, 5,
354 int currentAdjust
= terminal
.getTextAdjustY();
355 int newAdjust
= currentAdjust
;
357 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
359 } catch (NumberFormatException e
) {
360 textAdjustY
.setText(Integer
.toString(currentAdjust
));
362 textAdjustY
.setText(Integer
.toString(newAdjust
));
363 if (newAdjust
!= currentAdjust
) {
364 terminal
.setTextAdjustY(newAdjust
);
370 int currentAdjust
= terminal
.getTextAdjustY();
371 int newAdjust
= currentAdjust
;
373 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
375 } catch (NumberFormatException e
) {
376 textAdjustY
.setText(Integer
.toString(currentAdjust
));
378 textAdjustY
.setText(Integer
.toString(newAdjust
));
379 if (newAdjust
!= currentAdjust
) {
380 terminal
.setTextAdjustY(newAdjust
);
387 textAdjustHeight
= addField(col
, 6, 3, true,
388 Integer
.toString(terminal
.getTextAdjustHeight()),
391 int currentAdjust
= terminal
.getTextAdjustHeight();
392 int newAdjust
= currentAdjust
;
394 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
395 } catch (NumberFormatException e
) {
396 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
398 if (newAdjust
!= currentAdjust
) {
399 terminal
.setTextAdjustHeight(newAdjust
);
405 addSpinner(col
+ 3, 6,
408 int currentAdjust
= terminal
.getTextAdjustHeight();
409 int newAdjust
= currentAdjust
;
411 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
413 } catch (NumberFormatException e
) {
414 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
416 textAdjustHeight
.setText(Integer
.toString(newAdjust
));
417 if (newAdjust
!= currentAdjust
) {
418 terminal
.setTextAdjustHeight(newAdjust
);
424 int currentAdjust
= terminal
.getTextAdjustHeight();
425 int newAdjust
= currentAdjust
;
427 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
429 } catch (NumberFormatException e
) {
430 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
432 textAdjustHeight
.setText(Integer
.toString(newAdjust
));
433 if (newAdjust
!= currentAdjust
) {
434 terminal
.setTextAdjustHeight(newAdjust
);
441 textAdjustWidth
= addField(col
, 7, 3, true,
442 Integer
.toString(terminal
.getTextAdjustWidth()),
445 int currentAdjust
= terminal
.getTextAdjustWidth();
446 int newAdjust
= currentAdjust
;
448 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
449 } catch (NumberFormatException e
) {
450 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
452 if (newAdjust
!= currentAdjust
) {
453 terminal
.setTextAdjustWidth(newAdjust
);
459 addSpinner(col
+ 3, 7,
462 int currentAdjust
= terminal
.getTextAdjustWidth();
463 int newAdjust
= currentAdjust
;
465 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
467 } catch (NumberFormatException e
) {
468 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
470 textAdjustWidth
.setText(Integer
.toString(newAdjust
));
471 if (newAdjust
!= currentAdjust
) {
472 terminal
.setTextAdjustWidth(newAdjust
);
478 int currentAdjust
= terminal
.getTextAdjustWidth();
479 int newAdjust
= currentAdjust
;
481 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
483 } catch (NumberFormatException e
) {
484 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
486 textAdjustWidth
.setText(Integer
.toString(newAdjust
));
487 if (newAdjust
!= currentAdjust
) {
488 terminal
.setTextAdjustWidth(newAdjust
);
496 addButton(i18n
.getString("okButton"), 18, getHeight() - 4,
500 TFontChooserWindow
.this.close();
504 TButton cancelButton
= addButton(i18n
.getString("cancelButton"),
508 // Restore old values, then close the window.
509 if (terminal
!= null) {
510 terminal
.setFont(oldFont
);
511 terminal
.setFontSize(oldFontSize
);
512 terminal
.setTextAdjustX(oldTextAdjustX
);
513 terminal
.setTextAdjustY(oldTextAdjustY
);
514 terminal
.setTextAdjustHeight(oldTextAdjustHeight
);
515 terminal
.setTextAdjustWidth(oldTextAdjustWidth
);
517 TFontChooserWindow
.this.close();
521 // Save this for last: make the cancel button default action.
522 activate(cancelButton
);
526 // ------------------------------------------------------------------------
527 // Event handlers ---------------------------------------------------------
528 // ------------------------------------------------------------------------
533 * @param keypress keystroke event
536 public void onKeypress(final TKeypressEvent keypress
) {
537 // Escape - behave like cancel
538 if (keypress
.equals(kbEsc
)) {
539 // Restore old values, then close the window.
540 if (terminal
!= null) {
541 terminal
.setFont(oldFont
);
542 terminal
.setFontSize(oldFontSize
);
544 getApplication().closeWindow(this);
549 super.onKeypress(keypress
);
552 // ------------------------------------------------------------------------
553 // TWindow ----------------------------------------------------------------
554 // ------------------------------------------------------------------------
564 CellAttributes color
= getTheme().getColor("ttext");
565 drawBox(left
, 6, left
+ 24, 14, color
, color
, 3, false);
566 putStringXY(left
+ 2, 6, i18n
.getString("sample"), color
);
567 for (int i
= 7; i
< 13; i
++) {
568 hLineXY(left
+ 1, i
, 22, GraphicsChars
.HATCH
, color
);
573 // ------------------------------------------------------------------------
574 // TFontChooserWindow -----------------------------------------------------
575 // ------------------------------------------------------------------------