View Javadoc

1   /*
2    *   Firemox 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.firemox.modifier.model;
20  
21  import java.awt.Image;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.util.HashMap;
26  
27  import net.sf.firemox.clickable.target.card.MCard;
28  import net.sf.firemox.modifier.ModifierType;
29  import net.sf.firemox.test.Test;
30  import net.sf.firemox.tools.MToolKit;
31  import net.sf.firemox.tools.Picture;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   * @since 0.85
36   */
37  public final class ObjectFactory {
38  
39  	/***
40  	 * Create a new instance of this class.
41  	 */
42  	private ObjectFactory() {
43  		super();
44  	}
45  
46  	/***
47  	 * Return object corresponding to the given name.
48  	 * 
49  	 * @param objectName
50  	 *          the object's name.
51  	 * @return object corresponding to the given name.
52  	 */
53  	public static ObjectModifierModel getObjectModifierModel(String objectName) {
54  		return objectModels.get(objectName);
55  	}
56  
57  	/***
58  	 * Return occurrences number of the given object with the given name.
59  	 * 
60  	 * @param objectName
61  	 *          the object's name.
62  	 * @param objectTest
63  	 *          The test applied on specific modifier to be removed.
64  	 * @param onCard
65  	 *          the card on which objects will be counted.
66  	 * @return occurrences number of this object attached to the given card.
67  	 */
68  	public static int getNbObject(String objectName, MCard onCard, Test objectTest) {
69  		return getObjectModifierModel(objectName).getNbObject(onCard, objectTest);
70  	}
71  
72  	/***
73  	 * Return picture of the given object.
74  	 * 
75  	 * @param objectName
76  	 *          the object's name
77  	 * @return picture of the given object.
78  	 */
79  	public static Image getObjectPicture(String objectName) {
80  		if (objectPictures.get(objectName) == null) {
81  			try {
82  				objectPictures.put(objectName, Picture
83  						.loadImage(MToolKit.getTbsPicture("objects/"
84  								+ objectName.replaceAll("/", "") + ".gif")));
85  			} catch (MalformedURLException e) {
86  				// IGNORING
87  			}
88  		}
89  		return objectPictures.get(objectName);
90  	}
91  
92  	/***
93  	 * Remove if can, one instance of given object with the specified name.
94  	 * 
95  	 * @param objectName
96  	 *          the object to remove.
97  	 * @param fromCard
98  	 *          the card the object will be removed from.
99  	 * @param objectTest
100 	 *          The test applied on specific modifier to be removed.
101 	 */
102 	public static void removeObjectModifier(String objectName, MCard fromCard,
103 			Test objectTest) {
104 		getObjectModifierModel(objectName).removeObject(fromCard, objectTest);
105 	}
106 
107 	/***
108 	 * Read the available objects.
109 	 * <ul>
110 	 * Structure of InputStream : Data[size]
111 	 * <li>nb of objects [1]</li>
112 	 * <li>objects i [...]</li>
113 	 * </ul>
114 	 * 
115 	 * @param dbStream
116 	 *          the mdb stream's header.
117 	 * @throws IOException
118 	 *           error during the mdb stream read.
119 	 */
120 	public static void init(InputStream dbStream) throws IOException {
121 		if (objectModels == null) {
122 			objectModels = new HashMap<String, ObjectModifierModel>();
123 			objectPictures = new HashMap<String, Image>();
124 		} else {
125 			objectModels.clear();
126 			objectPictures.clear();
127 		}
128 		for (int count = dbStream.read(); count-- > 0;) {
129 			ObjectModifierModel obj = readObjectModifier(dbStream);
130 			objectModels.put(obj.getObjectName(), obj);
131 
132 			// is there another modifier bundled with this?
133 			while (dbStream.read() != 0) {
134 				((ModifierModel) obj).next = (ModifierModel) readObjectModifier(dbStream);
135 				obj = (ObjectModifierModel) ((ModifierModel) obj).next;
136 			}
137 		}
138 	}
139 
140 	/***
141 	 * Return the next ObjectModifierModel read from the given stream.
142 	 * 
143 	 * @param dbStream
144 	 *          the mdb stream's header.
145 	 * @return the next ObjectModifierModel read from the given stream.
146 	 * @throws IOException
147 	 *           If some other I/O error occurs
148 	 */
149 	private static ObjectModifierModel readObjectModifier(InputStream dbStream)
150 			throws IOException {
151 		final ModifierType objetType = ModifierType.deserialize(dbStream);
152 		ObjectModifierModel obj = null;
153 		switch (objetType) {
154 		case REGISTER_MODIFIER:
155 			obj = new ObjectRegisterModifierModel(dbStream);
156 			break;
157 		case IDCARD_MODIFIER:
158 			obj = new ObjectIdCardModifierModel(dbStream);
159 			break;
160 		case COLOR_MODIFIER:
161 			obj = new ObjectColorModifierModel(dbStream);
162 			break;
163 		case ABILITY_MODIFIER:
164 			obj = new ObjectAbilityModifierModel(dbStream);
165 			break;
166 		case PROPERTY_MODIFIER:
167 			obj = new ObjectPropertyModifierModel(dbStream);
168 			break;
169 		default:
170 			throw new InternalError("Unknown object modifier type : " + objetType);
171 		}
172 		return obj;
173 	}
174 
175 	/***
176 	 * Links of object name --> object type <br>
177 	 * Key : Object name : String <br>
178 	 * Value : ObjectModifierModel
179 	 */
180 	private static HashMap<String, ObjectModifierModel> objectModels;
181 
182 	/***
183 	 * Pictures of already used objects
184 	 */
185 	private static HashMap<String, Image> objectPictures;
186 
187 	/***
188 	 * Return the named register modifier madel.
189 	 * 
190 	 * @param name
191 	 *          the modifier name.
192 	 * @return the named register modifier madel.
193 	 */
194 	public static ObjectRegisterModifierModel getObjectRegisterModifierModel(
195 			String name) {
196 		return (ObjectRegisterModifierModel) objectModels.get(name);
197 	}
198 
199 	/***
200 	 * Return the named type modifier madel.
201 	 * 
202 	 * @param name
203 	 *          the modifier name.
204 	 * @return the named type modifier madel.
205 	 */
206 	public static ObjectIdCardModifierModel getObjectIdCardModifierModel(
207 			String name) {
208 		return (ObjectIdCardModifierModel) objectModels.get(name);
209 	}
210 
211 	/***
212 	 * Return the named color modifier madel.
213 	 * 
214 	 * @param name
215 	 *          the modifier name.
216 	 * @return the named color modifier madel.
217 	 */
218 	public static ObjectColorModifierModel getObjectColorModifierModel(String name) {
219 		return (ObjectColorModifierModel) objectModels.get(name);
220 	}
221 
222 	/***
223 	 * Return the named property modifier madel.
224 	 * 
225 	 * @param name
226 	 *          the modifier name.
227 	 * @return the named property modifier madel.
228 	 */
229 	public static ObjectPropertyModifierModel getObjectPropertyModifierModel(
230 			String name) {
231 		return (ObjectPropertyModifierModel) objectModels.get(name);
232 	}
233 }