View Javadoc

1   /*
2    * Created on 4 févr. 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.action.objectmap;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.magicproject.action.context.ActionContextWrapper;
27  import net.sf.magicproject.action.context.Wrapper;
28  import net.sf.magicproject.action.handler.FollowAction;
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.clickable.targetable.Targetable;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.test.TestOn;
33  import net.sf.magicproject.tools.MToolKit;
34  
35  /***
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.90
38   */
39  class ObjectMapSave extends ObjectMap implements FollowAction {
40  
41  	/***
42  	 * Create an instance of ObjectMapSave by reading a file Offset's file must
43  	 * pointing on the first byte of this action <br>
44  	 * <ul>
45  	 * Structure of InputStream : Data[size]
46  	 * <li>super [ObjectMap]</li>
47  	 * <li>testOn : the component this action is applyed on [TestOn]</li>
48  	 * <li>object's name [String]</li>
49  	 * </ul>
50  	 * 
51  	 * @param inputStream
52  	 *          file containing this action
53  	 * @throws IOException
54  	 *           If some other I/O error occurs
55  	 */
56  	ObjectMapSave(InputStream inputStream) throws IOException {
57  		super(inputStream);
58  		testOn = TestOn.deserialize(inputStream);
59  		objectName = MToolKit.readString(inputStream);
60  	}
61  
62  	@Override
63  	public boolean play(ContextEventListener context, Ability ability) {
64  		ability.getTargetable().addPrivateNamedObject(objectName,
65  				testOn.getTargetable(ability, null));
66  		return true;
67  	}
68  
69  	@Override
70  	public String toString(Ability ability) {
71  		return "";
72  	}
73  
74  	public void simulate(ActionContextWrapper actionContext,
75  			ContextEventListener context, Ability ability) {
76  		final Wrapper<Targetable> mapContext = new Wrapper<Targetable>();
77  		actionContext.actionContext = mapContext;
78  		mapContext.setObject(ability.getTargetable().getPrivateNamedObject(
79  				objectName));
80  		play(context, ability);
81  	}
82  
83  	public void rollback(ActionContextWrapper actionContext,
84  			ContextEventListener context, Ability ability) {
85  		final Targetable oldTargetable = ((Wrapper<Targetable>) actionContext.actionContext)
86  				.getObject();
87  		if (oldTargetable == null) {
88  			ability.getTargetable().removePrivateNamedObject(objectName);
89  		} else {
90  			ability.getTargetable().addPrivateNamedObject(objectName, oldTargetable);
91  		}
92  	}
93  
94  	/***
95  	 * IdToken indentifying the first targetable to save. If is 0, no card is
96  	 * saved.
97  	 */
98  	private TestOn testOn;
99  
100 	/***
101 	 * The object's name to manipulate.
102 	 */
103 	private String objectName;
104 }