/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 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 General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.resources; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Properties; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import javax.swing.ImageIcon; import javax.swing.JOptionPane; /** * * @author Chris Davoren */ public class ResourceManager { public static final String ROOT_PREFS_NODE = "/net/rubikscomplex/ufosge"; public static final String PREF_UFO_DIR = "ufodirectory"; public static final String PREF_SAVESELECTFRAME_POSX = "SaveSelectFramePosX"; public static final String PREF_SAVESELECTFRAME_POSY = "SaveSelectFramePosY"; public static final String PREF_BASEEDITDIALOG_POSX = "BaseEditDialogPosX"; public static final String PREF_BASEEDITDIALOG_POSY = "BaseEditDialogPosY"; public static final String PREF_RESEARCHEDITDIALOG_POSX = "ResearchEditDialogPosX"; public static final String PREF_RESEARCHEDITDIALOG_POSY = "ResearchEditDialogPosY"; public static final String PREF_SAVEEDITFRAME_POSX = "SaveEditFramePosX"; public static final String PREF_SAVEEDITFRAME_POSY = "SaveEditFramePosY"; public static final String PREF_SOLDIEREDITDIALOG_POSX = "SoldierEditDialogPosX"; public static final String PREF_SOLDIEREDITDIALOG_POSY = "SoldierEditDialogPosY"; public static final String PROP_VERSION = "ufosge.version"; public static final String PROP_FULLVERSION = "ufosge.fullversion"; public static ImageIcon icon = null; static Preferences userPrefsNode = null; static Properties properties = null; public static URL getImageResource(String imageFilename) throws FileNotFoundException { URL imageURL = ResourceManager.class.getResource("/images/" + imageFilename); if (imageURL == null) { throw new FileNotFoundException("Unable to load image file '" + imageFilename + "'"); } return imageURL; } public static ImageIcon getDefaultFrameIcon() { if (icon == null) { icon = new ImageIcon(ResourceManager.class.getResource("/images/icons/icon-32.png"), "32x32 Icon"); } return icon; } public static Preferences getUserPreferences() { if (userPrefsNode == null) { userPrefsNode = Preferences.userRoot().node(ROOT_PREFS_NODE); } return userPrefsNode; } public static void deleteUserPreferences() { try { getUserPreferences().removeNode(); getUserPreferences().flush(); } catch(BackingStoreException e) { JOptionPane.showMessageDialog(null, "Unable to delete user preferences:\n\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } public static Properties getProperties() { if(properties == null) { properties = new Properties(); try { InputStream propStream = ResourceManager.class.getResourceAsStream("/ufosge.properties"); // JOptionPane.showMessageDialog(null, propResource.toURI().toString()); if (propStream != null) { properties.load(propStream); } } catch(IOException e) { JOptionPane.showMessageDialog(null, "Error loading properties file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } catch(Exception e) { JOptionPane.showMessageDialog(null, "Error loading properties file: " + e.getMessage() + "\n\n" + e.getClass().toString(), "Error", JOptionPane.ERROR_MESSAGE); } } return properties; } public static String getProperty(String key) { Properties props = getProperties(); if(props == null) { return null; } return props.getProperty(key, null); } public static String getUserPreference(String key) { return getUserPreferences().get(key, null); } public static int getUserPreferenceInt(String key) { return getUserPreferences().getInt(key, -1); } public static void setUserPreference(String key, String value) { getUserPreferences().put(key, value); try { getUserPreferences().flush(); } catch(BackingStoreException e) { JOptionPane.showMessageDialog(null, "Unable to save preference '" + key + "':\n\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } public static void setUserPreferenceInt(String key, int value) { getUserPreferences().putInt(key, value); try { getUserPreferences().flush(); } catch(BackingStoreException e) { JOptionPane.showMessageDialog(null, "Unable to save preference '" + key + "':\n\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } }