001package jmri.server.web.app;
002
003import com.fasterxml.jackson.databind.JsonNode;
004import java.util.Locale;
005import java.util.Objects;
006import javax.annotation.Nonnull;
007import jmri.server.web.spi.WebMenuItem;
008
009/**
010 * A POJO for Web menu items generated from JSON.
011 *
012 * @author Randall Wood (C) 2016
013 */
014public class JsonMenuItem implements WebMenuItem {
015
016    public String path;
017    public String href = null;
018    public String iconClass = null;
019    public String title = null;
020    public int position = 300;
021    public boolean separatedBefore = false;
022    public boolean separatedAfter = false;
023    public boolean dynamic = false;
024
025    /**
026     * Create a menu item from a JSON object.
027     *
028     * @param node the JSON object containing the menu item
029     * @throws java.lang.IllegalArgumentException if node does not contain a
030     * <em>path</em> node
031     */
032    public JsonMenuItem(@Nonnull JsonNode node) throws IllegalArgumentException {
033        Objects.requireNonNull(node, "cannot parse null object");
034        this.path = node.path("path").asText(null); // NOI18N
035        if (this.path == null) {
036            throw new IllegalArgumentException("path not specified");
037        }
038        this.href = node.path("href").asText(this.href); // NOI18N
039        this.iconClass = node.path("iconClass").asText(this.iconClass); // NOI18N
040        this.title = node.path("title").asText(this.title); // NOI18N
041        this.position = node.path("position").asInt(this.position); // NOI18N
042        this.separatedBefore = node.path("separatedBefore").asBoolean(this.separatedBefore); // NOI18N
043        this.separatedAfter = node.path("separatedAfter").asBoolean(this.separatedAfter); // NOI18N
044        this.dynamic = node.path("dynamic").asBoolean(this.dynamic); // NOI18N
045    }
046
047    @Override
048    public String getPath() {
049        return this.path;
050    }
051
052    @Override
053    public String getHref() {
054        return this.href;
055    }
056
057    @Override
058    public String getIconClass() {
059        return this.iconClass;
060    }
061
062    @Override
063    public String getTitle(Locale locale) {
064        if (this.title == null) {
065            return Bundle.getMessage(locale, this.getPath());
066        }
067        return this.title;
068    }
069
070    @Override
071    public int getPosition() {
072        return this.position;
073    }
074
075    @Override
076    public boolean isSeparatedBefore() {
077        return this.separatedBefore;
078    }
079
080    @Override
081    public boolean isSeparatedAfter() {
082        return this.separatedAfter;
083    }
084
085    @Override
086    public boolean isDynamic() {
087        return this.dynamic;
088    }
089
090}