New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
index 8aeac19f27002419035c58ba6218cf36a692bc82..fc6a198feda3dd316c77cd4c415d766a2e886af0 100644 (file)
@@ -1,16 +1,17 @@
 package be.nikiroo.jvcard.tui.panes;
 
 import java.awt.Image;
-import java.util.Base64;
+import java.io.ByteArrayInputStream;
 import java.util.LinkedList;
 import java.util.List;
 
-import javax.swing.ImageIcon;
+import javax.imageio.ImageIO;
+import javax.xml.bind.DatatypeConverter;
 
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
-import be.nikiroo.jvcard.i18n.Trans;
+import be.nikiroo.jvcard.resources.Trans;
 import be.nikiroo.jvcard.tui.ImageTextControl;
 import be.nikiroo.jvcard.tui.KeyAction;
 import be.nikiroo.jvcard.tui.KeyAction.DataType;
@@ -62,19 +63,16 @@ public class ContactDetails extends MainContent {
 
        /**
         * Change the enclosed {@link Contact} from this {@link ContactDetails}.
+        * Also re-set the image.
         * 
         * @param contact
         *            the new {@link Contact}
         */
        public void setContact(Contact contact) {
-               if (this.contact == contact)
-                       return;
-
                this.contact = contact;
+               image = null;
 
-               if (contact == null) {
-                       image = null;
-               } else {
+               if (contact != null) {
                        infoPanel.removeAllComponents();
 
                        String name = contact.getPreferredDataValue("FN");
@@ -106,22 +104,28 @@ public class ContactDetails extends MainContent {
                        Data photo = contact.getPreferredData("PHOTO");
                        if (photo != null) {
                                TypeInfo encoding = null;
-                               TypeInfo type = null;
                                for (int index = 0; index < photo.size(); index++) {
                                        TypeInfo info = photo.get(index);
                                        if (info.getName() != null) {
                                                if (info.getName().equalsIgnoreCase("ENCODING"))
                                                        encoding = info;
-                                               if (info.getName().equalsIgnoreCase("TYPE"))
-                                                       type = info;
+                                               // We don't check for the "TYPE" anymore, we just defer
+                                               // it to ImageIcon
                                        }
                                }
 
                                if (encoding != null && encoding.getValue() != null
                                                && encoding.getValue().equalsIgnoreCase("b")) {
 
-                                       image = new ImageIcon(Base64.getDecoder().decode(
-                                                       photo.getValue())).getImage();
+                                       try {
+                                               image = ImageIO.read(new ByteArrayInputStream(
+                                                               DatatypeConverter.parseBase64Binary(photo
+                                                                               .getValue())));
+                                               image.toString();
+                                       } catch (Exception e) {
+                                               System.err.println("Cannot parse image for contact: "
+                                                               + contact.getPreferredDataValue("UID"));
+                                       }
                                }
                        }
                }
@@ -170,6 +174,14 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
+               // TODO: add "normal" edit and remove this one into RAW edit
+               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'e',
+                               Trans.StringId.KEY_ACTION_EDIT_CONTACT) {
+                       @Override
+                       public Object getObject() {
+                               return contact;
+                       }
+               });
 
                return actions;
        }