Changeset 5404 in josm


Ignore:
Timestamp:
Aug 7, 2012 10:16:56 PM (10 months ago)
Author:
jttt
Message:

Speed up tagging preset dialog with lots of comboboxes

File:
1 edited

Legend:

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

    r5316 r5404  
    117117        public static PresetType forPrimitiveType(org.openstreetmap.josm.data.osm.OsmPrimitiveType type) { 
    118118            switch (type) { 
    119                 case NODE: 
    120                     return NODE; 
    121                 case WAY: 
    122                     return WAY; 
    123                 case CLOSEDWAY: 
    124                     return CLOSEDWAY; 
    125                 case RELATION: 
    126                 case MULTIPOLYGON: 
    127                     return RELATION; 
    128                 default: 
    129                     throw new IllegalArgumentException("Unexpected primitive type: " + type); 
     119            case NODE: 
     120                return NODE; 
     121            case WAY: 
     122                return WAY; 
     123            case CLOSEDWAY: 
     124                return CLOSEDWAY; 
     125            case RELATION: 
     126            case MULTIPOLYGON: 
     127                return RELATION; 
     128            default: 
     129                throw new IllegalArgumentException("Unexpected primitive type: " + type); 
    130130            } 
    131131        } 
     
    166166        public static MatchType ofString(String type) { 
    167167            for (MatchType i : EnumSet.allOf(MatchType.class)) { 
    168                 if (i.getValue().equals(type)) { 
     168                if (i.getValue().equals(type)) 
    169169                    return i; 
    170                 } 
    171170            } 
    172171            throw new IllegalArgumentException(type + " is not allowed"); 
     
    229228        Boolean matches(Map<String, String> tags) { 
    230229            switch (MatchType.ofString(match)) { 
    231                 case NONE: 
    232                     return null; 
    233                 case KEY: 
    234                     return tags.containsKey(key) ? true : null; 
    235                 case KEY_REQUIRED: 
    236                     return tags.containsKey(key); 
    237                 case KEY_VALUE: 
    238                     return tags.containsKey(key) && (getValues().contains(tags.get(key))); 
    239                 default: 
    240                     throw new IllegalStateException(); 
     230            case NONE: 
     231                return null; 
     232            case KEY: 
     233                return tags.containsKey(key) ? true : null; 
     234            case KEY_REQUIRED: 
     235                return tags.containsKey(key); 
     236            case KEY_VALUE: 
     237                return tags.containsKey(key) && (getValues().contains(tags.get(key))); 
     238            default: 
     239                throw new IllegalStateException(); 
    241240            } 
    242241        } 
     
    306305        private final File zipIcons = TaggingPreset.zipIcons; 
    307306 
     307        // Cached size (currently only for Combo) to speed up preset dialog initialization 
     308        private int prefferedWidth = -1; 
     309        private int prefferedHeight = -1; 
     310 
    308311        public String getListDisplay() { 
    309312            if (value.equals(DIFFERENT)) 
     
    339342            return translated 
    340343                    ? Utils.firstNonNull(locale_display_value, tr(display_value), trc(value_context, value)) 
    341                     : Utils.firstNonNull(display_value, value); 
     344                            : Utils.firstNonNull(display_value, value); 
    342345        } 
    343346 
     
    345348            return translated 
    346349                    ? Utils.firstNonNull(locale_short_description, tr(short_description)) 
    347                     : short_description; 
     350                            : short_description; 
    348351        } 
    349352 
     
    444447        @Override 
    445448        public Collection<String> getValues() { 
    446             if (default_ == null || default_.isEmpty()) { 
     449            if (default_ == null || default_.isEmpty()) 
    447450                return Collections.emptyList(); 
    448             } 
    449451            return Collections.singleton(default_); 
    450452        } 
     
    662664                e.locale_display_value = locale_display_values != null 
    663665                        ? display_array[i] 
    664                         : trc(values_context, fixPresetString(display_array[i])); 
    665                 if (short_descriptions_array != null) { 
    666                     e.locale_short_description = locale_short_descriptions != null 
    667                             ? short_descriptions_array[i] 
    668                             : tr(fixPresetString(short_descriptions_array[i])); 
    669                 } 
    670                 lhm.put(value_array[i], e); 
    671                 display_array[i] = e.getDisplayValue(true); 
     666                                : trc(values_context, fixPresetString(display_array[i])); 
     667                        if (short_descriptions_array != null) { 
     668                            e.locale_short_description = locale_short_descriptions != null 
     669                                    ? short_descriptions_array[i] 
     670                                            : tr(fixPresetString(short_descriptions_array[i])); 
     671                        } 
     672                        lhm.put(value_array[i], e); 
     673                        display_array[i] = e.getDisplayValue(true); 
    672674            } 
    673675 
     
    732734        } 
    733735 
    734         protected ListCellRenderer getListCellRenderer() { 
    735             return new ListCellRenderer() { 
    736  
    737                 JLabel lbl = new JLabel(); 
    738                 JComponent dummy = new JComponent() { 
    739                 }; 
    740  
    741                 public Component getListCellRendererComponent( 
    742                         JList list, 
    743                         Object value, 
    744                         int index, 
    745                         boolean isSelected, 
    746                         boolean cellHasFocus) { 
    747                     if (isSelected) { 
    748                         lbl.setBackground(list.getSelectionBackground()); 
    749                         lbl.setForeground(list.getSelectionForeground()); 
     736        private static ListCellRenderer RENDERER = new ListCellRenderer() { 
     737 
     738            JLabel lbl = new JLabel(); 
     739 
     740            public Component getListCellRendererComponent( 
     741                    JList list, 
     742                    Object value, 
     743                    int index, 
     744                    boolean isSelected, 
     745                    boolean cellHasFocus) { 
     746                PresetListEntry item = (PresetListEntry) value; 
     747 
     748                // Only return cached size, item is not shown 
     749                if (!list.isShowing() && item.prefferedWidth != -1 && item.prefferedHeight != -1) { 
     750                    if (index == -1) { 
     751                        lbl.setPreferredSize(new Dimension(item.prefferedWidth, 10)); 
    750752                    } else { 
    751                         lbl.setBackground(list.getBackground()); 
    752                         lbl.setForeground(list.getForeground()); 
    753                     } 
    754  
    755                     PresetListEntry item = (PresetListEntry) value; 
    756                     lbl.setOpaque(true); 
    757                     lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN)); 
    758                     lbl.setText("<html>" + item.getListDisplay() + "</html>"); 
    759                     lbl.setIcon(item.getIcon()); 
    760                     lbl.setEnabled(list.isEnabled()); 
    761                     // We do not want the editor to have the maximum height of all 
    762                     // entries. Return a dummy with bogus height. 
    763                     if (index == -1) { 
    764                         dummy.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10)); 
    765                         return dummy; 
     753                        lbl.setPreferredSize(new Dimension(item.prefferedWidth, item.prefferedHeight)); 
    766754                    } 
    767755                    return lbl; 
    768756                } 
    769             }; 
     757 
     758                lbl.setPreferredSize(null); 
     759 
     760 
     761                if (isSelected) { 
     762                    lbl.setBackground(list.getSelectionBackground()); 
     763                    lbl.setForeground(list.getSelectionForeground()); 
     764                } else { 
     765                    lbl.setBackground(list.getBackground()); 
     766                    lbl.setForeground(list.getForeground()); 
     767                } 
     768 
     769                lbl.setOpaque(true); 
     770                lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN)); 
     771                lbl.setText("<html>" + item.getListDisplay() + "</html>"); 
     772                lbl.setIcon(item.getIcon()); 
     773                lbl.setEnabled(list.isEnabled()); 
     774 
     775                // Cache size 
     776                item.prefferedWidth = lbl.getPreferredSize().width; 
     777                item.prefferedHeight = lbl.getPreferredSize().height; 
     778 
     779                // We do not want the editor to have the maximum height of all 
     780                // entries. Return a dummy with bogus height. 
     781                if (index == -1) { 
     782                    lbl.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10)); 
     783                } 
     784                return lbl; 
     785            } 
     786        }; 
     787 
     788 
     789        protected ListCellRenderer getListCellRenderer() { 
     790            return RENDERER; 
    770791        } 
    771792 
     
    949970     */ 
    950971    private static String[] splitEscaped(char delimiter, String s) { 
    951         if (s == null) { 
     972        if (s == null) 
    952973            return new String[0]; 
    953         } 
    954974        List<String> result = new ArrayList<String>(); 
    955975        boolean backslash = false; 
     
    12651285 
    12661286    static public EnumSet<PresetType> getType(String types) throws SAXException { 
    1267         if (typeCache.containsKey(types)) { 
     1287        if (typeCache.containsKey(types)) 
    12681288            return typeCache.get(types); 
    1269         } 
    12701289        EnumSet<PresetType> result = EnumSet.noneOf(PresetType.class); 
    12711290        for (String type : Arrays.asList(types.split(","))) { 
     
    13661385                        lastrole = (Roles) o; 
    13671386                    } else if (o instanceof Role) { 
    1368                         if (lastrole == null) { 
     1387                        if (lastrole == null) 
    13691388                            throw new SAXException(tr("Preset role element without parent")); 
    1370                         } 
    13711389                        lastrole.roles.add((Role) o); 
    13721390                    } else if (o instanceof PresetListEntry) { 
     
    16951713 
    16961714    public boolean matches(Collection<PresetType> t, Map<String, String> tags, boolean onlyShowable) { 
    1697         if (onlyShowable && !isShowable()) { 
     1715        if (onlyShowable && !isShowable()) 
    16981716            return false; 
    1699         } else if (!typeMatches(t)) { 
     1717        else if (!typeMatches(t)) 
    17001718            return false; 
    1701         } 
    17021719        boolean atLeastOnePositiveMatch = false; 
    17031720        for (Item item : data) { 
    17041721            Boolean m = item.matches(tags); 
    1705             if (m != null && !m) { 
     1722            if (m != null && !m) 
    17061723                return false; 
    1707             } else if (m != null) { 
     1724            else if (m != null) { 
    17081725                atLeastOnePositiveMatch = true; 
    17091726            } 
Note: See TracChangeset for help on using the changeset viewer.