001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform.preferences.lastsettings;
005    
006    import graphlab.platform.attribute.AttributeListener;
007    import graphlab.platform.attribute.NotifiableAttributeSetImpl;
008    import graphlab.platform.StaticUtils;
009    import graphlab.platform.parameter.Parameter;
010    
011    import java.io.*;
012    import java.lang.reflect.Field;
013    import java.util.HashSet;
014    import java.util.Iterator;
015    import java.util.Map;
016    import java.util.prefs.BackingStoreException;
017    import java.util.prefs.InvalidPreferencesFormatException;
018    
019    /**
020     * @author Rouzbeh Ebrahimi
021     */
022    public class LastSettings implements AttributeListener {
023        private boolean defaultsAreSet = false;
024        private HashSet<String> keys = new HashSet<String>();
025        private HashSet<Object> registeredObjects = new HashSet<Object>();
026        private java.util.prefs.Preferences builtInPrefs = java.util.prefs.Preferences.userRoot();
027        private java.util.prefs.Preferences graphPrefs = builtInPrefs.node("graph");
028        private HashSet<Class> registeredClasses = new HashSet<Class>();
029        private File file = new File("prefs");
030    
031        {
032            file.mkdir();
033        }
034    
035        public LastSettings() {
036            try {
037                File file = new File(this.file, "graph.xml");
038                FileInputStream is = new FileInputStream(file);
039                graphPrefs.importPreferences(is);
040                is.close();
041    
042            } catch (IOException e) {
043                e.printStackTrace();
044            } catch (InvalidPreferencesFormatException e) {
045                e.printStackTrace();
046            }
047        }
048    
049        public void registerSetting(Object o) {
050    
051            if (!registeredClasses.contains(o.getClass())) {
052                registeredClasses.add(o.getClass());
053                registeredObjects.add(o);
054                loadSettings(o);
055    
056            } else {
057            }
058        }
059    
060        private void saveField(Field f, java.util.prefs.Preferences t, Object o) {
061            try {
062                String key = f.getName();
063                Object value = f.get(o);
064                t.put(key, value.toString());
065    
066            } catch (IllegalAccessException e) {
067                e.printStackTrace();
068            }
069    
070        }
071    
072        private ByteArrayOutputStream convertObjectToByteArray(Object value) {
073            ByteArrayOutputStream baos = new ByteArrayOutputStream();
074            ObjectOutputStream oos;
075            try {
076                oos = new ObjectOutputStream(baos);
077                oos.writeObject(value);
078                oos.close();
079            } catch (IOException e) {
080                e.printStackTrace();
081            }
082            return baos;
083        }
084    
085        private void loadField(Field f, java.util.prefs.Preferences t, Object o) {
086            String key = f.getName();
087            Object value = t.get(key, null);
088            String m = f.getType().toString();
089            Object obj = StaticUtils.fromString(m.substring(6, m.length()), value.toString());
090            try {
091                if (obj != null)
092                    f.set(o, obj);
093            } catch (IllegalAccessException e) {
094                e.printStackTrace();
095            }
096    
097        }
098    
099        private void loadSettings(Object o) {
100            try {
101    
102                String objectName = o.getClass().getName();
103    
104                java.util.prefs.Preferences t = graphPrefs.node(objectName);
105                t.put("object", objectName);
106                for (Field f : o.getClass().getFields()) {
107                    UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class);
108                    Parameter anot2 = f.getAnnotation(Parameter.class);
109                    if (anot != null || anot2 != null) {
110                        loadField(f, t, o);
111                    }
112    
113                }
114    //            return GAttrFrame.showEditDialog(p, true).getReturnStatus();
115    //            performLoadJob();
116                return;
117            }
118            catch (Exception e) {
119                e.printStackTrace();
120            }
121            return;
122        }
123    
124        private NotifiableAttributeSetImpl refactorSerializables(NotifiableAttributeSetImpl x) {
125            NotifiableAttributeSetImpl y = new NotifiableAttributeSetImpl();
126            Map<String, Object> map = x.getAttrs();
127            Iterator<String> iterator = map.keySet().iterator();
128            for (; iterator.hasNext();) {
129                String key = iterator.next();
130                Object value = map.get(key);
131                if (value instanceof Serializable) {
132                    y.put(key, value);
133                }
134            }
135            return y;
136        }
137    
138        public void attributeUpdated(String name, Object oldVal, Object newVal) {
139            try {
140                oldVal.getClass().getDeclaredField(name).set(oldVal, newVal);
141            } catch (IllegalAccessException e) {
142    //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
143            } catch (NoSuchFieldException e) {
144    //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
145            }
146        }
147    
148    
149        public void saveSettings() {
150            for (Object o : registeredObjects) {
151                String objectName = o.getClass().getName();
152                java.util.prefs.Preferences t = graphPrefs.node(objectName);
153                t.put("object", objectName);
154                for (Field f : o.getClass().getFields()) {
155                    UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class);
156                    Parameter anot2 = f.getAnnotation(Parameter.class);
157                    if (anot != null || anot2 != null) {
158                        try {
159                            saveField(f, t, f.get(o));
160                        } catch (IllegalAccessException e) {
161                            e.printStackTrace();
162                        }
163                    }
164    
165                }
166            }
167            try {
168                graphPrefs.exportSubtree(new FileOutputStream(new File(file, "graph.xml")));
169    //            graphPrefs.exportNode(new FileOutputStream(new File("/graph.xml")));
170            } catch (IOException e) {
171    
172                e.printStackTrace();
173            } catch (BackingStoreException e) {
174                e.printStackTrace();
175            }
176    
177        }
178    
179    }