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.objectmap;
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.context.Wrapper;
26  import net.sf.magicproject.action.handler.FollowAction;
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.Targetable;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.tools.MToolKit;
31  
32  /***
33   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34   * @since 0.90
35   */
36  class ObjectMapRemove extends ObjectMap implements FollowAction {
37  
38  	/***
39  	 * Create an instance of ObjectMapRemove by reading a file Offset's file
40  	 * pointing on the first byte of this action <br>
41  	 * <ul>
42  	 * Structure of InputStream : Data[size]
43  	 * <li>idAction [1]</li>
44  	 * <li>object's name + '\0' [...]</li>
45  	 * </ul>
46  	 * 
47  	 * @param inputFile
48  	 *          file containing this action
49  	 * @throws IOException
50  	 *           If some other I/O error occurs
51  	 */
52  	ObjectMapRemove(InputStream inputFile) throws IOException {
53  		super(inputFile);
54  		objectName = MToolKit.readString(inputFile);
55  	}
56  
57  	@Override
58  	public boolean play(ContextEventListener context, Ability ability) {
59  		ability.getTargetable().removePrivateNamedObject(objectName);
60  		return true;
61  	}
62  
63  	@Override
64  	public String toString(Ability ability) {
65  		return "";
66  	}
67  
68  	public void simulate(ActionContextWrapper actionContext,
69  			ContextEventListener context, Ability ability) {
70  		final Wrapper<Targetable> mapContext = new Wrapper<Targetable>();
71  		actionContext.actionContext = mapContext;
72  		mapContext.setObject(ability.getTargetable().getPrivateNamedObject(
73  				objectName));
74  		play(context, ability);
75  	}
76  
77  	public void rollback(ActionContextWrapper actionContext,
78  			ContextEventListener context, Ability ability) {
79  		final Targetable oldTargetable = ((Wrapper<Targetable>) actionContext.actionContext)
80  				.getObject();
81  		if (oldTargetable != null) {
82  			ability.getTargetable().addPrivateNamedObject(objectName, oldTargetable);
83  		}
84  	}
85  
86  	/***
87  	 * The object's name to manipulate.
88  	 */
89  	private String objectName;
90  }