New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / ImageTextControl.java
index 4ee35e97ba34bb95e8c1b1b7b9dafcde56edca43..dc0548260a860e019eef25c5d8e3153545690b78 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.jvcard.tui;
 
 import java.awt.Image;
 
+import be.nikiroo.jvcard.launcher.Main;
 import be.nikiroo.jvcard.tui.ImageText.Mode;
 
 import com.googlecode.lanterna.TerminalSize;
@@ -9,14 +10,29 @@ import com.googlecode.lanterna.gui2.BorderLayout;
 import com.googlecode.lanterna.gui2.Panel;
 import com.googlecode.lanterna.gui2.TextBox;
 
+/**
+ * A {@link Panel} containing an {@link ImageText} rendering.
+ * 
+ * @author niki
+ *
+ */
 public class ImageTextControl extends Panel {
-       private ImageText img;
+       private ImageText image;
        private TextBox txt;
        private int mode;
 
+       /**
+        * Create a new {@link ImageTextControl} for the given {@link Image} and
+        * {@link TerminalSize}.
+        * 
+        * @param image
+        *            the {@link Image} to render
+        * @param size
+        *            the target size of this control
+        */
        public ImageTextControl(Image image, TerminalSize size) {
                Mode mode = Mode.DOUBLE_DITHERING;
-               if (!UiColors.getInstance().isUnicode()) {
+               if (!Main.isUnicode()) {
                        mode = Mode.ASCII;
                }
 
@@ -27,11 +43,17 @@ public class ImageTextControl extends Panel {
                }
 
                this.setLayoutManager(new BorderLayout());
-               setImg(new ImageText(image, size, mode));
+               setSize(size);
+               setImage(new ImageText(image, size, mode, false));
        }
 
+       /**
+        * Cycle through the available rendering modes if possible.
+        * 
+        * @return TRUE if it was possible to switch modes
+        */
        public boolean switchMode() {
-               if (img == null || !UiColors.getInstance().isUnicode())
+               if (image == null || !Main.isUnicode())
                        return false;
 
                Mode[] modes = Mode.values();
@@ -39,25 +61,46 @@ public class ImageTextControl extends Panel {
                if (mode >= modes.length)
                        mode = 0;
 
-               img.setMode(modes[mode]);
-               setImg(img);
+               image.setMode(modes[mode]);
+               setImage(image);
 
                return true;
        }
 
+       /**
+        * Invert the colours.
+        */
        public void invertColor() {
-               if (img != null) {
-                       img.setColorInvert(!img.isColorInvert());
-                       setImg(img);
+               if (image != null) {
+                       image.setColorInvert(!image.isColorInvert());
+                       setImage(image);
                }
        }
 
-       private void setImg(ImageText img) {
-               this.img = img;
+       @Override
+       public synchronized Panel setSize(TerminalSize size) {
+               if (image != null)
+                       image.setSize(size);
+
+               super.setSize(size);
+
+               setImage(image);
+
+               return this;
+       };
+
+       /**
+        * Set/reset the {@link ImageText} to render.
+        * 
+        * @param image
+        *            the new {@link ImageText}
+        */
+       private void setImage(ImageText image) {
+               this.image = image;
                removeAllComponents();
                txt = null;
-               if (img != null) {
-                       txt = new TextBox(img.getText());
+               if (image != null) {
+                       txt = new TextBox(image.getText());
                        this.addComponent(txt, BorderLayout.Location.CENTER);
                }
        }