source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Roles.java@ 9492

Last change on this file since 9492 was 9492, checked in by Don-vip, 8 years ago

presets items: fix sonar issues, add javadoc

File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets.items;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.Collection;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Set;
11
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14
15import org.openstreetmap.josm.actions.search.SearchAction;
16import org.openstreetmap.josm.actions.search.SearchCompiler;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Tag;
19import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
20import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.ImageProvider;
23import org.xml.sax.SAXException;
24
25public 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 member_expression) throws SAXException {
55 try {
56 final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
57 searchSetting.text = member_expression;
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}
Note: See TracBrowser for help on using the repository browser.