View Javadoc

1   /*
2    * Created on 4 févr. 2005
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.action.targetlist;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.magicproject.action.Actiontype;
27  import net.sf.magicproject.action.UserAction;
28  import net.sf.magicproject.action.handler.StandardAction;
29  import net.sf.magicproject.clickable.ability.Ability;
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.stack.StackManager;
34  import net.sf.magicproject.token.IdConst;
35  
36  /***
37   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38   * @since 0.82
39   */
40  public abstract class TargetList extends UserAction implements StandardAction {
41  
42  	/***
43  	 * Create an instance of TargetList by reading a file Offset's file must
44  	 * pointing on the first byte of this action <br>
45  	 * <ul>
46  	 * Structure of InputStream : Data[size]
47  	 * <li>idAction [1]</li>
48  	 * </ul>
49  	 * 
50  	 * @param inputFile
51  	 *          file containing this action
52  	 * @throws IOException
53  	 *           If some other I/O error occurs
54  	 */
55  	protected TargetList(InputStream inputFile) throws IOException {
56  		super(inputFile);
57  		listIndex = ExpressionFactory.readNextExpression(inputFile);
58  	}
59  
60  	@Override
61  	public final Actiontype getIdAction() {
62  		return Actiontype.TARGET_LIST;
63  	}
64  
65  	public abstract boolean play(ContextEventListener context, Ability ability);
66  
67  	@Override
68  	public abstract String toString(Ability ability);
69  
70  	@Override
71  	public final String toHtmlString(Ability ability, int times,
72  			ContextEventListener context) {
73  		if (actionName != null && actionName.charAt(0) != '%'
74  				&& actionName.charAt(0) != '@' && actionName.indexOf("%n") != -1) {
75  			return super.toHtmlString(ability, times, context);
76  		}
77  		return "";
78  	}
79  
80  	/***
81  	 * Return the list index.
82  	 * 
83  	 * @param context
84  	 *          is the context attached to this action.
85  	 * @param ability
86  	 *          is the ability owning this test. The card component of this
87  	 *          ability should correspond to the card owning this test too.
88  	 * @return the list index.
89  	 */
90  	protected int getlistIndex(Ability ability, ContextEventListener context) {
91  		final int listIndex = this.listIndex.getValue(ability, null, context);
92  		if (listIndex == IdConst.ALL) {
93  			if (StackManager.SAVED_TARGET_LISTS.isEmpty()) {
94  				throw new RuntimeException(
95  						"Non empty saved target-list was expected ability="
96  								+ ability.getLog(context) + ", action=" + this);
97  			}
98  			return StackManager.SAVED_TARGET_LISTS.size() - 1;
99  		}
100 		return listIndex;
101 	}
102 
103 	/***
104 	 * The index of list where this operation would be applied on. If this index
105 	 * is negative ie. -1, the operation would be applied on current target-list.
106 	 */
107 	private final Expression listIndex;
108 }