View Javadoc

1   /*
2    * Created on Dec 28, 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  package net.sf.magicproject.modifier;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.ability.AbilityFactory;
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.operation.Operation;
32  import net.sf.magicproject.operation.OperationFactory;
33  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
34  
35  /***
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.82
38   */
39  public class AbilityModifierModel extends ModifierModel {
40  
41  	/***
42  	 * Creates a new instance of AbilityModifierModel <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>[super]</li>
46  	 * <li>operation [Operation]</li>
47  	 * <li>abilities [Ability[]]</li>
48  	 * </ul>
49  	 * 
50  	 * @param inputFile
51  	 *          the input stream where action will be read
52  	 * @throws IOException
53  	 *           if error occurred during the reading process from the specified
54  	 *           input stream
55  	 */
56  	AbilityModifierModel(InputStream inputFile) throws IOException {
57  		super(inputFile);
58  		this.op = OperationFactory.readNextOperation(inputFile);
59  		abilitiesToAdd = new Ability[inputFile.read()];
60  		for (int i = abilitiesToAdd.length; i-- > 0;) {
61  			abilitiesToAdd[i] = AbilityFactory.readAbility(inputFile,
62  					SystemCard.instance);
63  		}
64  	}
65  
66  	@Override
67  	public void addModifierFromModel(Ability ability, MCard target) {
68  		final Ability[] copyAbilities = new Ability[abilitiesToAdd.length];
69  		for (int i = copyAbilities.length; i-- > 0;) {
70  			copyAbilities[i] = abilitiesToAdd[i].clone(target);
71  		}
72  		final AbilityModifier newModifier = new AbilityModifier(name, target,
73  				ability, whileCondition, linkedEvents, until, linked, layer, op,
74  				copyAbilities);
75  		target.addModifier(newModifier);
76  		newModifier.refresh();
77  		if (next != null) {
78  			next.addModifierFromModel(ability, target);
79  		}
80  	}
81  
82  	/***
83  	 * Create a new instance from the given model.
84  	 * 
85  	 * @param other
86  	 *          the instance to copy
87  	 */
88  	protected AbilityModifierModel(AbilityModifierModel other) {
89  		super(other);
90  		op = other.op;
91  		abilitiesToAdd = other.abilitiesToAdd;
92  	}
93  
94  	@Override
95  	public AbilityModifierModel clone() {
96  		return new AbilityModifierModel(this);
97  	}
98  
99  	@Override
100 	public String toHtmlString(Ability ability, ContextEventListener context) {
101 		if (abilitiesToAdd.length == 0) {
102 			return LanguageManagerMDB.getString("add-ability-modifier-"
103 					+ op.getOperator());
104 		}
105 		return LanguageManagerMDB.getString("add-ability-modifier-"
106 				+ (op.getOperator()), abilitiesToAdd[0].toHtmlString(context));
107 	}
108 
109 	/***
110 	 * The list of abilities added/removed by this modifier
111 	 */
112 	protected Ability[] abilitiesToAdd;
113 
114 	/***
115 	 * Indicates the operation applied to previous value with the value of this
116 	 * modifier.
117 	 */
118 	protected Operation op;
119 }