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    
005    package graphlab.graph.atributeset;
006    
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.platform.attribute.AttributeSet;
009    import graphlab.platform.attribute.NotifiableAttributeSetImpl;
010    import graphlab.platform.lang.ArrayX;
011    
012    import java.awt.*;
013    import java.util.HashMap;
014    import java.util.Map;
015    
016    /**
017     * this class provides a way to have a Graph object as a NotifiableAttributeSet
018     * this is usefull whenever some one wants to work blindly with graph attributes
019     * for example on saving graph to a gml xml file it is important to have all attributes
020     * saved, the meaning of values of attributes is not important, or when a property editor
021     * wants to show and edit the attributes of graph to the user, at that time it can use
022     * a XAttribute to have better looks see GraphPropertyEditor class as an example.
023     * <p/>
024     * An other usage of this class is whenever some one wants to listen to changes of
025     * a user defined or a rare attribute which normally has no listening capability,
026     * for example when you want to change the program according to Graph ID whenever it
027     * changes. ID attribute on graph has not a formal listening way, so this class is usefull
028     * at that time.
029     *
030     * @author Azin Azadi
031     * @see graphlab.ui.AttributeSetView
032     * @see graphlab.ui.NotifiableAttributeSetView
033     * @see graphlab.plugins.main.core.actions.GraphPropertyEditor
034     */
035    public class GraphAttrSet implements AttributeSet {
036    
037        GraphModel g;
038        NotifiableAttributeSetImpl atrs = new NotifiableAttributeSetImpl();
039    
040        public static final String EDGEDEFAULT = "edgedefault";
041        public static final String EDGEDEFAULT_DIRECTED = "directed";
042        public static final String EDGEDEFAULT_UNDIRECTED = "undirected";
043    
044        //    public static final String ID = "id";
045        public static final String DIRECTED = "directed";
046        public static final String LABEL = "label";
047        public static final String ZOOM = "Zoom";
048        public static final String FONT = "Font";
049        public static final String DRAW_VERTEX_LABELS = "Show Vertex Labels";
050        public static final String DRAW_EDGE_LABELS = "Show Edge Labels";
051        public static final String IS_EDGES_CURVED = "Is Edges Curved";
052    
053        //****//
054        //    public static final String CENTERX = "centerx";
055        //    public static final String CENTERY = "centery";
056        public static final String name = "Graph.GraphModel";
057    
058        public Map<String, Object> getAttrs() {
059            Map<String, Object> ret = new HashMap<String, Object>();
060            ret.put(DIRECTED, g.isDirected() ? EDGEDEFAULT_DIRECTED : EDGEDEFAULT_UNDIRECTED);
061            ret.put(LABEL, g.getLabel());
062            ret.put(ZOOM, g.getZoom());
063            ret.put(FONT, g.getFont());
064            ret.put(DRAW_VERTEX_LABELS, g.isDrawVertexLabels());
065            ret.put(DRAW_EDGE_LABELS, g.isDrawEdgeLabels());
066            ret.put(IS_EDGES_CURVED, g.isEdgesCurved());
067            if (g.getUserDefinedAttributes() != null)
068                ret.putAll(g.getUserDefinedAttributes());
069            return ret;
070        }
071    
072        public void put(String atrName, Object val) {
073            if (atrName.equals(LABEL)) {
074                g.setLabel((String) val);
075            } else if (atrName.equals(ZOOM)) {
076                g.setZoom((ArrayX<String>) val);
077            } else if (atrName.equals(FONT)) {
078                g.setFont((Font) val);
079            } else if (atrName.equals(DRAW_VERTEX_LABELS)) {
080                g.setDrawVertexLabels((Boolean) val);
081            } else if (atrName.equals(IS_EDGES_CURVED)) {
082                g.setIsEdgesCurved((Boolean) val);
083            } else if (atrName.equals(DRAW_EDGE_LABELS)) {
084                g.setDrawEdgeLabels((Boolean) val);
085            } else {
086                g.setUserDefinedAttribute(atrName, val);
087            }
088    
089        }
090    
091        public Object get(String atrName) {
092            Object ret = null;
093            if (atrName.equals(LABEL)) {
094                ret = g.getLabel();
095            } else if (atrName.equals(DIRECTED)) {
096                ret = g.isDirected();
097            } else if (atrName.equals(ZOOM)) {
098                ret = g.getZoom();
099            } else if (atrName.equals(FONT)) {
100                ret = g.getFont();
101            } else if (atrName.equals(DRAW_VERTEX_LABELS)) {
102                ret = g.isDrawVertexLabels();
103            } else if (atrName.equals(IS_EDGES_CURVED)) {
104                ret = g.isEdgesCurved();
105            } else if (atrName.equals(DRAW_EDGE_LABELS)) {
106                ret = g.isDrawEdgeLabels();
107            } else {
108                ret = g.getUserDefinedAttribute(atrName);
109            }
110            return ret;
111        }
112    
113        public GraphAttrSet(GraphModel g) {
114            this.g = g;
115        }
116    }