001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets.items;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007import java.util.Collection;
008import java.util.LinkedList;
009import java.util.List;
010import java.util.Set;
011
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014
015import org.openstreetmap.josm.actions.search.SearchAction;
016import org.openstreetmap.josm.actions.search.SearchCompiler;
017import org.openstreetmap.josm.data.osm.OsmPrimitive;
018import org.openstreetmap.josm.data.osm.Tag;
019import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
020import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
021import org.openstreetmap.josm.tools.GBC;
022import org.openstreetmap.josm.tools.ImageProvider;
023import org.xml.sax.SAXException;
024
025public class Roles extends TaggingPresetItem {
026
027    public static class Role {
028        public Set<TaggingPresetType> types;
029        public String key;
030        /** The text to display */
031        public String text;
032        /** The context used for translating {@link #text} */
033        public String text_context;
034        /** The localized version of {@link #text}. */
035        public String locale_text;
036        public SearchCompiler.Match memberExpression;
037
038        public boolean required;
039        private long count;
040
041        public void setType(String types) throws SAXException {
042            this.types = getType(types);
043        }
044
045        public void setRequisite(String str) throws SAXException {
046            if ("required".equals(str)) {
047                required = true;
048            } else if (!"optional".equals(str))
049                throw new SAXException(tr("Unknown requisite: {0}", str));
050        }
051
052        public void setMember_expression(String member_expression) throws SAXException {
053            try {
054                final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
055                searchSetting.text = member_expression;
056                searchSetting.caseSensitive = true;
057                searchSetting.regexSearch = true;
058                this.memberExpression = SearchCompiler.compile(searchSetting);
059            } catch (SearchCompiler.ParseError ex) {
060                throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex);
061            }
062        }
063
064        public void setCount(String count) {
065            this.count = Long.parseLong(count);
066        }
067
068        /**
069         * Return either argument, the highest possible value or the lowest allowed value
070         */
071        public long getValidCount(long c) {
072            if (count > 0 && !required)
073                return c != 0 ? count : 0;
074            else if (count > 0)
075                return count;
076            else if (!required)
077                return c != 0 ? c : 0;
078            else
079                return c != 0 ? c : 1;
080        }
081
082        public boolean addToPanel(JPanel p) {
083            String cstring;
084            if (count > 0 && !required) {
085                cstring = "0,"+count;
086            } else if (count > 0) {
087                cstring = String.valueOf(count);
088            } else if (!required) {
089                cstring = "0-...";
090            } else {
091                cstring = "1-...";
092            }
093            if (locale_text == null) {
094                locale_text = getLocaleText(text, text_context, null);
095            }
096            p.add(new JLabel(locale_text+':'), GBC.std().insets(0, 0, 10, 0));
097            p.add(new JLabel(key), GBC.std().insets(0, 0, 10, 0));
098            p.add(new JLabel(cstring), types == null ? GBC.eol() : GBC.std().insets(0, 0, 10, 0));
099            if (types != null) {
100                JPanel pp = new JPanel();
101                for (TaggingPresetType t : types) {
102                    pp.add(new JLabel(ImageProvider.get(t.getIconName())));
103                }
104                p.add(pp, GBC.eol());
105            }
106            return true;
107        }
108    }
109
110    public final List<Role> roles = new LinkedList<>();
111
112    @Override
113    public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
114        p.add(new JLabel(" "), GBC.eol()); // space
115        if (!roles.isEmpty()) {
116            JPanel proles = new JPanel(new GridBagLayout());
117            proles.add(new JLabel(tr("Available roles")), GBC.std().insets(0, 0, 10, 0));
118            proles.add(new JLabel(tr("role")), GBC.std().insets(0, 0, 10, 0));
119            proles.add(new JLabel(tr("count")), GBC.std().insets(0, 0, 10, 0));
120            proles.add(new JLabel(tr("elements")), GBC.eol());
121            for (Role i : roles) {
122                i.addToPanel(proles);
123            }
124            p.add(proles, GBC.eol());
125        }
126        return false;
127    }
128
129    @Override
130    public void addCommands(List<Tag> changedTags) {
131    }
132}