Ticket #21408: 21408-4.patch

File 21408-4.patch, 9.4 KB (added by marcello@…, 3 years ago)

patch: fix fill-default property, add tests

  • src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItemGuiSupport.java

     
    8484    }
    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
    9093     */
  • src/org/openstreetmap/josm/gui/tagging/presets/items/Combo.java

     
    153153            p.add(combobox, GBC.eol().fill(GBC.HORIZONTAL)); // NOSONAR
    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             }
     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);
    164162        }
     163
    165164        combobox.addActionListener(l -> support.fireItemValueModified(this, key, getSelectedItem().value));
    166165        combobox.addComponentListener(new ComponentListener());
    167166
  • src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java

     
    2222import javax.swing.ListCellRenderer;
    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;
    2728import org.openstreetmap.josm.gui.widgets.JosmListCellRenderer;
     
    9394    /** Helps avoid duplicate list entries */
    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
    99100    /**
     
    317318    /**
    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.
     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"
    324331     */
    325     protected String getInitialValue(Usage usage) {
     332    protected String getInitialValue(Usage usage, TaggingPresetItemGuiSupport support) {
    326333        String initialValue = null;
    327         originalValue = null;
     334        originalValue = "";
    328335
    329336        if (usage.hasUniqueValue()) {
    330337            // all selected primitives have the same not empty value for this key
     
    334341            // at least one primitive has a value for this key (but not all have the same one)
    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_;
     346            if (!support.isPresetInitiallyMatches() && isUseLastAsDefault() && LAST_VALUES.containsKey(key)) {
     347                initialValue = LAST_VALUES.get(key);
     348            } else {
     349                initialValue = default_;
     350            }
    347351        }
    348         return initialValue;
     352        return initialValue != null ? initialValue : "";
    349353    }
    350354
    351355    @Override
     
    353357        String value = getSelectedItem().value;
    354358
    355359        // no change if same as before
    356         if (value.isEmpty() && originalValue == null)
    357             return;
    358360        if (value.equals(originalValue))
    359361            return;
    360362        changedTags.add(new Tag(key, value));
  • src/org/openstreetmap/josm/gui/tagging/presets/items/MultiSelect.java

     
    4848        model.clear();
    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.
    5454        presetListEntries.forEach(this::addEntry);
     
    6161        }
    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);
    6767                addEntry(e);
  • test/unit/org/openstreetmap/josm/gui/tagging/presets/items/ComboTest.java

     
    2727     */
    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    /**
    3333     * Unit test for {@link Combo#addToPanel}.
     
    5454        OsmPrimitive waySI = OsmUtils.createPrimitive("way addr:country=SI");
    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);
    6162        combo.addToPanel(new JPanel(), TaggingPresetItemGuiSupport.create(false, wayTagged));
     
    7879        assertEquals("SI", 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
    8397        combo.use_last_as_default = 1; // untagged objects only
     
    103117        assertEquals("SI", combo.getSelectedItem().value);
    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();
    108123    }