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.modifier.ObjectModifierModel;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.tools.MToolKit;
33  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
34  
35  /***
36   * 
37   */
38  class AddObject extends UserAction implements LoopAction {
39  
40  	/***
41  	 * <ul>
42  	 * Structure of stream : Data[size]
43  	 * <li>[super]</li>
44  	 * <li>object name [String]</li>
45  	 * <li>model [ObjectModel]</li>
46  	 * </ul>
47  	 * 
48  	 * @param inputFile
49  	 *          stream containing this action.
50  	 * @throws IOException
51  	 *           If some other I/O error occurs
52  	 * @see Actiontype
53  	 */
54  	AddObject(InputStream inputFile) throws IOException {
55  		super(inputFile);
56  		final String objectName = MToolKit.readString(inputFile).intern();
57  		final ObjectModifierModel object = ObjectFactory
58  				.getObjectModifierModel(objectName);
59  		if (object == null) {
60  			throw new RuntimeException("Unknown Object '" + objectName + "'");
61  		}
62  		objectModifierModel = (ObjectModifierModel) object.clone();
63  	}
64  
65  	@Override
66  	public Actiontype getIdAction() {
67  		return Actiontype.ADD_OBJECT;
68  	}
69  
70  	@Override
71  	public String toString(Ability ability) {
72  		return "addobject-" + objectModifierModel.getObjectName();
73  	}
74  
75  	@Override
76  	public String toHtmlString(Ability ability, int times,
77  			ContextEventListener context) {
78  		if (times == 1) {
79  			return LanguageManagerMDB.getString("addobject-1-"
80  					+ objectModifierModel.getObjectName().replaceAll("/", ""));
81  		}
82  		if (times == -1) {
83  			// Preemption
84  			return LanguageManagerMDB.getString("addobject-%n",
85  					objectModifierModel.getObjectName().replaceAll("/", "")).replaceAll(
86  					"%n", "" + MdbLoader.unknownSmlManaHtml);
87  		}
88  		return LanguageManagerMDB.getString("addobject-%n",
89  				objectModifierModel.getObjectName().replaceAll("/", "")).replaceAll(
90  				"%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("addobject-1", objectModifierModel
109 				.getObjectName().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 					"AddObject action is only supported for Card component");
119 		}
120 		if (checkTimeStamp(context, (MCard) target)) {
121 			objectModifierModel.addModifierFromModel(ability, (MCard) target);
122 			target.repaint();
123 		}
124 		return true;
125 	}
126 
127 	public int getStartIndex() {
128 		return StackManager.getInstance().getTargetedList().size() - 1;
129 	}
130 
131 	/***
132 	 * The object to add to the current target list.
133 	 */
134 	private ObjectModifierModel objectModifierModel;
135 }