View Javadoc

1   /*
2    * Created on Sep 6, 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.util.List;
25  
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.MEventListener;
29  import net.sf.magicproject.expression.Expression;
30  import net.sf.magicproject.operation.Operation;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.test.Test;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.86 Operation is used instead of add/remove boolean
37   *        <code>hasNot</code>
38   */
39  public class ColorModifier extends Modifier {
40  
41  	/***
42  	 * Creates a new instance of ColorModifier <br>
43  	 * 
44  	 * @param name
45  	 *          the modifier name
46  	 * @param to
47  	 *          the component to be attached to
48  	 * @param ability
49  	 *          is the ability owning this test. The card component of this
50  	 *          ability should correspond to the card owning this test too.
51  	 * @param whileCondition
52  	 *          the test conditionning the activation of this modifier.
53  	 * @param linkedEvents
54  	 *          the events to be registered. This events would cause this modifier
55  	 *          to be updated.
56  	 * @param until
57  	 *          this modifier exists while this test is true.
58  	 * @param linked
59  	 *          is the creator is linked to this modifier.
60  	 * @param layer
61  	 *          is the strategy used to add this modifier within the existing
62  	 *          chain.
63  	 * @param idColor
64  	 *          the color to add/remove/...
65  	 * @param op
66  	 *          the operation applied to previous value with the value of this
67  	 *          modifier.
68  	 */
69  	ColorModifier(String name, MCard to, Ability ability, Test whileCondition,
70  			List<MEventListener> linkedEvents, List<MEventListener> until,
71  			boolean linked, int layer, Expression idColor, Operation op) {
72  		super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
73  		this.idColor = idColor;
74  		this.op = op;
75  	}
76  
77  	/***
78  	 * Return the modified idColor
79  	 * 
80  	 * @param oldIdColor
81  	 *          The previous idColor.
82  	 * @return the modified idColor.
83  	 */
84  	public int getIdColor(int oldIdColor) {
85  		if (activated) {
86  			if (next != null) {
87  				return ((ColorModifier) next).getIdColor(op.process(oldIdColor, idColor
88  						.getValue(ability, to, null)));
89  			}
90  			return op.process(oldIdColor, idColor.getValue(ability, to, null));
91  		}
92  		if (next != null) {
93  			return ((ColorModifier) next).getIdColor(oldIdColor);
94  		}
95  		return oldIdColor;
96  	}
97  
98  	@Override
99  	public Modifier removeModifier(Modifier modifier) {
100 		if (this == modifier) {
101 			if (activated) {
102 				StackManager.postRefreshColor(to);
103 			}
104 			return next;
105 		}
106 		return super.removeModifier(modifier);
107 	}
108 
109 	@Override
110 	public final void removeFromManager() {
111 		super.removeFromManager();
112 		if (!unregisteredModifier) {
113 			to.removeModifier(this);
114 		}
115 		unregisteredModifier = true;
116 	}
117 
118 	@Override
119 	public void refresh() {
120 		boolean oldActivated = activated;
121 		activated = whileCondition.test(ability, to);
122 
123 		// this color has changed
124 		if (oldActivated != activated) {
125 			StackManager.postRefreshColor(to);
126 		}
127 	}
128 
129 	/***
130 	 * The modified idColor
131 	 */
132 	protected Expression idColor;
133 
134 	/***
135 	 * Indicates the operation applied to previous value with the value of this
136 	 * modifier.
137 	 */
138 	protected Operation op;
139 
140 }