2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package com.jme.system;
35 import java.io.FileInputStream;
36 import java.io.FileNotFoundException;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.util.Properties;
40 import java.util.logging.Logger;
44 * <code>PropertiesIO</code> handles loading and saving a properties file that
45 * defines the display settings. A property file is identified during creation
46 * of the object. The properties file should have the following format:<br><br>
52 * FULLSCREEN=false<br>
56 * @version $Id: PropertiesIO.java,v 1.8 2007/08/02 22:14:06 nca Exp $
58 public class PropertiesIO {
59 private static final Logger logger = Logger.getLogger(PropertiesIO.class
63 * The default width, used if there is a problem with the properties file.
65 public static final int DEFAULT_WIDTH = 640;
67 * The default height, used if there is a problem with the properties file.
69 public static final int DEFAULT_HEIGHT = 480;
71 * The default depth, used if there is a problem with the properties file.
73 public static final int DEFAULT_DEPTH = 16;
75 * The default frequency, used if there is a problem with the properties
78 public static final int DEFAULT_FREQ = 60;
80 * The default fullscreen flag, used if there is a problem with the
83 public static final boolean DEFAULT_FULLSCREEN = true;
85 * The default renderer flag, used if there is a problem with the
88 public static final String DEFAULT_RENDERER = "LWJGL";
91 private Properties prop;
92 //the file that contains our properties.
93 private String filename;
96 * Constructor creates the <code>PropertiesIO</code> object for use.
98 * @param filename the properties file to use.
99 * @throws JmeException if the filename is null.
101 public PropertiesIO(String filename) {
102 if (null == filename) {
103 throw new JmeException("Must give a valid filename");
106 this.filename = filename;
107 prop = new Properties();
109 logger.info("PropertiesIO created");
113 * <code>load</code> attempts to load the properties file defined during
114 * instantiation and put all properties in the table. If there is a problem
115 * loading or reading the file, false is returned. If all goes well, true is
118 * @return the success of the load, true indicated success and false
121 public boolean load() {
122 FileInputStream fin = null;
124 fin = new FileInputStream(filename);
125 } catch (FileNotFoundException e) {
126 logger.warning("Could not load properties. Creating a new one.");
135 } catch (IOException e) {
136 logger.warning("Could not load properties. Creating a new one.");
140 //confirm that the properties file has all the data we need.
141 if (null == prop.getProperty("WIDTH")
142 || null == prop.getProperty("HEIGHT")
143 || null == prop.getProperty("DEPTH")
144 || null == prop.getProperty("FULLSCREEN")) {
145 logger.warning("Properties file not complete.");
149 logger.info("Read properties");
154 * <code>save</code> overwrites the properties file with the given
157 * @param width the width of the resolution.
158 * @param height the height of the resolution.
159 * @param depth the bits per pixel.
160 * @param freq the frequency of the monitor.
161 * @param fullscreen use fullscreen or not.
162 * @return true if save was successful, false otherwise.
164 public boolean save(int width, int height, int depth, int freq,
165 boolean fullscreen, String renderer) {
167 FileOutputStream fout;
169 fout = new FileOutputStream(filename);
173 prop.put("WIDTH", "" + width);
174 prop.put("HEIGHT", "" + height);
175 prop.put("DEPTH", "" + depth);
176 prop.put("FREQ", "" + freq);
177 prop.put("FULLSCREEN", "" + fullscreen);
178 prop.put("RENDERER", renderer);
180 prop.store(fout, "Properties");
183 } catch (IOException e) {
184 logger.warning("Could not save properties: " + e.toString());
187 logger.info("Saved properties");
192 * <code>getWidth</code> returns the width as read from the properties
193 * file. If the properties file does not contain width or was not read
194 * properly, the default width is returned.
196 * @return the width determined by the properties file, or the default.
198 public int getWidth() {
199 String w = prop.getProperty("WIDTH");
201 return DEFAULT_WIDTH;
204 return Integer.parseInt(w);
208 * <code>getHeight</code> returns the height as read from the properties
209 * file. If the properties file does not contain height or was not read
210 * properly, the default height is returned.
212 * @return the height determined by the properties file, or the default.
214 public int getHeight() {
215 String h = prop.getProperty("HEIGHT");
217 return DEFAULT_HEIGHT;
220 return Integer.parseInt(h);
224 * <code>getDepth</code> returns the depth as read from the properties
225 * file. If the properties file does not contain depth or was not read
226 * properly, the default depth is returned.
228 * @return the depth determined by the properties file, or the default.
230 public int getDepth() {
231 String d = prop.getProperty("DEPTH");
233 return DEFAULT_DEPTH;
236 return Integer.parseInt(d);
240 * <code>getFreq</code> returns the frequency of the monitor as read from
241 * the properties file. If the properties file does not contain frequency
242 * or was not read properly the default frequency is returned.
244 * @return the frequency determined by the properties file, or the default.
246 public int getFreq() {
247 String f = prop.getProperty("FREQ");
252 return Integer.parseInt(f);
256 * <code>getFullscreen</code> returns the fullscreen flag as read from the
257 * properties file. If the properties file does not contain the fullscreen
258 * flag or was not read properly, the default fullscreen flag is returned.
260 * @return the fullscreen flag determined by the properties file, or the
263 public boolean getFullscreen() {
264 String f = prop.getProperty("FULLSCREEN");
266 return DEFAULT_FULLSCREEN;
269 return Boolean.valueOf(prop.getProperty("FULLSCREEN"));
274 * <code>getRenderer</code> returns the requested rendering API, or the
276 * @return the rendering API or the default.
278 public String getRenderer() {
279 String renderer = prop.getProperty("RENDERER");
280 if(null == renderer) {
281 return DEFAULT_RENDERER;
288 * <code>get</code> takes an arbitrary string as a key and returns any
289 * value associated with it, null if none.
290 * @param key the key to use for data retrieval.
291 * @return the string associated with the key, null if none.
293 public String get(String key) {
294 return prop.getProperty(key);
298 * <code>set</code> adds a key/value pair to the properties list.
299 * @param key the key of the pair.
300 * @param value the value of the pair.
302 public void set(String key, String value) {
303 prop.setProperty(key, value);