+package org.argeo.slc.jemmy;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.netbeans.jemmy.operators.ComponentOperator;\r
+\r
+public class AbstractComponentWrapper implements ComponentWrapper {\r
+\r
+ protected ComponentWrapper parent;\r
+\r
+ /**\r
+ * List of children ComponentWrapper\r
+ */\r
+ protected List children = new ArrayList();\r
+\r
+ protected WrapperLocator locator;\r
+\r
+ protected String prefix;\r
+\r
+ public ComponentOperator find() {\r
+ return locator.find(this);\r
+ }\r
+\r
+ protected String createNewKey(String oldKey) {\r
+ return (prefix == null) ? oldKey : (prefix + "." + oldKey);\r
+ }\r
+\r
+ protected void addToAccessorMap(Map accessors, String oldKey,\r
+ Object accessor) {\r
+ String newKey = createNewKey(oldKey);\r
+ if (accessors.containsKey(newKey)) {\r
+ throw new ConfigRuntimeException("An Accessor with key '" + newKey\r
+ + "' was already registered");\r
+ }\r
+ accessors.put(newKey, accessor);\r
+ }\r
+\r
+ public Map getAccessors(Class accessorClass) {\r
+ Map accessors = new HashMap();\r
+ if (accessorClass.isInstance(this)) {\r
+ addToAccessorMap(accessors, ((Accessor) this).getFieldName(), this);\r
+ }\r
+ for (int i = 0; i < children.size(); i++) {\r
+ Map childAccessors = ((ComponentWrapper) children.get(i))\r
+ .getAccessors(accessorClass);\r
+\r
+ Set entries = childAccessors.entrySet();\r
+ Iterator it = entries.iterator();\r
+\r
+ while (it.hasNext()) {\r
+ Map.Entry keyValue = (Map.Entry) it.next();\r
+ addToAccessorMap(accessors, keyValue.getKey().toString(),\r
+ keyValue.getValue());\r
+ }\r
+ }\r
+ return accessors;\r
+ }\r
+\r
+ public ComponentWrapper getParent() {\r
+ return parent;\r
+ }\r
+\r
+ public void setParent(ComponentWrapper parent) {\r
+ if (this.parent != null) {\r
+ throw new ConfigRuntimeException("Parent already set");\r
+ }\r
+ this.parent = parent;\r
+ }\r
+\r
+ public List getChildren() {\r
+ return children;\r
+ }\r
+\r
+ public void setChildren(List children) {\r
+ this.children = children;\r
+\r
+ // check that all elements of the list are ComponentWrapper\r
+ // and set their parent\r
+ for (int i = 0; i < this.children.size(); i++) {\r
+ ComponentWrapper wrapper = (ComponentWrapper) this.children.get(i);\r
+ if (wrapper == null) {\r
+ throw new ConfigRuntimeException(\r
+ "Children of ComponentWrappers must be ComponentWrappers");\r
+ }\r
+ wrapper.setParent(this);\r
+ }\r
+ }\r
+\r
+ public WrapperLocator getLocator() {\r
+ return locator;\r
+ }\r
+\r
+ public void setLocator(WrapperLocator locator) {\r
+ this.locator = locator;\r
+ }\r
+\r
+ public String getPrefix() {\r
+ return prefix;\r
+ }\r
+\r
+ public void setPrefix(String prefix) {\r
+ this.prefix = prefix;\r
+ }\r
+\r
+}\r