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.MCard;
29 import net.sf.magicproject.event.context.ContextEventListener;
30 import net.sf.magicproject.expression.Expression;
31 import net.sf.magicproject.expression.ExpressionFactory;
32 import net.sf.magicproject.expression.IntValue;
33 import net.sf.magicproject.operation.Operation;
34 import net.sf.magicproject.operation.OperationFactory;
35 import net.sf.magicproject.token.IdCardColors;
36 import net.sf.magicproject.ui.i18n.LanguageManager;
37 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
38
39 /***
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 * @since 0.86 Operation is used instead of add/remove boolean
42 * <code>hasNot</code>
43 */
44 public class ColorModifierModel extends ModifierModel {
45
46 /***
47 * Creates a new instance of ColorModifierModel <br>
48 * <ul>
49 * Structure of InputStream : Data[size]
50 * <li>[super]</li>
51 * <li>op [Operation]</li>
52 * <li>idColor [Expression]</li>
53 * </ul>
54 *
55 * @param inputFile
56 * the input stream where action will be read
57 * @throws IOException
58 * if error occurred during the reading process from the specified
59 * input stream
60 */
61 ColorModifierModel(InputStream inputFile) throws IOException {
62 super(inputFile);
63 this.op = OperationFactory.readNextOperation(inputFile);
64 this.idColor = ExpressionFactory.readNextExpression(inputFile);
65 }
66
67 /***
68 * Create a new instance from the given model.
69 *
70 * @param other
71 * the instance to copy
72 */
73 protected ColorModifierModel(ColorModifierModel other) {
74 super(other);
75 idColor = other.idColor;
76 op = other.op;
77 }
78
79 @Override
80 public ColorModifierModel clone() {
81 return new ColorModifierModel(this);
82 }
83
84 @Override
85 public void addModifierFromModel(Ability ability, MCard target) {
86 final ColorModifier newModifier = new ColorModifier(name, target, ability,
87 whileCondition, linkedEvents, until, linked, layer,
88 recalculate ? idColor : new IntValue(idColor.getValue(ability, target,
89 null)), op);
90 target.addModifier(newModifier);
91 newModifier.refresh();
92 if (next != null) {
93 next.addModifierFromModel(ability, target);
94 }
95 }
96
97 @Override
98 public String toHtmlString(Ability ability, ContextEventListener context) {
99 final int idColor = this.idColor.getValue(ability, null, context);
100 String tmpString = null;
101 for (int i = IdCardColors.CARD_COLOR_VALUES.length; i-- > 1;) {
102 if (MCard.hasIdColor(idColor, IdCardColors.CARD_COLOR_VALUES[i])) {
103 if (tmpString == null) {
104 tmpString = LanguageManager
105 .getString(IdCardColors.CARD_COLOR_NAMES[i]);
106 } else {
107 tmpString += ", "
108 + LanguageManager.getString(IdCardColors.CARD_COLOR_NAMES[i]);
109 }
110 }
111 }
112 return LanguageManagerMDB.getString("add-color-modifier-"
113 + op.getOperator(), tmpString);
114 }
115
116 /***
117 * The modified idColor
118 */
119 protected Expression idColor;
120
121 /***
122 * Indicates the operation applied to previous value with the value of this
123 * modifier.
124 */
125 protected Operation op;
126
127 }