View Javadoc
1   /******************************************************************************
2    * ChatTabs.java - Manage the tabs in the chat system.
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright(c) 2011 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.1  2011/06/22 06:01:43  dick
10   * Manage the tabs in the chat system.
11   *
12   */
13  
14  /* 
15   * This program is free software: you can redistribute it and/or modify
16   * it under the terms of the GNU General Public License as published by
17   * the Free Software Foundation, either version 3 of the License, or
18   * (at your option) any later version.
19   *
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   *
25   * You should have received a copy of the GNU General Public License
26   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27   *
28   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
29   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
30   * 
31   */
32  package com.buckosoft.fibs.BuckoFIBS.gui.chatWindow;
33  
34  import javax.swing.JTabbedPane;
35  import javax.swing.event.ChangeEvent;
36  import javax.swing.event.ChangeListener;
37  
38  import com.buckosoft.fibs.BuckoFIBS.BFProperties;
39  import com.buckosoft.fibs.net.FIBSMessages;
40  
41  /** Manage the tabs in the chat system.
42   * @author Dick Balaska
43   * @since 2011/06/21
44   * @version $Revision$ <br> $Date$
45   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/chatWindow/ChatTabs.java">cvs ChatTabs.java</a>
46   */
47  public class ChatTabs extends JTabbedPane implements FIBSMessages, ChangeListener {
48  	private static final long serialVersionUID = 1L;
49  	private	BFProperties	bfProperties;
50  	private	ChatPane		chatPane;
51  
52  	enum Type {
53  		Kibitz,
54  		Shout,
55  		Tell
56  	}
57  
58  	/**
59  	 * 
60  	 */
61  	public ChatTabs(ChatPane chatPane) {
62  		super();
63  		this.chatPane = chatPane;
64  		initialize();
65  		initTab(Type.Kibitz, "Kibitz");
66  		initTab(Type.Shout, "Shout");
67  	}
68  
69  	/**
70  	 * This method initializes this
71  	 * 
72  	 * @return void
73  	 */
74  	private void initialize() {
75  		this.setSize(300, 200);
76  		addChangeListener(this);
77  	}
78  
79  	public void setBFProperties(BFProperties properties) {
80  		this.bfProperties = properties;
81  	}
82  	
83  	public void stateChanged(ChangeEvent ce) {
84  		this.chatPane.notifyTabChanged();
85  	}
86  
87  	private void initTab(Type type, String name) {
88  		ChatOutput co = new ChatOutput();
89  		//ButtonTabComponent btc = new ButtonTabComponent(this);
90  		this.add(name, co);
91  		int i = this.getTabCount();
92  		if (type != Type.Kibitz && type != Type.Shout)	// Can't delete kibitz and shout
93  			this.setTabComponentAt(i-1, new ButtonTabComponent(this));
94  	}
95  
96  	private final String verbs[]  = {"says", "shouts", "whispers", "kibitzes",
97  									"tell",  "shout",  "whisper",  "kibitz"};
98  
99  	private	Type modeToType[] = {
100 			Type.Tell, Type.Shout, Type.Kibitz, Type.Kibitz, 
101 			Type.Tell, Type.Shout, Type.Kibitz, Type.Kibitz 
102 	};
103 
104 	protected void addChatMessage(String name, int cookie, String message) {
105 		if (cookie == FIBS_NobodyHeardYou) {
106 			ChatOutput co = getChatOutput(Type.Kibitz, null);
107 			co.writeBareMessage(message);
108 			return;
109 		}
110 		int mode =  cookie - CLIP_SAYS;
111 		Type type = modeToType[mode];
112 		ChatOutput co = getChatOutput(type, name);
113 		if (mode < 4)
114 			co.writeMessageFromPlayer(name, verbs[mode], message);
115 		else
116 			co.writeMessageFromYou(name, verbs[mode], message);
117 	}
118 
119 	/** Get the requested ChatOutput pane.
120 	 * If it is a Tell, and it doesn't exist, create it.
121 	 * @param type
122 	 * @param name
123 	 * @return
124 	 */
125 	private ChatOutput getChatOutput(Type type, String name) {
126 		if (type == Type.Kibitz) {
127 			if (bfProperties.isChangeTabOnNewMsg())
128 				this.setSelectedIndex(0);
129 			return((ChatOutput)this.getComponent(0));
130 		}
131 		if (type == Type.Shout) {
132 			if (bfProperties.isChangeTabOnNewMsg() && !bfProperties.isDontChangeTabOnShout())
133 				this.setSelectedIndex(1);
134 			return((ChatOutput)this.getComponent(1));
135 		}
136 
137 		int i;
138 		i = this.indexOfTab(name);
139 		if (i == -1) {
140 			initTab(Type.Tell, name);
141 			i = this.indexOfTab(name);
142 		}
143 		if (bfProperties.isChangeTabOnNewMsg())
144 			this.setSelectedIndex(i);
145 		return((ChatOutput)this.getComponentAt(i));
146 	}
147 }