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