transcriptwindow.java.svn-base
来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 515 行 · 第 1/2 页
SVN-BASE
515 行
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.spark.ui;
import org.jdesktop.swingx.calendar.DateUtils;
import org.jivesoftware.Spark;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DelayInformation;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.ContextMenuListener;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* The <CODE>TranscriptWindow</CODE> class. Provides a default implementation
* of a Chat Window. In general, extensions could override this class
* to offer more support within the chat, but should not be necessary.
*/
public class TranscriptWindow extends ChatArea implements ContextMenuListener {
private final SimpleDateFormat notificationDateFormatter;
private final SimpleDateFormat messageDateFormatter;
private Date lastUpdated;
/**
* The default font used in the chat window for all messages.
*/
private Font defaultFont;
private Date lastPost;
/**
* Creates a default instance of <code>TranscriptWindow</code>.
*/
public TranscriptWindow() {
setEditable(false);
// Set Default Font
final LocalPreferences pref = SettingsManager.getLocalPreferences();
int fontSize = pref.getChatRoomFontSize();
defaultFont = new Font("Dialog", Font.PLAIN, fontSize);
addMouseListener(this);
addMouseMotionListener(this);
setDragEnabled(true);
addContextMenuListener(this);
// Make sure ctrl-c works
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("Ctrl c"), "copy");
getActionMap().put("copy", new AbstractAction("copy") {
public void actionPerformed(ActionEvent evt) {
StringSelection stringSelection = new StringSelection(getSelectedText());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
});
notificationDateFormatter = new SimpleDateFormat("EEEEE, MMMMM d, yyyy");
messageDateFormatter = new SimpleDateFormat("h:mm a");
}
/**
* Inserts a component into the transcript window.
*
* @param component the component to insert.
*/
public void addComponent(Component component) {
final StyledDocument doc = (StyledDocument)getDocument();
// The image must first be wrapped in a style
Style style = doc.addStyle("StyleName", null);
StyleConstants.setComponent(style, component);
// Insert the image at the end of the text
try {
doc.insertString(doc.getLength(), "ignored text", style);
doc.insertString(doc.getLength(), "\n", null);
}
catch (BadLocationException e) {
Log.error(e);
}
}
/**
* Create and insert a message from the current user.
*
* @param nickname the nickname of the current user.
* @param message the message to insert.
* @param foreground the color to use for the message foreground.
*/
public void insertMessage(String nickname, Message message, Color foreground) {
// Check interceptors.
for (TranscriptWindowInterceptor interceptor : SparkManager.getChatManager().getTranscriptWindowInterceptors()) {
boolean handled = interceptor.isMessageIntercepted(this, nickname, message);
if (handled) {
// Do nothing.
return;
}
}
String body = message.getBody();
try {
DelayInformation inf = (DelayInformation)message.getExtension("x", "jabber:x:delay");
Date sentDate = null;
if (inf != null) {
sentDate = inf.getStamp();
body = "(Offline) " + body;
}
else {
sentDate = new Date();
}
String date = getDate(sentDate);
// Agent color is always blue
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, foreground);
final Document doc = getDocument();
styles.removeAttribute("link");
StyleConstants.setFontSize(styles, defaultFont.getSize());
doc.insertString(doc.getLength(), date + nickname + ": ", styles);
// Reset Styles for message
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, Color.black);
setText(body);
insertText("\n");
}
catch (BadLocationException e) {
Log.error("Error message.", e);
}
}
/**
* Inserts a full line using a prefix and message.
*
* @param prefix the prefix to use. If null is used, then only the message will be inserted.
* @param message the message to insert.
* @param foreground the foreground color for the message.
*/
public void insertPrefixAndMessage(String prefix, String message, Color foreground) {
try {
// Agent color is always blue
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, foreground);
final Document doc = getDocument();
styles.removeAttribute("link");
StyleConstants.setFontSize(styles, defaultFont.getSize());
if (prefix != null) {
doc.insertString(doc.getLength(), prefix + ": ", styles);
}
// Reset Styles for message
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, Color.black);
setText(message);
insertText("\n");
}
catch (BadLocationException e) {
Log.error("Error message.", e);
}
}
/**
* Create and insert a notification message. A notification message generally is a
* presence update, but can be used for most anything related to the room.
*
* @param message the information message to insert.
* @param foregroundColor the foreground color to use.
*/
public synchronized void insertNotificationMessage(String message, Color foregroundColor) {
try {
// Agent color is always blue
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, foregroundColor);
final Document doc = getDocument();
styles.removeAttribute("link");
StyleConstants.setFontSize(styles, defaultFont.getSize());
doc.insertString(doc.getLength(), "", styles);
// Reset Styles for message
StyleConstants.setBold(styles, false);
StyleConstants.setForeground(styles, foregroundColor);
setText(message);
insertText("\n");
// Default back to black
StyleConstants.setForeground(styles, Color.black);
}
catch (BadLocationException ex) {
Log.error("Error message.", ex);
}
}
/**
* Create and insert a notification message. A notification message generally is a
* presence update, but can be used for most anything related to the room.
*
* @param text the text to insert.
* @param bold true to use bold text.
* @param underline true to have text underlined.
* @param foreground the foreground color.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?