ChatPane.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui;
import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import org.xnap.gui.util.GUIHelper;
import org.xnap.util.Preferences;
/**
* Provides a {@link JTextPane} that displays chat messages.
*/
public class ChatPane extends JScrollPane implements PropertyChangeListener {
//--- Constant(s) ---
//--- Data field(s) ---
private static Preferences prefs = Preferences.getInstance();
/**
* A common style context for all panels.
*/
private static StyleContext styles = new StyleContext();
static {
updateStyles();
PropertyChangeListener l = new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
updateStyles();
}
};
prefs.addPropertyChangeListener("chatFont", l);
prefs.addPropertyChangeListener("chatMessageColor", l);
prefs.addPropertyChangeListener("chatUserColor", l);
prefs.addPropertyChangeListener("chatInfoColor", l);
prefs.addPropertyChangeListener("chatErrorColor", l);
}
private JTextPane jtp;
//--- Constructor(s) ---
public ChatPane()
{
jtp = new JTextPane();
// jtp.setContentType("text/rtf");
jtp.setBackground(prefs.getColor("chatBackgroundColor"));
jtp.setEditable(false);
jtp.setBorder(new EmptyBorder(5, 5, 5, 5));
setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy
(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setViewportView(jtp);
prefs.addPropertyChangeListener("chatBackgroundColor", this);
}
//--- Method(s) ---
public static Style getStyle(String key)
{
return styles.getStyle(key);
}
public static void setStyle(String key, Color color, Font font)
{
Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
Style s = styles.addStyle(key, def);
StyleConstants.setForeground(s, color);
StyleConstants.setFontFamily(s, font.getName());
StyleConstants.setFontSize(s, font.getSize());
StyleConstants.setItalic(s, font.isItalic());
StyleConstants.setBold(s, font.isBold());
}
public static void updateStyles()
{
Font font = prefs.getFont("chatFont");
setStyle("chatMessage", prefs.getColor("chatMessageColor"), font);
setStyle("chatUser", prefs.getColor("chatUserColor"), font);
setStyle("chatInfo", prefs.getColor("chatInfoColor"), font);
setStyle("chatError", prefs.getColor("chatErrorColor"), font);
}
public void appendLater(final String text, final String style)
{
Runnable r = new Runnable()
{
public void run()
{
append(text, style);
}
};
SwingUtilities.invokeLater(r);
}
public void appendLater(final String text)
{
appendLater(text, StyleContext.DEFAULT_STYLE);
}
public void append(String text, String style)
{
boolean autoScroll = GUIHelper.shouldScroll(getVerticalScrollBar());
try {
if (style.equals("chatMessage")) {
appendColored(text, style);
}
else {
Document doc = jtp.getDocument();
doc.insertString(doc.getLength(), text, getStyle(style));
// RTFEditorKit rtf = new RTFEditorKit();
// try {
// rtf.read(new StringReader(text), doc, doc.getLength());
// }
// catch (IOException ie){
// }
}
}
catch (BadLocationException e) {
}
if (autoScroll) {
GUIHelper.scrollToEnd(jtp);
}
}
public void appendColored(String text, String style)
throws BadLocationException
{
Style s = styles.addStyle(null, getStyle(style));
Document doc = jtp.getDocument();
boolean escape = false;
for (int i = 0; i < text.length(); i++) {
if (escape) {
escape = false;
switch (text.charAt(i)) {
case 0:
case '0':
StyleConstants.setForeground(s, Color.white);
break;
case 1:
StyleConstants.setForeground(s, Color.black);
break;
case '1':
char peek = (i < text.length()) ? text.charAt(i + 1) : 255;
i++;
switch (peek) {
case '0': // 10
StyleConstants.setForeground
(s, new Color(0, 204, 204));
break;
case '1': // 11
StyleConstants.setForeground
(s, new Color(51, 238, 255));
break;
case '2': // 12
StyleConstants.setForeground
(s, new Color(0, 0, 255));
break;
case '3': // 13
StyleConstants.setForeground
(s, new Color(238, 34, 238));
break;
case '4': // 14
StyleConstants.setForeground
(s, new Color(119, 119, 119));
break;
case '5': // 15
StyleConstants.setForeground
(s, new Color(153, 153, 153));
break;
default:
StyleConstants.setForeground(s, Color.black);
i--;
}
break;
case 2:
case '2':
// blue
StyleConstants.setForeground(s, new Color(0, 0, 204));
break;
case 3:
case '3':
// dark green
StyleConstants.setForeground(s, new Color(0, 204, 0));
break;
case 4:
case '4':
// red
StyleConstants.setForeground(s, new Color(221, 0, 0));
break;
case 5:
case '5':
// brown
StyleConstants.setForeground(s, new Color(170, 0, 0));
break;
case 6:
case '6':
// purple
StyleConstants.setForeground(s, new Color(187, 0, 187));
break;
case 7:
case '7':
// orange
StyleConstants.setForeground(s, new Color(255, 170, 0));
break;
case 8:
case '8':
// yellow
StyleConstants.setForeground(s, new Color(238, 221, 34));
break;
case 9:
case '9':
// green
StyleConstants.setForeground(s, new Color(51, 222, 85));
break;
case 10:
// teal
StyleConstants.setForeground(s, new Color(0, 204, 204));
break;
case 11:
// cyan
StyleConstants.setForeground(s, new Color(51, 238, 255));
break;
case 12:
// blue
StyleConstants.setForeground(s, new Color(0, 0, 255));
break;
case 13:
// magenta
StyleConstants.setForeground(s, new Color(238, 34, 238));
break;
case 14:
// dark gray
StyleConstants.setForeground(s, new Color(119, 119, 119));
break;
case 15:
// light gray
StyleConstants.setForeground(s, new Color(153, 153, 153));
break;
}
}
else {
switch (text.charAt(i)) {
case 2:
StyleConstants.setBold(s, true);
break;
case 3:
escape = true;
break;
case 7:
StyleConstants.setBold(s, false);
StyleConstants.setBold(s, false);
case 15:
StyleConstants.setBold(s, false);
break;
case 22:
// reversed: swap colors ?
break;
case 31:
StyleConstants.setUnderline(s, true);
default:
ensureIsReadable(s);
doc.insertString(doc.getLength(), text.charAt(i) + "", s);
// RTFEditorKit rtf = new RTFEditorKit();
// try {
// rtf.read(new StringReader(text), doc, doc.getLength());
// }
// catch (IOException ie) {
// }
}
}
}
}
private void ensureIsReadable(Style s)
{
Color c = jtp.getBackground();
double bLightness = getLightness(c);
c = StyleConstants.getForeground(s);
double fLightness = getLightness(c);
while (Math.abs(fLightness - bLightness) <= 0.2) {
if ((fLightness - bLightness) < 0) {
c = c.darker();
}
else if (fLightness == bLightness) {
if (fLightness > 0.5) {
c = c.darker();
}
else {
c = c.brighter();
}
}
else {
c = c.brighter();
}
fLightness = getLightness(c);
}
StyleConstants.setForeground(s, c);
}
private double getLightness(Color c)
{
return Math.pow((0.3 * c.getRed() + 0.59 * c.getGreen()
+ 0.11 * c.getBlue()) / 255.0,
1.0 / 3.0);
}
public void propertyChange(PropertyChangeEvent e)
{
jtp.setBackground(prefs.getColor("chatBackgroundColor"));
}
public void setText(String newValue)
{
jtp.setText(newValue);
}
}
The table below shows all metrics for ChatPane.java.




