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