New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / ImageTextControl.java
1 package be.nikiroo.jvcard.tui;
2
3 import java.awt.Image;
4
5 import be.nikiroo.jvcard.launcher.Main;
6 import be.nikiroo.jvcard.tui.ImageText.Mode;
7
8 import com.googlecode.lanterna.TerminalSize;
9 import com.googlecode.lanterna.gui2.BorderLayout;
10 import com.googlecode.lanterna.gui2.Panel;
11 import com.googlecode.lanterna.gui2.TextBox;
12
13 /**
14 * A {@link Panel} containing an {@link ImageText} rendering.
15 *
16 * @author niki
17 *
18 */
19 public class ImageTextControl extends Panel {
20 private ImageText image;
21 private TextBox txt;
22 private int mode;
23
24 /**
25 * Create a new {@link ImageTextControl} for the given {@link Image} and
26 * {@link TerminalSize}.
27 *
28 * @param image
29 * the {@link Image} to render
30 * @param size
31 * the target size of this control
32 */
33 public ImageTextControl(Image image, TerminalSize size) {
34 Mode mode = Mode.DOUBLE_DITHERING;
35 if (!Main.isUnicode()) {
36 mode = Mode.ASCII;
37 }
38
39 Mode[] modes = Mode.values();
40 for (int i = 0; i < modes.length; i++) {
41 if (mode == modes[i])
42 this.mode = i;
43 }
44
45 this.setLayoutManager(new BorderLayout());
46 setSize(size);
47 setImage(new ImageText(image, size, mode, false));
48 }
49
50 /**
51 * Cycle through the available rendering modes if possible.
52 *
53 * @return TRUE if it was possible to switch modes
54 */
55 public boolean switchMode() {
56 if (image == null || !Main.isUnicode())
57 return false;
58
59 Mode[] modes = Mode.values();
60 mode++;
61 if (mode >= modes.length)
62 mode = 0;
63
64 image.setMode(modes[mode]);
65 setImage(image);
66
67 return true;
68 }
69
70 /**
71 * Invert the colours.
72 */
73 public void invertColor() {
74 if (image != null) {
75 image.setColorInvert(!image.isColorInvert());
76 setImage(image);
77 }
78 }
79
80 @Override
81 public synchronized Panel setSize(TerminalSize size) {
82 if (image != null)
83 image.setSize(size);
84
85 super.setSize(size);
86
87 setImage(image);
88
89 return this;
90 };
91
92 /**
93 * Set/reset the {@link ImageText} to render.
94 *
95 * @param image
96 * the new {@link ImageText}
97 */
98 private void setImage(ImageText image) {
99 this.image = image;
100 removeAllComponents();
101 txt = null;
102 if (image != null) {
103 txt = new TextBox(image.getText());
104 this.addComponent(txt, BorderLayout.Location.CENTER);
105 }
106 }
107 }