Ticket #17072: 17072.1.patch
File 17072.1.patch, 7.1 KB (added by , 4 years ago) |
---|
-
src/org/openstreetmap/josm/gui/layer/geoimage/ImageDisplay.java
import java.awt.geom.Rectangle2D; 24 24 import java.awt.image.BufferedImage; 25 25 import java.awt.image.ImageObserver; 26 26 import java.io.File; 27 import java.util.Objects; 27 28 28 29 import javax.swing.JComponent; 29 30 import javax.swing.SwingUtilities; … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 48 49 /** The file that is currently displayed */ 49 50 private ImageEntry entry; 50 51 52 /** The previous file that is currently displayed. Cleared on paint. Only used to help improve UI error information. */ 53 private ImageEntry oldEntry; 54 51 55 /** The image currently displayed */ 52 56 private transient Image image; 53 57 … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 94 98 private static double bilinUpper; 95 99 private static double bilinLower; 96 100 101 /** Show a background for the error text (may be hard on eyes) */ 102 private static final BooleanProperty ERROR_MESSAGE_BACKGROUND = new BooleanProperty("geoimage.message.error.background", false); 103 97 104 @Override 98 105 public void preferenceChanged(PreferenceChangeEvent e) { 99 106 if (e == null || … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 345 352 } 346 353 347 354 ImageDisplay.this.image = img; 355 // This will clear the loading info box 356 ImageDisplay.this.oldEntry = ImageDisplay.this.entry; 348 357 visibleRect = new VisRect(0, 0, width, height); 349 358 350 359 Logging.debug("Loaded {0} with dimensions {1}x{2} memoryTaken={3}m exifOrientationSwitchedDimension={4}", … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 690 699 */ 691 700 public void setImage(ImageEntry entry) { 692 701 synchronized (this) { 702 this.oldEntry = this.entry; 693 703 this.entry = entry; 694 image = null; 704 if (entry == null) { 705 image = null; 706 this.oldEntry = null; 707 } 695 708 errorLoading = false; 696 709 } 697 710 repaint(); … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 724 737 @Override 725 738 public void paintComponent(Graphics g) { 726 739 ImageEntry entry; 740 ImageEntry oldEntry; 727 741 Image image; 728 742 VisRect visibleRect; 729 743 boolean errorLoading; … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 731 745 synchronized (this) { 732 746 image = this.image; 733 747 entry = this.entry; 748 oldEntry = this.oldEntry; 734 749 visibleRect = this.visibleRect; 735 750 errorLoading = this.errorLoading; 736 751 } … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 740 755 } 741 756 742 757 Dimension size = getSize(); 743 if (entry == null) { 744 g.setColor(Color.black); 745 if (emptyText == null) { 746 emptyText = tr("No image"); 747 } 748 String noImageStr = emptyText; 749 Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(noImageStr, g); 750 g.drawString(noImageStr, 751 (int) ((size.width - noImageSize.getWidth()) / 2), 752 (int) ((size.height - noImageSize.getHeight()) / 2)); 753 } else if (image == null) { 754 g.setColor(Color.black); 755 String loadingStr; 756 if (!errorLoading) { 757 loadingStr = tr("Loading {0}", entry.getFile().getName()); 758 } else { 759 loadingStr = tr("Error on file {0}", entry.getFile().getName()); 760 } 761 Rectangle2D noImageSize = g.getFontMetrics(g.getFont()).getStringBounds(loadingStr, g); 762 g.drawString(loadingStr, 763 (int) ((size.width - noImageSize.getWidth()) / 2), 764 (int) ((size.height - noImageSize.getHeight()) / 2)); 765 } else { 758 // Draw the image first, then draw error information 759 if (image != null && (entry != null || oldEntry != null)) { 766 760 Rectangle r = new Rectangle(visibleRect); 767 761 Rectangle target = calculateDrawImageRectangle(visibleRect, size); 768 762 double scale = target.width / (double) r.width; // pixel ratio is 1:1 769 763 770 764 if (selectedRect == null && !visibleRect.isDragUpdate && 771 bilinLower < scale && scale < bilinUpper) {765 bilinLower < scale && scale < bilinUpper) { 772 766 try { 773 767 BufferedImage bi = ImageProvider.toBufferedImage(image, r); 774 768 if (bi != null) { … … public class ImageDisplay extends JComponent implements Destroyable, PreferenceC 851 845 g.drawString(line, x, y + ascent); 852 846 } 853 847 } 848 final String errorMessage; 849 // If the new entry is null, then there is no image. 850 if (entry == null) { 851 if (emptyText == null) { 852 emptyText = tr("No image"); 853 } 854 errorMessage = emptyText; 855 } else if (image == null || !Objects.equals(entry, oldEntry)) { 856 // The image is not necessarily null when loading anymore. If the oldEntry is not the same as the new entry, 857 // we are probably still loading the image. (oldEntry gets set to entry when the image finishes loading). 858 if (!errorLoading) { 859 errorMessage = tr("Loading {0}", entry.getFile().getName()); 860 } else { 861 errorMessage = tr("Error on file {0}", entry.getFile().getName()); 862 } 863 } else { 864 errorMessage = null; 865 } 866 if (errorMessage != null && !errorMessage.trim().isEmpty()) { 867 Rectangle2D errorStringSize = g.getFontMetrics(g.getFont()).getStringBounds(errorMessage, g); 868 if (Boolean.TRUE.equals(ERROR_MESSAGE_BACKGROUND.get())) { 869 int height = g.getFontMetrics().getHeight(); 870 int descender = g.getFontMetrics().getDescent(); 871 g.setColor(getBackground()); 872 int width = (int) (errorStringSize.getWidth() * 1); 873 // top-left of text 874 int tlx = (int) ((size.getWidth() - errorStringSize.getWidth()) / 2); 875 int tly = (int) ((size.getHeight() - 3 * errorStringSize.getHeight()) / 2 + descender); 876 g.fillRect(tlx, tly, width, height); 877 } 878 879 // lower-left of text 880 int llx = (int) ((size.width - errorStringSize.getWidth()) / 2); 881 int lly = (int) ((size.height - errorStringSize.getHeight()) / 2); 882 g.setColor(getForeground()); 883 g.drawString(errorMessage, llx, lly); 884 } 854 885 } 855 886 856 887 static Point img2compCoord(VisRect visibleRect, int xImg, int yImg, Dimension compSize) {