Changeset 24643 in osm


Ignore:
Timestamp:
2010-12-07T16:02:32+01:00 (14 years ago)
Author:
upliner
Message:

jmapviewer: show imagery attribution

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r22281 r24643  
    33//License: GPL. Copyright 2008 by Jan Peter Stotz
    44
     5import java.awt.Color;
    56import java.awt.Dimension;
    67import java.awt.Font;
    78import java.awt.Graphics;
     9import java.awt.Image;
    810import java.awt.Insets;
    911import java.awt.Point;
     
    1113import java.awt.event.ActionListener;
    1214import java.awt.event.MouseEvent;
     15import java.awt.font.TextAttribute;
     16import java.awt.geom.Rectangle2D;
     17import java.util.HashMap;
    1318import java.util.LinkedList;
    1419import java.util.List;
     
    2732import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    2833import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     34import org.openstreetmap.josm.data.coor.LatLon;
    2935
    3036/**
     
    7480
    7581    private TileSource tileSource;
     82
     83    // Attribution
     84    private Image attrImage;
     85    private String attrTermsUrl;
     86    public static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10);
     87    public static final Font ATTR_LINK_FONT;
     88
     89    static {
     90        HashMap<TextAttribute, Integer> aUnderline = new HashMap<TextAttribute, Integer>();
     91        aUnderline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
     92        ATTR_LINK_FONT = ATTR_FONT.deriveFont(aUnderline);
     93    }
    7694
    7795    /**
     
    211229            int oldZoom = this.zoom;
    212230            this.zoom = zoom;
    213             if (oldZoom != zoom)
     231            if (oldZoom != zoom) {
    214232                zoomChanged(oldZoom);
    215             if (zoomSlider.getValue() != zoom)
     233            }
     234            if (zoomSlider.getValue() != zoom) {
    216235                zoomSlider.setValue(zoom);
     236            }
    217237        } finally {
    218238            setIgnoreRepaint(false);
     
    268288     */
    269289    public void setDisplayToFitMapRectangle() {
    270         if (mapRectangleList == null || mapRectangleList.size() == 0) {
     290        if (mapRectangleList == null || mapRectangleList.size() == 0)
    271291            return;
    272         }
    273292        int x_min = Integer.MAX_VALUE;
    274293        int y_min = Integer.MAX_VALUE;
     
    502521            }
    503522        }
     523        paintAttribution(g);
    504524    }
    505525
     
    513533        }
    514534    }
    515    
     535
    516536    /**
    517537     * Moves the visible map pane.
     
    676696        zoomSlider.setMaximum(tileSource.getMaxZoom());
    677697        tileController.cancelOutstandingJobs();
    678         if (zoom > tileSource.getMaxZoom())
     698        if (zoom > tileSource.getMaxZoom()) {
    679699            setZoom(tileSource.getMaxZoom());
     700        }
     701        boolean requireAttr = tileSource.requiresAttribution();
     702        if (requireAttr) {
     703            attrImage = tileSource.getAttributionImage();
     704            attrTermsUrl = tileSource.getTermsOfUseURL();
     705        } else {
     706            attrImage = null;
     707            attrTermsUrl = null;
     708        }
    680709        repaint();
    681710    }
     
    715744        tileController.setTileLoader(loader);
    716745    }
     746
     747    private void paintAttribution(Graphics g) {
     748        if (!tileSource.requiresAttribution()) return;
     749        // Draw attribution
     750        Font font = g.getFont();
     751        g.setFont(ATTR_LINK_FONT);
     752
     753        Rectangle2D termsStringBounds = g.getFontMetrics().getStringBounds("Background Terms of Use", g);
     754        int textHeight = (int) termsStringBounds.getHeight() - 5;
     755        int termsTextY = getHeight() - textHeight;
     756        if (attrTermsUrl != null) {
     757            int x = 2;
     758            int y = getHeight() - textHeight;
     759            g.setColor(Color.black);
     760            g.drawString("Background Terms of Use", x + 1, y + 1);
     761            g.setColor(Color.white);
     762            g.drawString("Background Terms of Use", x, y);
     763        }
     764
     765        // Draw attribution logo
     766        if (attrImage != null) {
     767            int x = 2;
     768            int height = attrImage.getHeight(null);
     769            int y = termsTextY - height - textHeight - 5;
     770            g.drawImage(attrImage, x, y, null);
     771        }
     772
     773        g.setFont(ATTR_FONT);
     774        Coordinate topLeft = getPosition(0,0);
     775        Coordinate bottomRight = getPosition(getWidth(),getHeight());
     776        String attributionText = tileSource.getAttributionText(zoom,
     777                new LatLon(topLeft.getLat(),topLeft.getLon()),
     778                new LatLon(bottomRight.getLat(),bottomRight.getLon()));
     779        Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
     780        {
     781            int x = getWidth() - (int) stringBounds.getWidth();
     782            int y = getHeight() - textHeight;
     783            g.setColor(Color.black);
     784            g.drawString(attributionText, x + 1, y + 1);
     785            g.setColor(Color.white);
     786            g.drawString(attributionText, x, y);
     787        }
     788
     789        g.setFont(font);
     790    }
    717791}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/TileSource.java

    r24489 r24643  
    101101    public boolean requiresAttribution();
    102102
     103    // FIXME: JMapViewer shouldn't reference JOSM classes.
    103104    /**
    104105     * @param zoom The optional zoom level for the view.
Note: See TracChangeset for help on using the changeset viewer.