/*- RichEditor.java -----------------------------------------------+
| |
| Copyright (C) 2008-2009 Prateek Jain, pchat |
| prateekjainaa@gmail.com |
| http://prateekjainaa.blogspot.com |
| |
| 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 2 |
| of the License, 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. |
| |
| A copy of the GNU General Public License may be found in the |
| installation directory named "GNUGPL.txt" |
| |
+-----------------------------------------------------------------+
*/
package client;
import java.awt.Color;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import commoms.CommandIF;
public class RichEditor extends JTextPane {
String icoSad;
String icoSmily;
String icoTongue;
String icoWinking;
String icoOh;
ClientApplet capp;
RichEditor(ClientApplet cap) {
super();
capp = cap;
setEditable(false);
setContentType("text/html");
HTMLEditorKit kit = new HTMLEditorKit();
StyleSheet css = new StyleSheet();
css.addRule("BODY{ margin : 0;}");
css.addRule("P{ margin : 0;}");
css.addRule("A{ color:#0000FF; text-decoration:underline;}");
kit.setStyleSheet(css);
setEditorKit(kit);
setBackground(new Color(249, 249, 250));
}
/**
* signifies an error and reports it to the user
*
* @param s
* the error message
*/
public void error(String s) {
HTMLDocument doc = (HTMLDocument) getDocument();
HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
try {
kit.insertHTML(doc, doc.getLength(),
"<font color=#CC0000><b> " + s + "<b><br></font>",
0, 0, HTML.Tag.FONT);
setCaretPosition(doc.getLength());
} catch (Throwable t) {
}
}
public void updateMsg(CommandIF comm) {
String s = comm.getMessage();
String pvt = "";
String sender = "";
if(comm.isPrivateMessage()) {
List<String> list = comm.getRecipientNames();
String to = "";
Iterator itr = list.iterator();
while(itr.hasNext()) {
to = itr.next().toString() + ", ";
}
pvt = "<font color=green><b>( pvt. message ) to </font><font color=blue>"+to+"</font><br>";
}
sender = "<font color=red><b>"+comm.getMsgFrom()+":</font>";
if(null == comm.getMsgFrom()) {
sender = "";
}
s = "<font color=black><b> " + s + "<b></font>";
HTMLDocument doc = (HTMLDocument) getDocument();
HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
System.out.println("message befor rep " + s);
s = replaceImage(":\\)", s, capp.getSmileyPath());
s = replaceImage(":\\(", s, capp.getSaddyPath());
s = replaceImage(":\\$", s, capp.getWinkingPath());
s = replaceImage(":p", s, capp.getTonguePath());
s = replaceImage(":o", s, capp.getOhPath());
s = replaceImage(":\\?", s, capp.getQuestionPath());
String msg = sender + pvt + s;
try {
kit.insertHTML(doc, doc.getLength(), msg,
0, 0, HTML.Tag.FONT);
setCaretPosition(doc.getLength());
capp.playRecieveSound();
} catch (Throwable t) {
}
}
private String replaceImage(String match, String stmt, String replaceWith) {
String sst = stmt;
//System.out.println("Got " + sst);
Pattern pat = Pattern.compile(match);
Matcher mat = pat.matcher(sst);
String finalstr = sst;
if(mat.find()) {
finalstr = mat.replaceAll("<img width=\"24\" height=\"24\" src=\"" +replaceWith+"\" alt=\":)\">");
}
//System.out.println("returnig " + finalstr);
return finalstr;
}
}