📄 conversationarea.java
字号:
/* Copyright (C) 2003 Adam Olsen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import java.awt.Color;import java.awt.Font;import java.awt.Point;import java.awt.event.*;import java.io.File;import java.io.OutputStreamWriter;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.Locale;import java.util.ResourceBundle;import java.util.Vector;import java.util.regex.Pattern;import javax.swing.JEditorPane;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;import javax.swing.text.html.StyleSheet;import com.valhalla.gui.Standard;import com.valhalla.settings.Settings;/** * Represents an appending JEditorPane. Text appended to this pane will cause * it's scrollbar to scroll to the bottom. Also handles hyperlink events * * @author Adam Olsen * @version 1.1 */public class ConversationArea extends JEditorPane implements HyperlinkListener { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private JScrollPane scroll; private HTMLEditorKit kit = (HTMLEditorKit) getEditorKit(); private HTMLDocument doc = (HTMLDocument) getDocument(); private javax.swing.Timer timer; private Vector lines = new Vector(); private StyleSheet myStyleSheet = new StyleSheet(); protected PrintWriter logOut; private OutputStreamWriter fw; private boolean next = false; /** * Default constructor - sets up the JEditorPane * * @param bufferTime * the buffer delay in milliseconds */ public ConversationArea() { super("text/html; enctype='UTF-8'", ""); timer = new javax.swing.Timer(50, new ShowHandler()); String fontString = Settings.getInstance().getProperty( "messageWindowFont"); Font windowFont; if (fontString == null) windowFont = new Font("Sans", Font.PLAIN, 12); else windowFont = Font.decode(fontString); loadStyleSheet(windowFont); kit.setStyleSheet(myStyleSheet); doc = new HTMLDocument(myStyleSheet); setDocument(doc); setEditable(false); addHyperlinkListener(this); setBackground(Color.WHITE); } /** * Sets the log file * * @param logFile * the file to use for logging * @param encoding * the encoding of the log file * (if null, empty, or invalid, the platform default encoding will * be used) */ public void setLogFile(File logFile, String encoding) { try { fw = null; try { fw = new OutputStreamWriter( new FileOutputStream(logFile, true), encoding); } catch (UnsupportedEncodingException ex) { } catch (NullPointerException ex) { } if (fw == null) { fw = new OutputStreamWriter( new FileOutputStream(logFile, true)); } logOut = new PrintWriter(fw, true); } catch (Exception ex) { fw = null; } } /** * Closes the log file */ public void closeLog() { if (fw == null) return; try { fw.close(); } catch (Exception e) { } } /** * Loads a new font for the JEditorPane * * @param windowFont * the font to load */ public void loadStyleSheet(Font windowFont) { String ruleString = "font-size: " + windowFont.getSize() + "; font-family: " + windowFont.getName() + "; "; if (windowFont.isItalic()) ruleString += "font-style: italic; "; else ruleString += "font-style: normal; "; if (windowFont.isBold()) ruleString += "font-weight: bold; "; else ruleString += "font-weight: normal; "; myStyleSheet.addRule("body { " + ruleString + " }"); myStyleSheet .addRule("body a { color: #5B62BC; text-decoration: underline }"); } /** * Sets this window's scrollbar * * @param scroll * the scrollbar */ public void setScroll(JScrollPane scroll) { this.scroll = scroll; } /** * Appends text to the ConversationArea Scrolls down if the scrollbar is * already at the bottom, or does nothing if the person has scrolled up any * * @param html * the text to append */ public void append(String html) { int index = html.indexOf("\n"); while (index > -1) { String newHtml = html.substring(0, index); html = html.substring(index + 1); append(newHtml); index = html.indexOf("\n"); } if (html.equals("")) { html = " "; } html = html.replaceAll("^/", "/"); lines.add(html); if (!timer.isRunning()) timer.start(); } /** * Actually appends the buffered data to the JEditorPane * * @author Adam Olsen * @version 2.0 */ class ShowHandler implements ActionListener { public synchronized void actionPerformed(ActionEvent ae) { if (lines.size() > 0) { String line = (String) lines.firstElement(); JScrollBar bar = scroll.getVerticalScrollBar(); boolean end = bar.getValue() - (bar.getMaximum() - bar.getModel().getExtent()) >= -16; Point p = scroll.getViewport().getViewPosition(); boolean scrFlag = bar.isVisible(); p.y += 50; // just so it's not the first line (might scroll a // bit) int pos = viewToModel(p); if (pos < 0) pos = 0; try { if (logOut != null) { logOut.println(line + "<br>"); } // replace emoticon symbols String emoLine = Emoticons.getInstance().replaceIcons(line); kit.insertHTML(doc, doc.getLength(), emoLine, 0, 0, null); if (!end && scrFlag) setCaretPosition(pos); else setCaretPosition(doc.getLength()); scroll.repaint(); } catch (Exception e) { com.valhalla.Logger.debug("ShowHandler.class Exception: " + e.toString()); } lines.remove(line); } if (lines.size() == 0) timer.stop(); repaint(); } } /** * Appends text without buffering * * @param line * the text to append */ public void plainAppend(String line) { try { // replace emoticon symbols line = Emoticons.getInstance().replaceIcons(line); kit.insertHTML(doc, doc.getLength(), line, 0, 0, null); } catch (Exception ex) { } } /** * Moves the cursor to the end of the editorpane */ public void moveToEnd() { try { setCaretPosition(doc.getLength()); } catch (Exception e) { } } /** * Is called when someone clicks on a hyperlink in the window */ public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { String app = Settings.getInstance().getProperty( "browserApplication"); if (Pattern.matches("^mailto\\:.*", e.getURL().toString())) app = Settings.getInstance() .getProperty("emailApplication"); if (app != null && !app.equals("")) { /* tail */ String command = app .replaceAll("%s", e.getURL().toString()); command = command.replaceAll("\\%l", e.getURL().toString()); // for // backwards // compatability Runtime.getRuntime().exec(command); /* tail */ } else Standard.warningMessage(this, resources .getString("hyperlink"), resources .getString("noApplication")); } catch (java.io.IOException ex) { Standard.warningMessage(this, resources.getString("hyperlink"), resources.getString("errorExecutingApplication")); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -