Changeset 18260 in josm for trunk


Ignore:
Timestamp:
2021-10-09T15:05:34+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21408 - fix fill-default property, add tests (patch by marcello)

Location:
trunk
Files:
5 edited

Legend:

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

    r18257 r18260  
    8585
    8686    /**
    87      * Returns true if all selected primitives matched this preset (before opening the dialog)
     87     * Returns true if all selected primitives matched this preset (before opening the dialog).
     88     * <p>
     89     * This usually means that the preset dialog was opened from the Tags / Memberships panel as
     90     * opposed to being opened by selection from the menu or toolbar or the search.
    8891     *
    8992     * @return true if the preset initially matched
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Combo.java

    r18257 r18260  
    154154        }
    155155
    156         String valueToSelect = getInitialValue(usage);
    157         if (valueToSelect != null) {
    158             PresetListEntry selItem = find(valueToSelect);
    159             if (selItem != null) {
    160                 combobox.setSelectedItem(selItem);
    161             } else {
    162                 combobox.setText(valueToSelect);
    163             }
    164         }
     156        String initialValue = getInitialValue(usage, support);
     157        PresetListEntry selItem = find(initialValue);
     158        if (selItem != null) {
     159            combobox.setSelectedItem(selItem);
     160        } else {
     161            combobox.setText(initialValue);
     162        }
     163
    165164        combobox.addActionListener(l -> support.fireItemValueModified(this, key, getSelectedItem().value));
    166165        combobox.addComponentListener(new ComponentListener());
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java

    r18258 r18260  
    2323
    2424import org.openstreetmap.josm.data.osm.Tag;
     25import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItemGuiSupport;
    2526import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSelector;
    2627import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
     
    9495    protected final Map<String, PresetListEntry> seenValues = new TreeMap<>();
    9596    protected Usage usage;
    96     /** Used to see if the user edited the value. May be null. */
     97    /** Used to see if the user edited the value. */
    9798    protected String originalValue;
    9899
     
    318319     * Returns the initial value to use for this preset.
    319320     * <p>
    320      * The initial value is the value shown in the control when the preset dialogs opens.
     321     * The initial value is the value shown in the control when the preset dialog opens. For a
     322     * discussion of all the options see the enclosed tickets.
    321323     *
    322324     * @param usage The key Usage
     325     * @param support The support
    323326     * @return The initial value to use.
    324      */
    325     protected String getInitialValue(Usage usage) {
     327     *
     328     * @see "https://josm.openstreetmap.de/ticket/5564"
     329     * @see "https://josm.openstreetmap.de/ticket/12733"
     330     * @see "https://josm.openstreetmap.de/ticket/17324"
     331     */
     332    protected String getInitialValue(Usage usage, TaggingPresetItemGuiSupport support) {
    326333        String initialValue = null;
    327         originalValue = null;
     334        originalValue = "";
    328335
    329336        if (usage.hasUniqueValue()) {
     
    335342            initialValue = DIFFERENT;
    336343            originalValue = initialValue;
    337         } else if (PROP_FILL_DEFAULT.get() || isForceUseLastAsDefault()) {
     344        } else if (!usage.hadKeys() || isForceUseLastAsDefault() || PROP_FILL_DEFAULT.get()) {
    338345            // at this point no primitive had any value for this key
    339             // use the last value no matter what
    340             initialValue = LAST_VALUES.get(key);
    341         } else if (!usage.hadKeys() && isUseLastAsDefault()) {
    342             // use the last value only on objects with no keys at all
    343             initialValue = LAST_VALUES.get(key);
    344         } else if (!usage.hadKeys()) {
    345             // use the default only on objects with no keys at all
    346             initialValue = default_;
    347         }
    348         return initialValue;
     346            if (!support.isPresetInitiallyMatches() && isUseLastAsDefault() && LAST_VALUES.containsKey(key)) {
     347                initialValue = LAST_VALUES.get(key);
     348            } else {
     349                initialValue = default_;
     350            }
     351        }
     352        return initialValue != null ? initialValue : "";
    349353    }
    350354
     
    354358
    355359        // no change if same as before
    356         if (value.isEmpty() && originalValue == null)
    357             return;
    358360        if (value.equals(originalValue))
    359361            return;
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/MultiSelect.java

    r18257 r18260  
    4949        // disable if the selected primitives have different values
    5050        list.setEnabled(usage.hasUniqueValue() || usage.unused());
    51         String initialValue = getInitialValue(usage);
     51        String initialValue = getInitialValue(usage, support);
    5252
    5353        // Add values from the preset.
     
    6262
    6363        // Select the values in the initial value.
    64         if (initialValue != null && !DIFFERENT.equals(initialValue)) {
     64        if (!initialValue.isEmpty() && !DIFFERENT.equals(initialValue)) {
    6565            for (String value : initialValue.split(String.valueOf(delimiter), -1)) {
    6666                PresetListEntry e = new PresetListEntry(value, this);
  • trunk/test/unit/org/openstreetmap/josm/gui/tagging/presets/items/ComboTest.java

    r18254 r18260  
    2828    @RegisterExtension
    2929    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    30     public JOSMTestRules test = new JOSMTestRules().main().i18n("de");
     30    public JOSMTestRules test = new JOSMTestRules().preferences().main().i18n("de");
    3131
    3232    /**
     
    5555        KeyedItem.LAST_VALUES.clear();
    5656        KeyedItem.LAST_VALUES.put("addr:country", "AT");
     57        Combo.PROP_FILL_DEFAULT.put(false);
     58        combo.use_last_as_default = 0;
    5759
    58         combo.use_last_as_default = 0;
    5960        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, way));
    6061        assertEquals("", combo.getSelectedItem().value);
     
    7980        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayAT, waySI));
    8081        assertEquals(Combo.DIFFERENT, combo.getSelectedItem().value);
     82
     83        Combo.PROP_FILL_DEFAULT.put(true);
     84        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, way));
     85        assertEquals("AT", combo.getSelectedItem().value);
     86        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayTagged));
     87        assertEquals("AT", combo.getSelectedItem().value);
     88        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayAT));
     89        assertEquals("AT", combo.getSelectedItem().value);
     90        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, waySI));
     91        assertEquals("SI", combo.getSelectedItem().value);
     92        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayAT, waySI));
     93        assertEquals(Combo.DIFFERENT, combo.getSelectedItem().value);
     94        Combo.PROP_FILL_DEFAULT.put(false);
    8195        combo.default_ = null;
    8296
     
    104118        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayAT, waySI));
    105119        assertEquals(Combo.DIFFERENT, combo.getSelectedItem().value);
     120        combo.use_last_as_default = 0;
    106121
    107122        KeyedItem.LAST_VALUES.clear();
Note: See TracChangeset for help on using the changeset viewer.