Ignore:
Timestamp:
2014-08-16T23:50:43+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #3916 - WMS: Improve exception handling. Proper message is now displayed on tiles.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java

    r7291 r7425  
    99import java.awt.Component;
    1010import java.awt.Font;
    11 import java.awt.Graphics;
     11import java.awt.Graphics2D;
    1212import java.awt.GridBagLayout;
    1313import java.awt.event.ActionEvent;
     14import java.awt.font.FontRenderContext;
     15import java.awt.font.LineBreakMeasurer;
     16import java.awt.font.TextAttribute;
     17import java.awt.font.TextLayout;
    1418import java.awt.image.BufferedImage;
    1519import java.awt.image.BufferedImageOp;
    1620import java.awt.image.ConvolveOp;
    1721import java.awt.image.Kernel;
     22import java.text.AttributedCharacterIterator;
     23import java.text.AttributedString;
     24import java.util.Hashtable;
    1825import java.util.List;
     26import java.util.Map;
    1927
    2028import javax.swing.AbstractAction;
     
    6876    private final ImageryAdjustAction adjustAction = new ImageryAdjustAction(this);
    6977
     78    /**
     79     * Constructs a new {@code ImageryLayer}.
     80     */
    7081    public ImageryLayer(ImageryInfo info) {
    7182        super(info.getName());
     
    8192    }
    8293
    83     public double getPPD(){
     94    public double getPPD() {
    8495        if (!Main.isDisplayingMapView()) return Main.getProjection().getDefaultZoomInPPD();
    8596        ProjectionBounds bounds = Main.map.mapView.getProjectionBounds();
     
    231242    }
    232243
    233     public void drawErrorTile(BufferedImage img) {
    234         Graphics g = img.getGraphics();
     244    /**
     245     * Draws a red error tile when imagery tile cannot be fetched.
     246     * @param img The buffered image
     247     * @param message Additional error message to display
     248     */
     249    public void drawErrorTile(BufferedImage img, String message) {
     250        Graphics2D g = (Graphics2D) img.getGraphics();
    235251        g.setColor(Color.RED);
    236252        g.fillRect(0, 0, img.getWidth(), img.getHeight());
    237         g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(36.0f));
     253        g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(24.0f));
    238254        g.setColor(Color.BLACK);
    239255
    240256        String text = tr("ERROR");
    241         g.drawString(text, (img.getWidth() + g.getFontMetrics().stringWidth(text)) / 2, img.getHeight()/2);
    242     }
    243 
    244     /* (non-Javadoc)
    245      * @see org.openstreetmap.josm.gui.layer.Layer#destroy()
    246      */
     257        g.drawString(text, (img.getWidth() - g.getFontMetrics().stringWidth(text)) / 2, g.getFontMetrics().getHeight()+5);
     258        if (message != null) {
     259            float drawPosY = 2.5f*g.getFontMetrics().getHeight()+10;
     260            if (!message.contains(" ")) {
     261                g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(18.0f));
     262                g.drawString(message, 5, (int)drawPosY);
     263            } else {
     264                // Draw message on several lines
     265                Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
     266                map.put(TextAttribute.FAMILY, "Serif");
     267                map.put(TextAttribute.SIZE, new Float(18.0));
     268                AttributedString vanGogh = new AttributedString(message, map);
     269                // Create a new LineBreakMeasurer from the text
     270                AttributedCharacterIterator paragraph = vanGogh.getIterator();
     271                int paragraphStart = paragraph.getBeginIndex();
     272                int paragraphEnd = paragraph.getEndIndex();
     273                FontRenderContext frc = g.getFontRenderContext();
     274                LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);
     275                // Set break width to width of image with some margin
     276                float breakWidth = img.getWidth()-10;
     277                // Set position to the index of the first character in the text
     278                lineMeasurer.setPosition(paragraphStart);
     279                // Get lines until the entire paragraph has been displayed
     280                while (lineMeasurer.getPosition() < paragraphEnd) {
     281                    // Retrieve next layout
     282                    TextLayout layout = lineMeasurer.nextLayout(breakWidth);
     283
     284                    // Compute pen x position
     285                    float drawPosX = layout.isLeftToRight() ? 0 : breakWidth - layout.getAdvance();
     286
     287                    // Move y-coordinate by the ascent of the layout
     288                    drawPosY += layout.getAscent();
     289
     290                    // Draw the TextLayout at (drawPosX, drawPosY)
     291                    layout.draw(g, drawPosX, drawPosY);
     292
     293                    // Move y-coordinate in preparation for next layout
     294                    drawPosY += layout.getDescent() + layout.getLeading();
     295                }
     296            }
     297        }
     298    }
     299
    247300    @Override
    248301    public void destroy() {
Note: See TracChangeset for help on using the changeset viewer.