Coverage Report - com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.ChatPane
 
Classes in this File Line Coverage Branch Coverage Complexity
ChatPane
0%
0/116
0%
0/28
2.133
ChatPane$1
0%
0/3
N/A
2.133
ChatPane$2
0%
0/3
N/A
2.133
ChatPane$Command
0%
0/7
N/A
2.133
 
 1  
 /******************************************************************************
 2  
  * ChatPane.java - Component that encapsulates the Chat subsystem.
 3  
  * $Id$
 4  
  * 
 5  
  * BuckoFIBS - Backgammon by BuckoSoft
 6  
  * Copyright(c) 2009,2010 - Dick Balaska - BuckoSoft, Corp.
 7  
  * 
 8  
  * $Log$
 9  
  * Revision 1.7  2011/06/22 06:13:20  dick
 10  
  * Fix utf-8 copyright.
 11  
  *
 12  
  * Revision 1.6  2011/06/22 06:02:10  dick
 13  
  * Support the new ChatTabs.
 14  
  *
 15  
  * Revision 1.5  2011/05/06 23:16:06  dick
 16  
  * Catch and ignore a scroll exception.
 17  
  *
 18  
  * Revision 1.4  2010/12/22 04:38:18  dick
 19  
  * Select the player that we just maybe added.
 20  
  *
 21  
  * Revision 1.3  2010/03/03 13:12:22  inim
 22  
  * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
 23  
  *
 24  
  * Revision 1.2  2010/03/03 12:19:49  inim
 25  
  * 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.
 26  
  *
 27  
  * Revision 1.1  2010/02/04 05:57:53  inim
 28  
  * Mavenized project folder layout
 29  
  *
 30  
  * Revision 1.4  2010/01/25 21:07:24  dick
 31  
  * Allow the program to add players and pre-select which command.
 32  
  *
 33  
  * Revision 1.3  2009/02/14 15:37:51  dick
 34  
  * BuckoFIBS is released under the GNU license.
 35  
  *
 36  
  * Revision 1.2  2009/02/05 05:58:44  dick
 37  
  * Bleh, editing the cvs log to get rid of the toodoo marker.
 38  
  *
 39  
  * Revision 1.1  2009/01/28 19:38:39  dick
 40  
  * package com.buckosoft.fibs.gui.chatWindow becomes com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.
 41  
  *
 42  
  * Revision 1.7  2009/01/28 08:32:30  dick
 43  
  * Get rid of todo tag.
 44  
  *
 45  
  * Revision 1.6  2009/01/22 05:05:37  dick
 46  
  * SEND_CHAT_COMMAND becomes the more generic SEND_COMMAND.
 47  
  *
 48  
  * Revision 1.5  2009/01/16 07:27:10  dick
 49  
  * ChatPane now sends messages.
 50  
  *
 51  
  * Revision 1.4  2009/01/09 07:19:17  dick
 52  
  * Scroll to the bottom on output.
 53  
  *
 54  
  * Revision 1.3  2008/12/11 08:49:57  dick
 55  
  * Inbound chat is being written to the window.
 56  
  *
 57  
  * Revision 1.2  2008/12/07 22:51:23  dick
 58  
  * Javadoc.
 59  
  *
 60  
  * Revision 1.1  2008/10/08 23:06:04  dick
 61  
  * Define the Chat panel.
 62  
  */
 63  
 
 64  
 /* 
 65  
  * This program is free software: you can redistribute it and/or modify
 66  
  * it under the terms of the GNU General Public License as published by
 67  
  * the Free Software Foundation, either version 3 of the License, or
 68  
  * (at your option) any later version.
 69  
  *
 70  
  * This program is distributed in the hope that it will be useful,
 71  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 72  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 73  
  * GNU General Public License for more details.
 74  
  *
 75  
  * You should have received a copy of the GNU General Public License
 76  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 77  
  *
 78  
  * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
 79  
  * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
 80  
  * 
 81  
  */
 82  
 package com.buckosoft.fibs.BuckoFIBS.gui.chatWindow;
 83  
 
 84  
 import java.awt.Dimension;
 85  
 import java.awt.Font;
 86  
 import java.awt.GridBagConstraints;
 87  
 import java.awt.GridBagLayout;
 88  
 import java.awt.Insets;
 89  
 
 90  
 import javax.swing.JComboBox;
 91  
 import javax.swing.JPanel;
 92  
 import javax.swing.JTextField;
 93  
 
 94  
 import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher;
 95  
 
 96  
 
 97  
 /** Component that encapsulates the Chat subsystem.
 98  
  * @author Dick Balaska
 99  
  * @since 2008/10/01
 100  
  * @version $Revision$ <br> $Date$
 101  
  * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/chatWindow/ChatPane.java">cvs ChatPane.java</a>
 102  
  */
 103  0
 public class ChatPane extends JPanel {
 104  
         private        final static boolean DEBUG = false;
 105  
         private static final long serialVersionUID = 1L;
 106  
 
 107  0
         private        CommandDispatcher        commandDispatcher = null;  //  @jve:decl-index=0:
 108  
 
 109  0
         private JTextField jChatInput = null;
 110  
 
 111  0
         private ChatTabs chatTabs = null;
 112  
 
 113  0
         private JComboBox<Command> jCommandComboBox = null;
 114  0
         private JComboBox<String> jComboBoxPlayer = null;
 115  
 
 116  
         /** The possible "chat" command types.
 117  
          */
 118  0
         public enum Command {
 119  0
                 Command,
 120  0
                 Kibitz,
 121  0
                 Say,
 122  0
                 Shout,
 123  0
                 Tell,
 124  0
                 Whisper
 125  
         }
 126  
 
 127  
         /**
 128  
          * This is the default constructor
 129  
          */
 130  
         public ChatPane() {
 131  0
                 super();
 132  0
                 getChatTabs();
 133  0
                 initialize();
 134  0
         }
 135  
 
 136  
         /** Set the reference to the CommandDispatcher
 137  
          * @param commandDispatcher The application CommandDispatcher
 138  
          */
 139  
         public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
 140  0
                 this.commandDispatcher = commandDispatcher;
 141  0
                 chatTabs.setBFProperties(this.commandDispatcher.getProperties());
 142  0
         }
 143  
 
 144  
         /** Write an incoming chat message to the screen
 145  
          * @param name 
 146  
          * @param s The message
 147  
          */
 148  
         public void writeChatMessage(String name, int cookie, String text) {
 149  0
                 chatTabs.addChatMessage(name, cookie, text);
 150  0
         }
 151  
 
 152  
         private void sendChatCommand() {
 153  0
                 StringBuilder sb = new StringBuilder();
 154  0
                 if (!this.getJComboBoxCommand().getSelectedItem().equals(Command.Command)) {
 155  0
                         sb.append(this.getJComboBoxCommand().getSelectedItem());
 156  0
                         sb.append(" ");
 157  
                 }
 158  0
                 if (this.getJComboBoxCommand().getSelectedItem().equals(Command.Tell)) {
 159  0
                         String s = (String)this.getJComboBoxPlayer().getSelectedItem();
 160  0
                         maybeAddPlayer(s);
 161  0
                         sb.append(s);
 162  0
                         sb.append(" ");
 163  
                 }
 164  0
                 sb.append(this.getJChatInput().getText());
 165  
                 if (DEBUG)
 166  
                         System.out.println(sb.toString());
 167  0
                 this.commandDispatcher.dispatch(CommandDispatcher.Command.SEND_COMMAND, sb.toString());
 168  
 
 169  0
         }
 170  
 
 171  
         /** Maybe add this player to the player dropdown box.
 172  
          * Add it unless it is a duplicate.
 173  
          * @param playerName The name of the player to add to the list
 174  
          */
 175  
         public void maybeAddPlayer(String playerName) {
 176  0
                 int z = this.getJComboBoxPlayer().getItemCount();
 177  0
                 boolean found = false;
 178  0
                 for (int i=0; i<z; i++) {
 179  0
                         if (this.getJComboBoxPlayer().getItemAt(i).equals(playerName)) {
 180  0
                                 found = true;
 181  0
                                 break;
 182  
                         }
 183  
                 }
 184  0
                 if (!found)
 185  0
                         this.getJComboBoxPlayer().addItem(playerName);
 186  0
                 this.getJComboBoxPlayer().setSelectedItem(playerName);
 187  0
         }
 188  
 
 189  
         /** Notification when a different ChatOutput is made visible
 190  
          */
 191  
         protected void notifyTabChanged() {
 192  0
                 if (chatTabs == null)
 193  0
                         return;                // don't die during init
 194  0
                 int i = this.chatTabs.getSelectedIndex();
 195  0
                 System.out.println("tab = " + i);
 196  0
                 if (i == 0)
 197  0
                         setupComboBoxCommand(Command.Kibitz);
 198  0
                 else if (i == 1)
 199  0
                         setupComboBoxCommand(Command.Shout);
 200  
                 else {
 201  0
                         setupComboBoxCommand(Command.Tell);
 202  0
                         String player = this.chatTabs.getTitleAt(i);
 203  0
                         maybeAddPlayer(player);
 204  
                 }
 205  0
                 this.setupComboBoxPlayer();
 206  0
         }
 207  
 
 208  
         /** This method initializes this */
 209  
         private void initialize() {
 210  0
                 GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
 211  0
                 gridBagConstraints11.fill = GridBagConstraints.VERTICAL;
 212  0
                 gridBagConstraints11.gridy = 1;
 213  0
                 gridBagConstraints11.weightx = 1.0;
 214  0
                 gridBagConstraints11.anchor = GridBagConstraints.WEST;
 215  0
                 gridBagConstraints11.gridx = 1;
 216  0
                 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
 217  0
                 gridBagConstraints1.fill = GridBagConstraints.NONE;
 218  0
                 gridBagConstraints1.gridy = 1;
 219  0
                 gridBagConstraints1.weightx = 1.0;
 220  0
                 gridBagConstraints1.anchor = GridBagConstraints.WEST;
 221  0
                 gridBagConstraints1.gridx = 0;
 222  0
                 GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
 223  0
                 gridBagConstraints4.fill = GridBagConstraints.BOTH;
 224  0
                 gridBagConstraints4.gridx = 2;
 225  0
                 gridBagConstraints4.gridy = 1;
 226  0
                 gridBagConstraints4.ipadx = 311;
 227  0
                 gridBagConstraints4.ipady = 0;
 228  0
                 gridBagConstraints4.weightx = 100.0;
 229  0
                 gridBagConstraints4.insets = new Insets(0, 0, 0, 0);
 230  0
                 GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
 231  0
                 gridBagConstraints3.gridy = 0;
 232  0
                 gridBagConstraints3.weightx = 2.0D;
 233  0
                 gridBagConstraints3.weighty = 2.0D;
 234  0
                 gridBagConstraints3.anchor = GridBagConstraints.NORTH;
 235  0
                 gridBagConstraints3.fill = GridBagConstraints.BOTH;
 236  0
                 gridBagConstraints3.gridwidth = 3;
 237  0
                 gridBagConstraints3.gridx = 0;
 238  0
                 GridBagConstraints gridBagConstraints = new GridBagConstraints();
 239  0
                 gridBagConstraints.gridx = 0;
 240  0
                 gridBagConstraints.gridy = 0;
 241  0
                 this.setLayout(new GridBagLayout());
 242  0
                 this.setSize(315, 221);
 243  0
                 this.add(getChatTabs(), gridBagConstraints3);
 244  0
                 this.add(getJChatInput(), gridBagConstraints4);
 245  0
                 this.add(getJComboBoxCommand(), gridBagConstraints1);
 246  0
                 this.add(getJComboBoxPlayer(), gridBagConstraints11);
 247  0
                 setupComboBoxPlayer();
 248  0
         }
 249  
 
 250  
         private void setupComboBoxPlayer() {
 251  0
                 if (getJComboBoxCommand().getSelectedItem().equals(Command.Tell))
 252  0
                         getJComboBoxPlayer().setVisible(true);
 253  
                 else
 254  0
                         getJComboBoxPlayer().setVisible(false);
 255  0
         }
 256  
 
 257  
         /** Make the player combobox point to this type
 258  
          * @param type The command type to set the combo box to.
 259  
          */
 260  
         public void setupComboBoxCommand(Command type) {
 261  0
                 getJComboBoxCommand().setSelectedItem(type);
 262  0
         }
 263  
 
 264  
         /**
 265  
          * This method initializes jChatInput        
 266  
          *         
 267  
          * @return javax.swing.JTextField        
 268  
          */
 269  
         private JTextField getJChatInput() {
 270  0
                 if (jChatInput == null) {
 271  0
                         jChatInput = new JTextField();
 272  0
                         jChatInput.setPreferredSize(new Dimension(315, 25));
 273  0
                         jChatInput.setFont(new Font("Dialog", Font.PLAIN, 12));
 274  0
                         jChatInput.addActionListener(new java.awt.event.ActionListener() {
 275  
                                 public void actionPerformed(java.awt.event.ActionEvent e) {
 276  0
                                         sendChatCommand();
 277  0
                                 }
 278  
                         });
 279  
                 }
 280  0
                 return jChatInput;
 281  
         }
 282  
 
 283  
         /**
 284  
          * This method initializes chatOutput        
 285  
          *         
 286  
          * @return javax.swing.JScrollPane        
 287  
          */
 288  
         private ChatTabs getChatTabs() {
 289  0
                 if (chatTabs == null) {
 290  0
                         chatTabs = new ChatTabs(this);
 291  
                 }
 292  0
                 return chatTabs;
 293  
         }
 294  
 
 295  
         /**
 296  
          * This method initializes jCommandComboBox        
 297  
          *         
 298  
          * @return javax.swing.JComboBox        
 299  
          */
 300  
         private JComboBox<Command> getJComboBoxCommand() {
 301  0
                 if (jCommandComboBox == null) {
 302  0
                         jCommandComboBox = new JComboBox<Command>();
 303  0
                         jCommandComboBox.setPreferredSize(new Dimension(100, 25));
 304  0
                         jCommandComboBox.addItemListener(new java.awt.event.ItemListener() {
 305  
                                 public void itemStateChanged(java.awt.event.ItemEvent e) {
 306  
                                         if (DEBUG)
 307  
                                                 System.out.println("itemStateChanged()");
 308  0
                                         setupComboBoxPlayer();
 309  0
                                 }
 310  
                         });
 311  0
                         for (Command c : Command.values())
 312  0
                                 jCommandComboBox.addItem(c);
 313  
                 }
 314  0
                 return jCommandComboBox;
 315  
         }
 316  
 
 317  
         /**
 318  
          * This method initializes jComboBoxPlayer        
 319  
          *         
 320  
          * @return javax.swing.JComboBox        
 321  
          */
 322  
         private JComboBox<String> getJComboBoxPlayer() {
 323  0
                 if (jComboBoxPlayer == null) {
 324  0
                         jComboBoxPlayer = new JComboBox<String>();
 325  0
                         jComboBoxPlayer.setEditable(true);
 326  0
                         jComboBoxPlayer.setPreferredSize(new Dimension(100, 25));
 327  
                 }
 328  0
                 return jComboBoxPlayer;
 329  
         }
 330  
 }  //  @jve:decl-index=0:visual-constraint="10,10"