4ee35e97ba34bb95e8c1b1b7b9dafcde56edca43
[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 public class ImageTextControl extends Panel {
13 private ImageText img;
14 private TextBox txt;
15 private int mode;
16
17 public ImageTextControl(Image image, TerminalSize size) {
18 Mode mode = Mode.DOUBLE_DITHERING;
19 if (!UiColors.getInstance().isUnicode()) {
20 mode = Mode.ASCII;
21 }
22
23 Mode[] modes = Mode.values();
24 for (int i = 0; i < modes.length; i++) {
25 if (mode == modes[i])
26 this.mode = i;
27 }
28
29 this.setLayoutManager(new BorderLayout());
30 setImg(new ImageText(image, size, mode));
31 }
32
33 public boolean switchMode() {
34 if (img == null || !UiColors.getInstance().isUnicode())
35 return false;
36
37 Mode[] modes = Mode.values();
38 mode++;
39 if (mode >= modes.length)
40 mode = 0;
41
42 img.setMode(modes[mode]);
43 setImg(img);
44
45 return true;
46 }
47
48 public void invertColor() {
49 if (img != null) {
50 img.setColorInvert(!img.isColorInvert());
51 setImg(img);
52 }
53 }
54
55 private void setImg(ImageText img) {
56 this.img = img;
57 removeAllComponents();
58 txt = null;
59 if (img != null) {
60 txt = new TextBox(img.getText());
61 this.addComponent(txt, BorderLayout.Location.CENTER);
62 }
63 }
64 }