View Javadoc

1   /*
2    *   Firemox 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.firemox.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.firemox.clickable.ability.Ability;
25  import net.sf.firemox.clickable.target.Target;
26  import net.sf.firemox.clickable.target.card.MCard;
27  import net.sf.firemox.deckbuilder.MdbLoader;
28  import net.sf.firemox.event.context.ContextEventListener;
29  import net.sf.firemox.modifier.model.ObjectFactory;
30  import net.sf.firemox.test.True;
31  import net.sf.firemox.test.TestOn;
32  import net.sf.firemox.tools.MToolKit;
33  import net.sf.firemox.ui.i18n.LanguageManagerMDB;
34  
35  /***
36   * Move an object from a component to another component.
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.93
40   */
41  public class MoveObject extends UserAction implements LoopAction {
42  
43  	/***
44  	 * Create an instance of MoveObject
45  	 * <ul>
46  	 * Structure of stream : Data[size]
47  	 * <li>object name [String]</li>
48  	 * <li>from [TestOn]</li>
49  	 * <li>to [TestOn]</li>
50  	 * </ul>
51  	 * 
52  	 * @param inputFile
53  	 *          is ignored
54  	 * @param card
55  	 *          owning this action
56  	 * @throws IOException
57  	 *           If some other I/O error occurs
58  	 */
59  	MoveObject(InputStream inputFile) throws IOException {
60  		super(inputFile);
61  		objectName = MToolKit.readString(inputFile).intern();
62  		from = TestOn.deserialize(inputFile);
63  		to = TestOn.deserialize(inputFile);
64  	}
65  
66  	@Override
67  	public Actiontype getIdAction() {
68  		return Actiontype.MOVE_OBJECT;
69  	}
70  
71  	@Override
72  	public String toString(Ability ability) {
73  		return "moveobject-" + objectName;
74  	}
75  
76  	@Override
77  	public String toHtmlString(Ability ability, int times,
78  			ContextEventListener context) {
79  		if (times == 1) {
80  			return LanguageManagerMDB.getString("moveobject-1", objectName
81  					.replaceAll("/", ""), from.toString(), to.toString());
82  		}
83  		if (times == -1) {
84  			// Preemption
85  			return LanguageManagerMDB.getString("moveobject-%n",
86  					objectName.replaceAll("/", ""), from.toString(), to.toString())
87  					.replaceAll("%n", "" + MdbLoader.unknownSmlManaHtml);
88  		}
89  		return LanguageManagerMDB.getString("moveobject-%n",
90  				objectName.replaceAll("/", ""), from.toString(), to.toString())
91  				.replaceAll("%n", "" + times);
92  	}
93  
94  	@Override
95  	public String toHtmlString(Ability ability, ContextEventListener context) {
96  		if (actionName != null) {
97  			if (actionName.charAt(0) == '%') {
98  				return "";
99  			}
100 			if (actionName.charAt(0) == '@') {
101 				final String picture = ActionFactory.PICTURES.get(actionName);
102 				if (picture != null) {
103 					return toHtmlString(ability, picture);
104 				}
105 			} else {
106 				return LanguageManagerMDB.getString(actionName);
107 			}
108 		}
109 		return LanguageManagerMDB.getString("moveobject-1", objectName.replaceAll(
110 				"/", ""), from.toString(), to.toString());
111 	}
112 
113 	public boolean continueLoop(ContextEventListener context, int loopingIndex,
114 			Ability ability) {
115 		// final Targetable from = StackManager.getInstance().getTargetedList().get(
116 		// loopingIndex);
117 		final MCard from = this.from.getCard(ability, null);
118 		if (!from.isCard()) {
119 			throw new InternalError(
120 					"TODO MoveObject action is only supported for Card component");
121 		}
122 		if (checkTimeStamp(context, from)) {
123 			ObjectFactory.removeObjectModifier(objectName, from, True.getInstance());
124 			MCard to = this.to.getCard(ability, null);
125 			ObjectFactory.getObjectModifierModel(objectName).addModifierFromModel(
126 					ability, to);
127 			from.repaint();
128 			to.repaint();
129 		}
130 		return true;
131 	}
132 
133 	/***
134 	 * @param ability
135 	 *          is the ability owning this test. The card component of this
136 	 *          ability should correspond to the card owning this test too.
137 	 * @param target
138 	 *          the component where objects will be removed from
139 	 * @param nbObjects
140 	 *          amount of required object
141 	 * @return true if the specified target contains the required object(s)
142 	 */
143 	public boolean checkObject(Ability ability, Target target, int nbObjects) {
144 		if (!target.isCard()) {
145 			throw new InternalError(
146 					"TODO MmoveObject action is only supported for Card component");
147 		}
148 		return ObjectFactory.getNbObject(objectName, (MCard) target, True
149 				.getInstance()) >= nbObjects;
150 	}
151 
152 	public int getStartIndex() {
153 		return 0;
154 	}
155 
156 	/***
157 	 * The object's name to remove from the current target list.
158 	 */
159 	private final String objectName;
160 
161 	/***
162 	 * The object's source of move.
163 	 */
164 	private final TestOn from;
165 
166 	/***
167 	 * The object's destination of move.
168 	 */
169 	private final TestOn to;
170 
171 }