1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
16 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
|
---|
20 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
---|
21 | import org.openstreetmap.josm.tools.GBC;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | public class Roles extends TaggingPresetItem {
|
---|
26 |
|
---|
27 | public static class Role {
|
---|
28 | public Set<TaggingPresetType> types; // NOSONAR
|
---|
29 | /** Role name used in a relation */
|
---|
30 | public String key; // NOSONAR
|
---|
31 | /** The text to display */
|
---|
32 | public String text; // NOSONAR
|
---|
33 | /** The context used for translating {@link #text} */
|
---|
34 | public String text_context; // NOSONAR
|
---|
35 | /** The localized version of {@link #text}. */
|
---|
36 | public String locale_text; // NOSONAR
|
---|
37 | /** An expression (cf. search dialog) for objects of this role */
|
---|
38 | public SearchCompiler.Match memberExpression; // NOSONAR
|
---|
39 |
|
---|
40 | public boolean required; // NOSONAR
|
---|
41 | private long count;
|
---|
42 |
|
---|
43 | public void setType(String types) throws SAXException {
|
---|
44 | this.types = getType(types);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void setRequisite(String str) throws SAXException {
|
---|
48 | if ("required".equals(str)) {
|
---|
49 | required = true;
|
---|
50 | } else if (!"optional".equals(str))
|
---|
51 | throw new SAXException(tr("Unknown requisite: {0}", str));
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void setMember_expression(String memberExpression) throws SAXException {
|
---|
55 | try {
|
---|
56 | final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
|
---|
57 | searchSetting.text = memberExpression;
|
---|
58 | searchSetting.caseSensitive = true;
|
---|
59 | searchSetting.regexSearch = true;
|
---|
60 | this.memberExpression = SearchCompiler.compile(searchSetting);
|
---|
61 | } catch (SearchCompiler.ParseError ex) {
|
---|
62 | throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void setCount(String count) {
|
---|
67 | this.count = Long.parseLong(count);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Return either argument, the highest possible value or the lowest allowed value
|
---|
72 | * @param c count
|
---|
73 | * @return the highest possible value or the lowest allowed value
|
---|
74 | * @see #required
|
---|
75 | */
|
---|
76 | public long getValidCount(long c) {
|
---|
77 | if (count > 0 && !required)
|
---|
78 | return c != 0 ? count : 0;
|
---|
79 | else if (count > 0)
|
---|
80 | return count;
|
---|
81 | else if (!required)
|
---|
82 | return c != 0 ? c : 0;
|
---|
83 | else
|
---|
84 | return c != 0 ? c : 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public boolean addToPanel(JPanel p) {
|
---|
88 | String cstring;
|
---|
89 | if (count > 0 && !required) {
|
---|
90 | cstring = "0,"+count;
|
---|
91 | } else if (count > 0) {
|
---|
92 | cstring = String.valueOf(count);
|
---|
93 | } else if (!required) {
|
---|
94 | cstring = "0-...";
|
---|
95 | } else {
|
---|
96 | cstring = "1-...";
|
---|
97 | }
|
---|
98 | if (locale_text == null) {
|
---|
99 | locale_text = getLocaleText(text, text_context, null);
|
---|
100 | }
|
---|
101 | p.add(new JLabel(locale_text+':'), GBC.std().insets(0, 0, 10, 0));
|
---|
102 | p.add(new JLabel(key), GBC.std().insets(0, 0, 10, 0));
|
---|
103 | p.add(new JLabel(cstring), types == null ? GBC.eol() : GBC.std().insets(0, 0, 10, 0));
|
---|
104 | if (types != null) {
|
---|
105 | JPanel pp = new JPanel();
|
---|
106 | for (TaggingPresetType t : types) {
|
---|
107 | pp.add(new JLabel(ImageProvider.get(t.getIconName())));
|
---|
108 | }
|
---|
109 | p.add(pp, GBC.eol());
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public final List<Role> roles = new LinkedList<>();
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
119 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
120 | if (!roles.isEmpty()) {
|
---|
121 | JPanel proles = new JPanel(new GridBagLayout());
|
---|
122 | proles.add(new JLabel(tr("Available roles")), GBC.std().insets(0, 0, 10, 0));
|
---|
123 | proles.add(new JLabel(tr("role")), GBC.std().insets(0, 0, 10, 0));
|
---|
124 | proles.add(new JLabel(tr("count")), GBC.std().insets(0, 0, 10, 0));
|
---|
125 | proles.add(new JLabel(tr("elements")), GBC.eol());
|
---|
126 | for (Role i : roles) {
|
---|
127 | i.addToPanel(proles);
|
---|
128 | }
|
---|
129 | p.add(proles, GBC.eol());
|
---|
130 | }
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void addCommands(List<Tag> changedTags) {
|
---|
136 | }
|
---|
137 | }
|
---|