Changeset 17783 in josm for trunk


Ignore:
Timestamp:
2021-04-14T20:52:18+02:00 (3 years ago)
Author:
simon04
Message:

fix #17072 - Geotagged images viewer should not clear between pictures (patch by taylor.smock)

File:
1 edited

Legend:

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

    r17782 r17783  
    2525import java.io.File;
    2626import java.io.IOException;
     27import java.util.Objects;
    2728
    2829import javax.imageio.ImageIO;
     
    5455    private ImageEntry entry;
    5556
     57    /** The previous file that is currently displayed. Cleared on paint. Only used to help improve UI error information. */
     58    private ImageEntry oldEntry;
     59
    5660    /** The image currently displayed */
    5761    private transient BufferedImage image;
     
    104108    private static double bilinUpper;
    105109    private static double bilinLower;
     110
     111    /** Show a background for the error text (may be hard on eyes) */
     112    private static final BooleanProperty ERROR_MESSAGE_BACKGROUND = new BooleanProperty("geoimage.message.error.background", false);
    106113
    107114    @Override
     
    373380
    374381                        ImageDisplay.this.image = img;
     382                        // This will clear the loading info box
     383                        ImageDisplay.this.oldEntry = ImageDisplay.this.entry;
    375384                        visibleRect = new VisRect(0, 0, width, height);
    376385
     
    732741    public void setImage(ImageEntry entry) {
    733742        synchronized (this) {
     743            this.oldEntry = this.entry;
    734744            this.entry = entry;
    735             image = null;
     745            if (entry == null) {
     746                image = null;
     747                this.oldEntry = null;
     748            }
    736749            errorLoading = false;
    737750        }
     
    771784    public void paintComponent(Graphics g) {
    772785        ImageEntry entry;
     786        ImageEntry oldEntry;
    773787        BufferedImage image;
    774788        VisRect visibleRect;
     
    778792            image = this.image;
    779793            entry = this.entry;
     794            oldEntry = this.oldEntry;
    780795            visibleRect = this.visibleRect;
    781796            errorLoading = this.errorLoading;
     
    787802
    788803        Dimension size = getSize();
    789         if (entry == null) {
    790             g.setColor(Color.black);
    791             if (emptyText == null) {
    792                 emptyText = tr("No image");
    793             }
    794             String noImageStr = emptyText;
    795             Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(noImageStr, g);
    796             g.drawString(noImageStr,
    797                     (int) ((size.width - noImageSize.getWidth()) / 2),
    798                     (int) ((size.height - noImageSize.getHeight()) / 2));
    799         } else if (image == null) {
    800             g.setColor(Color.black);
    801             String loadingStr;
    802             if (!errorLoading) {
    803                 loadingStr = tr("Loading {0}", entry.getFile().getName());
    804             } else {
    805                 loadingStr = tr("Error on file {0}", entry.getFile().getName());
    806             }
    807             Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(loadingStr, g);
    808             g.drawString(loadingStr,
    809                     (int) ((size.width - noImageSize.getWidth()) / 2),
    810                     (int) ((size.height - noImageSize.getHeight()) / 2));
    811         } else {
     804        // Draw the image first, then draw error information
     805        if (image != null && (entry != null || oldEntry != null)) {
    812806            Rectangle r = new Rectangle(visibleRect);
    813807            Rectangle target = calculateDrawImageRectangle(visibleRect, size);
     
    903897            }
    904898        }
     899        final String errorMessage;
     900        // If the new entry is null, then there is no image.
     901        if (entry == null) {
     902            if (emptyText == null) {
     903                emptyText = tr("No image");
     904            }
     905            errorMessage = emptyText;
     906        } else if (image == null || !Objects.equals(entry, oldEntry)) {
     907            // The image is not necessarily null when loading anymore. If the oldEntry is not the same as the new entry,
     908            // we are probably still loading the image. (oldEntry gets set to entry when the image finishes loading).
     909            if (!errorLoading) {
     910                errorMessage = tr("Loading {0}", entry.getFile().getName());
     911            } else {
     912                errorMessage = tr("Error on file {0}", entry.getFile().getName());
     913            }
     914        } else {
     915            errorMessage = null;
     916        }
     917        if (errorMessage != null && !errorMessage.trim().isEmpty()) {
     918            Rectangle2D errorStringSize = g.getFontMetrics(g.getFont()).getStringBounds(errorMessage, g);
     919            if (Boolean.TRUE.equals(ERROR_MESSAGE_BACKGROUND.get())) {
     920                int height = g.getFontMetrics().getHeight();
     921                int descender = g.getFontMetrics().getDescent();
     922                g.setColor(getBackground());
     923                int width = (int) (errorStringSize.getWidth() * 1);
     924                // top-left of text
     925                int tlx = (int) ((size.getWidth() - errorStringSize.getWidth()) / 2);
     926                int tly = (int) ((size.getHeight() - 3 * errorStringSize.getHeight()) / 2 + descender);
     927                g.fillRect(tlx, tly, width, height);
     928            }
     929
     930            // lower-left of text
     931            int llx = (int) ((size.width - errorStringSize.getWidth()) / 2);
     932            int lly = (int) ((size.height - errorStringSize.getHeight()) / 2);
     933            g.setColor(getForeground());
     934            g.drawString(errorMessage, llx, lly);
     935        }
    905936    }
    906937
     
    9821013    public void zoomBestFitOrOne() {
    9831014        ImageEntry entry;
     1015        ImageEntry oldEntry;
    9841016        Image image;
    9851017        VisRect visibleRect;
Note: See TracChangeset for help on using the changeset viewer.