| 1 | package be.nikiroo.jvcard.tui.panes; |
| 2 | |
| 3 | import java.awt.Image; |
| 4 | import java.util.Base64; |
| 5 | import java.util.LinkedList; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import javax.swing.ImageIcon; |
| 9 | |
| 10 | import be.nikiroo.jvcard.Contact; |
| 11 | import be.nikiroo.jvcard.Data; |
| 12 | import be.nikiroo.jvcard.TypeInfo; |
| 13 | import be.nikiroo.jvcard.i18n.Trans; |
| 14 | import be.nikiroo.jvcard.tui.ImageTextControl; |
| 15 | import be.nikiroo.jvcard.tui.KeyAction; |
| 16 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 17 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 18 | |
| 19 | import com.googlecode.lanterna.TerminalSize; |
| 20 | import com.googlecode.lanterna.gui2.BorderLayout; |
| 21 | import com.googlecode.lanterna.gui2.Panel; |
| 22 | import com.googlecode.lanterna.input.KeyType; |
| 23 | |
| 24 | public class ContactDetails extends MainContent { |
| 25 | private Contact contact; |
| 26 | private ImageTextControl txt; |
| 27 | |
| 28 | public ContactDetails(Contact contact) { |
| 29 | this.contact = contact; |
| 30 | |
| 31 | BorderLayout blayout = new BorderLayout(); |
| 32 | setLayoutManager(blayout); |
| 33 | |
| 34 | Panel top = new Panel(); |
| 35 | if (contact != null) { |
| 36 | Data photo = contact.getPreferredData("PHOTO"); |
| 37 | if (photo != null) { |
| 38 | TypeInfo encoding = null; |
| 39 | TypeInfo type = null; |
| 40 | for (TypeInfo info : photo.getTypes()) { |
| 41 | if (info.getName() != null) { |
| 42 | if (info.getName().equalsIgnoreCase("ENCODING")) |
| 43 | encoding = info; |
| 44 | if (info.getName().equalsIgnoreCase("TYPE")) |
| 45 | type = info; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (encoding != null && encoding.getValue() != null |
| 50 | && encoding.getValue().equalsIgnoreCase("b")) { |
| 51 | |
| 52 | Image img = new ImageIcon(Base64.getDecoder().decode( |
| 53 | photo.getValue())).getImage(); |
| 54 | |
| 55 | TerminalSize size = new TerminalSize(40, 20); |
| 56 | size = new TerminalSize(100, 50); |
| 57 | |
| 58 | txt = new ImageTextControl(img, size); |
| 59 | top.addComponent(txt); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | addComponent(top, BorderLayout.Location.TOP); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public DataType getDataType() { |
| 69 | return DataType.DATA; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public List<KeyAction> getKeyBindings() { |
| 74 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 75 | |
| 76 | // TODO |
| 77 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, |
| 78 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { |
| 79 | @Override |
| 80 | public boolean onAction() { |
| 81 | if (txt != null) { |
| 82 | txt.switchMode(); |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | }); |
| 88 | actions.add(new KeyAction(Mode.NONE, 'i', |
| 89 | Trans.StringId.DUMMY) { |
| 90 | @Override |
| 91 | public boolean onAction() { |
| 92 | if (txt != null) { |
| 93 | txt.invertColor(); |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | return actions; |
| 101 | } |
| 102 | } |