Ignore:
Timestamp:
2015-10-12T22:58:25+02:00 (9 years ago)
Author:
Don-vip
Message:

major code cleanup/refactoring of tagging presets: slay the monster TaggingPresetItems (60 Kb, 1600 lines) and extract all its internal classes to a new package gui.tagging.presets.items

Location:
trunk/src/org/openstreetmap/josm/gui/tagging/presets
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItem.java

    r8862 r8863  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.tagging;
     2package org.openstreetmap.josm.gui.tagging.presets;
    33
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.trc;
     6
     7import java.io.File;
    48import java.util.Arrays;
    59import java.util.Collection;
     10import java.util.EnumSet;
     11import java.util.LinkedHashMap;
    612import java.util.List;
    713import java.util.Map;
     14import java.util.Set;
    815
     16import javax.swing.ImageIcon;
    917import javax.swing.JPanel;
    1018
     
    1523import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    1624import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     25import org.openstreetmap.josm.tools.ImageProvider;
     26import org.xml.sax.SAXException;
    1727
    1828/**
     
    2131 */
    2232public abstract class TaggingPresetItem {
     33
     34    // cache the parsing of types using a LRU cache (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
     35    private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
    2336
    2437    protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
     
    4760     * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
    4861     */
    49     abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
     62    protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
    5063
    5164    /**
     
    5366     * @param changedTags The list of changed tags to modify if needed
    5467     */
    55     abstract void addCommands(List<Tag> changedTags);
    56 
    57     boolean requestFocusInWindow() {
    58         return false;
    59     }
     68    protected abstract void addCommands(List<Tag> changedTags);
    6069
    6170    /**
     
    6574     * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
    6675     */
    67     Boolean matches(Map<String, String> tags) {
     76    protected Boolean matches(Map<String, String> tags) {
    6877        return null;
    6978    }
     79
     80    protected static Set<TaggingPresetType> getType(String types) throws SAXException {
     81        if (types == null || types.isEmpty()) {
     82            throw new SAXException(tr("Unknown type: {0}", types));
     83        }
     84        if (TYPE_CACHE.containsKey(types))
     85            return TYPE_CACHE.get(types);
     86        Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
     87        for (String type : Arrays.asList(types.split(","))) {
     88            try {
     89                TaggingPresetType presetType = TaggingPresetType.fromString(type);
     90                result.add(presetType);
     91            } catch (IllegalArgumentException e) {
     92                throw new SAXException(tr("Unknown type: {0}", type), e);
     93            }
     94        }
     95        TYPE_CACHE.put(types, result);
     96        return result;
     97    }
     98
     99    protected static String fixPresetString(String s) {
     100        return s == null ? s : s.replaceAll("'", "''");
     101    }
     102
     103    protected static String getLocaleText(String text, String text_context, String defaultText) {
     104        if (text == null) {
     105            return defaultText;
     106        } else if (text_context != null) {
     107            return trc(text_context, fixPresetString(text));
     108        } else {
     109            return tr(fixPresetString(text));
     110        }
     111    }
     112
     113    protected static Integer parseInteger(String str) {
     114        if (str == null || str.isEmpty())
     115            return null;
     116        try {
     117            return Integer.valueOf(str);
     118        } catch (Exception e) {
     119            if (Main.isTraceEnabled()) {
     120                Main.trace(e.getMessage());
     121            }
     122        }
     123        return null;
     124    }
     125
     126    protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
     127        final Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
     128        ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
     129        if (maxSize != null) {
     130            imgProv.setMaxSize(maxSize);
     131        }
     132        return imgProv.get();
     133    }
    70134}
Note: See TracChangeset for help on using the changeset viewer.