Changeset 5158 in josm for trunk/src


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.