Changeset 3726 in josm for trunk


Ignore:
Timestamp:
2010-12-15T20:36:19+01:00 (13 years ago)
Author:
bastiK
Message:

add short_description (for sac_scale preset)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/data/defaultpresets.xml

    r3668 r3726  
    506506            <optional>
    507507                <text key="name" text="Name" default="" delete_if_empty="true" />
    508                 <combo key="sac_scale" text="SAC Scale" values="hiking,mountain_hiking,demanding_mountain_hiking,alpine_hiking,demanding_alpine_hiking,difficult_alpine_hiking" default="" delete_if_empty="true" />
     508                <combo key="sac_scale" text="SAC Scale" values="hiking,mountain_hiking,demanding_mountain_hiking,alpine_hiking,demanding_alpine_hiking,difficult_alpine_hiking" default="" delete_if_empty="true" display_values="T1 - hiking trail,T2 - mountain hiking trail,T3 - difficult,T4 - difficult\, exposed\, steep alpine trail,T5 - difficult\, exposed\, steep alpine trail&lt;br&gt;with climbing,T6 - difficult\, hazardous alpine trail with&lt;br&gt;climbing">
     509                    <short_description>Trail well cleared. Area flat or slightly sloped, no fall hazard</short_description>
     510                    <short_description>Trail with continuous line and balanced ascent. Terrain partially steep, fall hazard possible</short_description>
     511                    <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>
     512                    <short_description>sometimes need for hand use to get ahead. Terrain quite exposed, precarious grassy acclivities, jagged rocks, facile snow-free glaciers.</short_description>
     513                    <short_description>single plainly climbing up to second grade. Exposed, demanding terrain, jagged rocks, few dangerous glacier and snow</short_description>
     514                    <short_description>climbing up to second grade. Often very exposed, precarious jagged rocks, glacier with danger to slip and fall</short_description>
     515                </combo>
    509516                <combo key="surface" text="Surface" values="paved,unpaved,asphalt,concrete,metal,wood,paving_stones,cobblestone,gravel,pebblestone,compacted,grass_paver,grass,sand,ground" default="" delete_if_empty="true" />
    510517                <combo key="mtb:scale" text="MTB Scale" values="0,1,2,3,4,5" default="" delete_if_empty="true" />
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r3707 r3726  
    77
    88import java.awt.Component;
     9import java.awt.Dimension;
    910import java.awt.GridBagLayout;
    1011import java.awt.Image;
     
    2728import java.util.LinkedList;
    2829import java.util.List;
     30import java.util.Map;
    2931import java.util.TreeSet;
    3032
     
    3537import javax.swing.JComponent;
    3638import javax.swing.JLabel;
     39import javax.swing.JList;
    3740import javax.swing.JOptionPane;
    3841import javax.swing.JPanel;
    3942import javax.swing.JTextField;
     43import javax.swing.ListCellRenderer;
    4044import javax.swing.SwingUtilities;
    4145
     
    6165import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
    6266import org.openstreetmap.josm.gui.util.GuiHelper;
     67import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    6368import org.openstreetmap.josm.io.MirroredInputStream;
    6469import org.openstreetmap.josm.tools.GBC;
     
    344349        public String display_values;
    345350        public String locale_display_values;
     351        public String short_descriptions;
     352        public String locale_short_descriptions;
    346353        public String default_;
    347354        public boolean delete_if_empty = false;
     
    350357        public boolean required = false;
    351358
     359        private List<String> short_description_list;
    352360        private JComboBox combo;
    353         private LinkedHashMap<String,String> lhm;
     361        private Map<String, PresetListEnty> lhm;
    354362        private Usage usage;
    355         private String originalValue;
     363        private PresetListEnty originalValue;
    356364
    357365        @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
     
    363371            String[] value_array = values.split(",");
    364372            String[] display_array;
     373            String[] short_descriptions_array = null;
    365374
    366375            if(locale_display_values != null) {
    367                 display_array = locale_display_values.split(",");
    368             } else if(display_values != null) {
    369                 display_array = display_values.split(",");
     376                display_array = splitEscaped(locale_display_values);
     377            } else if (display_values != null) {
     378                display_array = splitEscaped(display_values);
    370379            } else {
    371380                display_array = value_array;
    372381            }
    373382
    374             if(use_last_as_default && def == null && lastValue.containsKey(key))
    375             {
     383            if (locale_short_descriptions != null) {
     384                short_descriptions_array = splitEscaped(locale_short_descriptions);
     385            } else if (short_descriptions != null) {
     386                short_descriptions_array = splitEscaped(short_descriptions);
     387            } else if (short_description_list != null) {
     388                short_descriptions_array = short_description_list.toArray(new String[0]);
     389            }
     390
     391            if (use_last_as_default && def == null && lastValue.containsKey(key)) {
    376392                def = lastValue.get(key);
    377393            }
     
    382398            }
    383399
    384             lhm = new LinkedHashMap<String,String>();
    385             if (!usage.hasUniqueValue() && !usage.unused()){
    386                 lhm.put(DIFFERENT, DIFFERENT);
     400            if (short_descriptions_array != null && short_descriptions_array.length != value_array.length) {
     401                System.err.println(tr("Broken tagging preset \"{0}-{1}\" - number of items in short_descriptions must be the same as in values", key, text));
     402                short_descriptions_array = null;
     403            }
     404
     405            lhm = new LinkedHashMap<String, PresetListEnty>();
     406            if (!usage.hasUniqueValue() && !usage.unused()) {
     407                lhm.put(DIFFERENT, new PresetListEnty(DIFFERENT));
    387408            }
    388409            for (int i=0; i<value_array.length; i++) {
    389                 lhm.put(value_array[i], (locale_display_values == null)
     410                PresetListEnty e = new PresetListEnty(value_array[i]);
     411                e.display_value = (locale_display_values == null)
    390412                        ? (values_context == null ? tr(display_array[i])
    391                                 : trc(values_context, display_array[i])) : display_array[i]);
     413                                : trc(values_context, display_array[i])) : display_array[i];
     414                if (short_descriptions_array != null) {
     415                    e.short_description = locale_short_descriptions == null ? tr(short_descriptions_array[i])
     416                            : short_descriptions_array[i];
     417                }
     418                lhm.put(value_array[i], e);
    392419            }
    393420            if(!usage.unused()){
    394421                for (String s : usage.values) {
    395422                    if (!lhm.containsKey(s)) {
    396                         lhm.put(s, s);
     423                        lhm.put(s, new PresetListEnty(s));
    397424                    }
    398425                }
    399426            }
    400427            if (def != null && !lhm.containsKey(def)) {
    401                 lhm.put(def, def);
    402             }
    403             if(!lhm.containsKey("")) {
    404                 lhm.put("", "");
    405             }
     428                lhm.put(def, new PresetListEnty(def));
     429            }
     430            lhm.put("", new PresetListEnty(""));
    406431
    407432            combo = new JComboBox(lhm.values().toArray());
     433            combo.setRenderer(new PresetComboListCellRenderer());
    408434            combo.setEditable(editable);
    409435            combo.setMaximumRowCount(13);
     
    414440
    415441            if (usage.hasUniqueValue() && !usage.unused()){
    416                 originalValue=usage.getFirst();
    417                 combo.setSelectedItem(lhm.get(originalValue));
     442                originalValue=lhm.get(usage.getFirst());
    418443            }
    419444            // use default only in case it is a totally new entry
    420445            else if(def != null && !usage.hadKeys()) {
    421                 combo.setSelectedItem(def);
    422                 originalValue=DIFFERENT;
     446                originalValue=lhm.get(DIFFERENT);
    423447            }
    424448            else if(usage.unused()){
    425                 combo.setSelectedItem("");
    426                 originalValue="";
     449                originalValue=lhm.get("");
    427450            }
    428451            else{
    429                 combo.setSelectedItem(DIFFERENT);
    430                 originalValue=DIFFERENT;
    431             }
     452                originalValue=lhm.get(DIFFERENT);
     453            }
     454            combo.setSelectedItem(originalValue);
    432455
    433456            if(locale_text == null) {
     
    442465            return true;
    443466        }
     467
     468        private static class PresetListEnty {
     469            String value;
     470            String display_value;
     471            String short_description;
     472
     473            public String getListDisplay() {
     474                if (value.equals(DIFFERENT))
     475                    return "<b>"+DIFFERENT.replaceAll("<", "&lt;").replaceAll(">", "&gt;")+"</b>";
     476
     477                if (value.equals(""))
     478                    return "&nbsp;";
     479
     480                StringBuilder res = new StringBuilder("<b>");
     481                if (display_value != null) {
     482                    res.append(display_value);
     483                } else {
     484                    res.append(value);
     485                }
     486                res.append("</b>");
     487                if (short_description != null) {
     488                    // wrap in table to restrict the text width
     489                    res.append("<br><table><td width='232'>(").append(short_description).append(")</td></table>");
     490                }
     491                return res.toString();
     492            }
     493
     494            public PresetListEnty(String value) {
     495                this.value = value;
     496                this.display_value = value;
     497            }
     498
     499            public PresetListEnty(String value, String display_value) {
     500                this.value = value;
     501                this.display_value = display_value;
     502            }
     503
     504            // toString is mainly used to initialize the Editor
     505            @Override
     506            public String toString() {
     507                if (value.equals(DIFFERENT))
     508                    return DIFFERENT;
     509                return display_value.replaceAll("<.*>", ""); // remove additional markup, e.g. <br>
     510            }
     511        }
     512
     513        private static class PresetComboListCellRenderer implements ListCellRenderer {
     514
     515            HtmlPanel lbl;
     516            JComponent dummy = new JComponent() {};
     517
     518            public PresetComboListCellRenderer() {
     519                lbl = new HtmlPanel();
     520            }
     521
     522            public Component getListCellRendererComponent(
     523                    JList list,
     524                    Object value,
     525                    int index,
     526                    boolean isSelected,
     527                    boolean cellHasFocus)
     528            {
     529                if (isSelected) {
     530                    lbl.setBackground(list.getSelectionBackground());
     531                    lbl.setForeground(list.getSelectionForeground());
     532                } else {
     533                    lbl.setBackground(list.getBackground());
     534                    lbl.setForeground(list.getForeground());
     535                }
     536
     537                PresetListEnty item = (PresetListEnty) value;
     538                String s = item.getListDisplay();
     539                lbl.setText(s);
     540                // We do not want the editor to have the maximum height of all
     541                // entries. Return a dummy with bogus height.
     542                if (index == -1) {
     543                    dummy.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10));
     544                    return dummy;
     545                }
     546                return lbl;
     547            }
     548        }
     549
     550        // allow escaped comma in comma separated list:
     551        // "A\, B\, C,one\, two" --> ["A, B, C", "one, two"]
     552        private static String[] splitEscaped(String s) {
     553            String[] res = s.replaceAll("\\\\,", "\u0091").split(",");
     554            for (int i=0; i<res.length; ++i) {
     555                res[i] = res[i].replaceAll("\u0091", ",");
     556            }
     557            return res;
     558        }
     559
    444560        @Override public void addCommands(List<Tag> changedTags) {
    445561            Object obj = combo.getSelectedItem();
     
    453569            {
    454570                for (String key : lhm.keySet()) {
    455                     String k = lhm.get(key);
     571                    String k = lhm.get(key).toString();
    456572                    if (k != null && k.equals(display)) {
    457573                        value=key;
     
    466582
    467583            // no change if same as before
    468             if (value.equals(originalValue) || (originalValue == null &&  value.length() == 0)) return;
     584            if (originalValue == null) {
     585                if (value.length() == 0)
     586                    return;
     587            } else if (value.equals(originalValue.toString()))
     588                return;
    469589
    470590            if (delete_if_empty && value.length() == 0) {
     
    474594                lastValue.put(key, value);
    475595            }
     596            System.err.print("change: "+key+" "+value);
    476597            changedTags.add(new Tag(key, value));
    477598        }
     599
     600        public void setShort_description(String s) {
     601            if (short_description_list == null) {
     602                short_description_list = new ArrayList<String>();
     603            }
     604            short_description_list.add(tr(s));
     605        }
     606
    478607        @Override boolean requestFocusInWindow() {return combo.requestFocusInWindow();}
    479608    }
Note: See TracChangeset for help on using the changeset viewer.