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