X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fpanes%2FContactDetails.java;h=be2c2017a15a82aae009dea6643b55340a51c730;hb=e119a1c1a924998b9315e46c96b1c750aab1deb9;hp=1fad960e46730006273c8ab47943fab1a85eb11d;hpb=bcb54330afff6a443ab43ee3d38cc7f863c701b7;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java index 1fad960..be2c201 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java @@ -1,154 +1,308 @@ package be.nikiroo.jvcard.tui.panes; +import java.awt.Image; import java.util.LinkedList; import java.util.List; -import com.googlecode.lanterna.input.KeyType; - 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.StringUtils; +import be.nikiroo.jvcard.resources.bundles.DisplayBundle; +import be.nikiroo.jvcard.resources.enums.DisplayOption; +import be.nikiroo.jvcard.resources.enums.ColorOption; +import be.nikiroo.jvcard.resources.enums.StringId; +import be.nikiroo.jvcard.tui.ImageTextControl; import be.nikiroo.jvcard.tui.KeyAction; import be.nikiroo.jvcard.tui.KeyAction.DataType; import be.nikiroo.jvcard.tui.KeyAction.Mode; -import be.nikiroo.jvcard.tui.StringUtils; -import be.nikiroo.jvcard.tui.UiColors.Element; +import be.nikiroo.jvcard.tui.UiColors; + +import com.googlecode.lanterna.TerminalSize; +import com.googlecode.lanterna.gui2.BorderLayout; +import com.googlecode.lanterna.gui2.Borders; +import com.googlecode.lanterna.gui2.Direction; +import com.googlecode.lanterna.gui2.Label; +import com.googlecode.lanterna.gui2.LinearLayout; +import com.googlecode.lanterna.gui2.Panel; +import com.googlecode.lanterna.input.KeyType; -public class ContactDetails extends MainContentList { +public class ContactDetails extends MainContent { private Contact contact; - private int mode; + private Panel top; + private ImageTextControl txtImage; + private Image image; + private boolean fullscreenImage; + private Panel infoPanel; + private Label note; + + // from .properties file: + private int labelSize = -1; + private String infoFormat = ""; + + // public ContactDetails(Contact contact) { - super(null, null); + // Get the .properties info: + DisplayBundle map = new DisplayBundle(); + labelSize = map.getInteger(DisplayOption.CONTACT_DETAILS_LABEL_WIDTH, + -1); + infoFormat = map.getString(DisplayOption.CONTACT_DETAILS_INFO); + // - this.contact = contact; - this.mode = 0; + BorderLayout blayout = new BorderLayout(); + setLayoutManager(blayout); - for (int i = 0; i < contact.getContent().size(); i++) { - addItem("[detail line]"); - } + top = new Panel(); + blayout = new BorderLayout(); + top.setLayoutManager(blayout); + + infoPanel = new Panel(); + infoPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL)); + top.addComponent(infoPanel, BorderLayout.Location.CENTER); + + Panel notePanel = new Panel(); + notePanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); + + notePanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NOTES_TITLE, "Notes:")); + note = UiColors.createLabel(ColorOption.VIEW_CONTACT_NORMAL, ""); + notePanel.addComponent(note); + + setContact(contact); + + addComponent(top.withBorder(Borders.doubleLineBevel()), + BorderLayout.Location.TOP); + addComponent(notePanel.withBorder(Borders.singleLineBevel()), + BorderLayout.Location.CENTER); } - @Override - protected List getLabel(int index, int width, boolean selected, - boolean focused) { - // TODO: from ini file? - int SIZE_COL_1 = 15; - - Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED - : Element.CONTACT_LINE; - Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED - : Element.CONTACT_LINE_SEPARATOR; - Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED - : Element.CONTACT_LINE_DIRTY; - - Data data = contact.getContent().get(index); - - List parts = new LinkedList(); - if (data.isDirty()) { - parts.add(new TextPart(" ", el)); - parts.add(new TextPart("*", elDirty)); - } else { - parts.add(new TextPart(" ", elSep)); - } - String name = " " + data.getName() + " "; - String value = null; - - StringBuilder valueBuilder = new StringBuilder(" "); - switch (mode) { - case 0: - valueBuilder.append(data.getValue()); - if (data.getGroup() != null && data.getGroup().length() > 0) { - valueBuilder.append("("); - valueBuilder.append(data.getGroup()); - valueBuilder.append(")"); - } - break; - case 1: - for (TypeInfo type : data.getTypes()) { - if (valueBuilder.length() > 1) - valueBuilder.append(", "); - valueBuilder.append(type.getName()); - valueBuilder.append(": "); - valueBuilder.append(type.getValue()); + /** + * 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) { + this.contact = contact; + image = null; + + if (contact != null) { + infoPanel.removeAllComponents(); + + String name = contact.getPreferredDataValue("FN"); + infoPanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NAME, name)); + infoPanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NORMAL, "")); + + // List of infos: + String[] infos = infoFormat.split("\\|"); + for (String info : infos) { + // # - "=FIELD" will take the preferred value for this field + // # - "+FIELD" will take the preferred value for this field and + // highlight it + // # - "#FIELD" will take all the values with this field's name + // # - "*FIELD" will take all the values with this field's name, + // highlighting the preferred one + // # + + boolean hl = false; + boolean all = false; + if (info.contains("+") || info.contains("#")) + hl = true; + if (info.contains("*") || info.contains("#")) + all = true; + + if (all || hl || info.contains("=")) { + ColorOption el = hl ? ColorOption.VIEW_CONTACT_HIGHLIGHT + : ColorOption.VIEW_CONTACT_NORMAL; + + int index = info.indexOf('='); + if (index < 0) + index = info.indexOf('+'); + if (index < 0) + index = info.indexOf('#'); + if (index < 0) + index = info.indexOf('*'); + + String label = info.substring(0, index); + String field = info.substring(index + 1); + + if (all) { + for (Data data : contact.getData(field)) { + if (data.isPreferred()) { + infoPanel.addComponent(UiColors.createLabel(el, + StringUtils.padString(label, labelSize) + + data.toString())); + } else { + infoPanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NORMAL, + StringUtils.padString(label, labelSize) + + data.toString())); + } + } + } else { + String val = contact.getPreferredDataValue(field); + if (val == null) + val = ""; + infoPanel.addComponent(UiColors.createLabel(el, + StringUtils.padString(label, labelSize) + val)); + } + } else { + String label = info; + infoPanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NORMAL, + StringUtils.padString(label, labelSize))); + } } - break; - } - valueBuilder.append(" "); + // end of list - value = valueBuilder.toString(); + infoPanel.addComponent(UiColors.createLabel( + ColorOption.VIEW_CONTACT_NORMAL, "")); - name = StringUtils.padString(name, SIZE_COL_1); - value = StringUtils.padString(value, width - SIZE_COL_1 - - getSeparator().length() - 2); + String notes = contact.getPreferredDataValue("NOTE"); + if (notes == null) + notes = ""; + note.setText(notes); - parts.add(new TextPart(name, el)); - parts.add(new TextPart(getSeparator(), elSep)); - parts.add(new TextPart(value, el)); + Data photo = contact.getPreferredData("PHOTO"); + if (photo != null) { + TypeInfo encoding = null; + for (TypeInfo info : photo) { + if (info.getName() != null) { + if (info.getName().equalsIgnoreCase("ENCODING")) + encoding = info; + // We don't check for the "TYPE" anymore, we just defer + // it to ImageIcon + } + } - return parts; - }; + if (encoding != null && encoding.getValue() != null + && encoding.getValue().equalsIgnoreCase("b")) { - @Override - public DataType getDataType() { - return DataType.DATA; + try { + image = StringUtils.toImage(photo.getValue()); + } catch (Exception e) { + System.err.println("Cannot parse image for contact: " + + contact.getPreferredDataValue("UID")); + } + } + } + } + + setImage(image); } @Override - public String getExitWarning() { - // TODO Auto-generated method stub - return null; + public DataType getDataType() { + return DataType.DATA; } @Override public List getKeyBindings() { - // TODO Auto-generated method stub List actions = new LinkedList(); - // TODO: add, remove - actions.add(new KeyAction(Mode.EDIT_DETAIL, 'd', Trans.StringId.DUMMY) { + actions.add(new KeyAction(Mode.NONE, KeyType.Tab, + StringId.KEY_ACTION_SWITCH_FORMAT) { @Override - public Object getObject() { - return contact.getContent().get(getSelectedIndex()); + public boolean onAction() { + if (txtImage != null) { + txtImage.switchMode(); + } + + return false; } }); - actions.add(new KeyAction(Mode.NONE, KeyType.Tab, - Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { + actions.add(new KeyAction(Mode.NONE, 'i', StringId.KEY_ACTION_INVERT) { @Override public boolean onAction() { - mode++; - if (mode > 1) - mode = 0; + if (txtImage != null) { + txtImage.invertColor(); + } return false; } }); + actions.add(new KeyAction(Mode.NONE, 'f', + StringId.KEY_ACTION_FULLSCREEN) { + @Override + public boolean onAction() { + fullscreenImage = !fullscreenImage; + setImage(image); + return false; + } + }); + // TODO: add "normal" edit + actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r', + StringId.KEY_ACTION_EDIT_CONTACT_RAW) { + @Override + public Object getObject() { + return contact; + } + }); return actions; } @Override - public Mode getMode() { - return Mode.CONTACT_DETAILS; + public synchronized Panel setSize(TerminalSize size) { + super.setSize(size); + setImage(image); + return this; } - @Override - public String getTitle() { - String title = null; + /** + * Set the {@link Image} to render and refresh it to the current size + * constraints. + * + * @param image + * the new {@link Image} + */ + private void setImage(Image image) { + this.image = image; - if (contact != null) { - title = contact.getPreferredDataValue("FN"); - if (title == null || title.length() == 0) - title = contact.getPreferredDataValue("N"); + if (txtImage != null && top.containsComponent(txtImage)) + top.removeComponent(txtImage); + + TerminalSize size = getTxtSize(); + if (size != null) { + if (txtImage != null) + txtImage.setSize(size); + else + txtImage = new ImageTextControl(image, size); } - return title; + if (size != null) { + top.addComponent(txtImage, BorderLayout.Location.LEFT); + } + + invalidate(); } - @Override - public String move(int x, int y) { - // TODO Auto-generated method stub + /** + * Compute the size to use for the {@link Image} text rendering. Return NULL + * in case of error. + * + * @return the {@link TerminalSize} to use or NULL if it is not possible + */ + private TerminalSize getTxtSize() { + if (image != null && getSize() != null && getSize().getColumns() > 0 + && getSize().getRows() > 0) { + if (fullscreenImage) { + return getSize(); + } else { + // TODO: configure size? + int w = getSize().getColumns() - 40; + int h = getSize().getRows() - 9; + if (w <= 0 || h <= 0) + return null; + + return new TerminalSize(w, h); + } + } + return null; } }