KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > util > AddressListRenderer


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.gui.util;
18
19 import java.util.Vector JavaDoc;
20
21 import org.columba.ristretto.message.Address;
22
23 /**
24  * An HTML link renderer for the Address class The addresses are rendered as
25  * HTML links: <code>
26  * &lt;A HREF="mailto:[email-address]"&gt;[display-name]&lt;/A&lt;
27  * </code>
28  *
29  * @author Timo Stich <tstich@users.sourceforge.net>
30  */

31 public class AddressListRenderer {
32
33     /**
34      * Makes sure that noone creates instances of this class. This is a helper
35      * class, the static methods should be used instead.
36      */

37     private AddressListRenderer() {
38     }
39
40     /**
41      * Returns a string buffer with the addresses in HTML links. The addresses
42      * will be put into HTML link: <code>
43      * &lt;A HREF="mailto:[email-address]"&gt;[display-name]&lt;/A&lt;
44      * </code>
45      *
46      * @param addresses
47      * addresses to render as HTML links.
48      * @return a String buffer.
49      */

50     public static String JavaDoc renderToHTMLWithLinks(Address[] addresses) {
51         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
52
53         if ((addresses != null) && (addresses.length > 0)) {
54             result.append(createAddressHTMLString(addresses[0]));
55
56             for (int i = 1; i < addresses.length; i++) {
57                 // result.append(", ");
58
result.append(createAddressHTMLString(addresses[i]));
59             }
60         }
61
62         return result.toString();
63     }
64
65     public static String JavaDoc[] convertToStringArray(Address[] addresses) {
66         Vector JavaDoc vector = new Vector JavaDoc();
67
68         if ((addresses != null) && (addresses.length > 0)) {
69             //String str = createAddressString(addresses[0]);
70
//vector.add(str);
71

72             for (int i = 0; i < addresses.length; i++) {
73                 // result.append(", ");
74
String JavaDoc str = createAddressString(addresses[i]);
75                 vector.add(str);
76             }
77         }
78
79         return (String JavaDoc[]) vector.toArray(new String JavaDoc[0]);
80     }
81
82     /**
83      * Adds the address to the string buffer as a HTML link.
84      *
85      * @param address
86      * the address to render as a HTML link.
87      * @param result
88      * the string buffer to add the HTML link to.
89      */

90     private static String JavaDoc createAddressHTMLString(Address address) {
91         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
92         //tstich: Someone added <html> tags here. This is not the right place!
93
result.append("<A HREF=\"mailto:");
94         if (address.getDisplayName().length() != 0) {
95             result.append(address.getDisplayName());
96             result.append(" ");
97             result.append("<" + address.getMailAddress() + ">");
98
99         } 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("&lt;" + address.getMailAddress() + "&gt;");
109
110         } else
111             result.append(address.getShortAddress());
112
113         result.append("</A>");
114
115         return result.toString();
116     }
117
118     private static String JavaDoc createAddressString(Address address) {
119         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 }
Popular Tags