001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.gui.util;
018:
019: import java.util.Vector;
020:
021: import org.columba.ristretto.message.Address;
022:
023: /**
024: * An HTML link renderer for the Address class The addresses are rendered as
025: * HTML links: <code>
026: * <A HREF="mailto:[email-address]">[display-name]</A<
027: * </code>
028: *
029: * @author Timo Stich <tstich@users.sourceforge.net>
030: */
031: public class AddressListRenderer {
032:
033: /**
034: * Makes sure that noone creates instances of this class. This is a helper
035: * class, the static methods should be used instead.
036: */
037: private AddressListRenderer() {
038: }
039:
040: /**
041: * Returns a string buffer with the addresses in HTML links. The addresses
042: * will be put into HTML link: <code>
043: * <A HREF="mailto:[email-address]">[display-name]</A<
044: * </code>
045: *
046: * @param addresses
047: * addresses to render as HTML links.
048: * @return a String buffer.
049: */
050: public static String renderToHTMLWithLinks(Address[] addresses) {
051: StringBuffer result = new StringBuffer();
052:
053: if ((addresses != null) && (addresses.length > 0)) {
054: result.append(createAddressHTMLString(addresses[0]));
055:
056: for (int i = 1; i < addresses.length; i++) {
057: // result.append(", ");
058: result.append(createAddressHTMLString(addresses[i]));
059: }
060: }
061:
062: return result.toString();
063: }
064:
065: public static String[] convertToStringArray(Address[] addresses) {
066: Vector vector = new Vector();
067:
068: if ((addresses != null) && (addresses.length > 0)) {
069: //String str = createAddressString(addresses[0]);
070: //vector.add(str);
071:
072: for (int i = 0; i < addresses.length; i++) {
073: // result.append(", ");
074: String str = createAddressString(addresses[i]);
075: vector.add(str);
076: }
077: }
078:
079: return (String[]) vector.toArray(new String[0]);
080: }
081:
082: /**
083: * Adds the address to the string buffer as a HTML link.
084: *
085: * @param address
086: * the address to render as a HTML link.
087: * @param result
088: * the string buffer to add the HTML link to.
089: */
090: private static String createAddressHTMLString(Address address) {
091: StringBuffer result = new StringBuffer();
092: //tstich: Someone added <html> tags here. This is not the right place!
093: result.append("<A HREF=\"mailto:");
094: if (address.getDisplayName().length() != 0) {
095: result.append(address.getDisplayName());
096: result.append(" ");
097: result.append("<" + address.getMailAddress() + ">");
098:
099: } else
100: result.append(address.getMailAddress());
101:
102: result.append("\">");
103:
104: if (address.getDisplayName().length() != 0) {
105:
106: result.append(address.getDisplayName());
107: result.append(" ");
108: result.append("<" + address.getMailAddress() + ">");
109:
110: } else
111: result.append(address.getShortAddress());
112:
113: result.append("</A>");
114:
115: return result.toString();
116: }
117:
118: private static String createAddressString(Address address) {
119: StringBuffer result = new StringBuffer();
120:
121: if (address.getDisplayName().length() != 0) {
122: result.append(address.getDisplayName());
123: result.append(" ");
124: result.append("<" + address.getMailAddress() + ">");
125:
126: } else
127: result.append(address.getMailAddress());
128:
129: return result.toString();
130: }
131:
132: }
|