View Javadoc

1   /*
2    * Created on Sep 14, 2004 
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   */
22  package net.sf.magicproject.modifier;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.card.CardFactory;
29  import net.sf.magicproject.clickable.targetable.card.MCard;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.expression.Expression;
32  import net.sf.magicproject.expression.ExpressionFactory;
33  import net.sf.magicproject.expression.IntValue;
34  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
35  
36  /***
37   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38   * @since 0.86 properties set is calculated as colors/idcard and register : they
39   *        follow timestamp rule.
40   */
41  public class PropertyModifierModel extends ModifierModel {
42  
43  	/***
44  	 * Creates a new instance of PropertyModifier <br>
45  	 * <ul>
46  	 * Structure of InputStream : Data[size]
47  	 * <li>[super]</li>
48  	 * <li>addProperty [boolean]</li>
49  	 * <li>propertyId [Expression]</li>
50  	 * </ul>
51  	 * 
52  	 * @param inputFile
53  	 *          the input stream where action will be read
54  	 * @throws IOException
55  	 *           if error occurred during the reading process from the specified
56  	 *           input stream
57  	 */
58  	PropertyModifierModel(InputStream inputFile) throws IOException {
59  		super(inputFile);
60  		addProperty = inputFile.read() == 1;
61  		propertyId = ExpressionFactory.readNextExpression(inputFile);
62  	}
63  
64  	/***
65  	 * Create a new instance from the given model.
66  	 * 
67  	 * @param other
68  	 *          the instance to copy
69  	 */
70  	protected PropertyModifierModel(PropertyModifierModel other) {
71  		super(other);
72  		addProperty = other.addProperty;
73  		propertyId = other.propertyId;
74  	}
75  
76  	@Override
77  	public void addModifierFromModel(Ability ability, MCard target) {
78  		final PropertyModifier newModifier = new PropertyModifier(name, target,
79  				ability, whileCondition, linkedEvents, until, linked, layer,
80  				recalculate ? propertyId : new IntValue(propertyId.getValue(ability,
81  						target, null)), addProperty);
82  		target.addModifier(newModifier);
83  		newModifier.refresh();
84  		if (next != null) {
85  			next.addModifierFromModel(ability, target);
86  		}
87  	}
88  
89  	@Override
90  	public PropertyModifierModel clone() {
91  		return new PropertyModifierModel(this);
92  	}
93  
94  	@Override
95  	public String toHtmlString(Ability ability, ContextEventListener context) {
96  		int pValue = propertyId.getValue(ability, null, context);
97  		final String propertyName = CardFactory.exportedProperties.get(pValue);
98  		if (propertyName == null) {
99  			// not name associated to this property
100 			if (addProperty)
101 				return LanguageManagerMDB.getString("add-property-modifier-add",
102 						"<font color='red'>(" + pValue + ")</font>");
103 			return LanguageManagerMDB.getString("add-property-modifier-remove",
104 					"<font color='red'>(" + pValue + ")</font>");
105 		}
106 		if (addProperty)
107 			return LanguageManagerMDB.getString("add-property-modifier-add",
108 					propertyName);
109 		return LanguageManagerMDB.getString("add-property-modifier-remove",
110 				propertyName);
111 	}
112 
113 	/***
114 	 * Indicates if this modifier remove occurence of the given property. If True,
115 	 * it adds one instance.
116 	 */
117 	protected boolean addProperty;
118 
119 	/***
120 	 * Property Id to add/remove
121 	 */
122 	protected Expression propertyId;
123 
124 }