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
.ECMA48Terminal
;
39 import jexer
.backend
.SwingTerminal
;
40 import jexer
.bits
.CellAttributes
;
41 import jexer
.bits
.GraphicsChars
;
42 import jexer
.event
.TKeypressEvent
;
43 import static jexer
.TKeypress
.*;
46 * TFontChooserWindow provides an easy UI for users to alter the running
50 public class TFontChooserWindow
extends TWindow
{
55 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TFontChooserWindow
.class.getName());
57 // ------------------------------------------------------------------------
58 // Variables --------------------------------------------------------------
59 // ------------------------------------------------------------------------
64 private SwingTerminal terminal
= null;
69 private ECMA48Terminal ecmaTerminal
= null;
74 private TComboBox fontName
;
79 private TField fontSize
;
82 * The X text adjustment.
84 private TField textAdjustX
;
87 * The Y text adjustment.
89 private TField textAdjustY
;
92 * The height text adjustment.
94 private TField textAdjustHeight
;
97 * The width text adjustment.
99 private TField textAdjustWidth
;
102 * The sixel palette size.
104 private TComboBox sixelPaletteSize
;
107 * The original font size.
109 private int oldFontSize
= 20;
114 private Font oldFont
= null;
117 * The original text adjust X value.
119 private int oldTextAdjustX
= 0;
122 * The original text adjust Y value.
124 private int oldTextAdjustY
= 0;
127 * The original text adjust height value.
129 private int oldTextAdjustHeight
= 0;
132 * The original text adjust width value.
134 private int oldTextAdjustWidth
= 0;
137 * The original sixel palette (number of colors) value.
139 private int oldSixelPaletteSize
= 1024;
141 // ------------------------------------------------------------------------
142 // Constructors -----------------------------------------------------------
143 // ------------------------------------------------------------------------
146 * Public constructor. The window will be centered on screen.
148 * @param application the TApplication that manages this window
150 public TFontChooserWindow(final TApplication application
) {
152 // Register with the TApplication
153 super(application
, i18n
.getString("windowTitle"), 0, 0, 60, 21, MODAL
);
156 newStatusBar(i18n
.getString("statusBar"));
158 if (getScreen() instanceof SwingTerminal
) {
159 terminal
= (SwingTerminal
) getScreen();
161 if (getScreen() instanceof ECMA48Terminal
) {
162 ecmaTerminal
= (ECMA48Terminal
) getScreen();
165 addLabel(i18n
.getString("fontName"), 1, 1, "ttext", false);
166 addLabel(i18n
.getString("fontSize"), 1, 2, "ttext", false);
167 addLabel(i18n
.getString("textAdjustX"), 1, 4, "ttext", false);
168 addLabel(i18n
.getString("textAdjustY"), 1, 5, "ttext", false);
169 addLabel(i18n
.getString("textAdjustHeight"), 1, 6, "ttext", false);
170 addLabel(i18n
.getString("textAdjustWidth"), 1, 7, "ttext", false);
171 addLabel(i18n
.getString("sixelPaletteSize"), 1, 9, "ttext", false);
174 if (terminal
== null) {
175 // Non-Swing case: we can't change anything
176 addLabel(i18n
.getString("unavailable"), col
, 1);
177 addLabel(i18n
.getString("unavailable"), col
, 2);
178 addLabel(i18n
.getString("unavailable"), col
, 4);
179 addLabel(i18n
.getString("unavailable"), col
, 5);
180 addLabel(i18n
.getString("unavailable"), col
, 6);
181 addLabel(i18n
.getString("unavailable"), col
, 7);
183 if (ecmaTerminal
== null) {
184 addLabel(i18n
.getString("unavailable"), col
, 9);
186 if (ecmaTerminal
!= null) {
187 oldSixelPaletteSize
= ecmaTerminal
.getSixelPaletteSize();
189 String
[] sixelSizes
= { "2", "256", "512", "1024", "2048" };
190 List
<String
> sizes
= new ArrayList
<String
>();
191 sizes
.addAll(Arrays
.asList(sixelSizes
));
192 sixelPaletteSize
= addComboBox(col
, 9, 10, sizes
, 0, 6,
196 ecmaTerminal
.setSixelPaletteSize(Integer
.parseInt(
197 sixelPaletteSize
.getText()));
198 } catch (NumberFormatException e
) {
204 sixelPaletteSize
.setText(Integer
.toString(oldSixelPaletteSize
));
207 if (terminal
!= null) {
208 oldFont
= terminal
.getFont();
209 oldFontSize
= terminal
.getFontSize();
210 oldTextAdjustX
= terminal
.getTextAdjustX();
211 oldTextAdjustY
= terminal
.getTextAdjustY();
212 oldTextAdjustHeight
= terminal
.getTextAdjustHeight();
213 oldTextAdjustWidth
= terminal
.getTextAdjustWidth();
215 String
[] fontNames
= GraphicsEnvironment
.
216 getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
217 List
<String
> fonts
= new ArrayList
<String
>();
218 fonts
.add(0, i18n
.getString("builtInTerminus"));
219 fonts
.addAll(Arrays
.asList(fontNames
));
220 fontName
= addComboBox(col
, 1, 25, fonts
, 0, 10,
223 if (fontName
.getText().equals(i18n
.
224 getString("builtInTerminus"))) {
226 terminal
.setDefaultFont();
228 terminal
.setFont(new Font(fontName
.getText(),
229 Font
.PLAIN
, terminal
.getFontSize()));
230 fontSize
.setText(Integer
.toString(
231 terminal
.getFontSize()));
232 textAdjustX
.setText(Integer
.toString(
233 terminal
.getTextAdjustX()));
234 textAdjustY
.setText(Integer
.toString(
235 terminal
.getTextAdjustY()));
236 textAdjustHeight
.setText(Integer
.toString(
237 terminal
.getTextAdjustHeight()));
238 textAdjustWidth
.setText(Integer
.toString(
239 terminal
.getTextAdjustWidth()));
246 fontSize
= addField(col
, 2, 3, true,
247 Integer
.toString(terminal
.getFontSize()),
250 int currentSize
= terminal
.getFontSize();
251 int newSize
= currentSize
;
253 newSize
= Integer
.parseInt(fontSize
.getText());
254 } catch (NumberFormatException e
) {
255 fontSize
.setText(Integer
.toString(currentSize
));
257 if (newSize
!= currentSize
) {
258 terminal
.setFontSize(newSize
);
259 textAdjustX
.setText(Integer
.toString(
260 terminal
.getTextAdjustX()));
261 textAdjustY
.setText(Integer
.toString(
262 terminal
.getTextAdjustY()));
263 textAdjustHeight
.setText(Integer
.toString(
264 terminal
.getTextAdjustHeight()));
265 textAdjustWidth
.setText(Integer
.toString(
266 terminal
.getTextAdjustWidth()));
272 addSpinner(col
+ 3, 2,
275 int currentSize
= terminal
.getFontSize();
276 int newSize
= currentSize
;
278 newSize
= Integer
.parseInt(fontSize
.getText());
280 } catch (NumberFormatException e
) {
281 fontSize
.setText(Integer
.toString(currentSize
));
283 fontSize
.setText(Integer
.toString(newSize
));
284 if (newSize
!= currentSize
) {
285 terminal
.setFontSize(newSize
);
286 textAdjustX
.setText(Integer
.toString(
287 terminal
.getTextAdjustX()));
288 textAdjustY
.setText(Integer
.toString(
289 terminal
.getTextAdjustY()));
290 textAdjustHeight
.setText(Integer
.toString(
291 terminal
.getTextAdjustHeight()));
292 textAdjustWidth
.setText(Integer
.toString(
293 terminal
.getTextAdjustWidth()));
299 int currentSize
= terminal
.getFontSize();
300 int newSize
= currentSize
;
302 newSize
= Integer
.parseInt(fontSize
.getText());
304 } catch (NumberFormatException e
) {
305 fontSize
.setText(Integer
.toString(currentSize
));
307 fontSize
.setText(Integer
.toString(newSize
));
308 if (newSize
!= currentSize
) {
309 terminal
.setFontSize(newSize
);
310 textAdjustX
.setText(Integer
.toString(
311 terminal
.getTextAdjustX()));
312 textAdjustY
.setText(Integer
.toString(
313 terminal
.getTextAdjustY()));
314 textAdjustHeight
.setText(Integer
.toString(
315 terminal
.getTextAdjustHeight()));
316 textAdjustWidth
.setText(Integer
.toString(
317 terminal
.getTextAdjustWidth()));
324 textAdjustX
= addField(col
, 4, 3, true,
325 Integer
.toString(terminal
.getTextAdjustX()),
328 int currentAdjust
= terminal
.getTextAdjustX();
329 int newAdjust
= currentAdjust
;
331 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
332 } catch (NumberFormatException e
) {
333 textAdjustX
.setText(Integer
.toString(currentAdjust
));
335 if (newAdjust
!= currentAdjust
) {
336 terminal
.setTextAdjustX(newAdjust
);
342 addSpinner(col
+ 3, 4,
345 int currentAdjust
= terminal
.getTextAdjustX();
346 int newAdjust
= currentAdjust
;
348 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
350 } catch (NumberFormatException e
) {
351 textAdjustX
.setText(Integer
.toString(currentAdjust
));
353 textAdjustX
.setText(Integer
.toString(newAdjust
));
354 if (newAdjust
!= currentAdjust
) {
355 terminal
.setTextAdjustX(newAdjust
);
361 int currentAdjust
= terminal
.getTextAdjustX();
362 int newAdjust
= currentAdjust
;
364 newAdjust
= Integer
.parseInt(textAdjustX
.getText());
366 } catch (NumberFormatException e
) {
367 textAdjustX
.setText(Integer
.toString(currentAdjust
));
369 textAdjustX
.setText(Integer
.toString(newAdjust
));
370 if (newAdjust
!= currentAdjust
) {
371 terminal
.setTextAdjustX(newAdjust
);
378 textAdjustY
= addField(col
, 5, 3, true,
379 Integer
.toString(terminal
.getTextAdjustY()),
382 int currentAdjust
= terminal
.getTextAdjustY();
383 int newAdjust
= currentAdjust
;
385 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
386 } catch (NumberFormatException e
) {
387 textAdjustY
.setText(Integer
.toString(currentAdjust
));
389 if (newAdjust
!= currentAdjust
) {
390 terminal
.setTextAdjustY(newAdjust
);
396 addSpinner(col
+ 3, 5,
399 int currentAdjust
= terminal
.getTextAdjustY();
400 int newAdjust
= currentAdjust
;
402 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
404 } catch (NumberFormatException e
) {
405 textAdjustY
.setText(Integer
.toString(currentAdjust
));
407 textAdjustY
.setText(Integer
.toString(newAdjust
));
408 if (newAdjust
!= currentAdjust
) {
409 terminal
.setTextAdjustY(newAdjust
);
415 int currentAdjust
= terminal
.getTextAdjustY();
416 int newAdjust
= currentAdjust
;
418 newAdjust
= Integer
.parseInt(textAdjustY
.getText());
420 } catch (NumberFormatException e
) {
421 textAdjustY
.setText(Integer
.toString(currentAdjust
));
423 textAdjustY
.setText(Integer
.toString(newAdjust
));
424 if (newAdjust
!= currentAdjust
) {
425 terminal
.setTextAdjustY(newAdjust
);
432 textAdjustHeight
= addField(col
, 6, 3, true,
433 Integer
.toString(terminal
.getTextAdjustHeight()),
436 int currentAdjust
= terminal
.getTextAdjustHeight();
437 int newAdjust
= currentAdjust
;
439 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
440 } catch (NumberFormatException e
) {
441 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
443 if (newAdjust
!= currentAdjust
) {
444 terminal
.setTextAdjustHeight(newAdjust
);
450 addSpinner(col
+ 3, 6,
453 int currentAdjust
= terminal
.getTextAdjustHeight();
454 int newAdjust
= currentAdjust
;
456 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
458 } catch (NumberFormatException e
) {
459 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
461 textAdjustHeight
.setText(Integer
.toString(newAdjust
));
462 if (newAdjust
!= currentAdjust
) {
463 terminal
.setTextAdjustHeight(newAdjust
);
469 int currentAdjust
= terminal
.getTextAdjustHeight();
470 int newAdjust
= currentAdjust
;
472 newAdjust
= Integer
.parseInt(textAdjustHeight
.getText());
474 } catch (NumberFormatException e
) {
475 textAdjustHeight
.setText(Integer
.toString(currentAdjust
));
477 textAdjustHeight
.setText(Integer
.toString(newAdjust
));
478 if (newAdjust
!= currentAdjust
) {
479 terminal
.setTextAdjustHeight(newAdjust
);
486 textAdjustWidth
= addField(col
, 7, 3, true,
487 Integer
.toString(terminal
.getTextAdjustWidth()),
490 int currentAdjust
= terminal
.getTextAdjustWidth();
491 int newAdjust
= currentAdjust
;
493 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
494 } catch (NumberFormatException e
) {
495 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
497 if (newAdjust
!= currentAdjust
) {
498 terminal
.setTextAdjustWidth(newAdjust
);
504 addSpinner(col
+ 3, 7,
507 int currentAdjust
= terminal
.getTextAdjustWidth();
508 int newAdjust
= currentAdjust
;
510 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
512 } catch (NumberFormatException e
) {
513 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
515 textAdjustWidth
.setText(Integer
.toString(newAdjust
));
516 if (newAdjust
!= currentAdjust
) {
517 terminal
.setTextAdjustWidth(newAdjust
);
523 int currentAdjust
= terminal
.getTextAdjustWidth();
524 int newAdjust
= currentAdjust
;
526 newAdjust
= Integer
.parseInt(textAdjustWidth
.getText());
528 } catch (NumberFormatException e
) {
529 textAdjustWidth
.setText(Integer
.toString(currentAdjust
));
531 textAdjustWidth
.setText(Integer
.toString(newAdjust
));
532 if (newAdjust
!= currentAdjust
) {
533 terminal
.setTextAdjustWidth(newAdjust
);
541 addButton(i18n
.getString("okButton"), 18, getHeight() - 4,
545 TFontChooserWindow
.this.close();
549 TButton cancelButton
= addButton(i18n
.getString("cancelButton"),
553 // Restore old values, then close the window.
554 if (terminal
!= null) {
555 terminal
.setFont(oldFont
);
556 terminal
.setFontSize(oldFontSize
);
557 terminal
.setTextAdjustX(oldTextAdjustX
);
558 terminal
.setTextAdjustY(oldTextAdjustY
);
559 terminal
.setTextAdjustHeight(oldTextAdjustHeight
);
560 terminal
.setTextAdjustWidth(oldTextAdjustWidth
);
562 if (ecmaTerminal
!= null) {
563 ecmaTerminal
.setSixelPaletteSize(oldSixelPaletteSize
);
565 TFontChooserWindow
.this.close();
569 // Save this for last: make the cancel button default action.
570 activate(cancelButton
);
574 // ------------------------------------------------------------------------
575 // Event handlers ---------------------------------------------------------
576 // ------------------------------------------------------------------------
581 * @param keypress keystroke event
584 public void onKeypress(final TKeypressEvent keypress
) {
585 // Escape - behave like cancel
586 if (keypress
.equals(kbEsc
)) {
587 // Restore old values, then close the window.
588 if (terminal
!= null) {
589 terminal
.setFont(oldFont
);
590 terminal
.setFontSize(oldFontSize
);
592 if (ecmaTerminal
!= null) {
593 ecmaTerminal
.setSixelPaletteSize(oldSixelPaletteSize
);
595 getApplication().closeWindow(this);
600 super.onKeypress(keypress
);
603 // ------------------------------------------------------------------------
604 // TWindow ----------------------------------------------------------------
605 // ------------------------------------------------------------------------
615 CellAttributes color
= getTheme().getColor("ttext");
616 drawBox(left
, 6, left
+ 24, 14, color
, color
, 3, false);
617 putStringXY(left
+ 2, 6, i18n
.getString("sample"), color
);
618 for (int i
= 7; i
< 13; i
++) {
619 hLineXY(left
+ 1, i
, 22, GraphicsChars
.HATCH
, color
);
624 // ------------------------------------------------------------------------
625 // TFontChooserWindow -----------------------------------------------------
626 // ------------------------------------------------------------------------