001package de.deepamehta.core.model.topicmaps;
002
003import de.deepamehta.core.JSONEnabled;
004import de.deepamehta.core.util.DeepaMehtaUtils;
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        DeepaMehtaUtils.toMap(viewProps, this.viewProps);
027    }
028
029    // ---
030
031    /**
032     * Convenience constructor that initializes the "dm4.topicmaps.x", "dm4.topicmaps.y", and "dm4.topicmaps.visibility"
033     * standard view properties.
034     */
035    public ViewProperties(int x, int y, boolean visibility) {
036        initPos(x, y);
037        initVisibility(visibility);
038    }
039
040    /**
041     * Convenience constructor that initializes the "dm4.topicmaps.x" and "dm4.topicmaps.y" standard view properties.
042     */
043    public ViewProperties(int x, int y) {
044        initPos(x, y);
045    }
046
047    /**
048     * Convenience constructor that initializes the "dm4.topicmaps.visibility" standard view property.
049     */
050    public ViewProperties(boolean visibility) {
051        initVisibility(visibility);
052    }
053
054    // -------------------------------------------------------------------------------------------------- Public Methods
055
056    public Object get(String propUri) {
057        return viewProps.get(propUri);
058    }
059
060    public void put(String propUri, Object value) {
061        viewProps.put(propUri, value);
062    }
063
064    // ---
065
066    /**
067     * Convenience getter.
068     */
069    public int getInt(String propUri) {
070        return (Integer) get(propUri);
071    }
072
073    /**
074     * Convenience getter.
075     */
076    public boolean getBoolean(String propUri) {
077        return (Boolean) get(propUri);
078    }
079
080    // ---
081
082    @Override
083    public Iterator<String> iterator() {
084        return viewProps.keySet().iterator();
085    }
086
087    @Override
088    public JSONObject toJSON() {
089        return new JSONObject(viewProps);
090    }
091
092    // ------------------------------------------------------------------------------------------------ Private  Methods
093
094    private void initPos(int x, int y) {
095        put("dm4.topicmaps.x", x);
096        put("dm4.topicmaps.y", y);
097    }
098
099    private void initVisibility(boolean visibility) {
100        put("dm4.topicmaps.visibility", visibility);
101    }
102}