Changeset 13122 in josm for trunk/src


Ignore:
Timestamp:
2017-11-14T01:41:49+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #15550 - Automatic line break of note tooltips (tested on western and ideographic scripts)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java

    r13118 r13122  
    1717import java.util.Collections;
    1818import java.util.List;
     19import java.util.regex.Pattern;
    1920
    2021import javax.swing.Action;
     
    5859public class NoteLayer extends AbstractModifiableLayer implements MouseListener, NoteDataUpdateListener {
    5960
     61    /**
     62     * Pattern to detect end of sentences followed by another one, or a link, in western script.
     63     * Group 1 (capturing): period, interrogation mark, exclamation mark
     64     * Group non capturing: at least one horizontal or vertical whitespace
     65     * Group 2 (capturing): a letter (any script), or any punctuation
     66     */
     67    private static final Pattern SENTENCE_MARKS_WESTERN = Pattern.compile("([\\.\\?\\!])(?:[\\h\\v]+)([\\p{L}\\p{Punct}])");
     68
     69    /**
     70     * Pattern to detect end of sentences followed by another one, or a link, in eastern script.
     71     * Group 1 (capturing): ideographic full stop
     72     * Group 2 (capturing): a letter (any script), or any punctuation
     73     */
     74    private static final Pattern SENTENCE_MARKS_EASTERN = Pattern.compile("(\\u3002)([\\p{L}\\p{Punct}])");
     75
    6076    private final NoteData noteData;
    6177
     
    205221        Dimension d = displayedPanel.getPreferredSize();
    206222        if (d.width > mv.getWidth() / 2) {
    207             // To make sure long notes such as https://www.openstreetmap.org/note/278197 are displayed correctly
    208             displayedPanel.setText(text.replaceAll("\\. ([\\p{Lower}\\p{Upper}\\p{Punct}])", "\\.<br>$1"));
    209         }
     223            // To make sure long notes are displayed correctly
     224            displayedPanel.setText(insertLineBreaks(text));
     225        }
     226    }
     227
     228    /**
     229     * Inserts HTML line breaks ({@code <br>} at the end of each sentence mark
     230     * (period, interrogation mark, exclamation mark, ideographic full stop).
     231     * @param longText a long text that does not fit on a single line without exceeding half of the map view
     232     * @return text with line breaks
     233     */
     234    static String insertLineBreaks(String longText) {
     235        return SENTENCE_MARKS_WESTERN.matcher(SENTENCE_MARKS_EASTERN.matcher(longText).replaceAll("$1<br>$2")).replaceAll("$1<br>$2");
    210236    }
    211237
Note: See TracChangeset for help on using the changeset viewer.