View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.magicproject.action.context.ActionContextWrapper;
25  import net.sf.magicproject.action.handler.InitAction;
26  import net.sf.magicproject.action.handler.StandardAction;
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.player.Player;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.network.ConnectionManager;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.test.TestOn;
33  import net.sf.magicproject.tools.Log;
34  import net.sf.magicproject.tools.MToolKit;
35  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
36  import net.sf.magicproject.ui.wizard.Wizard;
37  
38  /***
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.86
41   */
42  public abstract class MessagingAction extends UserAction implements
43  		BackgroundMessaging, StandardAction, InitAction {
44  
45  	/***
46  	 * Create an instance of Msg by reading a file Offset's file must pointing on
47  	 * the first byte of this action. <br>
48  	 * <ul>
49  	 * Structure of InputStream : Data[size]
50  	 * <li>controller [TestOn]</li>
51  	 * <li>text to display +'/0' [...]</li>
52  	 * </ul>
53  	 * 
54  	 * @param inputFile
55  	 *          file containing this action
56  	 * @throws IOException
57  	 *           if error occurred during the reading process from the specified
58  	 *           input stream
59  	 */
60  	MessagingAction(InputStream inputFile) throws IOException {
61  		super(inputFile);
62  		controller = TestOn.deserialize(inputFile);
63  		text = MToolKit.readString(inputFile);
64  		if (text.startsWith("$") || text.startsWith("%")) {
65  			text = LanguageManagerMDB.getString(text.substring(1));
66  		}
67  	}
68  
69  	/***
70  	 * Replay the current action as it was when it has been suspended.
71  	 * 
72  	 * @param context
73  	 *          is the context attached to this action.
74  	 * @param ability
75  	 *          is the ability owning this test. The card component of this
76  	 *          ability should correspond to the card owning this test too.
77  	 * @param wizard
78  	 *          the hidden wizard frame
79  	 */
80  	public final void replayAction(ContextEventListener context, Ability ability,
81  			Wizard wizard) {
82  		wizard.setVisible(true);
83  		if (Wizard.optionAnswer != Wizard.BACKGROUND_OPTION) {
84  			// valid answer
85  			boolean taken = false;
86  			if (!StackManager.noReplayToken.takeNoBlock()) {
87  				taken = true;
88  			}
89  			ConnectionManager.sendToOpponent(getMessagingActionId(),
90  					Wizard.optionAnswer, Wizard.indexAnswer);
91  			finishedMessage(Wizard.optionAnswer, Wizard.indexAnswer, context,
92  					ability, StackManager.actionManager.getActionContextNull());
93  			if (taken) {
94  				StackManager.noReplayToken.release();
95  			}
96  		}
97  	}
98  
99  	/***
100 	 * The returned value is piped to the 'modifyregister' action.
101 	 * 
102 	 * @param optionAnswer
103 	 *          the option value. yes/no/cancel,...
104 	 * @param indexAnswer
105 	 *          the optionnal value.
106 	 * @param context
107 	 *          the context of playing ability.
108 	 * @param ability
109 	 *          the playing ability.
110 	 * @param actionContext
111 	 *          the context of this action.
112 	 */
113 	public static final void finishedMessage(int optionAnswer, int indexAnswer,
114 			ContextEventListener context, Ability ability,
115 			ActionContextWrapper actionContext) {
116 		Log.debug("option was " + optionAnswer + ", index was " + indexAnswer);
117 		Player.unsetHandedPlayer();
118 		((MessagingAction) StackManager.actionManager.currentAction).setAnswer(
119 				optionAnswer, indexAnswer, context, ability, actionContext);
120 	}
121 
122 	public final boolean init(ActionContextWrapper actionContext,
123 			ContextEventListener context, Ability ability) {
124 		play(context, ability);
125 		return false;
126 	}
127 
128 	public boolean replay(ActionContextWrapper actionContext,
129 			ContextEventListener context, Ability ability) {
130 		// nothing to do
131 		return true;
132 	}
133 
134 	public abstract boolean play(ContextEventListener context, Ability ability);
135 
136 	/***
137 	 * The value is piped to the 'modifyregister' action.
138 	 * 
139 	 * @param optionAnswer
140 	 *          the option value. yes/no/cancel,...
141 	 * @param indexAnswer
142 	 *          the optionnal value.
143 	 * @param context
144 	 *          the context of playing ability.
145 	 * @param ability
146 	 *          the playing ability.
147 	 * @param actionContext
148 	 *          the context of this action.
149 	 */
150 	protected abstract void setAnswer(int optionAnswer, int indexAnswer,
151 			ContextEventListener context, Ability ability,
152 			ActionContextWrapper actionContext);
153 
154 	/***
155 	 * Return the message id
156 	 * 
157 	 * @return the message id
158 	 */
159 	protected abstract int getMessagingActionId();
160 
161 	/***
162 	 * The the player would answer to this message.
163 	 */
164 	protected TestOn controller;
165 
166 	/***
167 	 * The text to display
168 	 */
169 	protected String text;
170 }