View Javadoc
1   /******************************************************************************
2    * ChatOutput.java - Component that handles the Chat Putput.
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright(c) 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.4  2011/06/22 06:02:10  dick
10   * Support the new ChatTabs.
11   *
12   * Revision 1.3  2010/03/03 13:12:22  inim
13   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
14   *
15   * Revision 1.2  2010/03/03 12:19:49  inim
16   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
17   *
18   * Revision 1.1  2010/02/04 05:57:53  inim
19   * Mavenized project folder layout
20   *
21   * Revision 1.2  2009/02/14 15:37:51  dick
22   * BuckoFIBS is released under the GNU license.
23   *
24   * Revision 1.1  2009/01/28 19:38:39  dick
25   * package com.buckosoft.fibs.gui.chatWindow becomes com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.
26   *
27   * Revision 1.2  2008/12/11 08:49:57  dick
28   * Inbound chat is being written to the window.
29   *
30   * Revision 1.1  2008/10/08 23:09:41  dick
31   * Define the chat subsystem.
32   */
33  
34  /* 
35   * This program is free software: you can redistribute it and/or modify
36   * it under the terms of the GNU General Public License as published by
37   * the Free Software Foundation, either version 3 of the License, or
38   * (at your option) any later version.
39   *
40   * This program is distributed in the hope that it will be useful,
41   * but WITHOUT ANY WARRANTY; without even the implied warranty of
42   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43   * GNU General Public License for more details.
44   *
45   * You should have received a copy of the GNU General Public License
46   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
47   *
48   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
49   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
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  /** Component that handles the Chat output.
67   * @author Dick Balaska
68   * @since 2008/10/01
69   * @version $Revision$ <br> $Date$
70   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/chatWindow/ChatOutput.java">cvs ChatOutput.java</a>
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;  //  @jve:decl-index=0:
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;	// Player is sentence subject
83  	private final static int ST_PLAYEROBJ	= 3;	// Player is sentence object
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  	/** Default constructor
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 		// Initialize some styles.
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 		//StyleConstants.setBold(styles[ST_VERB], true);
132 		//StyleConstants.setForeground(styles[ST_VERB], new Color(201,43,80));
133 		StyleConstants.setForeground(styles[ST_VERB], new Color(55,100,227));
134 
135 		styles[ST_VERB_WHISPER] = doc.addStyle("verb", styles[ST_NORMAL]);
136 		//StyleConstants.setBold(styles[ST_VERB], true);
137 		//StyleConstants.setForeground(styles[ST_VERB], new Color(201,43,80));
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 	/** Write a plain message to the textpane.
144 	 * @param message The message to write.
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) {/* ignore */}		
153 	}
154 
155 	/** Write a message from another player to the textpane
156 	 * @param name The name of the player who sent the message
157 	 * @param verb The type of message sent
158 	 * @param message The message
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) {/* ignore */}
169 	}
170 
171 	/** Write a message You sent in the textpane.
172 	 * @param name The name of the player You sent the message to.  <br>
173 	 * 				Only useful for tell, "You tell bob hi!". <br>
174 	 * 				Null for kibitz and shout
175 	 * @param verb The type of message you sent
176 	 * @param message The message.
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) {/* ignore */}
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 }