Changeset 8264 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2015-04-25T17:04:23+02:00 (9 years ago)
Author:
simon04
Message:

hopefully fix #10864 - revert r8235, r8213 - Note layer: return to JToolTip based rendering - wrap long URLs using zero width space U+8203

File:
1 edited

Legend:

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

    r8235 r8264  
    22package org.openstreetmap.josm.gui.layer;
    33
    4 import static org.openstreetmap.josm.tools.I18n.marktr;
    54import static org.openstreetmap.josm.tools.I18n.tr;
    65
    7 import java.awt.Color;
     6import java.awt.Dimension;
    87import java.awt.Graphics2D;
    98import java.awt.Point;
     
    1817
    1918import javax.swing.Action;
    20 import javax.swing.BorderFactory;
    2119import javax.swing.Icon;
    2220import javax.swing.ImageIcon;
    23 import javax.swing.JTextArea;
     21import javax.swing.JToolTip;
    2422
    2523import org.openstreetmap.josm.Main;
     
    3129import org.openstreetmap.josm.data.osm.NoteData;
    3230import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    33 import org.openstreetmap.josm.data.preferences.ColorProperty;
    3431import org.openstreetmap.josm.gui.MapView;
    3532import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
     
    3734import org.openstreetmap.josm.gui.dialogs.NotesDialog;
    3835import org.openstreetmap.josm.io.NoteExporter;
     36import org.openstreetmap.josm.io.XmlWriter;
    3937import org.openstreetmap.josm.tools.ColorHelper;
    4038import org.openstreetmap.josm.tools.date.DateUtils;
     
    4644
    4745    private final NoteData noteData;
    48 
    49     /**
    50      * Property for note comment background
    51      */
    52     public static final ColorProperty PROP_BACKGROUND_COLOR = new ColorProperty(
    53             marktr("Note comment background"), Color.decode("#b8cfe5"));
    5446
    5547    /**
     
    10294
    10395    @Override
    104     public void paint(Graphics2D g, final MapView mv, Bounds box) {
     96    public void paint(Graphics2D g, MapView mv, Bounds box) {
    10597        for (Note note : noteData.getNotes()) {
    10698            Point p = mv.getPoint(note.getLatLon());
     
    119111        }
    120112        if (noteData.getSelectedNote() != null) {
    121             StringBuilder sb = new StringBuilder();
     113            StringBuilder sb = new StringBuilder("<html>");
    122114            sb.append(tr("Note"));
    123115            sb.append(" ").append(noteData.getSelectedNote().getId());
     
    126118                //closing a note creates an empty comment that we don't want to show
    127119                if (commentText != null && commentText.trim().length() > 0) {
    128                     sb.append("\n\n");
    129                     String userName = comment.getUser().getName().trim();
    130                     sb.append(userName.isEmpty() ? "<Anonymous>" : userName);
     120                    sb.append("<hr/>");
     121                    String userName = XmlWriter.encode(comment.getUser().getName());
     122                    if (userName == null || userName.trim().length() == 0) {
     123                        userName = "&lt;Anonymous&gt;";
     124                    }
     125                    sb.append(userName);
    131126                    sb.append(" on ");
    132127                    sb.append(DateUtils.getDateFormat(DateFormat.MEDIUM).format(comment.getCommentTimestamp()));
    133                     sb.append(":\n");
    134                     sb.append(comment.getText().trim());
     128                    sb.append(":<br/>");
     129                    String htmlText = XmlWriter.encode(comment.getText(), true);
     130                    htmlText = htmlText.replace("&#xA;", "<br/>"); //encode method leaves us with entity instead of \n
     131                    htmlText = htmlText.replace("/", "/\u200b"); //zero width space to wrap long URLs (see #10864)
     132                    sb.append(htmlText);
    135133                }
    136134            }
    137             JTextArea toolTip = new JTextArea() {
    138                 {
    139                     setColumns(Math.min(480, mv.getWidth() / 2) / getColumnWidth());
    140                     setSize(getPreferredSize().width + 6, getPreferredSize().height + 6); // +6 for border
    141                 }
    142             };
    143             toolTip.setText(sb.toString());
    144             toolTip.setLineWrap(true);
    145             toolTip.setWrapStyleWord(true);
    146             toolTip.setBackground(PROP_BACKGROUND_COLOR.get());
    147             toolTip.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    148 
     135            sb.append("</html>");
     136            JToolTip toolTip = new JToolTip();
     137            toolTip.setTipText(sb.toString());
    149138            Point p = mv.getPoint(noteData.getSelectedNote().getLatLon());
    150139
     
    155144            int ty = p.y - NotesDialog.ICON_SMALL_SIZE - 1;
    156145            g.translate(tx, ty);
    157             toolTip.paint(g);
     146
     147            //Carried over from the OSB plugin. Not entirely sure why it is needed
     148            //but without it, the tooltip doesn't get sized correctly
     149            for (int x = 0; x < 2; x++) {
     150                Dimension d = toolTip.getUI().getPreferredSize(toolTip);
     151                d.width = Math.min(d.width, (mv.getWidth() / 2));
     152                if (d.width > 0 && d.height > 0) {
     153                    toolTip.setSize(d);
     154                    try {
     155                        toolTip.paint(g);
     156                    } catch (IllegalArgumentException e) {
     157                        // See #11123 - https://bugs.openjdk.java.net/browse/JDK-6719550
     158                        // Ignore the exception, as Netbeans does: http://hg.netbeans.org/main-silver/rev/c96f4d5fbd20
     159                        Main.error(e, false);
     160                    }
     161                }
     162            }
    158163            g.translate(-tx, -ty);
    159164        }
Note: See TracChangeset for help on using the changeset viewer.