KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > quickmessage > SimpleHistory


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.quickmessage;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.lucane.common.ConnectInfo;
26
27 public class SimpleHistory
28 {
29     private static HashMap JavaDoc histories = new HashMap JavaDoc();
30     
31     public static void add(ConnectInfo friend, String JavaDoc user, String JavaDoc date, String JavaDoc message)
32     {
33         ArrayList JavaDoc history = (ArrayList JavaDoc)histories.get(friend.getName());
34         if(history == null)
35         {
36             history = new ArrayList JavaDoc();
37             histories.put(friend.getName(), history);
38         }
39         
40         HistoryItem item = new HistoryItem(user, date, message);
41         history.add(item);
42         if(history.size() > 5)
43             history.remove(0);
44     }
45     
46     public static String JavaDoc toHtml(ConnectInfo friend)
47     {
48         ArrayList JavaDoc history = (ArrayList JavaDoc)histories.get(friend.getName());
49         if(history == null)
50             return "";
51             
52         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
53
54         Iterator JavaDoc items = history.iterator();
55         while(items.hasNext())
56         {
57             HistoryItem item = (HistoryItem)items.next();
58             html.append(QuickMessage.formatMessage(item.user, item.date, item.message));
59             if(items.hasNext())
60                 html.append("<hr size='1' noshade>");
61         }
62         
63         return html.toString();
64     }
65 }
66
67 class HistoryItem
68 {
69     String JavaDoc user;
70     String JavaDoc date;
71     String JavaDoc message;
72     
73     public HistoryItem(String JavaDoc user, String JavaDoc date, String JavaDoc message)
74     {
75         this.user = user;
76         this.date = date;
77         this.message = message;
78     }
79     
80     public String JavaDoc toString()
81     {
82         return "-" + user + ":" + date + "-";
83     }
84 }
Popular Tags