- Timestamp:
- 2021-04-14T20:52:18+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageDisplay.java
r17782 r17783 25 25 import java.io.File; 26 26 import java.io.IOException; 27 import java.util.Objects; 27 28 28 29 import javax.imageio.ImageIO; … … 54 55 private ImageEntry entry; 55 56 57 /** The previous file that is currently displayed. Cleared on paint. Only used to help improve UI error information. */ 58 private ImageEntry oldEntry; 59 56 60 /** The image currently displayed */ 57 61 private transient BufferedImage image; … … 104 108 private static double bilinUpper; 105 109 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); 106 113 107 114 @Override … … 373 380 374 381 ImageDisplay.this.image = img; 382 // This will clear the loading info box 383 ImageDisplay.this.oldEntry = ImageDisplay.this.entry; 375 384 visibleRect = new VisRect(0, 0, width, height); 376 385 … … 732 741 public void setImage(ImageEntry entry) { 733 742 synchronized (this) { 743 this.oldEntry = this.entry; 734 744 this.entry = entry; 735 image = null; 745 if (entry == null) { 746 image = null; 747 this.oldEntry = null; 748 } 736 749 errorLoading = false; 737 750 } … … 771 784 public void paintComponent(Graphics g) { 772 785 ImageEntry entry; 786 ImageEntry oldEntry; 773 787 BufferedImage image; 774 788 VisRect visibleRect; … … 778 792 image = this.image; 779 793 entry = this.entry; 794 oldEntry = this.oldEntry; 780 795 visibleRect = this.visibleRect; 781 796 errorLoading = this.errorLoading; … … 787 802 788 803 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)) { 812 806 Rectangle r = new Rectangle(visibleRect); 813 807 Rectangle target = calculateDrawImageRectangle(visibleRect, size); … … 903 897 } 904 898 } 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 } 905 936 } 906 937 … … 982 1013 public void zoomBestFitOrOne() { 983 1014 ImageEntry entry; 1015 ImageEntry oldEntry; 984 1016 Image image; 985 1017 VisRect visibleRect;
Note:
See TracChangeset
for help on using the changeset viewer.