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.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 }