1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }