| 1 | package be.nikiroo.jvcard.tui.panes; |
| 2 | |
| 3 | import java.awt.Image; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import be.nikiroo.jvcard.Contact; |
| 8 | import be.nikiroo.jvcard.Data; |
| 9 | import be.nikiroo.jvcard.TypeInfo; |
| 10 | import be.nikiroo.jvcard.resources.StringUtils; |
| 11 | import be.nikiroo.jvcard.resources.bundles.DisplayBundle; |
| 12 | import be.nikiroo.jvcard.resources.enums.DisplayOption; |
| 13 | import be.nikiroo.jvcard.resources.enums.ColorOption; |
| 14 | import be.nikiroo.jvcard.resources.enums.StringId; |
| 15 | import be.nikiroo.jvcard.tui.ImageTextControl; |
| 16 | import be.nikiroo.jvcard.tui.KeyAction; |
| 17 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 18 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 19 | import be.nikiroo.jvcard.tui.UiColors; |
| 20 | |
| 21 | import com.googlecode.lanterna.TerminalSize; |
| 22 | import com.googlecode.lanterna.gui2.BorderLayout; |
| 23 | import com.googlecode.lanterna.gui2.Borders; |
| 24 | import com.googlecode.lanterna.gui2.Direction; |
| 25 | import com.googlecode.lanterna.gui2.Label; |
| 26 | import com.googlecode.lanterna.gui2.LinearLayout; |
| 27 | import com.googlecode.lanterna.gui2.Panel; |
| 28 | import com.googlecode.lanterna.input.KeyType; |
| 29 | |
| 30 | public class ContactDetails extends MainContent { |
| 31 | private Contact contact; |
| 32 | private Panel top; |
| 33 | private ImageTextControl txtImage; |
| 34 | private Image image; |
| 35 | private boolean fullscreenImage; |
| 36 | private Panel infoPanel; |
| 37 | private Label note; |
| 38 | |
| 39 | // from .properties file: |
| 40 | private int labelSize = -1; |
| 41 | private String infoFormat = ""; |
| 42 | |
| 43 | // |
| 44 | |
| 45 | public ContactDetails(Contact contact) { |
| 46 | // Get the .properties info: |
| 47 | DisplayBundle map = new DisplayBundle(); |
| 48 | labelSize = map.getInteger(DisplayOption.CONTACT_DETAILS_LABEL_WIDTH, |
| 49 | -1); |
| 50 | infoFormat = map.getString(DisplayOption.CONTACT_DETAILS_INFO); |
| 51 | // |
| 52 | |
| 53 | BorderLayout blayout = new BorderLayout(); |
| 54 | setLayoutManager(blayout); |
| 55 | |
| 56 | top = new Panel(); |
| 57 | blayout = new BorderLayout(); |
| 58 | top.setLayoutManager(blayout); |
| 59 | |
| 60 | infoPanel = new Panel(); |
| 61 | infoPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL)); |
| 62 | top.addComponent(infoPanel, BorderLayout.Location.CENTER); |
| 63 | |
| 64 | Panel notePanel = new Panel(); |
| 65 | notePanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); |
| 66 | |
| 67 | notePanel.addComponent(UiColors.createLabel( |
| 68 | ColorOption.VIEW_CONTACT_NOTES_TITLE, "Notes:")); |
| 69 | note = UiColors.createLabel(ColorOption.VIEW_CONTACT_NORMAL, ""); |
| 70 | notePanel.addComponent(note); |
| 71 | |
| 72 | setContact(contact); |
| 73 | |
| 74 | addComponent(top.withBorder(Borders.doubleLineBevel()), |
| 75 | BorderLayout.Location.TOP); |
| 76 | addComponent(notePanel.withBorder(Borders.singleLineBevel()), |
| 77 | BorderLayout.Location.CENTER); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Change the enclosed {@link Contact} from this {@link ContactDetails}. |
| 82 | * Also re-set the image. |
| 83 | * |
| 84 | * @param contact |
| 85 | * the new {@link Contact} |
| 86 | */ |
| 87 | public void setContact(Contact contact) { |
| 88 | this.contact = contact; |
| 89 | image = null; |
| 90 | |
| 91 | if (contact != null) { |
| 92 | infoPanel.removeAllComponents(); |
| 93 | |
| 94 | String name = contact.getPreferredDataValue("FN"); |
| 95 | infoPanel.addComponent(UiColors.createLabel( |
| 96 | ColorOption.VIEW_CONTACT_NAME, name)); |
| 97 | infoPanel.addComponent(UiColors.createLabel( |
| 98 | ColorOption.VIEW_CONTACT_NORMAL, "")); |
| 99 | |
| 100 | // List of infos: |
| 101 | String[] infos = infoFormat.split("\\|"); |
| 102 | for (String info : infos) { |
| 103 | // # - "=FIELD" will take the preferred value for this field |
| 104 | // # - "+FIELD" will take the preferred value for this field and |
| 105 | // highlight it |
| 106 | // # - "#FIELD" will take all the values with this field's name |
| 107 | // # - "*FIELD" will take all the values with this field's name, |
| 108 | // highlighting the preferred one |
| 109 | // # |
| 110 | |
| 111 | boolean hl = false; |
| 112 | boolean all = false; |
| 113 | if (info.contains("+") || info.contains("#")) |
| 114 | hl = true; |
| 115 | if (info.contains("*") || info.contains("#")) |
| 116 | all = true; |
| 117 | |
| 118 | if (all || hl || info.contains("=")) { |
| 119 | ColorOption el = hl ? ColorOption.VIEW_CONTACT_HIGHLIGHT |
| 120 | : ColorOption.VIEW_CONTACT_NORMAL; |
| 121 | |
| 122 | int index = info.indexOf('='); |
| 123 | if (index < 0) |
| 124 | index = info.indexOf('+'); |
| 125 | if (index < 0) |
| 126 | index = info.indexOf('#'); |
| 127 | if (index < 0) |
| 128 | index = info.indexOf('*'); |
| 129 | |
| 130 | String label = info.substring(0, index); |
| 131 | String field = info.substring(index + 1); |
| 132 | |
| 133 | if (all) { |
| 134 | Data pref = contact.getPreferredData(field); |
| 135 | for (Data data : contact.getData(field)) { |
| 136 | if (data == pref) { |
| 137 | infoPanel.addComponent(UiColors.createLabel(el, |
| 138 | StringUtils.padString(label, labelSize) |
| 139 | + data.toString())); |
| 140 | } else { |
| 141 | infoPanel.addComponent(UiColors.createLabel( |
| 142 | ColorOption.VIEW_CONTACT_NORMAL, |
| 143 | StringUtils.padString(label, labelSize) |
| 144 | + data.toString())); |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | String val = contact.getPreferredDataValue(field); |
| 149 | if (val == null) |
| 150 | val = ""; |
| 151 | infoPanel.addComponent(UiColors.createLabel(el, |
| 152 | StringUtils.padString(label, labelSize) + val)); |
| 153 | } |
| 154 | } else { |
| 155 | String label = info; |
| 156 | infoPanel.addComponent(UiColors.createLabel( |
| 157 | ColorOption.VIEW_CONTACT_NORMAL, |
| 158 | StringUtils.padString(label, labelSize))); |
| 159 | } |
| 160 | } |
| 161 | // end of list |
| 162 | |
| 163 | infoPanel.addComponent(UiColors.createLabel( |
| 164 | ColorOption.VIEW_CONTACT_NORMAL, "")); |
| 165 | |
| 166 | String notes = contact.getPreferredDataValue("NOTE"); |
| 167 | if (notes == null) |
| 168 | notes = ""; |
| 169 | note.setText(notes); |
| 170 | |
| 171 | Data photo = contact.getPreferredData("PHOTO"); |
| 172 | if (photo != null) { |
| 173 | TypeInfo encoding = null; |
| 174 | for (TypeInfo info : photo) { |
| 175 | if (info.getName() != null) { |
| 176 | if (info.getName().equalsIgnoreCase("ENCODING")) |
| 177 | encoding = info; |
| 178 | // We don't check for the "TYPE" anymore, we just defer |
| 179 | // it to ImageIcon |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (encoding != null && encoding.getValue() != null |
| 184 | && encoding.getValue().equalsIgnoreCase("b")) { |
| 185 | |
| 186 | try { |
| 187 | image = StringUtils.toImage(photo.getValue()); |
| 188 | } catch (Exception e) { |
| 189 | System.err.println("Cannot parse image for contact: " |
| 190 | + contact.getPreferredDataValue("UID")); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | setImage(image); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public DataType getDataType() { |
| 201 | return DataType.DATA; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public List<KeyAction> getKeyBindings() { |
| 206 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 207 | |
| 208 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, |
| 209 | StringId.KEY_ACTION_SWITCH_FORMAT) { |
| 210 | @Override |
| 211 | public boolean onAction() { |
| 212 | if (txtImage != null) { |
| 213 | txtImage.switchMode(); |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | }); |
| 219 | actions.add(new KeyAction(Mode.NONE, 'i', StringId.KEY_ACTION_INVERT) { |
| 220 | @Override |
| 221 | public boolean onAction() { |
| 222 | if (txtImage != null) { |
| 223 | txtImage.invertColor(); |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | }); |
| 229 | actions.add(new KeyAction(Mode.NONE, 'f', |
| 230 | StringId.KEY_ACTION_FULLSCREEN) { |
| 231 | @Override |
| 232 | public boolean onAction() { |
| 233 | fullscreenImage = !fullscreenImage; |
| 234 | setImage(image); |
| 235 | return false; |
| 236 | } |
| 237 | }); |
| 238 | // TODO: add "normal" edit |
| 239 | actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r', |
| 240 | StringId.KEY_ACTION_EDIT_CONTACT_RAW) { |
| 241 | @Override |
| 242 | public Object getObject() { |
| 243 | return contact; |
| 244 | } |
| 245 | }); |
| 246 | |
| 247 | return actions; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public synchronized Panel setSize(TerminalSize size) { |
| 252 | super.setSize(size); |
| 253 | setImage(image); |
| 254 | return this; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Set the {@link Image} to render and refresh it to the current size |
| 259 | * constraints. |
| 260 | * |
| 261 | * @param image |
| 262 | * the new {@link Image} |
| 263 | */ |
| 264 | private void setImage(Image image) { |
| 265 | this.image = image; |
| 266 | |
| 267 | if (txtImage != null && top.containsComponent(txtImage)) |
| 268 | top.removeComponent(txtImage); |
| 269 | |
| 270 | TerminalSize size = getTxtSize(); |
| 271 | if (size != null) { |
| 272 | if (txtImage != null) |
| 273 | txtImage.setSize(size); |
| 274 | else |
| 275 | txtImage = new ImageTextControl(image, size); |
| 276 | } |
| 277 | |
| 278 | if (size != null) { |
| 279 | top.addComponent(txtImage, BorderLayout.Location.LEFT); |
| 280 | } |
| 281 | |
| 282 | invalidate(); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Compute the size to use for the {@link Image} text rendering. Return NULL |
| 287 | * in case of error. |
| 288 | * |
| 289 | * @return the {@link TerminalSize} to use or NULL if it is not possible |
| 290 | */ |
| 291 | private TerminalSize getTxtSize() { |
| 292 | if (image != null && getSize() != null && getSize().getColumns() > 0 |
| 293 | && getSize().getRows() > 0) { |
| 294 | if (fullscreenImage) { |
| 295 | return getSize(); |
| 296 | } else { |
| 297 | // TODO: configure size? |
| 298 | int w = getSize().getColumns() - 40; |
| 299 | int h = getSize().getRows() - 9; |
| 300 | if (w <= 0 || h <= 0) |
| 301 | return null; |
| 302 | |
| 303 | return new TerminalSize(w, h); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return null; |
| 308 | } |
| 309 | } |