Changeset 17404 in osm for applications/editors/josm


Ignore:
Timestamp:
2009-08-31T13:05:26+02:00 (15 years ago)
Author:
guggis
Message:

applied #3365: patch by xeen: Wrap OpenStreetBug Tooltips

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/openstreetbugs/src/org/openstreetmap/josm/plugins/osb/OsbLayer.java

    r17035 r17404  
    3131
    3232import java.awt.Component;
     33import java.awt.Dimension;
    3334import java.awt.Graphics;
    3435import java.awt.Image;
     
    6869
    6970    private JToolTip tooltip = new JToolTip();
     71   
     72    private static ImageIcon iconError = OsbPlugin.loadIcon("icon_error16.png");
     73    private static ImageIcon iconValid = OsbPlugin.loadIcon("icon_valid16.png");
    7074
    7175    public OsbLayer(DataSet dataSet, String name) {
     
    117121    public void paint(Graphics g, MapView mv) {
    118122        Object[] nodes = data.nodes.toArray();
     123        // This loop renders all the bug icons
    119124        for (int i = 0; i < nodes.length; i++) {
    120125            Node node = (Node) nodes[i];
    121126
    122127            // don't paint deleted nodes
    123             if(node.deleted) {
    124                 continue;
    125             }
     128            if(node.deleted)
     129                continue;
    126130
    127131            Point p = mv.getPoint(node);
    128132
    129             ImageIcon icon = OsbPlugin.loadIcon("icon_error16.png");
    130             if("1".equals(node.get("state"))) {
    131                 icon = OsbPlugin.loadIcon("icon_valid16.png");
    132             }
     133            ImageIcon icon = ("1".equals(node.get("state"))) ? iconValid : iconError;
    133134            int width = icon.getIconWidth();
    134135            int height = icon.getIconHeight();
     
    139140                }
    140141            });
    141 
    142 
    143             if(selection != null && selection.contains(node)) {
    144                 // draw description
    145                 String desc = node.get("note");
    146                 if(desc != null) {
    147                     // format with html
    148                     StringBuilder sb = new StringBuilder("<html>");
    149                     //sb.append(desc.replaceAll("\\|", "<br>"));
    150                     sb.append(desc.replaceAll("<hr />", "<hr>"));
    151                     sb.append("</html>");
    152                     desc = sb.toString();
    153 
    154                     // determine tooltip dimensions
    155                     int tooltipWidth = 0;
    156                     Rectangle2D fontBounds = null;
    157                     String[] lines = desc.split("<hr>");
    158                     for (int j = 0; j < lines.length; j++) {
    159                         String line = lines[j];
    160                         fontBounds = g.getFontMetrics().getStringBounds(line, g);
    161                         tooltipWidth = Math.max(tooltipWidth, (int)fontBounds.getWidth());
    162                     }
    163 
    164                     // draw description as a tooltip
    165                     tooltip.setTipText(desc);
    166                     tooltip.setSize(tooltip.getUI().getPreferredSize(tooltip));
    167                    
    168                     int tx = p.x + (width / 2) + 5;
    169                     int ty = (int)(p.y - height / 2) -1;
    170                     g.translate(tx, ty);
    171                     tooltip.paint(g);
    172                     g.translate(-tx, -ty);
    173                 }
    174 
    175                 // draw selection border
    176                 g.setColor(ColorHelper.html2color(Main.pref.get("color.selected")));
    177                 g.drawRect(p.x - (width / 2), p.y - (height / 2), 16, 16);
    178             }
     142        }
     143       
     144        if(selection == null)
     145            return;
     146       
     147        // This loop renders the selection border and tooltips so they get drawn
     148        // on top of the bug icons
     149        for (int i = 0; i < nodes.length; i++) {
     150            Node node = (Node) nodes[i];
     151           
     152            if(node.deleted || !selection.contains(node))
     153                continue;
     154           
     155            // draw selection border
     156            Point p = mv.getPoint(node);
     157           
     158            ImageIcon icon = ("1".equals(node.get("state"))) ? iconValid : iconError;
     159            int width = icon.getIconWidth();
     160            int height = icon.getIconHeight();
     161           
     162            g.setColor(ColorHelper.html2color(Main.pref.get("color.selected")));
     163            g.drawRect(p.x - (width / 2), p.y - (height / 2), 16, 16);
     164           
     165            // draw description
     166            String desc = node.get("note");
     167            if(desc == null)
     168                continue;
     169
     170            // format with html
     171            StringBuilder sb = new StringBuilder("<html>");
     172            sb.append(desc.replaceAll("<hr />", "<hr>"));
     173            sb.append("</html>");
     174            desc = sb.toString();
     175
     176            // draw description as a tooltip
     177            tooltip.setTipText(desc);
     178           
     179            int tx = p.x + (width / 2) + 5;
     180            int ty = (int)(p.y - height / 2) -1;
     181            g.translate(tx, ty);
     182           
     183            // This limits the width of the tooltip to 2/3 of the drawing
     184            // area, which makes longer tooltips actually readable (they
     185            // would disappear if scrolled too much to the right)
     186           
     187            // Need to do this twice as otherwise getPreferredSize doesn't take
     188            // the reduced width into account
     189            for(int x = 0; x < 2; x++) {
     190                Dimension d = tooltip.getUI().getPreferredSize(tooltip);               
     191                d.width = Math.min(d.width, (int)(mv.getWidth()*2/3));
     192                tooltip.setSize(d);
     193                tooltip.paint(g);
     194            }
     195           
     196            g.translate(-tx, -ty);
    179197        }
    180198    }
Note: See TracChangeset for help on using the changeset viewer.