KeyAction management now more generic
[jvcard.git] / src / be / nikiroo / jvcard / tui / ImageTextControl.java
index 4ee35e97ba34bb95e8c1b1b7b9dafcde56edca43..33773b2dc280a2962e1d66c2312ca827d9621143 100644 (file)
@@ -9,11 +9,26 @@ 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()) {
@@ -27,11 +42,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 || !UiColors.getInstance().isUnicode())
                        return false;
 
                Mode[] modes = Mode.values();
@@ -39,25 +60,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);
                }
        }