001package systems.dmx.core.model.topicmaps;
002
003import systems.dmx.core.JSONEnabled;
004import systems.dmx.core.util.DMXUtils;
005
006import org.codehaus.jettison.json.JSONObject;
007
008import java.util.HashMap;
009import java.util.Iterator;
010import java.util.Map;
011
012
013
014public class ViewProperties implements Iterable<String>, JSONEnabled {
015
016    // ---------------------------------------------------------------------------------------------- Instance Variables
017
018    private Map<String, Object> viewProps = new HashMap();
019
020    // ---------------------------------------------------------------------------------------------------- Constructors
021
022    /**
023     * Note: invoked from JAX-RS message body reader (see Webservice's ObjectProvider.java).
024     */
025    public ViewProperties(JSONObject viewProps) {
026        DMXUtils.toMap(viewProps, this.viewProps);
027    }
028
029    // ---
030
031    public ViewProperties () {
032    }
033
034    /**
035     * Convenience constructor that initializes the "dmx.topicmaps.x", "dmx.topicmaps.y", "dmx.topicmaps.visibility",
036     * and "dmx.topicmaps.pinned" standard view properties.
037     */
038    public ViewProperties(int x, int y, boolean visibility, boolean pinned) {
039        initPos(x, y);
040        initVisibility(visibility);
041        initPinned(pinned);
042    }
043
044    /**
045     * Convenience constructor that initializes the "dmx.topicmaps.x" and "dmx.topicmaps.y" standard view properties.
046     */
047    public ViewProperties(int x, int y) {
048        initPos(x, y);
049    }
050
051    /**
052     * Convenience constructor that initializes the "dmx.topicmaps.visibility" standard view property.
053     */
054    public ViewProperties(boolean visibility) {
055        initVisibility(visibility);
056    }
057
058    // -------------------------------------------------------------------------------------------------- Public Methods
059
060    public Object get(String propUri) {
061        return viewProps.get(propUri);
062    }
063
064    public ViewProperties put(String propUri, Object value) {
065        viewProps.put(propUri, value);
066        return this;
067    }
068
069    // ---
070
071    /**
072     * Convenience getter.
073     */
074    public int getInt(String propUri) {
075        return (Integer) get(propUri);
076    }
077
078    /**
079     * Convenience getter.
080     */
081    public boolean getBoolean(String propUri) {
082        return (Boolean) get(propUri);
083    }
084
085    // ---
086
087    @Override
088    public Iterator<String> iterator() {
089        return viewProps.keySet().iterator();
090    }
091
092    @Override
093    public JSONObject toJSON() {
094        return new JSONObject(viewProps);
095    }
096
097    @Override
098    public String toString() {
099        return viewProps.toString();
100    }
101
102    // ------------------------------------------------------------------------------------------------ Private  Methods
103
104    private void initPos(int x, int y) {
105        put("dmx.topicmaps.x", x);
106        put("dmx.topicmaps.y", y);
107    }
108
109    private void initVisibility(boolean visibility) {
110        put("dmx.topicmaps.visibility", visibility);
111    }
112
113    private void initPinned(boolean pinned) {
114        put("dmx.topicmaps.pinned", pinned);
115    }
116}