X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FContact.java;h=553ca7680f486f34b1a38e9753dff0b4f06edb99;hb=296a0b75515b3a7424b98292c87cbbf2272b73f9;hp=21ed69b86f66e532adee8bc06a5bf317d00f9021;hpb=0b0b2b0ff1f5e21f7b0feb955b4b54855fb3d508;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Contact.java b/src/be/nikiroo/jvcard/Contact.java index 21ed69b..553ca76 100644 --- a/src/be/nikiroo/jvcard/Contact.java +++ b/src/be/nikiroo/jvcard/Contact.java @@ -7,6 +7,7 @@ import java.util.Map; import be.nikiroo.jvcard.parsers.Format; import be.nikiroo.jvcard.parsers.Parser; +import be.nikiroo.jvcard.tui.StringUtils; /** * A contact is the information that represent a contact person or organisation. @@ -155,7 +156,7 @@ public class Contact { * @return the {@link String} representation */ public String toString(String format) { - return toString(format, "|", null, -1); + return toString(format, "|", null, -1, true, false); } /** @@ -181,13 +182,17 @@ public class Contact { * @param width * a fixed width or -1 for "as long as needed" * + * @param unicode + * allow Uniode or only ASCII characters + * * @return the {@link String} representation */ public String toString(String format, String separator, String padding, - int width) { + int width, boolean unicode, boolean removeAccents) { StringBuilder builder = new StringBuilder(); - for (String str : toStringArray(format, separator, padding, width)) { + for (String str : toStringArray(format, separator, padding, width, + unicode)) { builder.append(str); } @@ -217,10 +222,13 @@ public class Contact { * @param width * a fixed width or -1 for "as long as needed" * + * @param unicode + * allow Uniode or only ASCII characters + * * @return the {@link String} representation */ public String[] toStringArray(String format, String separator, - String padding, int width) { + String padding, int width, boolean unicode) { if (width > -1) { int numOfFields = format.split("\\|").length; if (separator != null) @@ -235,7 +243,7 @@ public class Contact { List str = new LinkedList(); boolean first = true; - for (String s : toStringArray(format, width)) { + for (String s : toStringArray(format, width, unicode)) { if (!first) { str.add(separator); } @@ -269,10 +277,12 @@ public class Contact { * the format to use * @param width * a fixed width or -1 for "as long as needed" - * + * @param unicode + * allow Uniode or only ASCII characters + * * @return the {@link String} representation */ - public String[] toStringArray(String format, int width) { + public String[] toStringArray(String format, int width, boolean unicode) { List str = new LinkedList(); String[] formatFields = format.split("\\|"); @@ -286,7 +296,7 @@ public class Contact { for (int i = 0; i < formatFields.length; i++) { str.add(""); } - + return str.toArray(new String[] {}); } @@ -318,11 +328,14 @@ public class Contact { } String value = getPreferredDataValue(field); - if (value == null) + if (value == null) { value = ""; + } else { + value = StringUtils.sanitize(value, unicode); + } if (size > -1) { - value = fixedString(value, size); + value = StringUtils.padString(value, size); } expandedFields[i] = expand; @@ -371,11 +384,13 @@ public class Contact { for (int i = 0; i < values.length; i++) { if (expandedFields[i]) { if (remainder > 0) { - values[i] = values[i] + fixedString("", remainder); + values[i] = values[i] + + StringUtils.padString("", remainder); remainder = 0; } if (padPerItem > 0) { - values[i] = values[i] + fixedString("", padPerItem); + values[i] = values[i] + + StringUtils.padString("", padPerItem); } } } @@ -392,30 +407,6 @@ public class Contact { return str.toArray(new String[] {}); } - /** - * Fix the size of the given {@link String} either with space-padding or by - * shortening it. - * - * @param string - * the {@link String} to fix - * @param size - * the size of the resulting {@link String} - * - * @return the fixed {@link String} of size size - */ - static private String fixedString(String string, int size) { - int length = string.length(); - - if (length > size) { - string = string.substring(0, size); - } else if (length < size) { - string = string - + new String(new char[size - length]).replace('\0', ' '); - } - - return string; - } - /** * Add a {@link String} to the given {@link List}, but make sure it does not * exceed the maximum size, and truncate it if needed to fit. @@ -526,10 +517,41 @@ public class Contact { this.parent.setDirty(); } - public void setParent(Card parent) { + void setParent(Card parent) { this.parent = parent; for (Data data : datas) { data.setParent(this); } } + + /** + * Delete this {@link Contact} from its parent {@link Card} if any. + * + * @return TRUE in case of success + */ + public boolean delete() { + if (parent != null) { + List list = parent.getContactsList(); + for (int i = 0; i < list.size(); i++) { + if (list.get(i) == this) { + list.remove(i); + parent.setDirty(); + return true; + } + } + } + + return false; + } + + /** + * Notify this element and all its descendants that it is in pristine + * state (as opposed to dirty). + */ + void setPristine() { + dirty = false; + for (Data data : datas) { + data.setPristine(); + } + } }