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.database.propertyconfig;
20  
21  import net.sf.magicproject.database.Proxy;
22  import net.sf.magicproject.database.data.StringData;
23  import net.sf.magicproject.database.data.TranslatableData;
24  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
25  import net.sf.magicproject.xml.XmlParser.Node;
26  
27  /***
28   * Abstract class that holds information about a property name and its
29   * translation in the selected language in the <code>LanguageManager</code>
30   * class.
31   * 
32   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
33   * @since 0.90
34   */
35  public abstract class PropertyConfig {
36  
37  	/***
38  	 * The translated name of this property.
39  	 */
40  	private String translatedName;
41  
42  	/***
43  	 * The name of this property.
44  	 */
45  	private String name;
46  
47  	/***
48  	 * Default protected constructor.
49  	 * 
50  	 * @param name
51  	 *          the identifier or name of this property
52  	 */
53  	protected PropertyConfig(String name) {
54  		this.name = name;
55  	}
56  
57  	/***
58  	 * Indicates if the value of this property is translated or not.
59  	 * 
60  	 * @return <code>true</code> if the value of this property is translated,
61  	 *         <code>false</code> either
62  	 */
63  	abstract boolean isTranslated();
64  
65  	/***
66  	 * Returns the translated property name.
67  	 * 
68  	 * @return the translated property name
69  	 */
70  	public String getTranslatedName() {
71  		if (translatedName == null) {
72  			translatedName = LanguageManagerMDB.getString(name);
73  		}
74  		return translatedName;
75  	}
76  
77  	/***
78  	 * Returns the name of this property.
79  	 * 
80  	 * @return the name of this property
81  	 */
82  	public String getName() {
83  		return name;
84  	}
85  
86  	@Override
87  	public final int hashCode() {
88  		return name.hashCode();
89  	}
90  
91  	@Override
92  	public String toString() {
93  		return "[name=" + name + ",local=" + translatedName + "]";
94  	}
95  
96  	/***
97  	 * Parse the given stream to build the associated TranslatableData object to
98  	 * the specified card name.
99  	 * 
100 	 * @param cardName
101 	 *          the card name.
102 	 * @param node
103 	 *          the node containing the data used to build the
104 	 *          {@link TranslatableData} object.
105 	 * @return the {@link TranslatableData} object built from the given node.
106 	 */
107 	public TranslatableData parseProperty(String cardName, Node node) {
108 		return new StringData(this, node.getAttribute("value"));
109 	}
110 
111 	/***
112 	 * Parse the given stream to build the associated TranslatableData object to
113 	 * the specified card name.
114 	 * 
115 	 * @param cardName
116 	 *          the card name.
117 	 * @param stream
118 	 *          the stream containing the data used to build the
119 	 *          {@link TranslatableData} object.
120 	 * @param proxy
121 	 *          is the proxy this data come from. Is used to translate from
122 	 *          private-proxy to public-tbs value.
123 	 * @return the {@link TranslatableData} object built from the given stream.
124 	 */
125 	public TranslatableData parseProperty(String cardName, String stream,
126 			Proxy proxy) {
127 		return new StringData(this, proxy
128 				.getGlobalValueFromLocal(getName(), stream));
129 	}
130 }