retrofit from gjexer
[nikiroo-utils.git] / src / jexer / TFontChooserWindow.java
CommitLineData
e23ea538
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 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 */
29package jexer;
30
31import java.awt.Font;
32import java.awt.GraphicsEnvironment;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
36import java.util.ResourceBundle;
37
38import jexer.backend.SwingTerminal;
39import jexer.bits.CellAttributes;
40import jexer.bits.GraphicsChars;
41import jexer.event.TKeypressEvent;
42import static jexer.TKeypress.*;
43
44/**
45 * TFontChooserWindow provides an easy UI for users to alter the running
46 * font.
47 *
48 */
49public class TFontChooserWindow extends TWindow {
50
51 /**
52 * Translated strings.
53 */
54 private static final ResourceBundle i18n = ResourceBundle.getBundle(TFontChooserWindow.class.getName());
55
56 // ------------------------------------------------------------------------
57 // Variables --------------------------------------------------------------
58 // ------------------------------------------------------------------------
59
60 /**
61 * The Swing screen.
62 */
63 private SwingTerminal terminal = null;
64
65 /**
66 * The font name.
67 */
68 private TComboBox fontName;
69
70 /**
71 * The font size.
72 */
73 private TField fontSize;
74
75 /**
76 * The X text adjustment.
77 */
78 private TField textAdjustX;
79
80 /**
81 * The Y text adjustment.
82 */
83 private TField textAdjustY;
84
85 /**
86 * The height text adjustment.
87 */
88 private TField textAdjustHeight;
89
90 /**
91 * The width text adjustment.
92 */
93 private TField textAdjustWidth;
94
95 /**
96 * The original font size.
97 */
98 private int oldFontSize = 20;
99
100 /**
101 * The original font.
102 */
103 private Font oldFont = null;
104
105 /**
106 * The original text adjust X value.
107 */
108 private int oldTextAdjustX = 0;
109
110 /**
111 * The original text adjust Y value.
112 */
113 private int oldTextAdjustY = 0;
114
115 /**
116 * The original text adjust height value.
117 */
118 private int oldTextAdjustHeight = 0;
119
120 /**
121 * The original text adjust width value.
122 */
123 private int oldTextAdjustWidth = 0;
124
125 // ------------------------------------------------------------------------
126 // Constructors -----------------------------------------------------------
127 // ------------------------------------------------------------------------
128
129 /**
130 * Public constructor. The window will be centered on screen.
131 *
132 * @param application the TApplication that manages this window
133 */
134 public TFontChooserWindow(final TApplication application) {
135
136 // Register with the TApplication
137 super(application, i18n.getString("windowTitle"), 0, 0, 60, 18, MODAL);
138
139 // Add shortcut text
140 newStatusBar(i18n.getString("statusBar"));
141
142 if (getScreen() instanceof SwingTerminal) {
143 terminal = (SwingTerminal) getScreen();
144 }
145
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);
152
153 int col = 18;
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);
162 } else {
163 oldFont = terminal.getFont();
164 oldFontSize = terminal.getFontSize();
165 oldTextAdjustX = terminal.getTextAdjustX();
166 oldTextAdjustY = terminal.getTextAdjustY();
167 oldTextAdjustHeight = terminal.getTextAdjustHeight();
168 oldTextAdjustWidth = terminal.getTextAdjustWidth();
169
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,
176 new TAction() {
177 public void DO() {
178 if (fontName.getText().equals(i18n.
179 getString("builtInTerminus"))) {
180
181 terminal.setDefaultFont();
182 } else {
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()));
195 }
196 }
197 }
198 );
199
200 // Font size
201 fontSize = addField(col, 2, 3, true,
202 Integer.toString(terminal.getFontSize()),
203 new TAction() {
204 public void DO() {
205 int currentSize = terminal.getFontSize();
206 int newSize = currentSize;
207 try {
208 newSize = Integer.parseInt(fontSize.getText());
209 } catch (NumberFormatException e) {
210 fontSize.setText(Integer.toString(currentSize));
211 }
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()));
222 }
223 }
224 },
225 null);
226
227 addSpinner(col + 3, 2,
228 new TAction() {
229 public void DO() {
230 int currentSize = terminal.getFontSize();
231 int newSize = currentSize;
232 try {
233 newSize = Integer.parseInt(fontSize.getText());
234 newSize++;
235 } catch (NumberFormatException e) {
236 fontSize.setText(Integer.toString(currentSize));
237 }
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()));
249 }
250 }
251 },
252 new TAction() {
253 public void DO() {
254 int currentSize = terminal.getFontSize();
255 int newSize = currentSize;
256 try {
257 newSize = Integer.parseInt(fontSize.getText());
258 newSize--;
259 } catch (NumberFormatException e) {
260 fontSize.setText(Integer.toString(currentSize));
261 }
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()));
273 }
274 }
275 }
276 );
277
278 // textAdjustX
279 textAdjustX = addField(col, 4, 3, true,
280 Integer.toString(terminal.getTextAdjustX()),
281 new TAction() {
282 public void DO() {
283 int currentAdjust = terminal.getTextAdjustX();
284 int newAdjust = currentAdjust;
285 try {
286 newAdjust = Integer.parseInt(textAdjustX.getText());
287 } catch (NumberFormatException e) {
288 textAdjustX.setText(Integer.toString(currentAdjust));
289 }
290 if (newAdjust != currentAdjust) {
291 terminal.setTextAdjustX(newAdjust);
292 }
293 }
294 },
295 null);
296
297 addSpinner(col + 3, 4,
298 new TAction() {
299 public void DO() {
300 int currentAdjust = terminal.getTextAdjustX();
301 int newAdjust = currentAdjust;
302 try {
303 newAdjust = Integer.parseInt(textAdjustX.getText());
304 newAdjust++;
305 } catch (NumberFormatException e) {
306 textAdjustX.setText(Integer.toString(currentAdjust));
307 }
308 textAdjustX.setText(Integer.toString(newAdjust));
309 if (newAdjust != currentAdjust) {
310 terminal.setTextAdjustX(newAdjust);
311 }
312 }
313 },
314 new TAction() {
315 public void DO() {
316 int currentAdjust = terminal.getTextAdjustX();
317 int newAdjust = currentAdjust;
318 try {
319 newAdjust = Integer.parseInt(textAdjustX.getText());
320 newAdjust--;
321 } catch (NumberFormatException e) {
322 textAdjustX.setText(Integer.toString(currentAdjust));
323 }
324 textAdjustX.setText(Integer.toString(newAdjust));
325 if (newAdjust != currentAdjust) {
326 terminal.setTextAdjustX(newAdjust);
327 }
328 }
329 }
330 );
331
332 // textAdjustY
333 textAdjustY = addField(col, 5, 3, true,
334 Integer.toString(terminal.getTextAdjustY()),
335 new TAction() {
336 public void DO() {
337 int currentAdjust = terminal.getTextAdjustY();
338 int newAdjust = currentAdjust;
339 try {
340 newAdjust = Integer.parseInt(textAdjustY.getText());
341 } catch (NumberFormatException e) {
342 textAdjustY.setText(Integer.toString(currentAdjust));
343 }
344 if (newAdjust != currentAdjust) {
345 terminal.setTextAdjustY(newAdjust);
346 }
347 }
348 },
349 null);
350
351 addSpinner(col + 3, 5,
352 new TAction() {
353 public void DO() {
354 int currentAdjust = terminal.getTextAdjustY();
355 int newAdjust = currentAdjust;
356 try {
357 newAdjust = Integer.parseInt(textAdjustY.getText());
358 newAdjust++;
359 } catch (NumberFormatException e) {
360 textAdjustY.setText(Integer.toString(currentAdjust));
361 }
362 textAdjustY.setText(Integer.toString(newAdjust));
363 if (newAdjust != currentAdjust) {
364 terminal.setTextAdjustY(newAdjust);
365 }
366 }
367 },
368 new TAction() {
369 public void DO() {
370 int currentAdjust = terminal.getTextAdjustY();
371 int newAdjust = currentAdjust;
372 try {
373 newAdjust = Integer.parseInt(textAdjustY.getText());
374 newAdjust--;
375 } catch (NumberFormatException e) {
376 textAdjustY.setText(Integer.toString(currentAdjust));
377 }
378 textAdjustY.setText(Integer.toString(newAdjust));
379 if (newAdjust != currentAdjust) {
380 terminal.setTextAdjustY(newAdjust);
381 }
382 }
383 }
384 );
385
386 // textAdjustHeight
387 textAdjustHeight = addField(col, 6, 3, true,
388 Integer.toString(terminal.getTextAdjustHeight()),
389 new TAction() {
390 public void DO() {
391 int currentAdjust = terminal.getTextAdjustHeight();
392 int newAdjust = currentAdjust;
393 try {
394 newAdjust = Integer.parseInt(textAdjustHeight.getText());
395 } catch (NumberFormatException e) {
396 textAdjustHeight.setText(Integer.toString(currentAdjust));
397 }
398 if (newAdjust != currentAdjust) {
399 terminal.setTextAdjustHeight(newAdjust);
400 }
401 }
402 },
403 null);
404
405 addSpinner(col + 3, 6,
406 new TAction() {
407 public void DO() {
408 int currentAdjust = terminal.getTextAdjustHeight();
409 int newAdjust = currentAdjust;
410 try {
411 newAdjust = Integer.parseInt(textAdjustHeight.getText());
412 newAdjust++;
413 } catch (NumberFormatException e) {
414 textAdjustHeight.setText(Integer.toString(currentAdjust));
415 }
416 textAdjustHeight.setText(Integer.toString(newAdjust));
417 if (newAdjust != currentAdjust) {
418 terminal.setTextAdjustHeight(newAdjust);
419 }
420 }
421 },
422 new TAction() {
423 public void DO() {
424 int currentAdjust = terminal.getTextAdjustHeight();
425 int newAdjust = currentAdjust;
426 try {
427 newAdjust = Integer.parseInt(textAdjustHeight.getText());
428 newAdjust--;
429 } catch (NumberFormatException e) {
430 textAdjustHeight.setText(Integer.toString(currentAdjust));
431 }
432 textAdjustHeight.setText(Integer.toString(newAdjust));
433 if (newAdjust != currentAdjust) {
434 terminal.setTextAdjustHeight(newAdjust);
435 }
436 }
437 }
438 );
439
440 // textAdjustWidth
441 textAdjustWidth = addField(col, 7, 3, true,
442 Integer.toString(terminal.getTextAdjustWidth()),
443 new TAction() {
444 public void DO() {
445 int currentAdjust = terminal.getTextAdjustWidth();
446 int newAdjust = currentAdjust;
447 try {
448 newAdjust = Integer.parseInt(textAdjustWidth.getText());
449 } catch (NumberFormatException e) {
450 textAdjustWidth.setText(Integer.toString(currentAdjust));
451 }
452 if (newAdjust != currentAdjust) {
453 terminal.setTextAdjustWidth(newAdjust);
454 }
455 }
456 },
457 null);
458
459 addSpinner(col + 3, 7,
460 new TAction() {
461 public void DO() {
462 int currentAdjust = terminal.getTextAdjustWidth();
463 int newAdjust = currentAdjust;
464 try {
465 newAdjust = Integer.parseInt(textAdjustWidth.getText());
466 newAdjust++;
467 } catch (NumberFormatException e) {
468 textAdjustWidth.setText(Integer.toString(currentAdjust));
469 }
470 textAdjustWidth.setText(Integer.toString(newAdjust));
471 if (newAdjust != currentAdjust) {
472 terminal.setTextAdjustWidth(newAdjust);
473 }
474 }
475 },
476 new TAction() {
477 public void DO() {
478 int currentAdjust = terminal.getTextAdjustWidth();
479 int newAdjust = currentAdjust;
480 try {
481 newAdjust = Integer.parseInt(textAdjustWidth.getText());
482 newAdjust--;
483 } catch (NumberFormatException e) {
484 textAdjustWidth.setText(Integer.toString(currentAdjust));
485 }
486 textAdjustWidth.setText(Integer.toString(newAdjust));
487 if (newAdjust != currentAdjust) {
488 terminal.setTextAdjustWidth(newAdjust);
489 }
490 }
491 }
492 );
493
494 }
495
496 addButton(i18n.getString("okButton"), 18, getHeight() - 4,
497 new TAction() {
498 public void DO() {
499 // Close window.
500 TFontChooserWindow.this.close();
501 }
502 });
503
504 TButton cancelButton = addButton(i18n.getString("cancelButton"),
505 30, getHeight() - 4,
506 new TAction() {
507 public void DO() {
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);
516 }
517 TFontChooserWindow.this.close();
518 }
519 });
520
521 // Save this for last: make the cancel button default action.
522 activate(cancelButton);
523
524 }
525
526 // ------------------------------------------------------------------------
527 // Event handlers ---------------------------------------------------------
528 // ------------------------------------------------------------------------
529
530 /**
531 * Handle keystrokes.
532 *
533 * @param keypress keystroke event
534 */
535 @Override
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);
543 }
544 getApplication().closeWindow(this);
545 return;
546 }
547
548 // Pass to my parent
549 super.onKeypress(keypress);
550 }
551
552 // ------------------------------------------------------------------------
553 // TWindow ----------------------------------------------------------------
554 // ------------------------------------------------------------------------
555
556 /**
557 * Draw me on screen.
558 */
559 @Override
560 public void draw() {
561 super.draw();
562
563 int left = 34;
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);
569 }
570
571 }
572
573 // ------------------------------------------------------------------------
574 // TFontChooserWindow -----------------------------------------------------
575 // ------------------------------------------------------------------------
576
577}