001: /*- ChatPane.java -------------------------------------------------+
002: | |
003: | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
004: | countjoe@users.sourceforge.net |
005: | http://www.42llamas.com/LlamaChat/ |
006: | |
007: | This program is free software; you can redistribute it and/or |
008: | modify it under the terms of the GNU General Public License |
009: | as published by the Free Software Foundation; either version 2 |
010: | of the License, or (at your option) any later version |
011: | |
012: | This program is distributed in the hope that it will be useful, |
013: | but WITHOUT ANY WARRANTY; without even the implied warranty of |
014: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
015: | GNU General Public License for more details. |
016: | |
017: | A copy of the GNU General Public License may be found in the |
018: | installation directory named "GNUGPL.txt" |
019: | |
020: +-----------------------------------------------------------------+
021: */
022:
023: package client;
024:
025: import javax.swing.JTextPane;
026: import javax.swing.text.html.HTMLEditorKit;
027: import javax.swing.text.html.StyleSheet;
028: import javax.swing.text.html.HTMLDocument;
029: import javax.swing.text.html.HTML;
030: import java.lang.String;
031: import java.lang.StringBuffer;
032: import java.nio.CharBuffer;
033: import java.util.regex.Pattern;
034: import java.awt.Color;
035:
036: /* -------------------- JavaDoc Information ----------------------*/
037: /**
038: * This represents the chat pane where chat dialoge takes place
039: * @author Joseph Monti <a href="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
040: * @version 0.8
041: */
042: public class ChatPane extends JTextPane {
043:
044: String icoSad;
045: String icoSmily;
046: String icoTongue;
047: String icoWinking;
048: String icoOh;
049: LlamaChat llamaChat;
050:
051: ChatPane(LlamaChat lc) {
052: super ();
053:
054: llamaChat = lc;
055:
056: setEditable(false);
057: setContentType("text/html");
058: HTMLEditorKit kit = new HTMLEditorKit();
059: StyleSheet css = new StyleSheet();
060: css.addRule("BODY{ margin : 0;}");
061: css.addRule("P{ margin : 0;}");
062: css.addRule("A{ color:#0000FF; text-decoration:underline;}");
063: kit.setStyleSheet(css);
064: setEditorKit(kit);
065: setBackground(new Color(249, 249, 250));
066:
067: icoSad = "<img src=\"" + lc.locationURL
068: + "images/sad.gif\" height=\"14\" width=\"14\">";
069: icoSmily = "<img src=\"" + lc.locationURL
070: + "images/smiley.gif\" height=\"14\" width=\"14\">";
071: icoOh = "<img src=\"" + lc.locationURL
072: + "images/oh.gif\" height=\"14\" width=\"14\">";
073: icoTongue = "<img src=\"" + lc.locationURL
074: + "images/tongue.gif\" height=\"14\" width=\"14\">";
075: icoWinking = "<img src=\"" + lc.locationURL
076: + "images/winking.gif\" height=\"14\" width=\"14\">";
077:
078: addHyperlinkListener(lc.myHyperlinkListener);
079: }
080:
081: /**
082: * Sends a text to the chat window. Parses the message to pick
083: * out emoticons and links.
084: * @param un the name of the user sending the message
085: * @param message the message to be sent
086: * @param whisper indicates the message was a wisper and makes the
087: * message italic
088: */
089: public void sendText(String un, String message) {
090: sendText(un, message, false);
091: }
092:
093: /**
094: * Sends a text to the chat window. Parses the message to pick
095: * out emoticons and links.
096: * @param un the name of the user sending the message
097: * @param message the message to be sent
098: */
099: public void sendText(String un, String message, boolean whisper) {
100: StringBuffer buff = new StringBuffer();
101: HTMLDocument doc = (HTMLDocument) getDocument();
102: HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
103:
104: if (un == null || llamaChat.username == null) {
105: return;
106: }
107:
108: if (!"server".equals(un)) {
109: message = message.replaceAll("<", "<");
110: message = message.replaceAll(">", ">");
111: }
112: message = message.replaceAll("\n", "<br>");
113:
114: if (llamaChat.username.equals(un)) {
115: buff.append("<font color=#009900><b>");
116: } else if (un.equals("server")) {
117: buff.append("<font color=#990000><b>");
118: } else {
119: buff.append("<font color=#000099><b>");
120: }
121: buff.append(un);
122: buff.append("</b></font>");
123:
124: buff.append(" : ");
125: if (whisper) {
126: buff.append("<i>");
127: }
128: char[] tmp = message.toCharArray();
129: int start, end;
130: for (start = 0, end = 0; end < (tmp.length - 1); ++end) {
131: if (tmp[end] == ';') {
132: if (tmp[end + 1] == ')') {
133: if (end - start > 0) {
134: buff.append(tmp, start, end - start);
135: }
136: buff.append(icoWinking);
137: start = end += 2;
138: end--;
139: }
140: } else if (tmp[end] == ':') {
141: switch (tmp[end + 1]) {
142: case ')':
143: if (end - start > 0) {
144: buff.append(tmp, start, end - start);
145: }
146: buff.append(icoSmily);
147: start = end += 2;
148: end--;
149: break;
150: case '(':
151: if (end - start > 0) {
152: buff.append(tmp, start, end - start);
153: }
154: buff.append(icoSad);
155: start = end += 2;
156: end--;
157: break;
158: case 'P':
159: if (end - start > 0) {
160: buff.append(tmp, start, end - start);
161: }
162: buff.append(icoTongue);
163: start = end += 2;
164: end--;
165: break;
166: case 'o':
167: if (end - start > 0) {
168: buff.append(tmp, start, end - start);
169: }
170: buff.append(icoOh);
171: start = end += 2;
172: end--;
173: break;
174: }
175: } else if (end + 10 < tmp.length && tmp[end] == 'h'
176: && tmp[end + 1] == 't' && tmp[end + 2] == 't'
177: && tmp[end + 3] == 'p' && tmp[end + 4] == ':'
178: && tmp[end + 5] == '/' && tmp[end + 6] == '/') {
179: if (end - start > 0) {
180: buff.append(tmp, start, end - start);
181: }
182: int index;
183: for (index = 7; index < tmp.length
184: && Pattern.matches("[\\w.:/\\?\\=&%_\\-~]",
185: CharBuffer.wrap(tmp, end + index, 1)); index++) {
186: }
187:
188: buff.append("<a href=\"");
189: buff.append(tmp, end, index);
190: buff.append("\">");
191: buff.append(tmp, end, index);
192: buff.append("</a>");
193: start = end += index;
194: end--;
195: }
196: }
197: if (start < tmp.length) {
198: buff.append(tmp, start, tmp.length - start);
199: }
200: if (whisper) {
201: buff.append("</i>");
202: }
203: if (buff.length() > 0) {
204: try {
205: buff.append("<br>");
206: kit.insertHTML(doc, doc.getLength(), buff.toString(),
207: 0, 0, HTML.Tag.FONT);
208: setCaretPosition(doc.getLength());
209: } catch (Throwable t) {
210: t.printStackTrace();
211: }
212: }
213: if (message.startsWith("afk") || message.startsWith("brb")) {
214: llamaChat.afks.add(un);
215: llamaChat.updateList();
216: } else {
217: if (llamaChat.afks.contains(un)) {
218: llamaChat.afks.remove(un);
219: llamaChat.updateList();
220: }
221: }
222: }
223:
224: /**
225: * signifies an error and reports it to the user
226: * @param s the error message
227: */
228: public void error(String s) {
229: HTMLDocument doc = (HTMLDocument) getDocument();
230: HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
231: try {
232: kit.insertHTML(doc, doc.getLength(),
233: "<font color=#CC0000><b>ERROR : " + s
234: + "<b><br></font>", 0, 0, HTML.Tag.FONT);
235: setCaretPosition(doc.getLength());
236: } catch (Throwable t) {
237: }
238: }
239: }
|