OSDN Git Service

65a0b50768ea96dda984e832619ca6bc70ecc6b2
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / system / PropertiesIO.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 package com.jme.system;
34
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;
41
42
43 /**
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>
47  * FREQ=60<br>
48  * RENDERER=LWJGL<br>
49  * WIDTH=1280<br>
50  * HEIGHT=1024<br>
51  * DEPTH=32<br>
52  * FULLSCREEN=false<br>
53  * <br>
54  *
55  * @author Mark Powell
56  * @version $Id: PropertiesIO.java,v 1.8 2007/08/02 22:14:06 nca Exp $
57  */
58 public class PropertiesIO {
59     private static final Logger logger = Logger.getLogger(PropertiesIO.class
60             .getName());
61     
62     /**
63      * The default width, used if there is a problem with the properties file.
64      */
65     public static final int DEFAULT_WIDTH = 640;
66     /**
67      * The default height, used if there is a problem with the properties file.
68      */
69     public static final int DEFAULT_HEIGHT = 480;
70     /**
71      * The default depth, used if there is a problem with the properties file.
72      */
73     public static final int DEFAULT_DEPTH = 16;
74     /**
75      * The default frequency, used if there is a problem with the properties
76      * file.
77      */
78     public static final int DEFAULT_FREQ = 60;
79     /**
80      * The default fullscreen flag, used if there is a problem with the
81      * properties file.
82      */
83     public static final boolean DEFAULT_FULLSCREEN = true;
84     /**
85      * The default renderer flag, used if there is a problem with the
86      * properties file.
87      */
88     public static final String DEFAULT_RENDERER = "LWJGL";
89
90     //property object
91     private Properties prop;
92     //the file that contains our properties.
93     private String filename;
94
95     /**
96      * Constructor creates the <code>PropertiesIO</code> object for use.
97      *
98      * @param filename the properties file to use.
99      * @throws JmeException if the filename is null.
100      */
101     public PropertiesIO(String filename) {
102         if (null == filename) {
103             throw new JmeException("Must give a valid filename");
104         }
105
106         this.filename = filename;
107         prop = new Properties();
108
109         logger.info("PropertiesIO created");
110     }
111
112     /**
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
116      * returned.
117      * 
118      * @return the success of the load, true indicated success and false
119      *         indicates failure.
120      */
121     public boolean load() {
122         FileInputStream fin = null;
123         try {
124             fin = new FileInputStream(filename);
125         } catch (FileNotFoundException e) {
126             logger.warning("Could not load properties. Creating a new one.");
127             return false;
128         }
129
130         try {
131             if (fin != null) {
132                 prop.load(fin);
133                 fin.close();
134             }
135         } catch (IOException e) {
136             logger.warning("Could not load properties. Creating a new one.");
137             return false;
138         }
139
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.");
146             return false;
147         }
148
149         logger.info("Read properties");
150         return true;
151     }
152
153     /**
154      * <code>save</code> overwrites the properties file with the given
155      * parameters.
156      *
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.
163      */
164     public boolean save(int width, int height, int depth, int freq,
165             boolean fullscreen, String renderer) {
166
167         FileOutputStream fout;
168         try {
169             fout = new FileOutputStream(filename);
170
171             prop.clear();
172
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);
179
180             prop.store(fout, "Properties");
181
182             fout.close();
183         } catch (IOException e) {
184             logger.warning("Could not save properties: " + e.toString());
185             return false;
186         }
187         logger.info("Saved properties");
188         return true;
189     }
190
191     /**
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.
195      *
196      * @return the width determined by the properties file, or the default.
197      */
198     public int getWidth() {
199         String w = prop.getProperty("WIDTH");
200         if (null == w) {
201             return DEFAULT_WIDTH;
202         } 
203             
204         return Integer.parseInt(w);        
205     }
206
207     /**
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.
211      *
212      * @return the height determined by the properties file, or the default.
213      */
214     public int getHeight() {
215         String h = prop.getProperty("HEIGHT");
216         if (null == h) {
217             return DEFAULT_HEIGHT;
218         }
219             
220         return Integer.parseInt(h);        
221     }
222
223     /**
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.
227      *
228      * @return the depth determined by the properties file, or the default.
229      */
230     public int getDepth() {
231         String d = prop.getProperty("DEPTH");
232         if (null == d) {
233             return DEFAULT_DEPTH;
234         } 
235             
236         return Integer.parseInt(d);        
237     }
238
239     /**
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.
243      *
244      * @return the frequency determined by the properties file, or the default.
245      */
246     public int getFreq() {
247         String f = prop.getProperty("FREQ");
248         if(null == f) {
249             return DEFAULT_FREQ;
250         } 
251             
252         return Integer.parseInt(f);        
253     }
254
255     /**
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.
259      *
260      * @return the fullscreen flag determined by the properties file, or the
261      *      default.
262      */
263     public boolean getFullscreen() {
264         String f = prop.getProperty("FULLSCREEN");
265         if(null == f) {
266             return DEFAULT_FULLSCREEN;
267         }
268             
269         return Boolean.valueOf(prop.getProperty("FULLSCREEN"));        
270     }
271
272     /**
273      *
274      * <code>getRenderer</code> returns the requested rendering API, or the
275      * default.
276      * @return the rendering API or the default.
277      */
278     public String getRenderer() {
279         String renderer = prop.getProperty("RENDERER");
280         if(null == renderer) {
281             return DEFAULT_RENDERER;
282         } 
283             
284         return renderer;        
285     }
286
287     /**
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.
292      */
293     public String get(String key) {
294         return prop.getProperty(key);
295     }
296
297     /**
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.
301      */
302     public void set(String key, String value) {
303         prop.setProperty(key, value);
304     }
305 }