Coverage Report - com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.ChatOutput
 
Classes in this File Line Coverage Branch Coverage Complexity
ChatOutput
0%
0/65
0%
0/8
2.143
 
 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  0
         private        JTextPane                        textPane = new JTextPane();
 75  0
         private Dimension                        chatOutDimension = null;  //  @jve:decl-index=0:
 76  0
         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  0
         private        Style[] styles = new Style[ST_LAST];
 91  
         
 92  
         /** Default constructor
 93  
          */
 94  
         public ChatOutput() {
 95  0
                 super(new JTextPane());
 96  0
                 textPane = (JTextPane)this.getViewport().getComponent(0);
 97  0
                 document = textPane.getStyledDocument();
 98  0
                 addStylesToDocument(document);
 99  
                 
 100  0
                 initialize();
 101  0
                 this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 102  0
                 chatOutDimension = new Dimension(10,10);
 103  0
                 chatOutRectangle = new Rectangle(10,10);
 104  0
         }
 105  
 
 106  
         private void initialize() {
 107  0
                 this.setSize(300, 200);
 108  
 
 109  0
                 this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
 110  0
         }
 111  
 
 112  
         private void addStylesToDocument(StyledDocument doc) {
 113  
                 // Initialize some styles.
 114  0
                 Style def = StyleContext.getDefaultStyleContext().getStyle(
 115  
                                 StyleContext.DEFAULT_STYLE);
 116  
 
 117  0
                 styles[ST_NORMAL] = doc.addStyle("regular", def);
 118  
 
 119  0
                 styles[ST_YOU] = doc.addStyle("you", styles[ST_NORMAL]);
 120  0
                 StyleConstants.setForeground(styles[ST_YOU], new Color(178,41,223));
 121  0
                 StyleConstants.setItalic(styles[ST_YOU], true);
 122  
 
 123  0
                 styles[ST_PLAYERSUB] = doc.addStyle("player", styles[ST_NORMAL]);
 124  0
                 StyleConstants.setBold(styles[ST_PLAYERSUB], true);
 125  0
                 StyleConstants.setForeground(styles[ST_PLAYERSUB], new Color(63,66,193));
 126  
 
 127  0
                 styles[ST_PLAYEROBJ] = doc.addStyle("player", styles[ST_NORMAL]);
 128  0
                 StyleConstants.setForeground(styles[ST_PLAYEROBJ], new Color(63,66,193));
 129  
 
 130  0
                 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  0
                 StyleConstants.setForeground(styles[ST_VERB], new Color(55,100,227));
 134  
 
 135  0
                 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  0
                 StyleConstants.setForeground(styles[ST_VERB_WHISPER], new Color(55,198,126));
 139  
 
 140  0
                 styles[ST_MESSAGE] = doc.addStyle("message", styles[ST_NORMAL]);
 141  0
         }
 142  
 
 143  
         /** Write a plain message to the textpane.
 144  
          * @param message The message to write.
 145  
          */
 146  
         public void writeBareMessage(String message) {
 147  0
                 appendStyledText(ST_MESSAGE, message + eol);
 148  0
                 chatOutDimension = textPane.getSize(chatOutDimension);
 149  0
                 chatOutRectangle.y = chatOutDimension.height;
 150  
                 try {
 151  0
                         this.getViewport().scrollRectToVisible(chatOutRectangle);
 152  0
                 } catch (Exception e) {/* ignore */}                
 153  0
         }
 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  0
                 appendStyledText(ST_PLAYERSUB, name + " ");
 162  0
                 appendStyledText(ST_VERB, verb + " ");
 163  0
                 appendStyledText(ST_MESSAGE, message + eol);
 164  0
                 chatOutDimension = textPane.getSize(chatOutDimension);
 165  0
                 chatOutRectangle.y = chatOutDimension.height;
 166  
                 try {
 167  0
                         this.getViewport().scrollRectToVisible(chatOutRectangle);
 168  0
                 } catch (Exception e) {/* ignore */}
 169  0
         }
 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  0
                 appendStyledText(ST_YOU, "You ");
 180  0
                 if (verb.equals("whisper"))
 181  0
                         appendStyledText(ST_VERB_WHISPER, verb + " ");
 182  
                 else
 183  0
                         appendStyledText(ST_VERB, verb + " ");
 184  0
                 if (name != null)
 185  0
                         appendStyledText(ST_PLAYEROBJ, name + " ");
 186  0
                 appendStyledText(ST_MESSAGE, message + eol);
 187  0
                 chatOutDimension = textPane.getSize(chatOutDimension);
 188  0
                 chatOutRectangle.y = chatOutDimension.height;
 189  
                 try {
 190  0
                         this.getViewport().scrollRectToVisible(chatOutRectangle);
 191  0
                 } catch (Exception e) {/* ignore */}
 192  0
         }
 193  
 
 194  
         private void        appendStyledText(int style, String s) {
 195  0
                 if (style < 0 || style > ST_LAST)
 196  0
                         style = ST_NORMAL;
 197  
                 try {
 198  0
                         this.document.insertString(this.document.getLength(), s, styles[style]);
 199  0
                 } catch (BadLocationException e) {
 200  0
                         e.printStackTrace();
 201  0
                 }
 202  0
         }
 203  
 
 204  
 }