Changeset 10428 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2016-06-19T18:28:26+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r10410 r10428 272 272 /** ordered list of overlay images */ 273 273 protected List<ImageOverlay> overlayInfo; 274 /** <code>true</code> if icon must be grayed out */ 275 protected boolean isDisabled = false; 274 276 275 277 private static SVGUniverse svgUniverse; … … 351 353 this.additionalClassLoaders = image.additionalClassLoaders; 352 354 this.overlayInfo = image.overlayInfo; 355 this.isDisabled = image.isDisabled; 353 356 } 354 357 … … 601 604 602 605 /** 606 * Set, if image must be filtered to grayscale so it will look like disabled icon. 607 * 608 * @param disabled true, if image must be grayed out for disabled state 609 * @return the current object, for convenience 610 * @since 10428 611 */ 612 public ImageProvider setDisabled(boolean disabled) { 613 this.isDisabled = disabled; 614 return this; 615 } 616 617 /** 603 618 * Execute the image request and scale result. 604 619 * @return the requested image or null if the request failed … … 606 621 public ImageIcon get() { 607 622 ImageResource ir = getResource(); 608 if (ir == null) 623 624 if (ir == null) { 609 625 return null; 626 } 610 627 if (virtualMaxWidth != -1 || virtualMaxHeight != -1) 611 628 return ir.getImageIconBounded(new Dimension(virtualMaxWidth, virtualMaxHeight)); … … 638 655 ir = new ImageResource(ir, overlayInfo); 639 656 } 657 if (isDisabled) { 658 ir.setDisabled(true); 659 } 640 660 return ir; 641 661 } … … 718 738 719 739 /** 740 * Load an image from directory with a given file name and size. 741 * 742 * @param subdir subdirectory the image lies in 743 * @param name The icon name (base name with or without '.png' or '.svg' extension) 744 * @param size Target icon size 745 * @return The requested Image. 746 * @throws RuntimeException if the image cannot be located 747 * @since 10428 748 */ 749 public static ImageIcon get(String subdir, String name, ImageSizes size) { 750 return new ImageProvider(subdir, name).setSize(size).get(); 751 } 752 753 /** 720 754 * Load an empty image with a given size. 721 755 * … … 741 775 public static ImageIcon getIfAvailable(String subdir, String name) { 742 776 return new ImageProvider(subdir, name).setOptional(true).get(); 777 } 778 779 /** 780 * Load an image with a given file name and size. 781 * 782 * @param name The icon name (base name with or without '.png' or '.svg' extension) 783 * @param size Target icon size 784 * @return the requested image or null if the request failed 785 * @see #get(String, String) 786 * @since 10428 787 */ 788 public static ImageIcon get(String name, ImageSizes size) { 789 return new ImageProvider(name).setSize(size).get(); 743 790 } 744 791 … … 776 823 return null; 777 824 825 String prefix = ""; 826 if(isDisabled) 827 prefix = "dis:"+prefix; 778 828 if (name.startsWith("data:")) { 779 829 String url = name; 780 ImageResource ir = cache.get( url);830 ImageResource ir = cache.get(prefix+url); 781 831 if (ir != null) return ir; 782 832 ir = getIfAvailableDataUrl(url); 783 833 if (ir != null) { 784 cache.put( url, ir);834 cache.put(prefix+url, ir); 785 835 } 786 836 return ir; … … 791 841 if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(HTTPS_PROTOCOL)) { 792 842 String url = name; 793 ImageResource ir = cache.get( url);843 ImageResource ir = cache.get(prefix+url); 794 844 if (ir != null) return ir; 795 845 ir = getIfAvailableHttp(url, type); 796 846 if (ir != null) { 797 cache.put( url, ir);847 cache.put(prefix+url, ir); 798 848 } 799 849 return ir; 800 850 } else if (name.startsWith(WIKI_PROTOCOL)) { 801 ImageResource ir = cache.get( name);851 ImageResource ir = cache.get(prefix+name); 802 852 if (ir != null) return ir; 803 853 ir = getIfAvailableWiki(name, type); 804 854 if (ir != null) { 805 cache.put( name, ir);855 cache.put(prefix+name, ir); 806 856 } 807 857 return ir; … … 831 881 832 882 String fullName = subdir + name + ext; 833 String cacheName = fullName;883 String cacheName = prefix + fullName; 834 884 /* cache separately */ 835 885 if (dirs != null && !dirs.isEmpty()) { -
trunk/src/org/openstreetmap/josm/tools/ImageResource.java
r10409 r10428 11 11 import javax.swing.AbstractAction; 12 12 import javax.swing.Action; 13 import javax.swing.Icon; 13 14 import javax.swing.ImageIcon; 15 import javax.swing.JPanel; 16 import javax.swing.UIManager; 14 17 15 18 import org.openstreetmap.josm.gui.util.GuiSizesHelper; … … 43 46 */ 44 47 protected List<ImageOverlay> overlayInfo; 48 /** 49 * <code>true</code> if icon must be grayed out 50 */ 51 protected boolean isDisabled = false; 52 /** 53 * The base raster image for the final output 54 */ 45 55 private Image baseImage; 46 56 … … 51 61 public ImageResource(Image img) { 52 62 CheckParameterUtil.ensureParameterNotNull(img); 53 baseImage = img; 54 imgCache.put(DEFAULT_DIMENSION, scaleBaseImageIfNeeded(img)); 63 baseImage = scaleBaseImageIfNeeded(img); 55 64 } 56 65 … … 96 105 97 106 /** 107 * Set, if image must be filtered to grayscale so it will look like disabled icon. 108 * 109 * @param disabled true, if image must be grayed out for disabled state 110 * @return the current object, for convenience 111 * @since 10428 112 */ 113 public ImageResource setDisabled(boolean disabled) { 114 this.isDisabled = disabled; 115 return this; 116 } 117 118 /** 98 119 * Set both icons of an Action 99 120 * @param a The action for the icons … … 145 166 return new ImageIcon(img); 146 167 } 168 BufferedImage bimg; 147 169 if (svg != null) { 148 170 Dimension realDim = GuiSizesHelper.getDimensionDpiAdjusted(dim); 149 BufferedImagebimg = ImageProvider.createImageFromSvg(svg, realDim);171 bimg = ImageProvider.createImageFromSvg(svg, realDim); 150 172 if (bimg == null) { 151 173 return null; 152 174 } 153 if (overlayInfo != null) {154 for (ImageOverlay o : overlayInfo) {155 o.process(bimg);156 }157 }158 imgCache.put(dim, bimg);159 return new ImageIcon(bimg);160 175 } else { 161 176 if (baseImage == null) throw new AssertionError(); … … 173 188 } 174 189 Image i = icon.getImage().getScaledInstance(realWidth, realHeight, Image.SCALE_SMOOTH); 175 BufferedImagebimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB);190 bimg = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_INT_ARGB); 176 191 bimg.getGraphics().drawImage(i, 0, 0, null); 177 if (overlayInfo != null) { 178 for (ImageOverlay o : overlayInfo) { 179 o.process(bimg); 180 } 181 } 182 imgCache.put(dim, bimg); 183 return new ImageIcon(bimg); 184 } 192 } 193 if (overlayInfo != null) { 194 for (ImageOverlay o : overlayInfo) { 195 o.process(bimg); 196 } 197 } 198 if (isDisabled) { 199 //Use default Swing functionality to make icon look disabled by applying grayscaling filter. 200 Icon disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(null, new ImageIcon(bimg)); 201 202 //Convert Icon to ImageIcon with BufferedImage inside 203 bimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 204 disabledIcon.paintIcon(new JPanel(), bimg.getGraphics(), 0, 0); 205 } 206 imgCache.put(dim, bimg); 207 return new ImageIcon(bimg); 185 208 } 186 209
Note: See TracChangeset
for help on using the changeset viewer.