Changeset 4489 in josm


Ignore:
Timestamp:
Oct 6, 2011 10:14:16 AM (21 months ago)
Author:
bastiK
Message:

unified redundant attribution code from org.openstreetmap.gui.jmapviewer.JMapViewer and org.openstreetmap.josm.gui.layer.TMSLayer

wire the i18n support for the jmapviewer component (see [o26783])

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r4288 r4489  
    4242import javax.swing.UIManager; 
    4343 
     44import org.openstreetmap.gui.jmapviewer.FeatureAdapter; 
    4445import org.openstreetmap.josm.actions.JosmAction; 
    4546import org.openstreetmap.josm.actions.OpenFileAction; 
     
    7980import org.openstreetmap.josm.tools.I18n; 
    8081import org.openstreetmap.josm.tools.ImageProvider; 
     82import org.openstreetmap.josm.tools.OpenBrowser; 
    8183import org.openstreetmap.josm.tools.OsmUrlToBounds; 
    8284import org.openstreetmap.josm.tools.PlatformHook; 
     
    252254        validator = new OsmValidator(); 
    253255        MapView.addLayerChangeListener(validator); 
     256 
     257        // hooks for the jmapviewer component 
     258        FeatureAdapter.registerBrowserAdapter(new FeatureAdapter.BrowserAdapter() { 
     259            @Override 
     260            public void openLink(String url) { 
     261                OpenBrowser.displayUrl(url); 
     262            } 
     263        }); 
     264        FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter()); 
    254265 
    255266        toolbar.refreshToolbarControl(); 
     
    796807    /** 
    797808     * Replies the current projection. 
    798      *  
     809     * 
    799810     * @return 
    800811     */ 
     
    805816    /** 
    806817     * Sets the current projection 
    807      *  
     818     * 
    808819     * @param p the projection 
    809820     */ 
     
    849860    /** 
    850861     * Register a projection change listener 
    851      *  
     862     * 
    852863     * @param listener the listener. Ignored if null. 
    853864     */ 
     
    865876    /** 
    866877     * Removes a projection change listener 
    867      *  
     878     * 
    868879     * @param listener the listener. Ignored if null. 
    869880     */ 
  • trunk/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    r4465 r4489  
    215215 
    216216    public boolean handleAttribution(Point p, boolean click) { 
    217         TileSource ts = tileController.getTileSource(); 
    218         if(!ts.requiresAttribution()) 
    219             return false; 
    220  
    221         /* TODO: Somehow indicate the link is clickable state to user */ 
    222  
    223         if ((attrImageBounds != null && attrImageBounds.contains(p)) 
    224                 || (attrTextBounds != null && attrTextBounds.contains(p))) { 
    225             if (click) 
    226                 OpenBrowser.displayUrl(ts.getAttributionLinkURL()); 
    227             /*else 
    228                 Main.warn(ts.getAttributionLinkURL());*/ 
    229             return true; 
    230         } else if (attrToUBounds != null && attrToUBounds.contains(p)) { 
    231             if (click) 
    232                 OpenBrowser.displayUrl(ts.getTermsOfUseURL()); 
    233             /*else 
    234                 Main.warn(ts.getTermsOfUseURL());*/ 
    235             return true; 
    236         } 
    237         return false; 
     217        return attribution.handleAttribution(p, click); 
    238218    } 
    239219 
  • trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java

    r4465 r4489  
    3636import javax.swing.SwingUtilities; 
    3737 
     38import org.openstreetmap.gui.jmapviewer.AttributionSupport; 
    3839import org.openstreetmap.gui.jmapviewer.Coordinate; 
    3940import org.openstreetmap.gui.jmapviewer.JobDispatcher; 
     
    158159    JCheckBoxMenuItem showErrorsPopup; 
    159160    Tile showMetadataTile; 
    160     private Image attrImage; 
    161     private String attrTermsUrl; 
    162     private Rectangle attrImageBounds, attrToUBounds, attrTextBounds; 
     161    private AttributionSupport attribution = new AttributionSupport(); 
    163162    private static final Font InfoFont = new Font("sansserif", Font.BOLD, 13); 
    164     private static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10); 
    165     private static final Font ATTR_LINK_FONT; 
    166     static { 
    167         HashMap<TextAttribute, Integer> aUnderline = new HashMap<TextAttribute, Integer>(); 
    168         aUnderline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); 
    169         ATTR_LINK_FONT = ATTR_FONT.deriveFont(aUnderline); 
    170     } 
    171163 
    172164    protected boolean autoZoom; 
     
    248240    { 
    249241        this.tileSource = tileSource; 
    250         boolean requireAttr = tileSource.requiresAttribution(); 
    251         if(requireAttr) { 
    252             attrImage = tileSource.getAttributionImage(); 
    253             /*if(attrImage == null) { 
    254                 Main.debug("Attribution image was null."); 
    255             } else { 
    256                 Main.debug("Got an attribution image " + attrImage.getHeight(this) + "x" + attrImage.getWidth(this)); 
    257             }*/ 
    258  
    259             attrTermsUrl = tileSource.getTermsOfUseURL(); 
    260         } 
     242        attribution.initialize(tileSource); 
    261243 
    262244        currentZoomLevel = getBestZoom(); 
     
    477459                            tileOptionMenu.show(e.getComponent(), e.getX(), e.getY()); 
    478460                        } else if (e.getButton() == MouseEvent.BUTTON1) { 
    479                             if (!tileSource.requiresAttribution()) 
    480                                 return; 
    481  
    482                             if ((attrImageBounds != null && attrImageBounds.contains(e.getPoint())) 
    483                                     || (attrTextBounds != null && attrTextBounds.contains(e.getPoint()))) { 
    484                                 OpenBrowser.displayUrl(tileSource.getAttributionLinkURL()); 
    485                             } else if (attrToUBounds != null && attrToUBounds.contains(e.getPoint())) { 
    486                                 OpenBrowser.displayUrl(tileSource.getTermsOfUseURL()); 
    487                             } 
     461                            attribution.handleAttribution(e.getPoint(), true); 
    488462                        } 
    489463                    } 
     
    920894        return new Coordinate(ll.lat(),ll.lon()); 
    921895    } 
     896 
    922897    private final TileSet nullTileSet = new TileSet((LatLon)null, (LatLon)null, 0); 
    923898    private class TileSet { 
     
    12391214            this.paintTileText(ts, t, g, mv, displayZoomLevel, t); 
    12401215        } 
    1241  
    1242         if (tileSource.requiresAttribution()) { 
    1243             // Draw attribution 
    1244             Font font = g.getFont(); 
    1245             g.setFont(ATTR_LINK_FONT); 
    1246             g.setColor(Color.white); 
    1247  
    1248             // Draw terms of use text 
    1249             Rectangle2D termsStringBounds = g.getFontMetrics().getStringBounds("Background Terms of Use", g); 
    1250             int textRealHeight = (int) termsStringBounds.getHeight(); 
    1251             int textHeight = textRealHeight - 5; 
    1252             int textWidth = (int) termsStringBounds.getWidth(); 
    1253             int termsTextY = mv.getHeight() - textHeight; 
    1254             if(attrTermsUrl != null) { 
    1255                 int x = 2; 
    1256                 int y = mv.getHeight() - textHeight; 
    1257                 attrToUBounds = new Rectangle(x, y-textHeight, textWidth, textRealHeight); 
    1258                 myDrawString(g, "Background Terms of Use", x, y); 
    1259             } 
    1260  
    1261             // Draw attribution logo 
    1262             if(attrImage != null) { 
    1263                 int x = 2; 
    1264                 int imgWidth = attrImage.getWidth(this); 
    1265                 int height = attrImage.getHeight(this); 
    1266                 int y = termsTextY - height - textHeight - 5; 
    1267                 attrImageBounds = new Rectangle(x, y, imgWidth, height); 
    1268                 g.drawImage(attrImage, x, y, this); 
    1269             } 
    1270  
    1271             g.setFont(ATTR_FONT); 
    1272             String attributionText = tileSource.getAttributionText(displayZoomLevel, 
    1273                     getShiftedCoord(topLeft), getShiftedCoord(botRight)); 
    1274             Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g); 
    1275             { 
    1276                 int x = mv.getWidth() - (int) stringBounds.getWidth(); 
    1277                 int y = mv.getHeight() - textHeight; 
    1278                 myDrawString(g, attributionText, x, y); 
    1279                 attrTextBounds = new Rectangle(x, y-textHeight, textWidth, textRealHeight); 
    1280             } 
    1281  
    1282             g.setFont(font); 
    1283         } 
     1216         
     1217        attribution.paintAttribution(g, mv.getWidth(), mv.getHeight(), getShiftedCoord(topLeft), getShiftedCoord(botRight), displayZoomLevel, this); 
    12841218 
    12851219        //g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120); 
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r4477 r4489  
    2222import javax.swing.UIManager; 
    2323 
     24import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter; 
    2425import org.openstreetmap.josm.Main; 
    2526 
     
    203204     * For instance, {@code trn("There was an error!", "There were errors!", i)} or 
    204205     * {@code trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)}. 
    205      *  
     206     * 
    206207     * @param singularText the singular text to translate. 
    207208     * Must be a string literal. (No constants or local vars.) 
     
    694695        return 0; 
    695696    } 
     697 
     698    public static TranslationAdapter getTranslationAdapter() { 
     699        return new TranslationAdapter() { 
     700            @Override 
     701            public String tr(String text, Object... objects) { 
     702                return I18n.tr(text, objects); 
     703            } 
     704        }; 
     705    } 
    696706} 
Note: See TracChangeset for help on using the changeset viewer.