Changeset 5158 in josm


Ignore:
Timestamp:
2012-04-01T20:36:32+02:00 (12 years ago)
Author:
simon04
Message:

see #7552 - presets: allow icons for individual combo items

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/data/defaultpresets.xml

    r5156 r5158  
    4040  display_values: comma separated list of values to be displayed instead of the
    4141                  database values, order and number must be equal to values
    42   short_description: comma separated list of texts to be displayed below each
    43                     display_value. (Only if it is not possible to describe
    44                     the entry in 2-3 words.) Instead of comma separeted list, you can
    45                     also write it in the following form:
    46                         <short_description>first description</short_description>
    47                                 ...
    48                         <short_description>last description</short_description>
     42  short_descriptions: comma separated list of texts to be displayed below each
     43                      display_value. (Only if it is not possible to describe
     44                      the entry in 2-3 words.) Instead of comma separeted list
     45  instead using values, display_values and short_descriptions, the following form is also supported:
     46    <list_item value="" display_value="' short_description="" icon=""/>
    4947  default: default string to display (defaults to "")
    5048  use_last_as_default: true/false/force (default is "false")
     
    6058  display_values: delimiter-separated list of values to be displayed instead of the
    6159                  database values, order and number must be equal to values
    62   short_description: delimiter-separated list of texts to be displayed below each
    63                     display_value. (Only if it is not possible to describe
    64                     the entry in 2-3 words.) Instead of a separated list, you can
    65                     also write it in the following form:
    66                         <short_description>first description</short_description>
    67                                 ...
    68                         <short_description>last description</short_description>
     60  short_descriptions: delimiter-separated list of texts to be displayed below each
     61                     display_value. (Only if it is not possible to describe
     62                     the entry in 2-3 words.) Instead of a separated list
     63  instead using values, display_values and short_descriptions, the following form is also supported:
     64    <list_item value="" display_value="' short_description="" icon=""/>
    6965  default: default string to display (defaults to "")
    7066  use_last_as_default: true/false/force (default is "false")
     
    700696            <optional>
    701697                <text key="name" text="Name" />
    702                 <combo key="sac_scale" text="SAC Scale" values="hiking,mountain_hiking,demanding_mountain_hiking,alpine_hiking,demanding_alpine_hiking,difficult_alpine_hiking" display_values="T1 - hiking trail,T2 - mountain hiking trail,T3 - difficult\, exposed hiking trail,T4 - difficult\, exposed\, steep alpine trail,T5 - difficult alpine trail with climbing,T6 - hazardous alpine trail with climbing">
    703                     <short_description>Trail well cleared. Area flat or slightly sloped, no fall hazard</short_description>
    704                     <short_description>Trail with continuous line and balanced ascent. Terrain partially steep, fall hazard possible</short_description>
    705                     <short_description>exposed sites may be secured with ropes or chains, possible need to use hands for balance. Partly exposed sites with fall hazard, scree, pathless jagged rocks</short_description>
    706                     <short_description>sometimes need for hand use to get ahead. Terrain quite exposed, precarious grassy acclivities, jagged rocks, facile snow-free glaciers.</short_description>
    707                     <short_description>single plainly climbing up to second grade. Exposed, demanding terrain, jagged rocks, few dangerous glacier and snow</short_description>
    708                     <short_description>climbing up to second grade. Often very exposed, precarious jagged rocks, glacier with danger to slip and fall</short_description>
     698                <combo key="sac_scale" text="SAC Scale">
     699                    <list_entry value="hiking" display_value="T1 - hiking trail" short_description="Trail well cleared. Area flat or slightly sloped, no fall hazard" />
     700                    <list_entry value="mountain_hiking" display_value="T2 - mountain hiking trail" short_description="Trail with continuous line and balanced ascent. Terrain partially steep, fall hazard possible" />
     701                    <list_entry value="demanding_mountain_hiking" display_value="T3 - difficult, exposed hiking trail" short_description="exposed sites may be secured with ropes or chains, possible need to use hands for balance. Partly exposed sites with fall hazard, scree, pathless jagged rocks" />
     702                    <list_entry value="alpine_hiking" display_value="T4 - difficult, exposed, steep alpine trail" short_description="sometimes need for hand use to get ahead. Terrain quite exposed, precarious grassy acclivities, jagged rocks, facile snow-free glaciers" />
     703                    <list_entry value="demanding_alpine_hiking" display_value="T5 - difficult alpine trail with climbing" short_description="single plainly climbing up to second grade. Exposed, demanding terrain, jagged rocks, few dangerous glacier and snow" />
     704                    <list_entry value="difficult_alpine_hiking" display_value="T6 - hazardous alpine trail with climbing" short_description="climbing up to second grade. Often very exposed, precarious jagged rocks, glacier with danger to slip and fall" />
    709705                </combo>
    710706                <combo key="mtb:scale" text="MTB Scale" values="0,1,2,3,4,5" />
  • trunk/data/tagging-preset.xsd

    r5157 r5158  
    126126        </complexType>
    127127
     128        <complexType name="list_entry">
     129                <attribute name="value" type="string" use="required" />
     130                <attribute name="display_value" type="string" />
     131                <attribute name="short_description" type="string" />
     132                <attribute name="icon" type="string" />
     133        </complexType>
     134
    128135        <complexType name="combo">
    129                 <sequence>
    130                         <element name="short_description" type="string" minOccurs="0"
    131                                 maxOccurs="unbounded" />
    132                 </sequence>
    133                 <attribute name="key" type="string" use="required" />
    134                 <attribute name="text" type="string" />
    135                 <attribute name="text_context" type="string" />
    136                 <attribute name="values" type="string" use="required" />
     136                <!-- use either list_entry's or a combination of values/display_values -->
     137                <sequence>
     138                        <element name="list_entry" type="tns:list_entry" minOccurs="0" maxOccurs="unbounded" />
     139                </sequence>
     140                <attribute name="key" type="string" use="required" />
     141                <attribute name="text" type="string" />
     142                <attribute name="text_context" type="string" />
     143                <attribute name="values" type="string" />
    137144                <attribute name="values_context" type="string" />
    138145                <attribute name="display_values" type="string" />
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r5155 r5158  
    88import java.awt.Component;
    99import java.awt.Dimension;
     10import java.awt.Font;
    1011import java.awt.GridBagLayout;
    1112import java.awt.Image;
     
    7273import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
    7374import org.openstreetmap.josm.gui.util.GuiHelper;
    74 import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    7575import org.openstreetmap.josm.io.MirroredInputStream;
    7676import org.openstreetmap.josm.tools.GBC;
     
    263263    }
    264264
    265     protected static class PresetListEntry {
    266         String value;
    267         String display_value;
    268         String short_description;
     265    public static class PresetListEntry {
     266        public String value;
     267        public String display_value;
     268        public String short_description;
     269        public String icon;
    269270
    270271        public String getListDisplay() {
     
    284285            if (short_description != null) {
    285286                // wrap in table to restrict the text width
    286                 res.append("<br><table><td width='232'>(").append(short_description).append(")</td></table>");
     287                res.append("<div style=\"width:300px; padding:0 0 5px 5px\">").append(short_description).append("</div>");
    287288            }
    288289            return res.toString();
     290        }
     291
     292        public ImageIcon getIcon() {
     293            return icon == null ? null : ImageProvider.getIfAvailable(icon);
     294        }
     295
     296        public PresetListEntry() {
    289297        }
    290298
     
    304312            if (value.equals(DIFFERENT))
    305313                return DIFFERENT;
    306             return display_value.replaceAll("<.*>", ""); // remove additional markup, e.g. <br>
     314            return display_value == null ? value : display_value.replaceAll("<.*>", ""); // remove additional markup, e.g. <br>
    307315        }
    308316    }
     
    525533        public String match = MatchType.NONE.getValue();
    526534
    527         protected List<String> short_description_list;
    528535        protected JComponent component;
    529         protected Map<String, PresetListEntry> lhm;
     536        protected Map<String, PresetListEntry> lhm = new LinkedHashMap<String, PresetListEntry>();
    530537        protected Usage usage;
    531538        protected Object originalValue;
     
    543550            // find out if our key is already used in the selection.
    544551            usage = determineTextUsage(sel, key);
    545             String def = default_;
     552
     553            String[] display_array;
     554            if (lhm.isEmpty()) {
     555                display_array = initListEntriesFromAttributes();
     556            } else {
     557                if (values != null) {
     558                    System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
     559                            + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
     560                            key, text, "values", "list_entry"));
     561                }
     562                if (display_values != null || locale_display_values != null) {
     563                    System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
     564                            + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
     565                            key, text, "display_values", "list_entry"));
     566                }
     567                if (short_descriptions != null || locale_short_descriptions != null) {
     568                    System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
     569                            + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
     570                            key, text, "short_descriptions", "list_entry"));
     571                }
     572                display_array = new String[lhm.values().size()];
     573                int i = 0;
     574                for (PresetListEntry e : lhm.values()) {
     575                    display_array[i++] = e.display_value;
     576                }
     577            }
     578
     579            if (locale_text == null) {
     580                if (text_context != null) {
     581                    locale_text = trc(text_context, fixPresetString(text));
     582                } else {
     583                    locale_text = tr(fixPresetString(text));
     584                }
     585            }
     586            p.add(new JLabel(locale_text + ":"), GBC.std().insets(0, 0, 10, 0));
     587
     588            addToPanelAnchor(p, default_, display_array);
     589
     590            return true;
     591
     592        }
     593
     594        private String[] initListEntriesFromAttributes() {
    546595
    547596            char delChar = getDelChar();
     
    563612            } else if (short_descriptions != null) {
    564613                short_descriptions_array = splitEscaped(delChar, short_descriptions);
    565             } else if (short_description_list != null) {
    566                 short_descriptions_array = short_description_list.toArray(new String[0]);
    567             }
    568 
    569             if (!"false".equals(use_last_as_default) && def == null && lastValue.containsKey(key)) {
    570                 def = lastValue.get(key);
    571614            }
    572615
     
    581624            }
    582625
    583             lhm = new LinkedHashMap<String, PresetListEntry>();
    584626            if (!usage.hasUniqueValue() && !usage.unused()) {
    585627                lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT));
     
    597639            }
    598640
    599             if (locale_text == null) {
    600                 if (text_context != null) {
    601                     locale_text = trc(text_context, fixPresetString(text));
    602                 } else {
    603                     locale_text = tr(fixPresetString(text));
    604                 }
    605             }
    606             p.add(new JLabel(locale_text + ":"), GBC.std().insets(0, 0, 10, 0));
    607 
    608             addToPanelAnchor(p, def, display_array);
    609 
    610             return true;
    611 
     641            return display_array;
    612642        }
    613643
     
    653683        }
    654684
    655         public void setShort_description(String s) {
    656             if (short_description_list == null) {
    657                 short_description_list = new ArrayList<String>();
    658             }
    659             short_description_list.add(tr(s));
     685        public void addListEntry(PresetListEntry e) {
     686            lhm.put(e.value, e);
     687        }
     688
     689        public void addListEntries(Collection<PresetListEntry> e) {
     690            for (PresetListEntry i : e) {
     691                addListEntry(i);
     692            }
    660693        }
    661694
     
    668701            return new ListCellRenderer() {
    669702
    670                 HtmlPanel lbl = new HtmlPanel();
     703                JLabel lbl = new JLabel();
    671704                JComponent dummy = new JComponent() {
    672705                };
     
    687720
    688721                    PresetListEntry item = (PresetListEntry) value;
    689                     String s = item.getListDisplay();
    690                     lbl.setText(s);
     722                    lbl.setOpaque(true);
     723                    lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
     724                    lbl.setText("<html>" + item.getListDisplay() + "</html>");
     725                    lbl.setIcon(item.getIcon());
    691726                    lbl.setEnabled(list.isEnabled());
    692727                    // We do not want the editor to have the maximum height of all
     
    12921327        parser.map("space", Space.class);
    12931328        parser.map("key", Key.class);
     1329        parser.map("list_entry", PresetListEntry.class);
    12941330        LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
    12951331        TaggingPresetMenu lastmenu = null;
    12961332        Roles lastrole = null;
     1333        List<PresetListEntry> listEntries = new LinkedList<PresetListEntry>();
    12971334
    12981335        if (validate) {
     
    13281365                lastrole = null;
    13291366            } else {
    1330                 if(all.size() != 0) {
    1331                     if(o instanceof Roles) {
    1332                         all.getLast().data.add((Item)o);
     1367                if (all.size() != 0) {
     1368                    if (o instanceof Roles) {
     1369                        all.getLast().data.add((Item) o);
    13331370                        lastrole = (Roles) o;
    1334                     }
    1335                     else if(o instanceof Role) {
    1336                         if(lastrole == null)
     1371                    } else if (o instanceof Role) {
     1372                        if (lastrole == null) {
    13371373                            throw new SAXException(tr("Preset role element without parent"));
     1374                        }
    13381375                        lastrole.roles.add((Role) o);
    1339                     }
    1340                     else {
    1341                         all.getLast().data.add((Item)o);
     1376                    } else if (o instanceof PresetListEntry) {
     1377                        listEntries.add((PresetListEntry) o);
     1378                    } else {
     1379                        all.getLast().data.add((Item) o);
     1380                        if (o instanceof ComboMultiSelect) {
     1381                            ((ComboMultiSelect) o).addListEntries(listEntries);
     1382                        }
     1383                        listEntries = new LinkedList<PresetListEntry>();
    13421384                        lastrole = null;
    13431385                    }
Note: See TracChangeset for help on using the changeset viewer.