View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.magicproject.modifier;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.card.MCard;
27  import net.sf.magicproject.clickable.targetable.card.SystemCard;
28  import net.sf.magicproject.event.context.ContextEventListener;
29  import net.sf.magicproject.expression.Expression;
30  import net.sf.magicproject.expression.ExpressionFactory;
31  import net.sf.magicproject.expression.IntValue;
32  import net.sf.magicproject.operation.Operation;
33  import net.sf.magicproject.operation.OperationFactory;
34  import net.sf.magicproject.test.True;
35  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
36  
37  /***
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.85 Recalculate attribute is supported
40   */
41  public class RegisterIndirectionModel extends ModifierModel {
42  
43  	/***
44  	 * Creates a new instance of RegisterIndirectionModel <br>
45  	 * <ul>
46  	 * Structure of InputStream : Data[size]
47  	 * <li>[super]</li>
48  	 * <li>index [int]</li>
49  	 * <li>right value [Expression]</li>
50  	 * <li>operation [Operation]</li>
51  	 * </ul>
52  	 * 
53  	 * @param inputFile
54  	 *          is the file containing this event
55  	 * @throws IOException
56  	 *           if error occurred during the reading process from the specified
57  	 *           input stream
58  	 * @see net.sf.magicproject.token.IdTokens
59  	 */
60  	RegisterIndirectionModel(InputStream inputFile) throws IOException {
61  		super(inputFile);
62  		index = ExpressionFactory.readNextExpression(inputFile);
63  		rightExpression = ExpressionFactory.readNextExpression(inputFile);
64  		rightExpression.extractTriggeredEvents(linkedEvents, SystemCard.instance,
65  				True.getInstance());
66  		op = OperationFactory.readNextOperation(inputFile);
67  	}
68  
69  	@Override
70  	public void addModifierFromModel(Ability ability, MCard target) {
71  		int index = this.index.getValue(ability, null, null);
72  		RegisterIndirection newModifier = new RegisterIndirection(name, target,
73  				ability, whileCondition, linkedEvents, until, linked, layer, index,
74  				recalculate ? rightExpression : new IntValue(rightExpression.getValue(
75  						ability, target, null)), op);
76  		target.addModifier(newModifier, index);
77  		newModifier.refresh();
78  		if (next != null) {
79  			next.addModifierFromModel(ability, target);
80  		}
81  	}
82  
83  	/***
84  	 * Create a new instance from the given model.
85  	 * 
86  	 * @param other
87  	 *          the instance to copy
88  	 */
89  	protected RegisterIndirectionModel(RegisterIndirectionModel other) {
90  		super(other);
91  		index = other.index;
92  		rightExpression = other.rightExpression;
93  		op = other.op;
94  	}
95  
96  	@Override
97  	public RegisterIndirectionModel clone() {
98  		return new RegisterIndirectionModel(this);
99  	}
100 
101 	@Override
102 	public String toHtmlString(Ability ability, ContextEventListener context) {
103 		throw new InternalError("should not be called");
104 	}
105 
106 	/***
107 	 * Return the HTML code representing this action. If this action is named,
108 	 * it's name will be returned. If not, if existing, the picture associated to
109 	 * this action is returned. Otherwise, built-in action's text will be
110 	 * returned.
111 	 * 
112 	 * @param ability
113 	 *          is the ability owning this test. The card component of this
114 	 *          ability should correspond to the card owning this test too.
115 	 * @param context
116 	 * @param other
117 	 *          the immediatly followed register modifier instance.
118 	 * @return the HTML code representing this action. If this action is named,
119 	 *         it's name will be returned. If not, if existing, the picture
120 	 *         associated to this action is returned. Otherwise, built-in action's
121 	 *         text will be returned.
122 	 * @since 0.86
123 	 */
124 	public String toHtmlString(Ability ability, ContextEventListener context,
125 			RegisterIndirectionModel other) {
126 		int index = this.index.getPreemptionValue(ability, null, context);
127 		if (other == null) {
128 			return LanguageManagerMDB.getString("add-registerindirection-modifier-"
129 					+ op.getOperator() + "-" + index, String.valueOf(rightExpression
130 					.getValue(ability, null, context)));
131 		}
132 		int otherIndex = other.index.getPreemptionValue(ability, null, context);
133 		if (index > otherIndex) {
134 			return other.toHtmlString(ability, context, this);
135 		}
136 		return LanguageManagerMDB.getString("add-registerindirection-modifier-"
137 				+ op.getOperator() + "-" + index + "-" + other.op.getOperator() + "-"
138 				+ otherIndex,
139 				new String[] {
140 						String.valueOf(rightExpression.getValue(ability, null, context)),
141 						String.valueOf(other.rightExpression.getValue(ability, null,
142 								context)) });
143 	}
144 
145 	/***
146 	 * The looked for register index
147 	 */
148 	private final Expression index;
149 
150 	/***
151 	 * right integer value. The complex expression to use for the right value. Is
152 	 * null if the IdToken number is not a complex expression.
153 	 */
154 	private Expression rightExpression = null;
155 
156 	/***
157 	 * The associated operation of this indirection
158 	 */
159 	private Operation op;
160 
161 }