View Javadoc

1   /* 
2    * Msg.java 
3    * Created on 20 févr. 2004 
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.action;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.action.context.ActionContextWrapper;
28  import net.sf.magicproject.action.context.Int;
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.clickable.targetable.player.Player;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.network.IdMessages;
33  import net.sf.magicproject.operation.IdOperations;
34  import net.sf.magicproject.operation.OperationFactory;
35  import net.sf.magicproject.stack.StackManager;
36  import net.sf.magicproject.token.IdMessageBox;
37  import net.sf.magicproject.token.IdTokens;
38  import net.sf.magicproject.tools.Log;
39  import net.sf.magicproject.ui.MagicUIComponents;
40  import net.sf.magicproject.ui.wizard.Ok;
41  import net.sf.magicproject.ui.wizard.YesNo;
42  
43  /***
44   * Display a message box with a customizable ok button and text. It's possible
45   * to use the multi-language ability for text and button label if string starts
46   * with the '$' character. In case of type is 'yesno', the answer is 0 for
47   * "YES", and 1 for "NO". To test the answer of this question, test the register
48   * named 'stack' at the index '0'.
49   * 
50   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
51   * @since 0.71
52   * @since 0.82 support controller
53   */
54  public class Input extends MessagingAction {
55  
56  	/***
57  	 * Create an instance of Msg by reading a file Offset's file must pointing on
58  	 * the first byte of this action. <br>
59  	 * <ul>
60  	 * Structure of InputStream : Data[size]
61  	 * <li>controller [TestOn]</li>
62  	 * <li>text to display +'/0' [...]</li>
63  	 * <li>type of message box (idMessageBox) [1]</li>
64  	 * </ul>
65  	 * 
66  	 * @param inputFile
67  	 *          file containing this action
68  	 * @throws IOException
69  	 *           if error occurred during the reading process from the specified
70  	 *           input stream
71  	 */
72  	Input(InputStream inputFile) throws IOException {
73  		super(inputFile);
74  		type = IdMessageBox.deserialize(inputFile);
75  	}
76  
77  	@Override
78  	public final Actiontype getIdAction() {
79  		return Actiontype.MSG;
80  	}
81  
82  	@Override
83  	public boolean play(ContextEventListener context, Ability ability) {
84  		final Player controller = (Player) this.controller.getTargetable(ability,
85  				context, null);
86  		controller.setHandedPlayer();
87  		if (controller.isYou()) {
88  			Log.debug("Opponent is waiting for our answer");
89  			switch (type) {
90  			case yesno:
91  				// while the user has not given an answer
92  				replayAction(context, ability, new YesNo(context, ability, this,
93  						ability.getName(), ability.getAbilityTitle(), "wiz_question.gif",
94  						300, 250, text));
95  				break;
96  			case ok:
97  				replayAction(context, ability, new Ok(context, ability, this, ability
98  						.getName(), ability.getAbilityTitle(), "wiz_info.gif", 300, 250,
99  						text));
100 				break;
101 			default:
102 				throw new InternalError("Unknow message type : " + type);
103 			}
104 		} else {
105 			MagicUIComponents.logListing.append(controller.idPlayer,
106 					"Waiting for opponent's answer");
107 			Log.debug("Waiting for opponent's answer");
108 		}
109 		return false;
110 	}
111 
112 	@Override
113 	public String toString(Ability ability) {
114 		return "msgbox";
115 	}
116 
117 	@Override
118 	protected final int getMessagingActionId() {
119 		return IdMessages.MSG_ANSWER;
120 	}
121 
122 	@Override
123 	protected void setAnswer(int optionAnswer, int indexAnswer,
124 			ContextEventListener context, Ability ability,
125 			ActionContextWrapper actionContext) {
126 		if (actionContext != null) {
127 			actionContext.actionContext = new Int(optionAnswer);
128 		}
129 		StackManager.registers[IdTokens.MSG_ANSWER_INDEX] = OperationFactory
130 				.getOperation(IdOperations.SET).process(0, optionAnswer);
131 		StackManager.resolveStack();
132 	}
133 
134 	/***
135 	 * The type of message box
136 	 * 
137 	 * @see net.sf.magicproject.token.IdMessageBox
138 	 */
139 	private IdMessageBox type;
140 
141 }