Changeset 13162 in josm


Ignore:
Timestamp:
2017-11-25T17:20:10+01:00 (7 years ago)
Author:
Don-vip
Message:

see #15550 - much better dynamic resizing of note tooltips

Location:
trunk
Files:
2 edited

Legend:

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

    r13157 r13162  
    1919import java.util.Collections;
    2020import java.util.List;
     21import java.util.regex.Matcher;
    2122import java.util.regex.Pattern;
    2223
     
    7980    private static final Pattern SENTENCE_MARKS_EASTERN = Pattern.compile("(\\u3002)([\\p{L}\\p{Punct}])");
    8081
     82    private static final Pattern HTTP_LINK = Pattern.compile("(https?://[^\\s\\(\\)<>]+)");
     83    private static final Pattern HTML_LINK = Pattern.compile("<a href=\"[^\"]+\">([^<]+)</a>");
     84    private static final Pattern SLASH = Pattern.compile("([^/])/([^/])");
     85
    8186    private final NoteData noteData;
    8287
     
    231236    private void fixPanelSize(MapView mv, String text) {
    232237        int maxWidth = mv.getWidth() * 2/3;
     238        int maxHeight = mv.getHeight() * 2/3;
    233239        JEditorPane pane = displayedPanel.getEditorPane();
    234         if (pane.getPreferredSize().width > maxWidth && Config.getPref().getBoolean("note.text.break-on-sentence-mark", false)) {
     240        Dimension d = pane.getPreferredSize();
     241        if ((d.width > maxWidth || d.height > maxHeight) && Config.getPref().getBoolean("note.text.break-on-sentence-mark", false)) {
    235242            // To make sure long notes are displayed correctly
    236243            displayedPanel.setText(insertLineBreaks(text));
    237244        }
    238245        // If still too large, enforce maximum size
    239         Dimension d = pane.getPreferredSize();
    240         if (d.width > maxWidth) {
     246        d = pane.getPreferredSize();
     247        if (d.width > maxWidth || d.height > maxHeight) {
    241248            View v = (View) pane.getClientProperty(BasicHTML.propertyKey);
    242249            if (v == null) {
     
    290297                htmlText = htmlText.replace("&#xA;", "<br>");
    291298                // convert URLs to proper HTML links
    292                 htmlText = htmlText.replaceAll("(https?://[^\\s\\(\\)<>]+)", "<a href=\"$1\">$1</a>");
     299                htmlText = replaceLinks(htmlText);
    293300                sb.append(htmlText);
    294301            }
     
    297304        String result = sb.toString();
    298305        Logging.debug(result);
     306        return result;
     307    }
     308
     309    static String replaceLinks(String htmlText) {
     310        String result = HTTP_LINK.matcher(htmlText).replaceAll("<a href=\"$1\">$1</a>");
     311        Matcher m1 = HTML_LINK.matcher(result);
     312        if (m1.find()) {
     313            int last = 0;
     314            StringBuffer sb = new StringBuffer(); // Switch to StringBuilder when switching to Java 9
     315            do {
     316                sb.append(result, last, m1.start());
     317                last = m1.end();
     318                String link = m1.group(0);
     319                Matcher m2 = SLASH.matcher(link).region(link.indexOf('>'), link.lastIndexOf('<'));
     320                while (m2.find()) {
     321                    m2.appendReplacement(sb, "$1/\u200b$2"); //zero width space to wrap long URLs (see #10864, #15550)
     322                }
     323                m2.appendTail(sb);
     324            } while (m1.find());
     325            result = sb.append(result, last, result.length()).toString();
     326        }
    299327        return result;
    300328    }
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/NoteLayerTest.java

    r13122 r13162  
    5656        // CHECKSTYLE.ON: LineLength
    5757    }
     58
     59    /**
     60     * Unit test of {@link NoteLayer#replaceLinks}.
     61     */
     62    @Test
     63    public void testReplaceLinks() {
     64        // empty string
     65        assertEquals("", NoteLayer.replaceLinks(""));
     66        // no link
     67        assertEquals("no http link", NoteLayer.replaceLinks("no http link"));
     68        // just one link
     69        assertEquals("<a href=\"https://www.example.com/test\">https://www.example.com/\u200btest</a>",
     70                NoteLayer.replaceLinks("https://www.example.com/test"));
     71        // CHECKSTYLE.OFF: LineLength
     72        // text with several links (with and without slash)
     73        assertEquals("foo <a href=\"https://foo.example.com/test\">https://foo.example.com/\u200btest</a> bar <a href=\"https://bar.example.com\">https://bar.example.com</a> baz",
     74                NoteLayer.replaceLinks("foo https://foo.example.com/test bar https://bar.example.com baz"));
     75        // CHECKSTYLE.ON: LineLength
     76    }
    5877}
Note: See TracChangeset for help on using the changeset viewer.