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;
005    
006    import graphlab.platform.attribute.AttributeListener;
007    import graphlab.platform.lang.ArrayX;
008    import graphlab.platform.preferences.lastsettings.UserModifiableProperty;
009    
010    import java.lang.reflect.Field;
011    import java.util.HashMap;
012    import java.util.HashSet;
013    
014    /**
015     * @author Rouzbeh Ebrahimi
016     */
017    public class GraphPreferences extends AbstractPreference implements AttributeListener {
018        public Object oneInstance;
019        public HashSet<Object> oneInstances;
020        public static Preferences pref;
021    
022        public GraphPreferences(String name, Object oneInstance, String category) {
023            super(name, pref, category);
024            oneInstances = new HashSet<Object>();
025            this.oneInstance = oneInstance;
026            oneInstances.add(oneInstance);
027            defineListeners(this);
028        }
029    
030        public GraphPreferences(String name, HashSet<Object> oneInstances, String category) {
031            super(name, pref, category);
032            this.oneInstances = oneInstances;
033            defineListeners(this);
034        }
035    
036        public void defineAttributes(HashMap<Object, ArrayX> objectValues) {
037    
038            for (Object o : objectValues.keySet()) {
039                putAttribute(o.toString(), objectValues.get(o));
040            }
041    
042        }
043    
044        public void defineAttributes(HashMap<Object, Object> objectValues, boolean t) {
045    
046            for (Object o : objectValues.keySet()) {
047                putAttribute(o.toString(), objectValues.get(o));
048            }
049        }
050    
051        public void defineMultipleAttributes(HashMap<Object, HashMap<Object, ArrayX>> map) {
052    
053            for (Object o : map.keySet()) {
054                HashMap<Object, ArrayX> hashMap = map.get(o);
055                for (Object fields : hashMap.keySet()) {
056                    putAttribute(o.toString() + "*" + fields.toString(), hashMap.get(fields));
057    
058                }
059            }
060        }
061    
062        public void addObject(Object o) {
063            oneInstances.add(o);
064        }
065    
066        public void defineListeners(AttributeListener al) {
067    
068            attributeSet.addAttributeListener(al);
069        }
070    
071        public void attributeUpdated(String name, Object oldVal, Object newVal) {
072            for (Object o : oneInstances) {
073                for (Field f : o.getClass().getFields()) {
074                    UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class);
075                    if (anot != null && (anot.displayName()).equals(name)) {
076                        try {
077    //                            o.getClass().getDeclaredField(f.getName()).set(o, newVal);
078                            f.set(o, newVal);
079                        } catch (IllegalAccessException e) {
080                            e.printStackTrace();
081                        }
082                    }
083                }
084            }
085        }
086    }