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.clickable.targetable.card.SystemCard;
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.test.True;
37  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
38  
39  /***
40   * @author <a modifier="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan
41   *         </a>
42   * @since 0.85 Recalculate attribute is supported
43   */
44  public class RegisterModifierModel extends ModifierModel {
45  
46  	/***
47  	 * Creates a new instance of RegisterModifier <br>
48  	 * <ul>
49  	 * Structure of InputStream : Data[size]
50  	 * <li>[super]</li>
51  	 * <li>index [Expression]</li>
52  	 * <li>right value [Expression]</li>
53  	 * <li>operation [Operation]</li>
54  	 * </ul>
55  	 * 
56  	 * @param inputFile
57  	 *          is the file containing this event
58  	 * @throws IOException
59  	 *           if error occurred during the reading process from the specified
60  	 *           input stream
61  	 * @see net.sf.magicproject.token.IdTokens
62  	 */
63  	RegisterModifierModel(InputStream inputFile) throws IOException {
64  		super(inputFile);
65  		index = ExpressionFactory.readNextExpression(inputFile);
66  		rightExpression = ExpressionFactory.readNextExpression(inputFile);
67  		rightExpression.extractTriggeredEvents(linkedEvents, SystemCard.instance,
68  				True.getInstance());
69  		op = OperationFactory.readNextOperation(inputFile);
70  	}
71  
72  	/***
73  	 * Create a new instance from the given model.
74  	 * 
75  	 * @param other
76  	 *          the instance to copy
77  	 */
78  	protected RegisterModifierModel(RegisterModifierModel other) {
79  		super(other);
80  		index = other.index;
81  		rightExpression = other.rightExpression;
82  		linkedEvents = other.linkedEvents;
83  		op = other.op;
84  	}
85  
86  	@Override
87  	public void addModifierFromModel(Ability ability, MCard target) {
88  		int index = this.index.getValue(ability, null, null);
89  		RegisterModifier newModifier = new RegisterModifier(name, target, ability,
90  				whileCondition, linkedEvents, until, linked, layer, index,
91  				recalculate ? rightExpression : new IntValue(rightExpression.getValue(
92  						ability, target, null)), op);
93  		target.addModifier(newModifier, index);
94  		newModifier.refresh();
95  		if (next != null) {
96  			next.addModifierFromModel(ability, target);
97  		}
98  	}
99  
100 	@Override
101 	public RegisterModifierModel clone() {
102 		return new RegisterModifierModel(this);
103 	}
104 
105 	@Override
106 	public String toHtmlString(Ability ability, ContextEventListener context) {
107 		throw new InternalError("should not be called");
108 	}
109 
110 	/***
111 	 * Return the HTML code representing this action. If this action is named,
112 	 * it's name will be returned. If not, if existing, the picture associated to
113 	 * this action is returned. Otherwise, built-in action's text will be
114 	 * returned.
115 	 * 
116 	 * @param ability
117 	 *          is the ability owning this test. The card component of this
118 	 *          ability should correspond to the card owning this test too.
119 	 * @param context
120 	 * @param other
121 	 *          the immediatly followed register modifier instance.
122 	 * @return the HTML code representing this action. If this action is named,
123 	 *         it's name will be returned. If not, if existing, the picture
124 	 *         associated to this action is returned. Otherwise, built-in action's
125 	 *         text will be returned.
126 	 * @since 0.86
127 	 */
128 	public String toHtmlString(Ability ability, ContextEventListener context,
129 			RegisterModifierModel other) {
130 		int index = this.index.getPreemptionValue(ability, null, context);
131 		if (other == null) {
132 			return LanguageManagerMDB.getString("add-register-modifier-"
133 					+ op.getOperator() + "-" + index, String.valueOf(rightExpression
134 					.getValue(ability, null, context)));
135 		}
136 		int otherIndex = other.index.getPreemptionValue(ability, null, context);
137 		if (index > otherIndex) {
138 			return other.toHtmlString(ability, context, this);
139 		}
140 		return LanguageManagerMDB.getString("add-register-modifier-"
141 				+ op.getOperator() + "-" + index + "-" + other.op.getOperator() + "-"
142 				+ otherIndex,
143 				new String[] {
144 						String.valueOf(rightExpression.getValue(ability, null, context)),
145 						String.valueOf(other.rightExpression.getValue(ability, null,
146 								context)) });
147 	}
148 
149 	/***
150 	 * The looked for register index
151 	 */
152 	protected final Expression index;
153 
154 	/***
155 	 * The operation applied to the left and right values
156 	 */
157 	protected final Operation op;
158 
159 	/***
160 	 * right integer value. The complex expression to use for the right value. Is
161 	 * null if the IdToken number is not a complex expression.
162 	 */
163 	protected final Expression rightExpression;
164 
165 }