Changeset 7099 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2014-05-10T20:22:15+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/mappaint
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java
r7083 r7099 127 127 Cascade c = env.mc.getCascade(env.layer); 128 128 129 final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class); 129 final IconReference iconRef = c.get(keys[ICON_IMAGE_IDX], null, IconReference.class, true); 130 130 if (iconRef == null) 131 131 return null; -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Condition.java
r7083 r7099 419 419 public static class ExpressionCondition extends Condition { 420 420 421 private Expression e; 421 private final Expression e; 422 422 423 423 public ExpressionCondition(Expression e) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r7083 r7099 47 47 private static final List<Method> arrayFunctions; 48 48 private static final List<Method> parameterFunctions; 49 private static final Functions FUNCTIONS_INSTANCE = new Functions();49 private static final List<Method> parameterFunctionsEnv; 50 50 51 51 static { 52 52 arrayFunctions = new ArrayList<>(); 53 53 parameterFunctions = new ArrayList<>(); 54 parameterFunctionsEnv = new ArrayList<>(); 54 55 for (Method m : Functions.class.getDeclaredMethods()) { 55 56 Class<?>[] paramTypes = m.getParameterTypes(); … … 57 58 arrayFunctions.add(m); 58 59 } else { 59 parameterFunctions.add(m); 60 if (paramTypes[0].equals(Environment.class)) { 61 parameterFunctionsEnv.add(m); 62 } else { 63 parameterFunctions.add(m); 64 } 60 65 } 61 66 } … … 91 96 } 92 97 98 /** 99 * List of functions that can be used in MapCSS expressions. 100 * 101 * First parameter can be of type {@link Environment} (if needed). This is 102 * automatically filled in by JOSM and the user only sees the remaining 103 * arguments. 104 * When one of the user supplied arguments cannot be converted the 105 * expected type or is null, the function is not called and it returns null 106 * immediately. Add the annotation {@link NullableArguments} to allow 107 * null arguments. 108 * Every method must be static. 109 */ 93 110 @SuppressWarnings("UnusedDeclaration") 94 111 public static class Functions { 95 96 Environment env;97 112 98 113 /** … … 265 280 return Utils.color_int2float(c.getAlpha()); 266 281 } 267 282 268 283 /** 269 284 * Assembles the strings to one. … … 287 302 * Returns the value of the property {@code key}, e.g., {@code prop("width")}. 288 303 */ 289 public Object prop(String key) { 290 return prop(key, null); 304 public static Object prop(final Environment env, String key) { 305 return prop(env, key, null); 291 306 } 292 307 … … 294 309 * Returns the value of the property {@code key} from layer {@code layer}. 295 310 */ 296 public Object prop(String key, String layer) { 311 public static Object prop(final Environment env, String key, String layer) { 297 312 return env.getCascade(layer).get(key); 298 313 } … … 301 316 * Determines whether property {@code key} is set. 302 317 */ 303 public Boolean is_prop_set(String key) { 304 return is_prop_set(key, null); 318 public static Boolean is_prop_set(final Environment env, String key) { 319 return is_prop_set(env, key, null); 305 320 } 306 321 … … 308 323 * Determines whether property {@code key} is set on layer {@code layer}. 309 324 */ 310 public Boolean is_prop_set(String key, String layer) { 325 public static Boolean is_prop_set(final Environment env, String key, String layer) { 311 326 return env.getCascade(layer).containsKey(key); 312 327 } … … 315 330 * Gets the value of the key {@code key} from the object in question. 316 331 */ 317 public String tag(String key) { 332 public static String tag(final Environment env, String key) { 318 333 return env.osm == null ? null : env.osm.get(key); 319 334 } … … 322 337 * Gets the first non-null value of the key {@code key} from the object's parent(s). 323 338 */ 324 public String parent_tag(String key) { 339 public static String parent_tag(final Environment env, String key) { 325 340 if (env.parent == null) { 326 341 if (env.osm != null) { … … 338 353 } 339 354 340 public String child_tag(String key) { 355 public static String child_tag(final Environment env, String key) { 341 356 return env.child == null ? null : env.child.get(key); 342 357 } … … 345 360 * Determines whether the object has a tag with the given key. 346 361 */ 347 public boolean has_tag_key(String key) { 362 public static boolean has_tag_key(final Environment env, String key) { 348 363 return env.osm.hasKey(key); 349 364 } … … 352 367 * Returns the index of node in parent way or member in parent relation. 353 368 */ 354 public Float index() { 369 public static Float index(final Environment env) { 355 370 if (env.index == null) { 356 371 return null; … … 359 374 } 360 375 361 public String role() { 376 public static String role(final Environment env) { 362 377 return env.getRole(); 363 378 } … … 403 418 * Determines whether the JOSM search with {@code searchStr} applies to the object. 404 419 */ 405 public Boolean JOSM_search(String searchStr) { 420 public static Boolean JOSM_search(final Environment env, String searchStr) { 406 421 Match m; 407 422 try { … … 498 513 * @see OsmPrimitive#getUniqueId() 499 514 */ 500 public long osm_id() { 515 public static long osm_id(final Environment env) { 501 516 return env.osm.getUniqueId(); 502 517 } … … 607 622 for (Method m : parameterFunctions) { 608 623 if (m.getName().equals(name) && args.size() == m.getParameterTypes().length) 609 return new ParameterFunction(m, args); 624 return new ParameterFunction(m, args, false); 625 } 626 for (Method m : parameterFunctionsEnv) { 627 if (m.getName().equals(name) && args.size() == m.getParameterTypes().length-1) 628 return new ParameterFunction(m, args, true); 610 629 } 611 630 return NullExpression.INSTANCE; … … 717 736 } 718 737 } 719 738 720 739 /** 721 740 * Function that takes a certain number of argument with specific type. … … 727 746 728 747 private final Method m; 748 private final boolean nullable; 729 749 private final List<Expression> args; 730 750 private final Class<?>[] expectedParameterTypes; 731 732 public ParameterFunction(Method m, List<Expression> args) { 751 private final boolean needsEnvironment; 752 753 public ParameterFunction(Method m, List<Expression> args, boolean needsEnvironment) { 733 754 this.m = m; 755 this.nullable = m.getAnnotation(NullableArguments.class) != null; 734 756 this.args = args; 735 expectedParameterTypes = m.getParameterTypes(); 757 this.expectedParameterTypes = m.getParameterTypes(); 758 this.needsEnvironment = needsEnvironment; 736 759 } 737 760 738 761 @Override 739 762 public Object evaluate(Environment env) { 740 FUNCTIONS_INSTANCE.env = env; 741 Object[] convertedArgs = new Object[expectedParameterTypes.length]; 742 for (int i = 0; i < args.size(); ++i) { 743 convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]); 744 if (convertedArgs[i] == null && m.getAnnotation(NullableArguments.class) == null) { 745 return null; 763 Object[] convertedArgs; 764 765 if (needsEnvironment) { 766 convertedArgs = new Object[args.size()+1]; 767 convertedArgs[0] = env; 768 for (int i = 1; i < convertedArgs.length; ++i) { 769 convertedArgs[i] = Cascade.convertTo(args.get(i-1).evaluate(env), expectedParameterTypes[i]); 770 if (convertedArgs[i] == null && !nullable) { 771 return null; 772 } 773 } 774 } else { 775 convertedArgs = new Object[args.size()]; 776 for (int i = 0; i < convertedArgs.length; ++i) { 777 convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]); 778 if (convertedArgs[i] == null && !nullable) { 779 return null; 780 } 746 781 } 747 782 } 748 783 Object result = null; 749 784 try { 750 result = m.invoke( FUNCTIONS_INSTANCE, convertedArgs);785 result = m.invoke(null, convertedArgs); 751 786 } catch (IllegalAccessException | IllegalArgumentException ex) { 752 787 throw new RuntimeException(ex); … … 783 818 784 819 private final Method m; 820 private final boolean nullable; 785 821 private final List<Expression> args; 822 private final Class<?>[] expectedParameterTypes; 786 823 private final Class<?> arrayComponentType; 787 private final Object[] convertedArgs;788 824 789 825 public ArrayFunction(Method m, List<Expression> args) { 790 826 this.m = m; 827 this.nullable = m.getAnnotation(NullableArguments.class) != null; 791 828 this.args = args; 792 Class<?>[] expectedParameterTypes = m.getParameterTypes(); 793 convertedArgs = new Object[expectedParameterTypes.length]; 794 arrayComponentType = expectedParameterTypes[0].getComponentType(); 829 this.expectedParameterTypes = m.getParameterTypes(); 830 this.arrayComponentType = expectedParameterTypes[0].getComponentType(); 795 831 } 796 832 797 833 @Override 798 834 public Object evaluate(Environment env) { 835 Object[] convertedArgs = new Object[expectedParameterTypes.length]; 799 836 Object arrayArg = Array.newInstance(arrayComponentType, args.size()); 800 837 for (int i = 0; i < args.size(); ++i) { 801 838 Object o = Cascade.convertTo(args.get(i).evaluate(env), arrayComponentType); 802 if (o == null && m.getAnnotation(NullableArguments.class) == null) {839 if (o == null && !nullable) { 803 840 return null; 804 841 }
Note:
See TracChangeset
for help on using the changeset viewer.