PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spark/src/plugins/fastpath/src/java/org/jivesoftware/fastpath/workspace/panes/ChatHistory.java

https://github.com/joechen2010/IM
Java | 236 lines | 166 code | 44 blank | 26 comment | 12 complexity | 7b034bd2c251432cb92670bab83cbcfe MD5 | raw file
  1. /**
  2. * $RCSfile: ,v $
  3. * $Revision: $
  4. * $Date: $
  5. *
  6. * Copyright (C) 2004-2011 Jive Software. All rights reserved.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package org.jivesoftware.fastpath.workspace.panes;
  21. import java.awt.BorderLayout;
  22. import java.awt.Color;
  23. import java.awt.Dimension;
  24. import java.awt.Font;
  25. import java.awt.GridBagConstraints;
  26. import java.awt.GridBagLayout;
  27. import java.awt.Insets;
  28. import java.awt.event.MouseAdapter;
  29. import java.awt.event.MouseEvent;
  30. import java.util.Collection;
  31. import java.util.Date;
  32. import java.util.Iterator;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.DefaultListModel;
  35. import javax.swing.JFrame;
  36. import javax.swing.JLabel;
  37. import javax.swing.JList;
  38. import javax.swing.JOptionPane;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import org.jivesoftware.fastpath.FastpathPlugin;
  42. import org.jivesoftware.fastpath.FpRes;
  43. import org.jivesoftware.fastpath.resources.FastpathRes;
  44. import org.jivesoftware.smack.XMPPException;
  45. import org.jivesoftware.smack.util.StringUtils;
  46. import org.jivesoftware.smackx.workgroup.agent.AgentSession;
  47. import org.jivesoftware.smackx.workgroup.ext.history.AgentChatHistory;
  48. import org.jivesoftware.smackx.workgroup.ext.history.AgentChatSession;
  49. import org.jivesoftware.smackx.workgroup.packet.Transcript;
  50. import org.jivesoftware.spark.SparkManager;
  51. import org.jivesoftware.spark.util.ModelUtil;
  52. import org.jivesoftware.spark.util.log.Log;
  53. public class ChatHistory extends JPanel {
  54. private static final long serialVersionUID = 1L;
  55. private DefaultListModel model = new DefaultListModel();
  56. private AgentSession agentSession;
  57. private JList list;
  58. private JFrame mainFrame;
  59. private JFrame frame;
  60. public ChatHistory() {
  61. list = new JList(model);
  62. list.setCellRenderer(new HistoryItemRenderer());
  63. final JPanel mainPanel = new JPanel();
  64. mainPanel.setLayout(new BorderLayout());
  65. mainPanel.setBackground(Color.white);
  66. final BackgroundPane titlePane = new BackgroundPane() {
  67. public Dimension getPreferredSize() {
  68. final Dimension size = super.getPreferredSize();
  69. size.width = 0;
  70. return size;
  71. }
  72. };
  73. titlePane.setLayout(new GridBagLayout());
  74. titlePane.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
  75. JLabel userImage = new JLabel();
  76. userImage.setHorizontalAlignment(JLabel.LEFT);
  77. userImage.setText(FpRes.getString("title.previous.chats"));
  78. userImage.setIcon(FastpathRes.getImageIcon(FastpathRes.FASTPATH_IMAGE_24x24));
  79. titlePane.add(userImage, new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
  80. userImage.setFont(new Font("Dialog", Font.BOLD, 12));
  81. mainPanel.add(titlePane, BorderLayout.NORTH);
  82. mainPanel.add(list, BorderLayout.CENTER);
  83. setLayout(new BorderLayout());
  84. add(mainPanel, BorderLayout.CENTER);
  85. init();
  86. list.addMouseListener(new MouseAdapter() {
  87. public void mouseClicked(MouseEvent e) {
  88. if (e.getClickCount() == 2) {
  89. HistoryItem historyItem = (HistoryItem)list.getSelectedValue();
  90. showTranscript(historyItem.getSessionID());
  91. }
  92. }
  93. });
  94. }
  95. public void showDialog() {
  96. AgentSession agentSession = FastpathPlugin.getAgentSession();
  97. String workgroupName = StringUtils.parseName(agentSession.getWorkgroupJID());
  98. if (mainFrame == null) {
  99. mainFrame = new JFrame(FpRes.getString("title.personal.chats"));
  100. }
  101. if (mainFrame.isVisible()) {
  102. return;
  103. }
  104. mainFrame.setIconImage(SparkManager.getMainWindow().getIconImage());
  105. mainFrame.getContentPane().setLayout(new BorderLayout());
  106. final JScrollPane scrollPane = new JScrollPane(this);
  107. scrollPane.getVerticalScrollBar().setBlockIncrement(50);
  108. scrollPane.getVerticalScrollBar().setUnitIncrement(20);
  109. mainFrame.getContentPane().add(scrollPane);
  110. mainFrame.pack();
  111. mainFrame.setSize(400, 400);
  112. mainFrame.setLocationRelativeTo(SparkManager.getMainWindow());
  113. mainFrame.setVisible(true);
  114. }
  115. public void init() {
  116. model.removeAllElements();
  117. AgentChatHistory history = null;
  118. agentSession = FastpathPlugin.getAgentSession();
  119. String jid = SparkManager.getSessionManager().getBareAddress();
  120. try {
  121. history = agentSession.getAgentHistory(jid, 10, null);
  122. }
  123. catch (XMPPException e1) {
  124. Log.error("Error retrieving chat history.", e1);
  125. }
  126. try {
  127. model.removeAllElements();
  128. Collection sessions = history.getAgentChatSessions();
  129. Iterator iter = sessions.iterator();
  130. while (iter.hasNext()) {
  131. AgentChatSession chatSession = (AgentChatSession)iter.next();
  132. // Then were in a group chat
  133. final String nickname = chatSession.getVisitorsName();
  134. String email = chatSession.getVisitorsEmail();
  135. String sessionID = chatSession.getSessionID();
  136. String duration = ModelUtil.getTimeFromLong(chatSession.getDuration());
  137. String question = chatSession.getQuestion();
  138. if (!ModelUtil.hasLength(question)) {
  139. question = "No question was asked.";
  140. }
  141. Date startDate = chatSession.getStartDate();
  142. HistoryItem historyItem = new HistoryItem(nickname, startDate, email, question, duration);
  143. historyItem.setSessionID(sessionID);
  144. model.addElement(historyItem);
  145. }
  146. list.validate();
  147. list.repaint();
  148. }
  149. catch (Exception e1) {
  150. Log.error("Error retrieving chat history.", e1);
  151. }
  152. }
  153. private void showTranscript(String sessionID) {
  154. if (frame == null) {
  155. frame = new JFrame(FpRes.getString("title.chat.transcript"));
  156. frame.setIconImage(SparkManager.getMainWindow().getIconImage());
  157. }
  158. if (frame.isVisible()) {
  159. return;
  160. }
  161. Transcript transcript = null;
  162. try {
  163. transcript = FastpathPlugin.getAgentSession().getTranscript(sessionID);
  164. }
  165. catch (XMPPException e) {
  166. Log.error("Error showing transcripts.", e);
  167. }
  168. if (transcript == null) {
  169. JOptionPane.showMessageDialog(this, FpRes.getString("message.transcript.not.found.error"), FpRes.getString("title.error"), JOptionPane.ERROR_MESSAGE);
  170. return;
  171. }
  172. final ChatViewer chatViewer = new ChatViewer(transcript);
  173. frame.getContentPane().removeAll();
  174. frame.getContentPane().setLayout(new BorderLayout());
  175. frame.getContentPane().add(chatViewer, BorderLayout.CENTER);
  176. frame.pack();
  177. frame.setSize(600, 400);
  178. frame.setLocationRelativeTo(mainFrame);
  179. frame.setVisible(true);
  180. }
  181. /**
  182. * Lets make sure that the panel doesn't stretch past the
  183. * scrollpane view pane.
  184. *
  185. * @return the preferred dimension
  186. */
  187. public Dimension getPreferredSize() {
  188. final Dimension size = super.getPreferredSize();
  189. size.width = 0;
  190. return size;
  191. }
  192. }