From: jkauttio Date: Fri, 24 Apr 2015 13:10:19 +0000 (+0000) Subject: Fix a problem in variable finding utility which caused variables inside modules to... X-Git-Tag: v1.29.0~84 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2935df5e0e0f97a37533406036abd2d76d1ac3ed;p=simantics%2Fsysdyn.git Fix a problem in variable finding utility which caused variables inside modules to be ignored fixes #5794 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31210 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java index cac1aea8..54625720 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/RepresentationUtils.java @@ -161,35 +161,49 @@ public class RepresentationUtils { public static Variable getVariable(Configuration configuration, String name) { + if (name == null || name.isEmpty()) { + return null; + } + + // this function seems to be used with both whitespaced and + // underscored versions of the variable name + String[] components = name.replace('_', ' ').split("\\."); + Configuration conf = configuration; - // This function seems to be used with both whitespaced and - // underscored versions of the variable name. - String whitespacedName = name.replace('_', ' '); - String[] components = whitespacedName.split("\\."); - - for(String component : components) { - - Configuration newConf = null; - - for(IElement element : conf.getElements()) { - if(element instanceof Variable) { - Variable variable = (Variable) element; - if(variable.getName().equals(component)) { - return variable; - } - } else if(element instanceof Module) { - Module m = (Module)element; - if(m.getName().equals(component)) { - newConf = m.getType().getConfiguration(); - } + + for (int i = 0; i < components.length-1; i++) { + Module module = findModule(conf, components[i]); + if (module == null) { + // requested module could not be found + return null; + } + conf = module.getType().getConfiguration(); + } + + return findVariable(conf, components[components.length-1]); + } + + private static Module findModule(Configuration conf, String name) { + for (IElement element : conf.getElements()) { + if (element instanceof Module) { + Module module = (Module)element; + if (module.getName().equals(name)) { + return module; + } + } + } + return null; + } + + private static Variable findVariable(Configuration conf, String name) { + for (IElement element : conf.getElements()) { + if (element instanceof Variable) { + Variable variable = (Variable)element; + if (variable.getName().equals(name)) { + return variable; } } - - // If variable or configuration has not been found, return null - if(newConf == null) - return null; } - return null; }