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; // NOSONAR 029 /** Role name used in a relation */ 030 public String key; // NOSONAR 031 /** The text to display */ 032 public String text; // NOSONAR 033 /** The context used for translating {@link #text} */ 034 public String text_context; // NOSONAR 035 /** The localized version of {@link #text}. */ 036 public String locale_text; // NOSONAR 037 /** An expression (cf. search dialog) for objects of this role */ 038 public SearchCompiler.Match memberExpression; // NOSONAR 039 040 public boolean required; // NOSONAR 041 private long count; 042 043 public void setType(String types) throws SAXException { 044 this.types = getType(types); 045 } 046 047 public void setRequisite(String str) throws SAXException { 048 if ("required".equals(str)) { 049 required = true; 050 } else if (!"optional".equals(str)) 051 throw new SAXException(tr("Unknown requisite: {0}", str)); 052 } 053 054 public void setMember_expression(String memberExpression) throws SAXException { 055 try { 056 final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting(); 057 searchSetting.text = memberExpression; 058 searchSetting.caseSensitive = true; 059 searchSetting.regexSearch = true; 060 this.memberExpression = SearchCompiler.compile(searchSetting); 061 } catch (SearchCompiler.ParseError ex) { 062 throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex); 063 } 064 } 065 066 public void setCount(String count) { 067 this.count = Long.parseLong(count); 068 } 069 070 /** 071 * Return either argument, the highest possible value or the lowest allowed value 072 * @param c count 073 * @return the highest possible value or the lowest allowed value 074 * @see #required 075 */ 076 public long getValidCount(long c) { 077 if (count > 0 && !required) 078 return c != 0 ? count : 0; 079 else if (count > 0) 080 return count; 081 else if (!required) 082 return c != 0 ? c : 0; 083 else 084 return c != 0 ? c : 1; 085 } 086 087 public boolean addToPanel(JPanel p) { 088 String cstring; 089 if (count > 0 && !required) { 090 cstring = "0,"+count; 091 } else if (count > 0) { 092 cstring = String.valueOf(count); 093 } else if (!required) { 094 cstring = "0-..."; 095 } else { 096 cstring = "1-..."; 097 } 098 if (locale_text == null) { 099 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}