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