Source code: com/splendid/awtchat/SmileyTextArea.java
1 /*
2 Component SmileyTextArea,
3 which is intended to be part of Eteria IRC Client from Javier Kohen
4 2001 written by Frank Bartels for Splendid Internet GmbH, Kiel, Germany
5 */
6
7 package com.splendid.awtchat;
8
9 import java.io.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.util.*;
13
14 import ar.com.jkohen.awt.MircSmileyTextArea;
15 import ar.com.jkohen.awt.NickInfoPopup;
16 import ar.com.jkohen.util.Resources;
17
18
19 public class SmileyTextArea extends Panel
20 {
21 public static Font DEFAULT_FONT = new Font("Helvetica", Font.PLAIN, 12);
22
23 private static Resources res;
24
25 public static int SMOOTH = 0;
26 public static int FAST = 1;
27 public static int SAFE = 2; // mode's possible values
28
29 private SmileyTextAreaArea smileyText; // the text area
30 private Scrollbar scrollbar; // the scrollbar
31 private NickInfoPopup nick_pop;
32
33 // stuff for saving the smileys (prepared on init())
34 static Hashtable smileys = null;
35 static int maxCodeLength = 1;
36 static boolean staticinitialized = false;
37
38 // ====== Constructor, init() ======
39
40 /** Constructor. supply HyperlinkReceiver for getting the clicked URLs
41 */
42
43 public SmileyTextArea(HyperlinkReceiver hr, CopyText cp)
44 {
45 super();
46 setLayout(new BorderLayout());
47 nick_pop = new NickInfoPopup();
48 scrollbar = new Scrollbar(Scrollbar.VERTICAL, 5, 5, 0, 10); //val,vis,min,max 0,5,0,10->0..5
49 smileyText = new SmileyTextAreaArea(this, scrollbar, hr, cp, nick_pop);
50 add(smileyText, "Center");
51 add(scrollbar, "East");
52 add(nick_pop, "West");
53 init();
54 }
55
56 //static
57 //{ init(); Now called in constructor (only once); advantage: nicer error messages if any
58 //}
59
60 // Is called above
61 public static void init()
62 {
63 // this.sp = sp;
64 if (staticinitialized)
65 return;
66
67 smileys = new Hashtable();
68
69 // Read smileys from resource file
70 // EIRC WORKAROUND #35.
71 // Resource res=Resource.getBundle("com.splendid.awtchat.Smileys");
72
73 maxCodeLength = 1;
74 for (Enumeration e = res.img_resource.propertyNames(); e.hasMoreElements() ;)
75 {
76 String key = (String)e.nextElement();
77
78 // this version needs entries like (you could use 123 or thisnthat instead of sad):
79 // smiley.sad.emoticons=:-( :( :-<
80 // smiley.sad.image=smileys/sm_sad.gif
81
82 int imagepos = key.indexOf(".image");
83 if ((key.indexOf("smiley.") == 0 || key.indexOf("symbol.") == 0) && imagepos >= 8)
84 {
85 try
86 {
87 String filename = res.getLabel(key);
88 String smiley = res.getLabel(key.substring(0, imagepos) + ".emoticons");
89 StringTokenizer st = new StringTokenizer(smiley);
90 while (st.hasMoreTokens())
91 {
92 String code = (String)st.nextToken(); // the other are the ascii codes
93 Image image = res.getImage(key);
94 smileys.put(code, image);
95 if (code.length() > maxCodeLength)
96 maxCodeLength = code.length();
97 }
98 }
99 catch (MissingResourceException mre) {} // ignore errors
100 }
101
102 // this version commented out: smiley.sad=smileys/sm_sad.gif :-( :( :-<
103 // if (key.indexOf("smiley.") == 0)
104 // {
105 // String smiley = res.getString(key);
106 // StringTokenizer st = new StringTokenizer(smiley);
107 // String filename = (String)st.nextToken(); // first token is filename
108 // while (st.hasMoreTokens())
109 // {
110 // String code = (String)st.nextToken(); // the other are the ascii codes
111 // addSmiley(filename, code, sp);
112 // if (code.length()>maxCodeLength)
113 // maxCodeLength = code.length();
114 // }
115 // }
116 }
117 staticinitialized = true;
118 }
119
120 // public synchronized void addTextListener(TextListener l)
121 // {
122 // }
123
124 // ====== SubsequentIndent ======
125
126 /** set the indent of subsequent lines (>=0) if a line must be split (default is 12)
127 */
128
129 public void setSubsequentIndent(int indent) // allowed values: 0 and greater
130 { if (indent<0) indent=0;
131 smileyText.setSubsequentIndent(indent);
132 }
133
134 /** get the indent of subsequent lines if a line must be split (default is 12)
135 */
136
137 public int getSubsequentIndent()
138 { return(smileyText.getSubsequentIndent());
139 }
140
141 // ====== Colors ======
142
143 public void setBackground(Color c)
144 {
145 smileyText.setBackground(c);
146 }
147
148 public void setForeground(Color c)
149 {
150 smileyText.setForeground(c);
151 }
152
153 public Color getBackground()
154 {
155 return(smileyText.getBackground());
156 }
157
158 public Color getForeground()
159 {
160 return(smileyText.getForeground());
161 }
162
163 /** change one of the color paette entries (n may be a number from 0 to 15,
164 * it corresponds to the number associated with MircMessage.COLOR IRC attribute)
165 */
166 public void setColorPaletteEntry(int n, Color c)
167 {
168 smileyText.setColorPaletteEntry(n, c);
169 }
170
171 /** get one of the color paette entries (n may be a number from 0 to 15,
172 * it corresponds to the number associated with MircMessage.COLOR IRC attribute)
173 */
174
175 public Color getColorPaletteEntry(int n)
176 {
177 return(smileyText.getColorPaletteEntry(n));
178 }
179
180 /** get the static color palette.
181 */
182
183 public static Color [] getColorPalette()
184 {
185 return(SmileyTextAreaArea.fixedColors);
186 }
187
188 // ====== Font ======
189
190 /** change the font (only name and size are used)
191 */
192
193 public void setFont(Font f)
194 {
195 smileyText.changeFont(f.getName(), f.getSize());
196 }
197
198 /** change the font
199 */
200
201 public void setFont(String name, int size)
202 {
203 smileyText.changeFont(name, size);
204 }
205
206 /** get the actual base font (not bold and not italic)
207 */
208
209 public Font getFont()
210 {
211 return(smileyText.getFont());
212 }
213
214 // ====== Append ======
215
216 /** set the length of the text buffer (number of appends after which old text is deleted)
217 (set -1 for infinite buffer)
218 */
219
220 public void setBufferlen(int bufferlen)
221 {
222 smileyText.setBufferlen(bufferlen);
223 }
224
225 /** get the length of the text buffer (number of appends after which old text is deleted)
226 (-1 means infinite buffer)
227 */
228
229 public int getBufferlen()
230 {
231 return(smileyText.getBufferlen());
232 }
233
234 // ====== Append ======
235
236 /** insert a line of text (smileys, URLs and IRC attributes interpreted)
237 * (URLs == http://* | https://* | ftp://* | www.* | #* (case is ignored))
238 * (IRC attributes are (from MircMessage) BELL, BOLD, ITALIC,
239 * UNDERLINE, COLOR, REVERSE, RESET)
240 */
241
242 public void append(String s)
243 {
244 smileyText.append(s, true);
245 }
246
247 /** same as append, except, URL interpretation is suppressed (channels, email, web-adresses)
248 */
249
250 public void appendNoUrls(String s)
251 {
252 smileyText.append(s, false);
253 }
254
255 /** same as append, but add a NickInfoPopup
256 */
257
258 public void appendNickInfo(String s, String n)
259 {
260 smileyText.append(s, true, n);
261 }
262
263 /** Scrolling display mode, may be SMOOTH, FAST, SAFE (initially it is SMOOTH)
264 * Set mode to FAST if problems arise with smooth scrolling
265 * Set mode to SAFE if problems arise with any scrolling (always pains the whole)
266 */
267
268 public void setMode(int mode)
269 {
270 smileyText.setMode(mode);
271 }
272
273 //** Define if each line can be broken into multiples lines
274
275 public void setBreaks(boolean canBreak)
276 {
277 smileyText.setBreaks(canBreak);
278 }
279
280 //** Define tabulations
281
282 public void setTabs(int tabs[])
283 {
284 for (int i = 0; i < tabs.length; i++)
285 smileyText.addTab(tabs[i]);
286 }
287
288 //** Erase everything
289
290 public void clear()
291 {
292 smileyText.clean();
293 }
294
295 public void setSelectable(boolean b)
296 {
297 smileyText.isSelectable = b;
298 }
299 }