Ticket #5911: [mfloryan]_AttributionHint_for_SlippyMap.patch

File [mfloryan]_AttributionHint_for_SlippyMap.patch, 4.5 KB (added by mfloryan, 2 years ago)

Patch with new functionality

  • src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

     
    157157 
    158158    private final SizeButton iSizeButton = new SizeButton(); 
    159159    private final SourceButton iSourceButton; 
     160    private final AttributionHint iAttributionHint = new AttributionHint(this); 
    160161    private Bounds bbox; 
    161162 
    162163    // upper left and lower right corners of the selection rectangle (x/y on 
     
    203204            if (source.getName().equals(mapStyle)) { 
    204205                this.setTileSource(source); 
    205206                iSourceButton.setCurrentMap(source); 
     207                iAttributionHint.setAttribution(source.getAttributionText(zoom, getPosition(), getPosition())); 
    206208                foundSource = true; 
    207209                break; 
    208210            } 
    209211        } 
    210212        if (!foundSource) { 
    211             setTileSource(tileSources.get(0)); 
    212             iSourceButton.setCurrentMap(tileSources.get(0)); 
     213            TileSource firstMapSource = tileSources.get(0); 
     214            setTileSource(firstMapSource); 
     215            iSourceButton.setCurrentMap(firstMapSource); 
     216            iAttributionHint.setAttribution(firstMapSource.getAttributionText(zoom, getPosition(), getPosition())); 
    213217        } 
    214218 
    215219        new SlippyMapControler(this, this, iSizeButton, iSourceButton); 
     
    248252 
    249253            iSizeButton.paint(g); 
    250254            iSourceButton.paint((Graphics2D)g); 
     255            iAttributionHint.paint(g); 
    251256        } catch (Exception e) { 
    252257            e.printStackTrace(); 
    253258        } 
     
    340345    public void toggleMapSource(TileSource tileSource) { 
    341346        this.tileController.setTileCache(new MemoryTileCache()); 
    342347        this.setTileSource(tileSource); 
     348        this.iAttributionHint.setAttribution( 
     349                tileSource.getAttributionText(zoom, getPosition(), getPosition()) 
     350        ); 
    343351        PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique? 
    344352    } 
    345353 
  • src/org/openstreetmap/josm/gui/bbox/AttributionHint.java

     
     1package org.openstreetmap.josm.gui.bbox; 
     2 
     3import javax.swing.*; 
     4import java.awt.*; 
     5import java.awt.event.ActionEvent; 
     6 
     7/* A simple component to display attribution information on the slippy map 
     8 *  */ 
     9public class AttributionHint { 
     10 
     11    private static final float FONT_SIZE = 10.0f; 
     12    private final Timer timer; 
     13    private boolean isShown = false; 
     14    private JComponent parentWindow = null; 
     15    private String attributionText = ""; 
     16    private final int padding = 2; 
     17 
     18    public AttributionHint(JComponent parent) { 
     19        this.parentWindow = parent; 
     20        timer = new Timer(10 * 1000, new AbstractAction() { 
     21            public void actionPerformed(ActionEvent actionEvent) { 
     22                isShown = false; 
     23                if (parentWindow != null) { 
     24                    parentWindow.repaint(); 
     25                } 
     26            } 
     27        }); 
     28        timer.setRepeats(false); 
     29    } 
     30 
     31    /* 
     32     * Sets new attribution text to be shown for 10s 
     33     */ 
     34    public void setAttribution(String attributionText) { 
     35        this.attributionText = attributionText; 
     36        this.isShown = true; 
     37        timer.start(); 
     38    } 
     39 
     40    public void paint(Graphics g) { 
     41        if (isShown) { 
     42            Graphics2D g2 = (Graphics2D) g; 
     43            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     44            Rectangle bounds = g2.getClipBounds(); 
     45 
     46            g.setFont(g.getFont().deriveFont(Font.ITALIC).deriveFont(FONT_SIZE)); 
     47            FontMetrics fm = g.getFontMetrics(); 
     48 
     49            int textWidth = fm.stringWidth(attributionText); 
     50            int textLeft = (bounds.width - textWidth) / 2; 
     51            int textTop = bounds.height - fm.getHeight(); 
     52 
     53            g.setColor(new Color(0, 0, 139, 179)); 
     54            g2.fillRoundRect(textLeft - padding, textTop - padding, textWidth + 2 * padding, fm.getHeight() + padding, 5, 5); 
     55            g.setColor(Color.WHITE); 
     56            g2.drawString(attributionText, textLeft, textTop + fm.getAscent()); 
     57        } 
     58    } 
     59} 
     60 No newline at end of file