Changeset 13165 in josm for trunk/src/org


Ignore:
Timestamp:
2017-11-25T22:50:44+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #15550 - last tweaks in note tooltips size, placement and URL detection

File:
1 edited

Legend:

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

    r13162 r13165  
    8282    private static final Pattern HTTP_LINK = Pattern.compile("(https?://[^\\s\\(\\)<>]+)");
    8383    private static final Pattern HTML_LINK = Pattern.compile("<a href=\"[^\"]+\">([^<]+)</a>");
     84    private static final Pattern HTML_LINK_MARK = Pattern.compile("<a href=\"([^\"]+)([\\.\\?\\!])\">([^<]+)(?:[\\.\\?\\!])</a>");
    8485    private static final Pattern SLASH = Pattern.compile("([^/])/([^/])");
    8586
     
    203204        }
    204205
    205         Point screenloc = mv.getLocationOnScreen();
    206         int tx = screenloc.x + p.x + (iconWidth / 2) + 5;
    207         int ty = screenloc.y + p.y - iconHeight - 1;
     206        int xl = p.x - (iconWidth / 2) - 5;
     207        int xr = p.x + (iconWidth / 2) + 5;
     208        int yb = p.y - iconHeight - 1;
     209        int yt = p.y + (iconHeight / 2) + 2;
     210        Point pTooltip;
    208211
    209212        String text = getNoteToolTip(selectedNote);
     
    216219            displayedPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    217220            displayedPanel.enableClickableHyperlinks();
    218             fixPanelSize(mv, text);
     221            pTooltip = fixPanelSizeAndLocation(mv, text, xl, xr, yt, yb);
    219222            displayedWindow = new JWindow((MainFrame) Main.parent);
    220223            displayedWindow.setAutoRequestFocus(false);
     
    225228        } else {
    226229            displayedPanel.setText(text);
    227             fixPanelSize(mv, text);
     230            pTooltip = fixPanelSizeAndLocation(mv, text, xl, xr, yt, yb);
    228231        }
    229232
    230233        displayedWindow.pack();
    231         displayedWindow.setLocation(tx, ty);
     234        displayedWindow.setLocation(pTooltip);
    232235        displayedWindow.setVisible(mv.contains(p));
    233236        displayedNote = selectedNote;
    234237    }
    235238
    236     private void fixPanelSize(MapView mv, String text) {
    237         int maxWidth = mv.getWidth() * 2/3;
    238         int maxHeight = mv.getHeight() * 2/3;
     239    private Point fixPanelSizeAndLocation(MapView mv, String text, int xl, int xr, int yt, int yb) {
     240        int leftMaxWidth = (int) (0.95 * xl);
     241        int rightMaxWidth = (int) (0.95 * mv.getWidth() - xr);
     242        int topMaxHeight = (int) (0.95 * yt);
     243        int bottomMaxHeight = (int) (0.95 * mv.getHeight() - yb);
     244        int maxWidth = Math.max(leftMaxWidth, rightMaxWidth);
     245        int maxHeight = Math.max(topMaxHeight, bottomMaxHeight);
    239246        JEditorPane pane = displayedPanel.getEditorPane();
    240247        Dimension d = pane.getPreferredSize();
     
    253260            if (v != null) {
    254261                v.setSize(maxWidth, 0);
    255                 int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS)) + 30;
     262                int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
    256263                int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS)) + 10;
    257264                pane.setPreferredSize(new Dimension(w, h));
    258265            }
    259266        }
     267        d = pane.getPreferredSize();
     268        // place tooltip on left or right side of icon, based on its width
     269        Point screenloc = mv.getLocationOnScreen();
     270        return new Point(
     271                screenloc.x + (d.width > rightMaxWidth && d.width <= leftMaxWidth ? xl - d.width : xr),
     272                screenloc.y + (d.height > bottomMaxHeight && d.height <= topMaxHeight ? yt - d.height - 10 : yb));
    260273    }
    261274
     
    309322    static String replaceLinks(String htmlText) {
    310323        String result = HTTP_LINK.matcher(htmlText).replaceAll("<a href=\"$1\">$1</a>");
     324        result = HTML_LINK_MARK.matcher(result).replaceAll("<a href=\"$1\">$3</a>$2");
    311325        Matcher m1 = HTML_LINK.matcher(result);
    312326        if (m1.find()) {
Note: See TracChangeset for help on using the changeset viewer.