1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 package com.buckosoft.fibs.BuckoFIBS.gui.chatWindow;
53
54 import java.awt.Color;
55 import java.awt.Dimension;
56 import java.awt.Rectangle;
57
58 import javax.swing.JScrollPane;
59 import javax.swing.JTextPane;
60 import javax.swing.text.BadLocationException;
61 import javax.swing.text.Style;
62 import javax.swing.text.StyleConstants;
63 import javax.swing.text.StyleContext;
64 import javax.swing.text.StyledDocument;
65
66
67
68
69
70
71
72 public class ChatOutput extends JScrollPane {
73 private static final long serialVersionUID = 1L;
74 private JTextPane textPane = new JTextPane();
75 private Dimension chatOutDimension = null;
76 private Rectangle chatOutRectangle = null;
77
78 private final static String eol = "\n";
79
80 private final static int ST_NORMAL = 0;
81 private final static int ST_YOU = 1;
82 private final static int ST_PLAYERSUB = 2;
83 private final static int ST_PLAYEROBJ = 3;
84 private final static int ST_VERB = 4;
85 private final static int ST_VERB_WHISPER= 5;
86 private final static int ST_MESSAGE = 6;
87 private final static int ST_LAST = 7;
88
89 private StyledDocument document;
90 private Style[] styles = new Style[ST_LAST];
91
92
93
94 public ChatOutput() {
95 super(new JTextPane());
96 textPane = (JTextPane)this.getViewport().getComponent(0);
97 document = textPane.getStyledDocument();
98 addStylesToDocument(document);
99
100 initialize();
101 this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
102 chatOutDimension = new Dimension(10,10);
103 chatOutRectangle = new Rectangle(10,10);
104 }
105
106 private void initialize() {
107 this.setSize(300, 200);
108
109 this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
110 }
111
112 private void addStylesToDocument(StyledDocument doc) {
113
114 Style def = StyleContext.getDefaultStyleContext().getStyle(
115 StyleContext.DEFAULT_STYLE);
116
117 styles[ST_NORMAL] = doc.addStyle("regular", def);
118
119 styles[ST_YOU] = doc.addStyle("you", styles[ST_NORMAL]);
120 StyleConstants.setForeground(styles[ST_YOU], new Color(178,41,223));
121 StyleConstants.setItalic(styles[ST_YOU], true);
122
123 styles[ST_PLAYERSUB] = doc.addStyle("player", styles[ST_NORMAL]);
124 StyleConstants.setBold(styles[ST_PLAYERSUB], true);
125 StyleConstants.setForeground(styles[ST_PLAYERSUB], new Color(63,66,193));
126
127 styles[ST_PLAYEROBJ] = doc.addStyle("player", styles[ST_NORMAL]);
128 StyleConstants.setForeground(styles[ST_PLAYEROBJ], new Color(63,66,193));
129
130 styles[ST_VERB] = doc.addStyle("verb", styles[ST_NORMAL]);
131
132
133 StyleConstants.setForeground(styles[ST_VERB], new Color(55,100,227));
134
135 styles[ST_VERB_WHISPER] = doc.addStyle("verb", styles[ST_NORMAL]);
136
137
138 StyleConstants.setForeground(styles[ST_VERB_WHISPER], new Color(55,198,126));
139
140 styles[ST_MESSAGE] = doc.addStyle("message", styles[ST_NORMAL]);
141 }
142
143
144
145
146 public void writeBareMessage(String message) {
147 appendStyledText(ST_MESSAGE, message + eol);
148 chatOutDimension = textPane.getSize(chatOutDimension);
149 chatOutRectangle.y = chatOutDimension.height;
150 try {
151 this.getViewport().scrollRectToVisible(chatOutRectangle);
152 } catch (Exception e) {}
153 }
154
155
156
157
158
159
160 public void writeMessageFromPlayer(String name, String verb, String message) {
161 appendStyledText(ST_PLAYERSUB, name + " ");
162 appendStyledText(ST_VERB, verb + " ");
163 appendStyledText(ST_MESSAGE, message + eol);
164 chatOutDimension = textPane.getSize(chatOutDimension);
165 chatOutRectangle.y = chatOutDimension.height;
166 try {
167 this.getViewport().scrollRectToVisible(chatOutRectangle);
168 } catch (Exception e) {}
169 }
170
171
172
173
174
175
176
177
178 public void writeMessageFromYou(String name, String verb, String message) {
179 appendStyledText(ST_YOU, "You ");
180 if (verb.equals("whisper"))
181 appendStyledText(ST_VERB_WHISPER, verb + " ");
182 else
183 appendStyledText(ST_VERB, verb + " ");
184 if (name != null)
185 appendStyledText(ST_PLAYEROBJ, name + " ");
186 appendStyledText(ST_MESSAGE, message + eol);
187 chatOutDimension = textPane.getSize(chatOutDimension);
188 chatOutRectangle.y = chatOutDimension.height;
189 try {
190 this.getViewport().scrollRectToVisible(chatOutRectangle);
191 } catch (Exception e) {}
192 }
193
194 private void appendStyledText(int style, String s) {
195 if (style < 0 || style > ST_LAST)
196 style = ST_NORMAL;
197 try {
198 this.document.insertString(this.document.getLength(), s, styles[style]);
199 } catch (BadLocationException e) {
200 e.printStackTrace();
201 }
202 }
203
204 }