001    /*
002     * Copyright (C) 2003 Adam Olsen
003     * 
004     * This program is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU General Public License as published by the Free Software
006     * Foundation; either version 1, or (at your option) any later version.
007     * 
008     * This program is distributed in the hope that it will be useful, but WITHOUT
009     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
010     * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
011     * details.
012     * 
013     * You should have received a copy of the GNU General Public License along with
014     * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
015     * Ave, Cambridge, MA 02139, USA.
016     */
017    
018    package com.valhalla.settings;
019    
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.FileNotFoundException;
023    import java.io.FileOutputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.OutputStream;
027    import java.util.Properties;
028    
029    public class SettingsProperties extends Properties {
030        /**
031         * Sets a boolean
032         * 
033         * @param key
034         *            the key to set
035         * @param value
036         *            the value to set the key to
037         */
038        public void setBoolean(String key, boolean value) {
039            if (value)
040                setProperty(key, "true");
041            else
042                remove(key);
043        }
044    
045        /**
046         * Gets a boolean value
047         * 
048         * @param key
049         *            the key to get a boolean for
050         * @return a boolean based on the key
051         */
052        public boolean getBoolean(String key) {
053            return (getProperty(key) != null);
054        }
055    
056        public void loadSettings(String file) throws FileNotFoundException,
057                IOException {
058            File f = new File(file);
059            InputStream stream = new FileInputStream(f);
060            load(stream);
061            stream.close();
062        }
063    
064        public void saveSettings(String file, String comments) throws IOException {
065            File f = new File(file);
066            OutputStream stream = new FileOutputStream(f);
067            store(stream, comments);
068            stream.close();
069        }
070    }