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  package net.sf.magicproject.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.magicproject.clickable.ability.Ability;
25  import net.sf.magicproject.clickable.targetable.card.MCard;
26  import net.sf.magicproject.event.context.ContextEventListener;
27  import net.sf.magicproject.tools.Log;
28  import net.sf.magicproject.tools.MToolKit;
29  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
30  
31  import org.apache.commons.lang.StringUtils;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   * @see net.sf.magicproject.action.Actiontype
36   * @since 0.85 named action is supported.
37   */
38  public abstract class UserAction extends MAction {
39  
40  	/***
41  	 * Create an instance of MAction
42  	 * <ul>
43  	 * Structure of Stream : Data[size]
44  	 * <li>action name [String]</li>
45  	 * <li>debug data [String]</li>
46  	 * </ul>
47  	 * 
48  	 * @param inputStream
49  	 *          stream containing this action.
50  	 * @throws IOException
51  	 *           if error occurred during the reading process from the specified
52  	 *           input stream
53  	 */
54  	protected UserAction(InputStream inputStream) throws IOException {
55  		this(MToolKit.readString(inputStream), inputStream);
56  	}
57  
58  	/***
59  	 * Creates a new instance of UserAction <br>
60  	 * <ul>
61  	 * Structure of Stream : Data[size]
62  	 * <li>action name [String]</li>
63  	 * </ul>
64  	 * 
65  	 * @param actionName
66  	 *          the action's name.
67  	 * @param inputStream
68  	 *          stream containing this action.
69  	 */
70  	protected UserAction(String actionName, InputStream inputStream) {
71  		this(actionName, MToolKit.readText(inputStream));
72  	}
73  
74  	/***
75  	 * Creates a new instance of UserAction <br>
76  	 * 
77  	 * @param debugData
78  	 *          debug information.
79  	 * @param actionName
80  	 *          the action's name.
81  	 */
82  	protected UserAction(String actionName, String debugData) {
83  		super(debugData);
84  		if (actionName != null) {
85  			if (actionName.length() == 0) {
86  				this.actionName = null;
87  			} else {
88  				this.actionName = actionName.intern();
89  			}
90  		}
91  		ActionFactory.currentAction = this;
92  	}
93  
94  	/***
95  	 * Creates a new instance of UserAction <br>
96  	 * 
97  	 * @param actionName
98  	 *          the action's name.
99  	 */
100 	protected UserAction(String actionName) {
101 		this(actionName, (String) null);
102 	}
103 
104 	@Override
105 	public abstract Actiontype getIdAction();
106 
107 	@Override
108 	public int[] manaNeeded(Ability ability, ContextEventListener context) {
109 		return null;
110 	}
111 
112 	@Override
113 	public String toHtmlString(Ability ability, ContextEventListener context) {
114 		if (actionName != null) {
115 			if (actionName.charAt(0) == '%') {
116 				return "";
117 			}
118 			if (actionName.charAt(0) == '@') {
119 				final String picture = ActionFactory.PICTURES.get(actionName);
120 				if (picture != null) {
121 					return toHtmlString(ability, picture);
122 				}
123 			}
124 			if (actionName.indexOf("%n") != -1) {
125 				return LanguageManagerMDB.getString(actionName.replaceAll("%n", "1"));
126 			}
127 			return StringUtils.replaceOnce(LanguageManagerMDB.getString(actionName),
128 					"{this}", ability.getCard().toString());
129 		}
130 		// we return only the string representation
131 		return toString(ability);
132 	}
133 
134 	@Override
135 	public String toHtmlString(Ability ability, int times,
136 			ContextEventListener context) {
137 		if (actionName != null && actionName.charAt(0) != '%'
138 				&& actionName.charAt(0) != '@' && actionName.indexOf("%n") != -1) {
139 			if (times == 1) {
140 				return LanguageManagerMDB.getString(actionName.replaceAll("%n", "1"));
141 			}
142 			return LanguageManagerMDB.getString(actionName).replaceAll("%n",
143 					"" + times);
144 		}
145 		// we return only the string representation
146 		return super.toHtmlString(ability, times, context);
147 	}
148 
149 	@Override
150 	public abstract String toString(Ability ability);
151 
152 	/***
153 	 * Verify the timestamp of the specified card
154 	 * 
155 	 * @param context
156 	 *          the context to use to determine wether the timestamp is correct.
157 	 * @param card
158 	 *          the concerned object.
159 	 * @return true if the timestamp is correct.
160 	 */
161 	protected static boolean checkTimeStamp(ContextEventListener context,
162 			MCard card) {
163 		if (context != null && !context.checkTimeStamp(card)) {
164 			Log.debug("\t... bad timestamp");
165 			return false;
166 		}
167 		return true;
168 	}
169 
170 	@Override
171 	public String getActionName() {
172 		if (actionName != null
173 				&& (actionName.startsWith("%") || actionName.startsWith("@"))) {
174 			return actionName.substring(1);
175 		}
176 		return actionName;
177 	}
178 
179 	/***
180 	 * The name of this action
181 	 */
182 	protected String actionName;
183 }