Ticket #5911: [mfloryan]_AttributionHint_for_SlippyMap.patch
| File [mfloryan]_AttributionHint_for_SlippyMap.patch, 4.5 KB (added by , 15 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
157 157 158 158 private final SizeButton iSizeButton = new SizeButton(); 159 159 private final SourceButton iSourceButton; 160 private final AttributionHint iAttributionHint = new AttributionHint(this); 160 161 private Bounds bbox; 161 162 162 163 // upper left and lower right corners of the selection rectangle (x/y on … … 203 204 if (source.getName().equals(mapStyle)) { 204 205 this.setTileSource(source); 205 206 iSourceButton.setCurrentMap(source); 207 iAttributionHint.setAttribution(source.getAttributionText(zoom, getPosition(), getPosition())); 206 208 foundSource = true; 207 209 break; 208 210 } 209 211 } 210 212 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())); 213 217 } 214 218 215 219 new SlippyMapControler(this, this, iSizeButton, iSourceButton); … … 248 252 249 253 iSizeButton.paint(g); 250 254 iSourceButton.paint((Graphics2D)g); 255 iAttributionHint.paint(g); 251 256 } catch (Exception e) { 252 257 e.printStackTrace(); 253 258 } … … 340 345 public void toggleMapSource(TileSource tileSource) { 341 346 this.tileController.setTileCache(new MemoryTileCache()); 342 347 this.setTileSource(tileSource); 348 this.iAttributionHint.setAttribution( 349 tileSource.getAttributionText(zoom, getPosition(), getPosition()) 350 ); 343 351 PROP_MAPSTYLE.put(tileSource.getName()); // TODO Is name really unique? 344 352 } 345 353 -
src/org/openstreetmap/josm/gui/bbox/AttributionHint.java
1 package org.openstreetmap.josm.gui.bbox; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.ActionEvent; 6 7 /* A simple component to display attribution information on the slippy map 8 * */ 9 public 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
