]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jemmy/src/main/java/org/argeo/slc/jemmy/AbstractComponentWrapper.java
20d019f446424480239a15634754810f345b1541
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jemmy / src / main / java / org / argeo / slc / jemmy / AbstractComponentWrapper.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jemmy;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.netbeans.jemmy.operators.ComponentOperator;
26
27 public class AbstractComponentWrapper implements ComponentWrapper {
28
29 protected ComponentWrapper parent;
30
31 /**
32 * List of children ComponentWrapper
33 */
34 protected List children = new ArrayList();
35
36 protected WrapperLocator locator;
37
38 protected String prefix;
39
40 public ComponentOperator find() {
41 return locator.find(this);
42 }
43
44 protected String createNewKey(String oldKey) {
45 return (prefix == null) ? oldKey : (prefix + "." + oldKey);
46 }
47
48 protected void addToAccessorMap(Map accessors, String oldKey,
49 Object accessor) {
50 String newKey = createNewKey(oldKey);
51 if (accessors.containsKey(newKey)) {
52 throw new ConfigRuntimeException("An Accessor with key '" + newKey
53 + "' was already registered");
54 }
55 accessors.put(newKey, accessor);
56 }
57
58 public Map getAccessors(Class accessorClass) {
59 Map accessors = new HashMap();
60 if (accessorClass.isInstance(this)) {
61 addToAccessorMap(accessors, ((Accessor) this).getFieldName(), this);
62 }
63 for (int i = 0; i < children.size(); i++) {
64 Map childAccessors = ((ComponentWrapper) children.get(i))
65 .getAccessors(accessorClass);
66
67 Set entries = childAccessors.entrySet();
68 Iterator it = entries.iterator();
69
70 while (it.hasNext()) {
71 Map.Entry keyValue = (Map.Entry) it.next();
72 addToAccessorMap(accessors, keyValue.getKey().toString(),
73 keyValue.getValue());
74 }
75 }
76 return accessors;
77 }
78
79 public ComponentWrapper getParent() {
80 return parent;
81 }
82
83 public void setParent(ComponentWrapper parent) {
84 if (this.parent != null) {
85 throw new ConfigRuntimeException("Parent already set");
86 }
87 this.parent = parent;
88 }
89
90 public List getChildren() {
91 return children;
92 }
93
94 public void setChildren(List children) {
95 this.children = children;
96
97 // check that all elements of the list are ComponentWrapper
98 // and set their parent
99 for (int i = 0; i < this.children.size(); i++) {
100 ComponentWrapper wrapper = (ComponentWrapper) this.children.get(i);
101 if (wrapper == null) {
102 throw new ConfigRuntimeException(
103 "Children of ComponentWrappers must be ComponentWrappers");
104 }
105 wrapper.setParent(this);
106 }
107 }
108
109 public WrapperLocator getLocator() {
110 return locator;
111 }
112
113 public void setLocator(WrapperLocator locator) {
114 this.locator = locator;
115 }
116
117 public String getPrefix() {
118 return prefix;
119 }
120
121 public void setPrefix(String prefix) {
122 this.prefix = prefix;
123 }
124
125 }