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 * @param c count 071 * @return the highest possible value or the lowest allowed value 072 * @see #required 073 */ 074 public long getValidCount(long c) { 075 if (count > 0 && !required) 076 return c != 0 ? count : 0; 077 else if (count > 0) 078 return count; 079 else if (!required) 080 return c != 0 ? c : 0; 081 else 082 return c != 0 ? c : 1; 083 } 084 085 public boolean addToPanel(JPanel p) { 086 String cstring; 087 if (count > 0 && !required) { 088 cstring = "0,"+count; 089 } else if (count > 0) { 090 cstring = String.valueOf(count); 091 } else if (!required) { 092 cstring = "0-..."; 093 } else { 094 cstring = "1-..."; 095 } 096 if (locale_text == null) { 097 locale_text = getLocaleText(text, text_context, null); 098 } 099 p.add(new JLabel(locale_text+':'), GBC.std().insets(0, 0, 10, 0)); 100 p.add(new JLabel(key), GBC.std().insets(0, 0, 10, 0)); 101 p.add(new JLabel(cstring), types == null ? GBC.eol() : GBC.std().insets(0, 0, 10, 0)); 102 if (types != null) { 103 JPanel pp = new JPanel(); 104 for (TaggingPresetType t : types) { 105 pp.add(new JLabel(ImageProvider.get(t.getIconName()))); 106 } 107 p.add(pp, GBC.eol()); 108 } 109 return true; 110 } 111 } 112 113 public final List<Role> roles = new LinkedList<>(); 114 115 @Override 116 public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) { 117 p.add(new JLabel(" "), GBC.eol()); // space 118 if (!roles.isEmpty()) { 119 JPanel proles = new JPanel(new GridBagLayout()); 120 proles.add(new JLabel(tr("Available roles")), GBC.std().insets(0, 0, 10, 0)); 121 proles.add(new JLabel(tr("role")), GBC.std().insets(0, 0, 10, 0)); 122 proles.add(new JLabel(tr("count")), GBC.std().insets(0, 0, 10, 0)); 123 proles.add(new JLabel(tr("elements")), GBC.eol()); 124 for (Role i : roles) { 125 i.addToPanel(proles); 126 } 127 p.add(proles, GBC.eol()); 128 } 129 return false; 130 } 131 132 @Override 133 public void addCommands(List<Tag> changedTags) { 134 } 135}