Changeset 19261 in josm
- Timestamp:
- 2024-11-25T23:08:52+01:00 (8 weeks ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/resources/data/defaultpresets.xml
r19257 r19261 7533 7533 </combo> 7534 7534 <optional> 7535 <text key="inscription" text="Inscription" /> 7535 <text key="inscription" text="Inscription" multiline="true" /> 7536 7536 <text key="artist_name" text="Artist Name" /> 7537 7537 <combo key="material" text="Material" values="concrete,granite,metal,plastic,steel,stone,wood" /> … … 7547 7547 <reference ref="religious_catholic_christian" /> 7548 7548 <text key="start_date" text="Start date" /> 7549 <text key="inscription" text="Inscription" /> 7549 <text key="inscription" text="Inscription" multiline="true" /> 7550 7550 </item> <!-- Wayside Cross --> 7551 7551 <item name="Wayside Shrine" icon="presets/religion/wayside_shrine.svg" type="node,closedway" preset_name_label="true"> … … 7558 7558 <combo key="building" text="Building" values="wayside_shrine,yes" values_context="building" /> 7559 7559 <text key="start_date" text="Start date" /> 7560 <text key="inscription" text="Inscription" /> 7560 <text key="inscription" text="Inscription" multiline="true" /> 7561 7561 </item> <!-- Wayside Shrine --> 7562 7562 <item name="Boundary Stone" icon="presets/landmark/boundary_stone.svg" type="node,closedway" preset_name_label="true"> … … 7566 7566 <optional> 7567 7567 <text key="name" text="Name" /> 7568 <text key="inscription" text="Inscription" /> 7568 <text key="inscription" text="Inscription" multiline="true" /> 7569 7569 </optional> 7570 7570 <preset_link preset_name="Boundary Marker" text="Similar but different tags:" /> … … 7576 7576 <optional> 7577 7577 <text key="name" text="Name" /> 7578 <text key="inscription" text="Inscription" /> 7578 <text key="inscription" text="Inscription" multiline="true" /> 7579 7579 </optional> 7580 7580 </item> <!-- Milestone --> … … 8965 8965 <optional> 8966 8966 <reference ref="name_ref" /> 8967 <text key="inscription" text="Inscription" /> 8967 <text key="inscription" text="Inscription" multiline="true" /> 8968 8968 <combo key="material" text="Material" values="concrete,metal,plastic,steel,stone,wood" /> 8969 8969 </optional> -
trunk/resources/data/tagging-preset.xsd
r18966 r19261 649 649 </annotation> 650 650 </attribute> 651 <attribute name="multiline" type="boolean"> 652 <annotation> 653 <documentation> 654 The text is expected to be multiline, and newlines must not be normalized (default is false) 655 </documentation> 656 </annotation> 657 </attribute> 658 <attribute name="normalize" type="boolean"> 659 <annotation> 660 <documentation> 661 This performs normalization of the value by stripping extraneous spaces and consolidating whitespace (default is true for JOSM) 662 </documentation> 663 </annotation> 664 </attribute> 651 665 </attributeGroup> 652 666 -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Text.java
r18801 r19261 14 14 import java.util.Collections; 15 15 import java.util.List; 16 import java.util.regex.Pattern; 16 17 17 18 import javax.swing.AbstractButton; … … 48 49 */ 49 50 public class Text extends KeyedItem { 51 private static final Pattern MULTILINE_WHITESPACE_PATTERN = Pattern.compile("[\\s&&[^\n]]+"); 50 52 51 53 private static int auto_increment_selected; // NOSONAR … … 241 243 } 242 244 243 v = Utils.removeWhiteSpaces(v); 245 if (this.normalize) { 246 if (this.multiline) { 247 v = Utils.removeWhiteSpaces(MULTILINE_WHITESPACE_PATTERN, v); 248 } else { 249 v = Utils.removeWhiteSpaces(v); 250 } 251 } 244 252 245 253 if (!"false".equals(use_last_as_default) || auto_increment != null) { -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/TextItem.java
r17608 r19261 23 23 /** The context used for translating {@link #text} */ 24 24 public String text_context; // NOSONAR 25 26 /** {@code true} if the value is a multiline value */ 27 public boolean multiline; // NOSONAR 28 /** {@code true} if the value should be normalized */ 29 public boolean normalize = true; // NOSONAR 25 30 26 31 /** The localized version of {@link #text} */ -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r19133 r19261 821 821 */ 822 822 public static String removeWhiteSpaces(String s) { 823 return removeWhiteSpaces(WHITE_SPACES_PATTERN, s); 824 } 825 826 /** 827 * Removes leading, trailing, and multiple inner whitespaces from the given string, to be used as a key or value. 828 * @param s The string 829 * @param whitespaces The regex for whitespaces to remove outside the leading and trailing whitespaces (see {@link #strip(String)}) 830 * @return The string without leading, trailing or multiple inner whitespaces 831 * @since 19261 832 */ 833 public static String removeWhiteSpaces(Pattern whitespaces, String s) { 823 834 if (isEmpty(s)) { 824 835 return s; 825 836 } 826 return strip(s).replaceAll("\\s+"," ");837 return whitespaces.matcher(strip(s)).replaceAll(" "); 827 838 } 828 839 -
trunk/test/unit/org/openstreetmap/josm/gui/tagging/presets/items/TextTest.java
r18918 r19261 3 3 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertInstanceOf; 5 6 import static org.junit.jupiter.api.Assertions.assertTrue; 6 7 8 import java.text.MessageFormat; 9 import java.util.ArrayList; 10 import java.util.Collection; 11 import java.util.Collections; 12 import java.util.List; 13 import java.util.Map; 14 15 import javax.swing.JComponent; 7 16 import javax.swing.JPanel; 8 17 9 18 import org.junit.jupiter.api.Test; 19 import org.junit.jupiter.params.ParameterizedTest; 20 import org.junit.jupiter.params.provider.ValueSource; 21 import org.openstreetmap.josm.data.osm.Tag; 22 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 10 23 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItemGuiSupport; 11 24 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItemTest; 25 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType; 26 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 27 import org.openstreetmap.josm.gui.widgets.JosmTextField; 12 28 import org.openstreetmap.josm.testutils.annotations.Main; 13 29 … … 33 49 assertTrue(p.getComponentCount() > 0); 34 50 } 51 52 @org.openstreetmap.josm.testutils.annotations.TaggingPresets 53 @ParameterizedTest 54 @ValueSource(strings = {"\n\n\n\t\r {0}\n\n\n", "{0}"}) 55 void testNonRegression24023(String inscription) { 56 // There is a bit of "extra" whitespace in the string (` \n`). It is somewhat deliberate. We probably ought to remove the ` ` at some time. 57 final String expected = "This is a \nsample \ninscription"; 58 final String toTest = MessageFormat.format(inscription, expected).replace("sample ", "sample "); 59 final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(Collections.singleton(TaggingPresetType.NODE), Map.of("historic", "boundary_stone", "inscription", "bar"), false); 60 assertEquals(1, presets.size()); 61 final TaggingPreset preset = presets.iterator().next(); 62 final Text text = assertInstanceOf(Text.class, preset.data.get(5)); 63 final List<Tag> changeCommands = new ArrayList<>(1); 64 final JPanel panel = new JPanel(); 65 text.addToPanel(panel, TaggingPresetItemGuiSupport.create(false)); 66 JComponent value = assertInstanceOf(JComponent.class, panel.getComponent(1)); 67 while (value instanceof JPanel) { 68 value = (JComponent) value.getComponent(0); 69 } 70 final JosmTextField textField = assertInstanceOf(JosmTextField.class, value, "Until we support multiline editing, this should be a text field"); 71 textField.setText(toTest); 72 text.addCommands(changeCommands); 73 assertTrue(text.multiline); 74 assertTrue(text.normalize); 75 assertEquals(1, changeCommands.size()); 76 assertEquals(expected, changeCommands.get(0).getValue(), "If the only difference is a trailing space was removed, update the test."); 77 } 35 78 }
Note:
See TracChangeset
for help on using the changeset viewer.