- Timestamp:
- 2020-04-18T10:41:20+02:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/HiDPISupport.java
r16056 r16331 13 13 import java.util.Collections; 14 14 import java.util.List; 15 import java.util.Optional;16 15 import java.util.function.Function; 17 16 import java.util.function.UnaryOperator; … … 31 30 public final class HiDPISupport { 32 31 33 private static volatile Optional<Class<? extends Image>> baseMultiResolutionImageClass; 34 private static volatile Optional<Constructor<? extends Image>> baseMultiResolutionImageConstructor; 35 private static volatile Optional<Method> resolutionVariantsMethod; 32 private static final Class<? extends Image> baseMultiResolutionImageClass; 33 private static final Constructor<? extends Image> baseMultiResolutionImageConstructor; 34 private static final Method resolutionVariantsMethod; 35 36 static { 37 baseMultiResolutionImageClass = initBaseMultiResolutionImageClass(); 38 baseMultiResolutionImageConstructor = initBaseMultiResolutionImageConstructor(); 39 resolutionVariantsMethod = initResolutionVariantsMethod(); 40 } 36 41 37 42 private HiDPISupport() { … … 51 56 public static Image getMultiResolutionImage(Image base, ImageResource ir) { 52 57 double uiScale = getHiDPIScale(); 53 if (uiScale != 1.0 && getBaseMultiResolutionImageConstructor().isPresent()) {58 if (uiScale != 1.0 && baseMultiResolutionImageConstructor != null) { 54 59 ImageIcon zoomed = ir.getImageIcon(new Dimension( 55 60 (int) Math.round(base.getWidth(null) * uiScale), … … 70 75 public static Image getMultiResolutionImage(List<Image> imgs) { 71 76 CheckParameterUtil.ensureThat(!imgs.isEmpty(), "imgs is empty"); 72 Optional<Constructor<? extends Image>> baseMrImageConstructor = getBaseMultiResolutionImageConstructor(); 73 if (baseMrImageConstructor.isPresent()) { 77 if (baseMultiResolutionImageConstructor != null) { 74 78 try { 75 return baseM rImageConstructor.get().newInstance((Object) imgs.toArray(new Image[0]));79 return baseMultiResolutionImageConstructor.newInstance((Object) imgs.toArray(new Image[0])); 76 80 } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { 77 81 Logging.error("Unexpected error while instantiating object of class BaseMultiResolutionImage: " + ex); … … 90 94 */ 91 95 public static Image getBaseImage(Image img) { 92 Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass(); 93 Optional<Method> resVariantsMethod = getResolutionVariantsMethod(); 94 if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) { 96 if (baseMultiResolutionImageClass == null || resolutionVariantsMethod == null) { 95 97 return img; 96 98 } 97 if (baseM rImageClass.get().isInstance(img)) {99 if (baseMultiResolutionImageClass.isInstance(img)) { 98 100 try { 99 101 @SuppressWarnings("unchecked") 100 List<Image> imgVars = (List<Image>) res VariantsMethod.get().invoke(img);102 List<Image> imgVars = (List<Image>) resolutionVariantsMethod.invoke(img); 101 103 if (!imgVars.isEmpty()) { 102 104 return imgVars.get(0); … … 119 121 */ 120 122 public static List<Image> getResolutionVariants(Image img) { 121 Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass(); 122 Optional<Method> resVariantsMethod = getResolutionVariantsMethod(); 123 if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) { 123 if (baseMultiResolutionImageClass == null || resolutionVariantsMethod == null) { 124 124 return Collections.singletonList(img); 125 125 } 126 if (baseM rImageClass.get().isInstance(img)) {126 if (baseMultiResolutionImageClass.isInstance(img)) { 127 127 try { 128 128 @SuppressWarnings("unchecked") 129 List<Image> imgVars = (List<Image>) res VariantsMethod.get().invoke(img);129 List<Image> imgVars = (List<Image>) resolutionVariantsMethod.invoke(img); 130 130 if (!imgVars.isEmpty()) { 131 131 return imgVars; … … 189 189 public static Image processMRImages(List<Image> imgs, Function<List<Image>, Image> processor) { 190 190 CheckParameterUtil.ensureThat(!imgs.isEmpty(), "at least one element expected"); 191 if ( !getBaseMultiResolutionImageClass().isPresent()) {191 if (baseMultiResolutionImageClass != null) { 192 192 return processor.apply(imgs); 193 193 } … … 205 205 } 206 206 207 private static Optional<Class<? extends Image>> getBaseMultiResolutionImageClass() { 208 if (baseMultiResolutionImageClass == null) { 209 synchronized (HiDPISupport.class) { 210 if (baseMultiResolutionImageClass == null) { 211 try { 212 @SuppressWarnings("unchecked") 213 Class<? extends Image> c = (Class<? extends Image>) Class.forName("java.awt.image.BaseMultiResolutionImage"); 214 baseMultiResolutionImageClass = Optional.ofNullable(c); 215 } catch (ClassNotFoundException ex) { 216 // class is not present in Java 8 217 baseMultiResolutionImageClass = Optional.empty(); 218 Logging.trace(ex); 219 } 220 } 221 } 222 } 223 return baseMultiResolutionImageClass; 224 } 225 226 private static Optional<Constructor<? extends Image>> getBaseMultiResolutionImageConstructor() { 227 if (baseMultiResolutionImageConstructor == null) { 228 synchronized (HiDPISupport.class) { 229 if (baseMultiResolutionImageConstructor == null) { 230 getBaseMultiResolutionImageClass().ifPresent(klass -> { 231 try { 232 Constructor<? extends Image> constr = klass.getConstructor(Image[].class); 233 baseMultiResolutionImageConstructor = Optional.ofNullable(constr); 234 } catch (NoSuchMethodException ex) { 235 Logging.error("Cannot find expected constructor: " + ex); 236 } 237 }); 238 if (baseMultiResolutionImageConstructor == null) { 239 baseMultiResolutionImageConstructor = Optional.empty(); 240 } 241 } 242 } 243 } 244 return baseMultiResolutionImageConstructor; 245 } 246 247 private static Optional<Method> getResolutionVariantsMethod() { 248 if (resolutionVariantsMethod == null) { 249 synchronized (HiDPISupport.class) { 250 if (resolutionVariantsMethod == null) { 251 getBaseMultiResolutionImageClass().ifPresent(klass -> { 252 try { 253 Method m = klass.getMethod("getResolutionVariants"); 254 resolutionVariantsMethod = Optional.ofNullable(m); 255 } catch (NoSuchMethodException ex) { 256 Logging.error("Cannot find expected method: "+ex); 257 } 258 }); 259 if (resolutionVariantsMethod == null) { 260 resolutionVariantsMethod = Optional.empty(); 261 } 262 } 263 } 264 } 265 return resolutionVariantsMethod; 207 @SuppressWarnings("unchecked") 208 private static Class<? extends Image> initBaseMultiResolutionImageClass() { 209 try { 210 return (Class<? extends Image>) Class.forName("java.awt.image.BaseMultiResolutionImage"); 211 } catch (ClassNotFoundException ex) { 212 // class is not present in Java 8 213 Logging.trace(ex); 214 return null; 215 } 216 } 217 218 private static Constructor<? extends Image> initBaseMultiResolutionImageConstructor() { 219 try { 220 return baseMultiResolutionImageClass != null 221 ? baseMultiResolutionImageClass.getConstructor(Image[].class) 222 : null; 223 } catch (NoSuchMethodException ex) { 224 Logging.error("Cannot find expected constructor: " + ex); 225 return null; 226 } 227 } 228 229 private static Method initResolutionVariantsMethod() { 230 try { 231 return baseMultiResolutionImageClass != null 232 ? baseMultiResolutionImageClass.getMethod("getResolutionVariants") 233 : null; 234 } catch (NoSuchMethodException ex) { 235 Logging.error("Cannot find expected method: " + ex); 236 return null; 237 } 266 238 } 267 239 }
Note:
See TracChangeset
for help on using the changeset viewer.