/* * McbSettings.java * * Created on May 12, 2006, 10:07 AM */ package Current.popups.Settings; import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.reflect.*; import java.io.*; import org.pat.util.*; import Current.*; import Current.popups.Settings.Items.*; import Current.manage.*; /** * * @author pat */ public class Settings implements Serializable { public class Message { } protected TreeMap class_map=new TreeMap(new ClassComparator()); protected TreeMap name_map=new TreeMap(new StringComparator()); Manager M; protected void add(SettingsObject sobj) { class_map.put(sobj.getType(), sobj); name_map.put(sobj.getName(), sobj); M.mcbSend(new Message()); } protected SettingsObject getDefaultSettingsObject(Class C) { SettingsObject ret=(SettingsObject)(class_map.get(C)); if (ret!=null) return ret; // only reaches here if C is not in the TreeMap class_map try { Object inner=C.newInstance(); ret=new SettingsObject(inner); add(ret); return ret; } catch (Throwable T) { System.err.println("Failed to create object of type "+C.getClass().getName()); T.printStackTrace(); } // only reaches here if it fails to create a new object with Class C // return null if error return null; } /** This is the prefered way to get a new settings object */ public Object getDefault(Class C) { return getDefaultSettingsObject(C).getObject(); } /** This replaces the default settings object with a new object (obj). * This should only be used if the object is guaranteed to be the first * object created, since updates will not be propagated to the old * settings object. */ public void setDefault(Object obj) { SettingsObject so=(SettingsObject)(class_map.get(obj.getClass())); if (so!=null) { System.err.println("McbSettings.setDefault(Object) used on pre-existing default object of type "+obj.getClass()); } add(new SettingsObject(obj)); } /** Creates a new instance of McbSettings */ public Settings(Manager M) { this.M=M; try { loadProperties("default"); } catch (Throwable T) { System.out.println("Warning: no default settings saved."); } } /** load the properties from the file "/Files/Properties/filename" */ public void loadProperties(String filename) { Settings new_ms=null; try { InputStream in; try { in=M.getInputStream("Files/Properties/"+filename); } catch (Exception e) { System.err.println("Failed to find/load settings file named \"Files/Properties/"+filename+"\""); return; } ObjectInputStream obj_in=new ObjectInputStream(in); int num=obj_in.readInt(); Object new_obj; SettingsObject so; String class_name=null; for (int i=0; i