/* * IceBox - inventory management software for restaurants * Copyright (C) 2016 Delwink, LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3 only. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.delwink.icebox; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; /** * Static class for accessing user-specific preferences. * @author David McMackins II */ public final class Config { private static final File CONFIG_FILE = DataDir.getDataFile("icebox.properties"); private static final Config CONFIG = new Config(); private final Properties PROPERTIES; private Config() { PROPERTIES = new Properties(); PROPERTIES.setProperty("lang", "en_US"); PROPERTIES.setProperty("mainwindow.maximized", "n"); PROPERTIES.setProperty("mainwindow.width", "300"); PROPERTIES.setProperty("mainwindow.height", "600"); PROPERTIES.setProperty("setlaf", "y"); if (CONFIG_FILE.exists()) { try (FileInputStream stream = new FileInputStream(CONFIG_FILE)) { PROPERTIES.load(stream); } catch (IOException ex) { System.err.println("Failed to load config file"); } } } /** * Sets a user-specific preference. * @param key The name of the preference. * @param value The value of the preference. */ public static void put(String key, String value) { key = key.toLowerCase(); if (!value.equals(get(key))) { CONFIG.PROPERTIES.setProperty(key, value); try { CONFIG.PROPERTIES.store(new FileOutputStream(CONFIG_FILE), "Generated by IceBox"); } catch (IOException ex) { Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex); } } } /** * Gets a user-specific preference. * @param key The name of the preference. * @return The value of the preference. */ public static String get(String key) { return CONFIG.PROPERTIES.getProperty(key.toLowerCase()); } }