Index: trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java	(revision 13164)
+++ trunk/src/org/openstreetmap/josm/gui/layer/NoteLayer.java	(revision 13165)
@@ -82,4 +82,5 @@
     private static final Pattern HTTP_LINK = Pattern.compile("(https?://[^\\s\\(\\)<>]+)");
     private static final Pattern HTML_LINK = Pattern.compile("<a href=\"[^\"]+\">([^<]+)</a>");
+    private static final Pattern HTML_LINK_MARK = Pattern.compile("<a href=\"([^\"]+)([\\.\\?\\!])\">([^<]+)(?:[\\.\\?\\!])</a>");
     private static final Pattern SLASH = Pattern.compile("([^/])/([^/])");
 
@@ -203,7 +204,9 @@
         }
 
-        Point screenloc = mv.getLocationOnScreen();
-        int tx = screenloc.x + p.x + (iconWidth / 2) + 5;
-        int ty = screenloc.y + p.y - iconHeight - 1;
+        int xl = p.x - (iconWidth / 2) - 5;
+        int xr = p.x + (iconWidth / 2) + 5;
+        int yb = p.y - iconHeight - 1;
+        int yt = p.y + (iconHeight / 2) + 2;
+        Point pTooltip;
 
         String text = getNoteToolTip(selectedNote);
@@ -216,5 +219,5 @@
             displayedPanel.setBorder(BorderFactory.createLineBorder(Color.black));
             displayedPanel.enableClickableHyperlinks();
-            fixPanelSize(mv, text);
+            pTooltip = fixPanelSizeAndLocation(mv, text, xl, xr, yt, yb);
             displayedWindow = new JWindow((MainFrame) Main.parent);
             displayedWindow.setAutoRequestFocus(false);
@@ -225,16 +228,20 @@
         } else {
             displayedPanel.setText(text);
-            fixPanelSize(mv, text);
+            pTooltip = fixPanelSizeAndLocation(mv, text, xl, xr, yt, yb);
         }
 
         displayedWindow.pack();
-        displayedWindow.setLocation(tx, ty);
+        displayedWindow.setLocation(pTooltip);
         displayedWindow.setVisible(mv.contains(p));
         displayedNote = selectedNote;
     }
 
-    private void fixPanelSize(MapView mv, String text) {
-        int maxWidth = mv.getWidth() * 2/3;
-        int maxHeight = mv.getHeight() * 2/3;
+    private Point fixPanelSizeAndLocation(MapView mv, String text, int xl, int xr, int yt, int yb) {
+        int leftMaxWidth = (int) (0.95 * xl);
+        int rightMaxWidth = (int) (0.95 * mv.getWidth() - xr);
+        int topMaxHeight = (int) (0.95 * yt);
+        int bottomMaxHeight = (int) (0.95 * mv.getHeight() - yb);
+        int maxWidth = Math.max(leftMaxWidth, rightMaxWidth);
+        int maxHeight = Math.max(topMaxHeight, bottomMaxHeight);
         JEditorPane pane = displayedPanel.getEditorPane();
         Dimension d = pane.getPreferredSize();
@@ -253,9 +260,15 @@
             if (v != null) {
                 v.setSize(maxWidth, 0);
-                int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS)) + 30;
+                int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
                 int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS)) + 10;
                 pane.setPreferredSize(new Dimension(w, h));
             }
         }
+        d = pane.getPreferredSize();
+        // place tooltip on left or right side of icon, based on its width
+        Point screenloc = mv.getLocationOnScreen();
+        return new Point(
+                screenloc.x + (d.width > rightMaxWidth && d.width <= leftMaxWidth ? xl - d.width : xr),
+                screenloc.y + (d.height > bottomMaxHeight && d.height <= topMaxHeight ? yt - d.height - 10 : yb));
     }
 
@@ -309,4 +322,5 @@
     static String replaceLinks(String htmlText) {
         String result = HTTP_LINK.matcher(htmlText).replaceAll("<a href=\"$1\">$1</a>");
+        result = HTML_LINK_MARK.matcher(result).replaceAll("<a href=\"$1\">$3</a>$2");
         Matcher m1 = HTML_LINK.matcher(result);
         if (m1.find()) {
Index: trunk/test/unit/org/openstreetmap/josm/gui/layer/NoteLayerTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/layer/NoteLayerTest.java	(revision 13164)
+++ trunk/test/unit/org/openstreetmap/josm/gui/layer/NoteLayerTest.java	(revision 13165)
@@ -69,4 +69,7 @@
         assertEquals("<a href=\"https://www.example.com/test\">https://www.example.com/\u200btest</a>",
                 NoteLayer.replaceLinks("https://www.example.com/test"));
+        // link with dot
+        assertEquals("<a href=\"https://www.example.com\">https://www.example.com</a>.",
+                NoteLayer.replaceLinks("https://www.example.com."));
         // CHECKSTYLE.OFF: LineLength
         // text with several links (with and without slash)
