SPARK-307 Chat History not showing this week.

git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@4548 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Derek DeMoro
2006-07-18 17:13:23 +00:00
committed by derek
parent 0274a7934e
commit 719514a63d
7 changed files with 173 additions and 249 deletions

View File

@ -58,8 +58,8 @@ public class AccountCreationWizard extends JPanel {
private JDialog dialog;
private boolean registered;
private XMPPConnection con = null;
private JProgressBar bar;
private XMPPConnection connection = null;
private JProgressBar progressBar;
public AccountCreationWizard() {
@ -85,11 +85,11 @@ public class AccountCreationWizard extends JPanel {
add(serverLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
add(serverField, new GridBagConstraints(1, 3, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
bar = new JProgressBar();
progressBar = new JProgressBar();
add(bar, new GridBagConstraints(1, 4, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
bar.setVisible(false);
add(progressBar, new GridBagConstraints(1, 4, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
progressBar.setVisible(false);
add(createAccountButton, new GridBagConstraints(2, 5, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
@ -126,7 +126,7 @@ public class AccountCreationWizard extends JPanel {
return serverField.getText();
}
public boolean passwordsMatch() {
public boolean isPasswordValid() {
return getPassword().equals(getConfirmPassword());
}
@ -151,7 +151,7 @@ public class AccountCreationWizard extends JPanel {
errors = true;
errorMessage = "Please specify the server to create the account on.";
}
else if (!passwordsMatch()) {
else if (!isPasswordValid()) {
errors = true;
errorMessage = "The passwords do not match. Please confirm passwords.";
}
@ -162,10 +162,10 @@ public class AccountCreationWizard extends JPanel {
}
final Component ui = this;
bar.setIndeterminate(true);
bar.setStringPainted(true);
bar.setString("Registering with " + getServer() + ". Please wait...");
bar.setVisible(true);
progressBar.setIndeterminate(true);
progressBar.setStringPainted(true);
progressBar.setString("Registering with " + getServer() + ". Please wait...");
progressBar.setVisible(true);
final SwingWorker worker = new SwingWorker() {
int errorCode;
@ -173,13 +173,13 @@ public class AccountCreationWizard extends JPanel {
public Object construct() {
try {
createAccountButton.setEnabled(false);
con = getConnection();
connection = getConnection();
}
catch (XMPPException e) {
return e;
}
try {
final AccountManager accountManager = new AccountManager(con);
final AccountManager accountManager = new AccountManager(connection);
accountManager.createAccount(getUsername(), getPassword());
}
catch (XMPPException e) {
@ -189,8 +189,8 @@ public class AccountCreationWizard extends JPanel {
}
public void finished() {
bar.setVisible(false);
if (con == null) {
progressBar.setVisible(false);
if (connection == null) {
if (ui.isShowing()) {
createAccountButton.setEnabled(true);
JOptionPane.showMessageDialog(ui, "Unable to connect to " + getServer() + ".", "Account Creation Problem", JOptionPane.ERROR_MESSAGE);
@ -243,7 +243,7 @@ public class AccountCreationWizard extends JPanel {
private XMPPConnection getConnection() throws XMPPException {
LocalPreferences localPref = SettingsManager.getLocalPreferences();
XMPPConnection con = null;
XMPPConnection con;
// Get connection
@ -256,7 +256,7 @@ public class AccountCreationWizard extends JPanel {
String portString = serverName.substring(checkForPort + 1);
if (ModelUtil.hasLength(portString)) {
// Set new port.
port = Integer.valueOf(portString).intValue();
port = Integer.valueOf(portString);
}
}

View File

@ -23,16 +23,6 @@ import org.jivesoftware.sparkimpl.settings.JiveInfo;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import org.jivesoftware.sparkimpl.updater.CheckUpdates;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -42,11 +32,20 @@ import java.awt.event.WindowFocusListener;
import java.io.IOException;
import java.sql.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
/**
* The <code>MainWindow</code> class acts as both the DockableHolder and the proxy
* to the Workspace in Spark.
@ -76,8 +75,6 @@ public final class MainWindow extends JFrame implements ActionListener {
private static MainWindow singleton;
private static final Object LOCK = new Object();
private String title;
/**
* Returns the singleton instance of <CODE>MainWindow</CODE>,
* creating it if necessary.
@ -107,8 +104,6 @@ public final class MainWindow extends JFrame implements ActionListener {
* @param icon the icon used in the frame.
*/
private MainWindow(String title, ImageIcon icon) {
this.title = title;
// Initialize and dock the menus
configureMenu();
@ -167,7 +162,7 @@ public final class MainWindow extends JFrame implements ActionListener {
}
});
this.addWindowFocusListener(new Focuser());
this.addWindowFocusListener(new MainWindowFocusListener());
}
/**
@ -195,10 +190,10 @@ public final class MainWindow extends JFrame implements ActionListener {
* has been activated.
*/
private void fireWindowActivated() {
final Iterator iter = listeners.iterator();
while (iter.hasNext()) {
((MainWindowListener)iter.next()).mainWindowActivated();
for (MainWindowListener listener : listeners) {
listener.mainWindowActivated();
}
if (Spark.isMac()) {
setJMenuBar(mainWindowBar);
}
@ -209,9 +204,8 @@ public final class MainWindow extends JFrame implements ActionListener {
* is shutting down.
*/
private void fireWindowShutdown() {
final Iterator iter = listeners.iterator();
while (iter.hasNext()) {
((MainWindowListener)iter.next()).shutdown();
for (MainWindowListener listener : listeners) {
listener.shutdown();
}
}
@ -381,8 +375,8 @@ public final class MainWindow extends JFrame implements ActionListener {
}
});
int numberOfMillisecondsInTheFuture = 15000; // 15 sec
Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);
int delay = 15000; // 15 sec
Date timeToRun = new Date(System.currentTimeMillis() + delay);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@ -432,8 +426,7 @@ public final class MainWindow extends JFrame implements ActionListener {
return focused;
}
private class Focuser implements WindowFocusListener {
long timer;
private class MainWindowFocusListener implements WindowFocusListener {
public void windowGainedFocus(WindowEvent e) {
focused = true;
@ -490,17 +483,12 @@ public final class MainWindow extends JFrame implements ActionListener {
}
}
public String getMainWindowTitle() {
return title;
}
/**
* Displays the About Box for Spark.
*/
public void showAboutBox() {
private static void showAboutBox() {
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Default.getString(Default.APPLICATION_NAME) + " " + JiveInfo.getVersion(),
"About", JOptionPane.INFORMATION_MESSAGE);
"About", JOptionPane.INFORMATION_MESSAGE);
}
}

View File

@ -71,7 +71,7 @@ public class ChatContainer extends SparkTabbedPane implements MessageListener, C
* List of all ChatRoom Listeners.
*/
private final List chatRoomListeners = new ArrayList();
private final List chatRoomList = new ArrayList();
private final List<ChatRoom> chatRoomList = new ArrayList<ChatRoom>();
private final Map presenceMap = new HashMap();
private static final String WELCOME_TITLE = SparkRes.getString(SparkRes.WELCOME);
@ -994,7 +994,7 @@ public class ChatContainer extends SparkTabbedPane implements MessageListener, C
}
public Collection getChatRooms() {
public Collection<ChatRoom> getChatRooms() {
return chatRoomList;
}

View File

@ -17,38 +17,47 @@ import org.jivesoftware.smackx.packet.Time;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.borders.PartialLineBorder;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.profile.VCardManager;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* UI to display VCard Information in Wizards, Dialogs, Chat Rooms and any other container.
*
* @author Derek DeMoro
*/
public class VCardPanel extends JPanel {
private ChatRoomImpl chatRoom;
private JLabel avatarImage;
private final String jid;
private final JLabel avatarImage;
public VCardPanel(final ChatRoomImpl chatRoom) {
/**
* Generate a VCard Panel using the specified jid.
* @param jid the jid to use when retrieving the vcard information.
*/
public VCardPanel(final String jid) {
setLayout(new GridBagLayout());
setOpaque(false);
this.chatRoom = chatRoom;
this.jid = jid;
avatarImage = new JLabel();
SwingWorker worker = new SwingWorker() {
VCard vcard = null;
public Object construct() {
vcard = SparkManager.getVCardManager().getVCard(chatRoom.getParticipantJID());
vcard = SparkManager.getVCardManager().getVCard(jid);
return vcard;
}
@ -70,6 +79,7 @@ public class VCardPanel extends JPanel {
setupUI(vcard);
}
catch (Exception e) {
Log.error(e);
}
}
}
@ -104,7 +114,7 @@ public class VCardPanel extends JPanel {
usernameLabel.setText(firstName + " " + lastName);
}
else {
usernameLabel.setText(chatRoom.getTabTitle());
usernameLabel.setText(jid);
}
@ -136,7 +146,7 @@ public class VCardPanel extends JPanel {
final Time time = new Time();
time.setType(IQ.Type.GET);
String fullJID = SparkManager.getUserManager().getFullJID(chatRoom.getParticipantJID());
String fullJID = SparkManager.getUserManager().getFullJID(jid);
if (fullJID == null) {
return;
}

View File

@ -208,7 +208,7 @@ public class ChatRoomImpl extends ChatRoom {
SparkManager.getMessageEventManager().addMessageEventRequestListener(messageEventRequestListener);
// Add VCard Panel
final VCardPanel vcardPanel = new VCardPanel(this);
final VCardPanel vcardPanel = new VCardPanel(getParticipantJID());
getToolBar().add(vcardPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
}

View File

@ -14,18 +14,39 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* A single chat transcript mapped to one JID.
*
* @author Derek DeMoro
*/
public class ChatTranscript {
private final List messages = new ArrayList();
private final List<HistoryMessage> messages = new ArrayList<HistoryMessage>();
/**
* Add a <code>HistoryMessage</code> to users chat transcript.
*
* @param entry the HistoryMessage to add.
*/
public void addHistoryMessage(HistoryMessage entry) {
messages.add(entry);
}
public Collection getMessages() {
/**
* Returns all messages.
*
* @return all messages.
*/
public List<HistoryMessage> getMessages() {
return messages;
}
/**
* Returns a specified number of messages.
*
* @param number the number of messages to return.
* @return the specified number of messages, or all messages if number is greater than the current amount.
*/
public Collection getNumberOfEntries(int number) {
int listSize = messages.size();
@ -37,4 +58,6 @@ public class ChatTranscript {
return messages.subList(start, listSize);
}
}
}

View File

@ -16,25 +16,36 @@ import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.spark.component.panes.CollapsiblePane;
import org.jivesoftware.spark.component.panes.CollapsiblePaneListener;
import org.jivesoftware.spark.component.BackgroundPanel;
import org.jivesoftware.spark.plugin.ContextMenuListener;
import org.jivesoftware.spark.ui.ChatContainer;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomButton;
import org.jivesoftware.spark.ui.ChatRoomListener;
import org.jivesoftware.spark.ui.ContactItem;
import org.jivesoftware.spark.ui.ContactList;
import org.jivesoftware.spark.ui.TranscriptWindow;
import org.jivesoftware.spark.ui.VCardPanel;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.SwingWorker;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
@ -46,34 +57,12 @@ import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* The <code>ChatTranscriptPlugin</code> is responsible for transcript handling within Spark.
*
* @author Derek DeMoro
*/
public class ChatTranscriptPlugin implements ChatRoomListener {
private String LAST_YEAR = "LAST_YEAR";
private String LAST_MONTH = "LAST_MONTH";
private String LAST_WEEK = "LAST_WEEK";
private String LAST_TWO_MONTHS = "LAST_TWO_MONTHS";
private String LAST_SIX_MONTHS = "LAST_SIX_MONTHS";
private String BEFORE = "BEFORE";
/**
* Register the listeners for transcript persistence.
@ -138,11 +127,7 @@ public class ChatTranscriptPlugin implements ChatRoomListener {
}
public void persistConversations() {
ChatContainer chatRooms = SparkManager.getChatManager().getChatContainer();
Collection rooms = chatRooms.getChatRooms();
Iterator iter = rooms.iterator();
while (iter.hasNext()) {
ChatRoom room = (ChatRoom)iter.next();
for (ChatRoom room : SparkManager.getChatManager().getChatContainer().getChatRooms()) {
if (room instanceof ChatRoomImpl) {
ChatRoomImpl roomImpl = (ChatRoomImpl)room;
if (roomImpl.isActive()) {
@ -234,12 +219,12 @@ public class ChatTranscriptPlugin implements ChatRoomListener {
}
}
return new Boolean(isNew);
return Boolean.valueOf(isNew);
}
public void finished() {
Boolean boo = (Boolean)get();
if (boo.booleanValue()) {
if (boo) {
StyledDocument doc = (StyledDocument)roomWindow.getDocument();
final SimpleAttributeSet styles = new SimpleAttributeSet();
StyleConstants.setFontSize(styles, 12);
@ -329,173 +314,91 @@ public class ChatTranscriptPlugin implements ChatRoomListener {
}
private void showHistory(final String jid) {
final Map dateMap = new LinkedHashMap();
SwingWorker worker = new SwingWorker() {
final TranscriptWindow pane = new TranscriptWindow();
SwingWorker transcriptLoader = new SwingWorker() {
public Object construct() {
ChatTranscript transcript = ChatTranscripts.getChatTranscript(jid);
Iterator messages = transcript.getMessages().iterator();
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
cal.add(Calendar.DATE, -7);
Date lastWeek = cal.getTime();
cal.setTime(now);
cal.add(Calendar.MONTH, -1);
Date lastMonth = cal.getTime();
cal.setTime(now);
cal.add(Calendar.MONTH, -2);
Date twoMonths = cal.getTime();
cal.setTime(now);
cal.add(Calendar.MONTH, -6);
Date sixMonths = cal.getTime();
cal.setTime(now);
cal.add(Calendar.YEAR, -1);
Date lastYear = cal.getTime();
while (messages.hasNext()) {
HistoryMessage message = (HistoryMessage)messages.next();
Date datePosted = message.getDate();
String key = "";
if (datePosted.getTime() > lastWeek.getTime()) {
// Add to last week
key = "This Week";
}
else if (datePosted.getTime() > lastMonth.getTime()) {
// Add to last month
key = "One Month";
}
else if (datePosted.getTime() > twoMonths.getTime()) {
// Add to two months
key = "Two Months";
}
else if (datePosted.getTime() > sixMonths.getTime()) {
// Add to six months
key = "Six Months";
}
else if (datePosted.getTime() > lastYear.getTime()) {
// Add to last year.
key = "Year Ago";
}
else {
// Add to before.
key = "More than a year ago";
}
if (!dateMap.containsKey(key)) {
final List list = new ArrayList();
list.add(message);
dateMap.put(key, list);
}
else {
final List list = (ArrayList)dateMap.get(key);
list.add(message);
}
}
return pane;
return ChatTranscripts.getChatTranscript(jid);
}
public void finished() {
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
mainPanel.setBackground(Color.white);
JScrollPane scrollPane = new JScrollPane(mainPanel);
final JPanel mainPanel = new BackgroundPanel();
mainPanel.setLayout(new BorderLayout());
final VCardPanel topPanel = new VCardPanel(jid);
mainPanel.add(topPanel, BorderLayout.NORTH);
final TranscriptWindow window = new TranscriptWindow();
final JScrollPane pane = new JScrollPane(window);
pane.getVerticalScrollBar().setBlockIncrement(50);
pane.getVerticalScrollBar().setUnitIncrement(20);
mainPanel.add(pane, BorderLayout.CENTER);
ChatTranscript transcript = (ChatTranscript)get();
List<HistoryMessage> list = transcript.getMessages();
Collections.sort(list, dateComparator);
for (HistoryMessage message : list) {
String from = message.getFrom();
String nickname = StringUtils.parseName(from);
final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy h:mm a");
final String date = formatter.format(message.getDate());
String prefix = nickname + " [" + date + "]";
if (from.equals(SparkManager.getSessionManager().getJID())) {
window.insertCustomMessage(prefix, message.getBody());
}
else {
window.insertCustomOtherMessage(prefix, message.getBody());
}
// Iterate through and print dates
final Iterator d = dateMap.keySet().iterator();
List list = new ArrayList();
while (d.hasNext()) {
list.add(d.next());
}
Iterator dates = ModelUtil.reverseListIterator(list.listIterator());
while (dates.hasNext()) {
final String date = (String)dates.next();
// Create collapsible pane
final CollapsiblePane pane = new CollapsiblePane(date);
pane.setCollapsed(true);
mainPanel.add(pane);
pane.addCollapsiblePaneListener(new CollapsiblePaneListener() {
boolean expanded = false;
public void paneExpanded() {
if (!expanded) {
List messages = (List)dateMap.get(date);
final TranscriptWindow window = new TranscriptWindow() {
public Dimension getPreferredSize() {
final Dimension size = super.getPreferredSize();
size.width = 0;
return size;
}
};
Iterator messageIter = messages.iterator();
while (messageIter.hasNext()) {
HistoryMessage m = (HistoryMessage)messageIter.next();
String from = m.getFrom();
String nickname = StringUtils.parseName(from);
final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy h:mm a");
final String date = formatter.format(m.getDate());
String prefix = nickname + " [" + date + "]";
if (from.equals(SparkManager.getSessionManager().getJID())) {
window.insertCustomMessage(prefix, m.getBody());
}
else {
window.insertCustomOtherMessage(prefix, m.getBody());
}
}
pane.setContentPane(window);
expanded = true;
}
}
public void paneCollapsed() {
}
});
// Handle no history
if (transcript.getMessages().size() == 0) {
window.setText("There is no current chat history.");
}
final JFrame frame = new JFrame("History For " + jid);
frame.setIconImage(SparkRes.getImageIcon(SparkRes.HISTORY_16x16).getImage());
frame.getContentPane().setLayout(new BorderLayout());
scrollPane.getVerticalScrollBar().setBlockIncrement(50);
scrollPane.getVerticalScrollBar().setUnitIncrement(20);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setSize(600, 400);
window.setCaretPosition(0);
GraphicUtils.centerWindowOnScreen(frame);
frame.setVisible(true);
}
};
worker.start();
transcriptLoader.start();
}
/**
* Sort HistoryMessages by date.
*/
final Comparator<HistoryMessage> dateComparator = new Comparator() {
public int compare(Object o1, Object o2) {
final HistoryMessage historyMessageOne = (HistoryMessage)o1;
final HistoryMessage historyMessageTwo = (HistoryMessage)o2;
long time1 = historyMessageOne.getDate().getTime();
long time2 = historyMessageTwo.getDate().getTime();
if (time1 < time2) {
return 1;
}
else if (time1 > time2) {
return -1;
}
return 0;
}
};
}